View Javadoc
1   /*
2    * Copyright (C) 2003-2013 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.search.connector;
18  
19  import java.text.Normalizer;
20  import java.util.ArrayList;
21  import java.util.Calendar;
22  import java.util.List;
23  import java.util.Locale;
24  
25  import javax.jcr.Node;
26  import javax.jcr.nodetype.NodeDefinition;
27  import javax.jcr.nodetype.NodeType;
28  import javax.jcr.nodetype.NodeTypeIterator;
29  import javax.jcr.nodetype.NodeTypeManager;
30  
31  import org.apache.commons.lang.LocaleUtils;
32  import org.exoplatform.commons.api.search.data.SearchContext;
33  import org.exoplatform.container.xml.InitParams;
34  import org.exoplatform.services.cms.drives.DriveData;
35  import org.exoplatform.services.cms.impl.Utils;
36  import org.exoplatform.services.cms.templates.TemplateService;
37  import org.exoplatform.services.jcr.RepositoryService;
38  import org.exoplatform.services.jcr.core.ManageableRepository;
39  import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeDefinitionImpl;
40  import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeManagerImpl;
41  import org.exoplatform.services.security.ConversationState;
42  import org.exoplatform.services.wcm.search.QueryCriteria;
43  import org.exoplatform.services.wcm.search.ResultNode;
44  import org.exoplatform.services.wcm.search.base.AbstractPageList;
45  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
46  
47  public abstract class BaseContentSearchServiceConnector extends BaseSearchServiceConnector {
48    
49    public static List<String> excluded_nodetypes = new ArrayList<String>();
50  
51    public BaseContentSearchServiceConnector(InitParams initParams) throws Exception {
52      super(initParams);
53    }
54  
55    /**
56     * {@inheritDoc}
57     */
58    @Override
59    protected QueryCriteria createQueryCriteria(String query, long offset, long limit, String sort, String order) {
60      QueryCriteria criteria = new QueryCriteria();
61      //set content types
62      criteria.setContentTypes(getSearchedDocTypes());
63      criteria.setNodeTypes(getNodeTypes());
64      criteria.setKeyword(removeAccents(query.toLowerCase()));
65      criteria.setSearchWebpage(false);
66      criteria.setSearchDocument(true);
67      criteria.setSearchWebContent(true);
68      if(query.contains("~")) {
69        criteria.setFuzzySearch(true);
70      }
71      criteria.setLiveMode(true);
72      criteria.setOffset(offset);
73      criteria.setLimit(limit);
74      criteria.setSortBy(sort);
75      criteria.setOrderBy(order);
76      
77      if (ConversationState.getCurrent().getIdentity().getUserId() != null) {
78        criteria.setSearchPath("");
79      }
80      return criteria;
81    }
82  
83    /**
84     * {@inheritDoc}
85     */
86    @Override
87    protected AbstractPageList<ResultNode> searchNodes(QueryCriteria criteria, SearchContext context) throws Exception {
88      String localeParam = context.getParamValue(SearchContext.RouterParams.LANG.create());
89      Locale locale = localeParam != null ? LocaleUtils.toLocale(localeParam) : null;
90      return siteSearch_.searchSiteContents(WCMCoreUtils.getUserSessionProvider(),
91                                             criteria, locale, (int)criteria.getLimit(), true);
92    }
93  
94    @Override
95    protected String getFileType(ResultNode node) throws Exception {
96      return org.exoplatform.services.cms.impl.Utils.getFileType(node);
97    }
98  
99    /**
100    * returns the primary types of the specific search service:
101    * nt:file for file search, all other document types for document search
102    * @return searched doc types
103    */
104   protected abstract String[] getSearchedDocTypes();
105   
106   /**
107    * returns the primary types of the specific search service:
108    * nt:file for file search, null for document search
109    * @return searched doc types
110    */
111   protected abstract String[] getNodeTypes();
112   
113   /**
114    * {@inheritDoc}
115    */
116   @Override
117   protected String getTitleResult(ResultNode node) throws Exception {
118     return node.getTitle();
119   }
120   
121   /**
122    * {@inheritDoc}
123    */
124   protected String getImageUrl(Node node) {
125     return "/eXoSkin/skin/images/themes/default/Icons/TypeIcons/uiIconsType64x64.png";
126   }
127   
128   /**
129    * {@inheritDoc}
130    */
131   protected String getDetails(ResultNode retNode, SearchContext context) throws Exception {
132     DriveData driveData = documentService.getDriveOfNode(retNode.getPath(), ConversationState.getCurrent().getIdentity().getUserId(), Utils.getMemberships());
133     Calendar date = getDate(retNode);
134     return getDriveTitle(driveData) + fileSize(retNode) + formatDate(date);
135   } 
136 
137 }
138