1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.search;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.Document;
28  import com.liferay.portal.kernel.search.DocumentSummary;
29  import com.liferay.portal.kernel.search.Field;
30  import com.liferay.portal.kernel.search.Hits;
31  import com.liferay.portal.kernel.search.Indexer;
32  import com.liferay.portal.kernel.search.SearchException;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.InstancePool;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.kernel.xml.Element;
37  import com.liferay.portal.model.Portlet;
38  import com.liferay.portal.service.PortletLocalServiceUtil;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.ratings.model.RatingsStats;
42  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
43  
44  import java.util.Date;
45  
46  import javax.portlet.PortletURL;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  /**
51   * <a href="HitsOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Charles May
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
58  
59      public abstract Hits getHits(
60              long companyId, String keywords, int start, int end)
61          throws Exception;
62  
63      public abstract String getSearchPath();
64  
65      public abstract String getTitle(String keywords);
66  
67      public String search(
68              HttpServletRequest request, String keywords, int startPage,
69              int itemsPerPage, String format)
70          throws SearchException {
71  
72          try {
73              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
74                  WebKeys.THEME_DISPLAY);
75  
76              int start = (startPage * itemsPerPage) - itemsPerPage;
77              int end = startPage * itemsPerPage;
78  
79              Hits results = getHits(
80                  themeDisplay.getCompanyId(), keywords, start, end);
81  
82              int total = results.getLength();
83  
84              Object[] values = addSearchResults(
85                  keywords, startPage, itemsPerPage, total, start,
86                  getTitle(keywords), getSearchPath(), format, themeDisplay);
87  
88              com.liferay.portal.kernel.xml.Document doc =
89                  (com.liferay.portal.kernel.xml.Document)values[0];
90              Element root = (Element)values[1];
91  
92              for (int i = 0; i < results.getDocs().length; i++) {
93                  Document result = results.doc(i);
94  
95                  String portletId = result.get(Field.PORTLET_ID);
96  
97                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
98                      themeDisplay.getCompanyId(), portletId);
99  
100                 //String portletTitle = PortalUtil.getPortletTitle(
101                 //  portletId, themeDisplay.getUser());
102 
103                 long groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
104 
105                 PortletURL portletURL = getPortletURL(
106                     request, portletId, groupId);
107 
108                 Indexer indexer = (Indexer)InstancePool.get(
109                     portlet.getIndexerClass());
110 
111                 DocumentSummary docSummary = indexer.getDocumentSummary(
112                     result, portletURL);
113 
114                 String title = docSummary.getTitle();
115                 String url = getURL(themeDisplay, groupId, result, portletURL);
116                 Date modifedDate = result.getDate(Field.MODIFIED);
117                 String content = docSummary.getContent();
118 
119                 String[] tags = new String[0];
120 
121                 Field tagsEntriesField = result.getFields().get(
122                     Field.TAGS_ENTRIES);
123 
124                 if (tagsEntriesField != null) {
125                     tags = tagsEntriesField.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, title, url, modifedDate, content, tags, ratings,
147                     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 }