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