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 abstract Hits getHits(
51              long companyId, long groupId, long userId, String keywords,
52              int start, int end)
53          throws Exception;
54  
55      public abstract String getSearchPath();
56  
57      public abstract String getTitle(String keywords);
58  
59      public String search(
60              HttpServletRequest request, long groupId, long userId,
61              String keywords, int startPage, int itemsPerPage, String format)
62          throws SearchException {
63  
64          try {
65              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66                  WebKeys.THEME_DISPLAY);
67  
68              int start = (startPage * itemsPerPage) - itemsPerPage;
69              int end = startPage * itemsPerPage;
70  
71              Hits results = getHits(
72                  themeDisplay.getCompanyId(), groupId, userId, keywords, start,
73                  end);
74  
75              String[] queryTerms = results.getQueryTerms();
76  
77              int total = results.getLength();
78  
79              Object[] values = addSearchResults(
80                  queryTerms, keywords, startPage, itemsPerPage, total, start,
81                  getTitle(keywords), getSearchPath(), format, themeDisplay);
82  
83              com.liferay.portal.kernel.xml.Document doc =
84                  (com.liferay.portal.kernel.xml.Document)values[0];
85              Element root = (Element)values[1];
86  
87              for (int i = 0; i < results.getDocs().length; i++) {
88                  Document result = results.doc(i);
89  
90                  String portletId = result.get(Field.PORTLET_ID);
91  
92                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
93                      themeDisplay.getCompanyId(), portletId);
94  
95                  //String portletTitle = PortalUtil.getPortletTitle(
96                  //  portletId, themeDisplay.getUser());
97  
98                  String snippet = results.snippet(i);
99  
100                 long resultGroupId = GetterUtil.getLong(
101                     result.get(Field.GROUP_ID));
102 
103                 PortletURL portletURL = getPortletURL(
104                     request, portletId, resultGroupId);
105 
106                 Indexer indexer = (Indexer)InstancePool.get(
107                     portlet.getIndexerClass());
108 
109                 DocumentSummary docSummary = indexer.getDocumentSummary(
110                     result, snippet, portletURL);
111 
112                 String title = docSummary.getTitle();
113                 String url = getURL(
114                     themeDisplay, resultGroupId, result, portletURL);
115                 Date modifedDate = result.getDate(Field.MODIFIED);
116                 String content = docSummary.getContent();
117 
118                 String[] tags = new String[0];
119 
120                 Field tagsEntriesField = result.getFields().get(
121                     Field.TAGS_ENTRIES);
122 
123                 if (tagsEntriesField != null) {
124                     tags = tagsEntriesField.getValues();
125                 }
126 
127                 double ratings = 0.0;
128 
129                 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
130                 long entryClassPK = GetterUtil.getLong(
131                     result.get(Field.ENTRY_CLASS_PK));
132 
133                 if ((Validator.isNotNull(entryClassName)) &&
134                     (entryClassPK > 0)) {
135 
136                     RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
137                         entryClassName, entryClassPK);
138 
139                     ratings = stats.getTotalScore();
140                 }
141 
142                 double score = results.score(i);
143 
144                 addSearchResult(
145                     root, title, url, modifedDate, content, tags, ratings,
146                     score, format);
147             }
148 
149             if (_log.isDebugEnabled()) {
150                 _log.debug("Return\n" + doc.asXML());
151             }
152 
153             return doc.asXML();
154         }
155         catch (Exception e) {
156             throw new SearchException(e);
157         }
158     }
159 
160     protected String getURL(
161             ThemeDisplay themeDisplay, long groupId, Document result,
162             PortletURL portletURL)
163         throws Exception {
164 
165         return portletURL.toString();
166     }
167 
168     private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
169 
170 }