1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.search;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.search.Document;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.Hits;
22  import com.liferay.portal.kernel.search.Indexer;
23  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
24  import com.liferay.portal.kernel.search.SearchContext;
25  import com.liferay.portal.kernel.search.SearchException;
26  import com.liferay.portal.kernel.search.Summary;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.kernel.util.WebKeys;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.service.PortletLocalServiceUtil;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portlet.ratings.model.RatingsStats;
35  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
36  
37  import java.util.Date;
38  
39  import javax.portlet.PortletURL;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  /**
44   * <a href="HitsOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Charles May
47   * @author Brian Wing Shun Chan
48   */
49  public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
50  
51      public abstract String getPortletId();
52  
53      public abstract String getSearchPath();
54  
55      public abstract String getTitle(String keywords);
56  
57      public String search(
58              HttpServletRequest request, long groupId, long userId,
59              String keywords, int startPage, int itemsPerPage, String format)
60          throws SearchException {
61  
62          try {
63              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
64                  WebKeys.THEME_DISPLAY);
65  
66              int start = (startPage * itemsPerPage) - itemsPerPage;
67              int end = startPage * itemsPerPage;
68  
69              SearchContext searchContext = SearchContextFactory.getInstance(
70                  request);
71  
72              searchContext.setGroupId(groupId);
73              searchContext.setEnd(end);
74              searchContext.setKeywords(keywords);
75              searchContext.setScopeStrict(false);
76              searchContext.setStart(start);
77              searchContext.setUserId(userId);
78  
79              Portlet portlet = PortletLocalServiceUtil.getPortletById(
80                  themeDisplay.getCompanyId(), getPortletId());
81  
82              Indexer indexer = IndexerRegistryUtil.getIndexer(
83                  portlet.getIndexerClass());
84  
85              Hits results = indexer.search(searchContext);
86  
87              String[] queryTerms = results.getQueryTerms();
88  
89              int total = results.getLength();
90  
91              Object[] values = addSearchResults(
92                  queryTerms, keywords, startPage, itemsPerPage, total, start,
93                  getTitle(keywords), getSearchPath(), format, themeDisplay);
94  
95              com.liferay.portal.kernel.xml.Document doc =
96                  (com.liferay.portal.kernel.xml.Document)values[0];
97              Element root = (Element)values[1];
98  
99              for (int i = 0; i < results.getDocs().length; i++) {
100                 Document result = results.doc(i);
101 
102                 String portletId = result.get(Field.PORTLET_ID);
103 
104                 String snippet = results.snippet(i);
105 
106                 long resultGroupId = GetterUtil.getLong(
107                     result.get(Field.GROUP_ID));
108 
109                 PortletURL portletURL = getPortletURL(
110                     request, portletId, resultGroupId);
111 
112                 Summary summary = indexer.getSummary(
113                     result, snippet, portletURL);
114 
115                 String title = summary.getTitle();
116                 String url = getURL(
117                     themeDisplay, resultGroupId, result, portletURL);
118                 Date modifedDate = result.getDate(Field.MODIFIED);
119                 String content = summary.getContent();
120 
121                 String[] tags = new String[0];
122 
123                 Field assetTagNamesField = result.getFields().get(
124                     Field.ASSET_TAG_NAMES);
125 
126                 if (assetTagNamesField != null) {
127                     tags = assetTagNamesField.getValues();
128                 }
129 
130                 double ratings = 0.0;
131 
132                 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
133                 long entryClassPK = GetterUtil.getLong(
134                     result.get(Field.ENTRY_CLASS_PK));
135 
136                 if ((Validator.isNotNull(entryClassName)) &&
137                     (entryClassPK > 0)) {
138 
139                     RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
140                         entryClassName, entryClassPK);
141 
142                     ratings = stats.getTotalScore();
143                 }
144 
145                 double score = results.score(i);
146 
147                 addSearchResult(
148                     root, title, url, modifedDate, content, tags, ratings,
149                     score, format);
150             }
151 
152             if (_log.isDebugEnabled()) {
153                 _log.debug("Return\n" + doc.asXML());
154             }
155 
156             return doc.asXML();
157         }
158         catch (Exception e) {
159             throw new SearchException(e);
160         }
161     }
162 
163     protected String getURL(
164             ThemeDisplay themeDisplay, long groupId, Document result,
165             PortletURL portletURL)
166         throws Exception {
167 
168         return portletURL.toString();
169     }
170 
171     private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
172 
173 }