1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.DocumentSummary;
21  import com.liferay.portal.kernel.search.Field;
22  import com.liferay.portal.kernel.search.Hits;
23  import com.liferay.portal.kernel.search.Indexer;
24  import com.liferay.portal.kernel.search.SearchException;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Element;
29  import com.liferay.portal.model.Portlet;
30  import com.liferay.portal.service.PortletLocalServiceUtil;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.ratings.model.RatingsStats;
34  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
35  
36  import java.util.Date;
37  
38  import javax.portlet.PortletURL;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  /**
43   * <a href="HitsOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Charles May
46   * @author Brian Wing Shun Chan
47   */
48  public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
49  
50      public DocumentSummary getDocumentSummary(
51          Indexer indexer, Document document, String snippet,
52          PortletURL portletURL) {
53  
54          return indexer.getDocumentSummary(document, snippet, portletURL);
55      }
56  
57      public abstract Hits getHits(
58              long companyId, long groupId, long userId, String keywords,
59              int start, int end)
60          throws Exception;
61  
62      public abstract String getSearchPath();
63  
64      public abstract String getTitle(String keywords);
65  
66      public String search(
67              HttpServletRequest request, long groupId, long userId,
68              String keywords, int startPage, int itemsPerPage, String format)
69          throws SearchException {
70  
71          try {
72              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73                  WebKeys.THEME_DISPLAY);
74  
75              int start = (startPage * itemsPerPage) - itemsPerPage;
76              int end = startPage * itemsPerPage;
77  
78              Hits results = getHits(
79                  themeDisplay.getCompanyId(), groupId, userId, keywords, start,
80                  end);
81  
82              String[] queryTerms = results.getQueryTerms();
83  
84              int total = results.getLength();
85  
86              Object[] values = addSearchResults(
87                  queryTerms, keywords, startPage, itemsPerPage, total, start,
88                  getTitle(keywords), getSearchPath(), format, themeDisplay);
89  
90              com.liferay.portal.kernel.xml.Document doc =
91                  (com.liferay.portal.kernel.xml.Document)values[0];
92              Element root = (Element)values[1];
93  
94              for (int i = 0; i < results.getDocs().length; i++) {
95                  Document result = results.doc(i);
96  
97                  String portletId = result.get(Field.PORTLET_ID);
98  
99                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
100                     themeDisplay.getCompanyId(), portletId);
101 
102                 //String portletTitle = PortalUtil.getPortletTitle(
103                 //  portletId, themeDisplay.getUser());
104 
105                 String snippet = results.snippet(i);
106 
107                 long resultGroupId = GetterUtil.getLong(
108                     result.get(Field.GROUP_ID));
109 
110                 if (resultGroupId == 0) {
111                     resultGroupId = themeDisplay.getScopeGroupId();
112                 }
113 
114                 PortletURL portletURL = getPortletURL(
115                     request, portletId, resultGroupId);
116 
117                 Indexer indexer = (Indexer)InstancePool.get(
118                     portlet.getIndexerClass());
119 
120                 DocumentSummary docSummary = getDocumentSummary(
121                     indexer, result, snippet, portletURL);
122 
123                 String title = docSummary.getTitle();
124                 String url = getURL(
125                     themeDisplay, resultGroupId, result, portletURL);
126                 Date modifedDate = result.getDate(Field.MODIFIED);
127                 String content = docSummary.getContent();
128 
129                 String[] tags = new String[0];
130 
131                 Field tagsEntriesField = result.getFields().get(
132                     Field.TAGS_ENTRIES);
133 
134                 if (tagsEntriesField != null) {
135                     tags = tagsEntriesField.getValues();
136                 }
137 
138                 double ratings = 0.0;
139 
140                 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
141                 long entryClassPK = GetterUtil.getLong(
142                     result.get(Field.ENTRY_CLASS_PK));
143 
144                 if ((Validator.isNotNull(entryClassName)) &&
145                     (entryClassPK > 0)) {
146 
147                     RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
148                         entryClassName, entryClassPK);
149 
150                     ratings = stats.getTotalScore();
151                 }
152 
153                 double score = results.score(i);
154 
155                 addSearchResult(
156                     root, title, url, modifedDate, content, tags, ratings,
157                     score, format);
158             }
159 
160             if (_log.isDebugEnabled()) {
161                 _log.debug("Return\n" + doc.asXML());
162             }
163 
164             return doc.asXML();
165         }
166         catch (Exception e) {
167             throw new SearchException(e);
168         }
169     }
170 
171     protected String getURL(
172             ThemeDisplay themeDisplay, long groupId, Document result,
173             PortletURL portletURL)
174         throws Exception {
175 
176         return portletURL.toString();
177     }
178 
179     private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
180 
181 }