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.kernel.search;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.util.WebKeys;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.service.PortletLocalServiceUtil;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portlet.ratings.model.RatingsStats;
27  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
28  
29  import java.util.Date;
30  
31  import javax.portlet.PortletURL;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  /**
36   * <a href="HitsOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Charles May
39   * @author Brian Wing Shun Chan
40   */
41  public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
42  
43      public abstract String getPortletId();
44  
45      public abstract String getSearchPath();
46  
47      public Summary getSummary(
48          Indexer indexer, Document document, String snippet,
49          PortletURL portletURL) {
50  
51          return indexer.getSummary(document, snippet, portletURL);
52      }
53  
54      public abstract String getTitle(String keywords);
55  
56      public String search(
57              HttpServletRequest request, long groupId, long userId,
58              String keywords, int startPage, int itemsPerPage, String format)
59          throws SearchException {
60  
61          try {
62              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
63                  WebKeys.THEME_DISPLAY);
64  
65              int start = (startPage * itemsPerPage) - itemsPerPage;
66              int end = startPage * itemsPerPage;
67  
68              SearchContext searchContext = SearchContextFactory.getInstance(
69                  request);
70  
71              searchContext.setGroupIds(new long[] {groupId});
72              searchContext.setEnd(end);
73              searchContext.setKeywords(keywords);
74              searchContext.setScopeStrict(false);
75              searchContext.setStart(start);
76              searchContext.setUserId(userId);
77  
78              Portlet portlet = PortletLocalServiceUtil.getPortletById(
79                  themeDisplay.getCompanyId(), getPortletId());
80  
81              Indexer indexer = portlet.getIndexerInstance();
82  
83              Hits results = indexer.search(searchContext);
84  
85              String[] queryTerms = results.getQueryTerms();
86  
87              int total = results.getLength();
88  
89              Object[] values = addSearchResults(
90                  queryTerms, keywords, startPage, itemsPerPage, total, start,
91                  getTitle(keywords), getSearchPath(), format, themeDisplay);
92  
93              com.liferay.portal.kernel.xml.Document doc =
94                  (com.liferay.portal.kernel.xml.Document)values[0];
95              Element root = (Element)values[1];
96  
97              for (int i = 0; i < results.getDocs().length; i++) {
98                  Document result = results.doc(i);
99  
100                 String portletId = result.get(Field.PORTLET_ID);
101 
102                 String snippet = results.snippet(i);
103 
104                 long resultGroupId = GetterUtil.getLong(
105                     result.get(Field.GROUP_ID));
106 
107                 PortletURL portletURL = getPortletURL(
108                     request, portletId, resultGroupId);
109 
110                 Summary summary = getSummary(
111                     indexer, result, snippet, portletURL);
112 
113                 String title = summary.getTitle();
114                 String url = getURL(
115                     themeDisplay, resultGroupId, result, portletURL);
116                 Date modifedDate = result.getDate(Field.MODIFIED);
117                 String content = summary.getContent();
118 
119                 String[] tags = new String[0];
120 
121                 Field assetTagNamesField = result.getFields().get(
122                     Field.ASSET_TAG_NAMES);
123 
124                 if (assetTagNamesField != null) {
125                     tags = assetTagNamesField.getValues();
126                 }
127 
128                 double ratings = 0.0;
129 
130                 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
131                 long entryClassPK = GetterUtil.getLong(
132                     result.get(Field.ENTRY_CLASS_PK));
133 
134                 if ((Validator.isNotNull(entryClassName)) &&
135                     (entryClassPK > 0)) {
136 
137                     RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
138                         entryClassName, entryClassPK);
139 
140                     ratings = stats.getTotalScore();
141                 }
142 
143                 double score = results.score(i);
144 
145                 addSearchResult(
146                     root, resultGroupId, title, url, modifedDate, content, tags,
147                     ratings, score, format);
148             }
149 
150             if (_log.isDebugEnabled()) {
151                 _log.debug("Return\n" + doc.asXML());
152             }
153 
154             return doc.asXML();
155         }
156         catch (Exception e) {
157             throw new SearchException(e);
158         }
159     }
160 
161     protected String getURL(
162             ThemeDisplay themeDisplay, long groupId, Document result,
163             PortletURL portletURL)
164         throws Exception {
165 
166         return portletURL.toString();
167     }
168 
169     private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
170 
171 }