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.util.ArrayList;
20  import java.util.List;
21  import java.util.Locale;
22  
23  import javax.jcr.Node;
24  import javax.jcr.RepositoryException;
25  
26  import org.apache.commons.lang.LocaleUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.exoplatform.commons.api.search.data.SearchContext;
29  import org.exoplatform.container.xml.InitParams;
30  import org.exoplatform.services.cms.drives.DriveData;
31  import org.exoplatform.services.cms.impl.Utils;
32  import org.exoplatform.services.security.ConversationState;
33  import org.exoplatform.services.wcm.search.QueryCriteria;
34  import org.exoplatform.services.wcm.search.ResultNode;
35  import org.exoplatform.services.wcm.search.base.AbstractPageList;
36  import org.exoplatform.services.wcm.search.base.ArrayNodePageList;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  
39  /**
40   * The search should be capable to search for site pages.
41   */
42  public class PageSearchServiceConnector extends BaseSearchServiceConnector {
43  
44    public PageSearchServiceConnector(InitParams initParams) throws Exception {
45      super(initParams);
46    }
47  
48    /**
49     * {@inheritDoc}
50     */
51    @Override
52    protected QueryCriteria createQueryCriteria(String query,
53                                                long offset,
54                                                long limit,
55                                                String sort,
56                                                String order) {
57      QueryCriteria criteria = new QueryCriteria();
58      //set content types
59      criteria.setContentTypes(new String[] {"gtn:language", "exo:pageMetadata"});
60      criteria.setFulltextSearchProperty(new String[] {"exo:metaKeywords", "exo:metaDescription", "gtn:name"});
61      criteria.setKeyword(removeAccents(Utils.escapeIllegalCharacterInQuery(query).toLowerCase()));
62      criteria.setSearchWebpage(true);
63      criteria.setSearchDocument(false);
64      criteria.setSearchWebContent(false);
65      criteria.setLiveMode(true);
66      criteria.setOffset(offset);
67      criteria.setLimit(limit);
68      criteria.setSortBy(sort);
69      criteria.setOrderBy(order);
70      if(query.contains("~")) {
71        criteria.setFuzzySearch(true);
72      }
73      return criteria;
74    }
75  
76    @Override
77    protected AbstractPageList<ResultNode> searchNodes(QueryCriteria criteria, SearchContext context) throws Exception {
78      if (StringUtils.isBlank(criteria.getSiteName())) {//return empty list of result
79        return new ArrayNodePageList<ResultNode>(
80            new ArrayList<ResultNode>(), (int)criteria.getLimit());
81      } else {
82        String[] siteNames = criteria.getSiteName().split(",");
83        String localeParam = context.getParamValue(SearchContext.RouterParams.LANG.create());
84        Locale locale = localeParam != null ? LocaleUtils.toLocale(localeParam) : null;
85        if (siteNames.length == 1) {//just search for 1 site
86          return siteSearch_.searchPageContents(WCMCoreUtils.getUserSessionProvider(),
87                                                criteria, locale,(int)criteria.getLimit(), false);
88        } else {//search for many sites
89          int limit = (int)criteria.getLimit();
90          int offset = (int)criteria.getOffset();
91          criteria.setOffset(0);
92          List<ResultNode> ret = new ArrayList<ResultNode>();
93  
94          for (String site : siteNames) {
95            criteria.setSiteName(site);
96            AbstractPageList<ResultNode> resultList = 
97                siteSearch_.searchPageContents(WCMCoreUtils.getUserSessionProvider(),
98                                                  criteria, locale, (int)criteria.getLimit(), false);
99            if (resultList.getAvailable() <= offset) {
100             offset -= resultList.getAvailable();
101           } else if (resultList.getAvailable() > offset) {
102             for (int i = 0; i < limit; i++)
103               if (offset + i < resultList.getAvailable()) {
104                 ResultNode resNode = resultList.getPage( ( offset + i) / resultList.getPageSize() + 1).
105                                                 get((offset + i) % resultList.getPageSize());
106                 ret.add(resNode);
107                 if (limit == 0) break;
108               }
109           }
110           if (limit == 0) break;
111         }
112         return new ArrayNodePageList<ResultNode>(ret, (int)criteria.getLimit());
113       }
114     }
115   }
116 
117   @Override
118   protected ResultNode filterNode(ResultNode node) throws RepositoryException {
119     return node;
120   }
121   
122   @Override
123   protected String getPath(ResultNode node, SearchContext context) throws Exception {
124     return node.getUserNavigationURI();
125   }
126 
127   @Override
128   protected String getFileType(ResultNode node) throws Exception {
129     return "FileDefault";
130   }
131   
132   /**
133    * {@inheritDoc}
134    */
135   @Override
136   protected String getTitleResult(ResultNode node) throws Exception {
137     try {
138       return ((Node)node.getSession().getItem(node.getProperty("mop:link/mop:page")
139                                      .getString())).getProperty("gtn:name").getString();
140     } catch (Exception e) {
141       return node.getTitle();
142     }
143   }
144   
145   /**
146    * {@inheritDoc}
147    */
148   @Override
149   protected String getImageUrl(Node node) {
150     return "/eXoSkin/skin/images/system/unified-search/page.png";
151   }
152   
153   /**
154    * {@inheritDoc}
155    */
156   protected String getDetails(ResultNode retNode, SearchContext context) throws Exception {
157     DriveData driveData = documentService.getDriveOfNode(retNode.getPath(), ConversationState.getCurrent().getIdentity().getUserId(), Utils.getMemberships());
158     return getDriveTitle(driveData);
159   }
160   
161 }