1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.search;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.search.Document;
25  import com.liferay.portal.kernel.search.DocumentSummary;
26  import com.liferay.portal.kernel.search.Field;
27  import com.liferay.portal.kernel.search.Hits;
28  import com.liferay.portal.kernel.search.Indexer;
29  import com.liferay.portal.kernel.search.SearchException;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstancePool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.kernel.xml.Element;
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  /**
48   * <a href="HitsOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Charles May
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
55  
56      public abstract Hits getHits(
57              long companyId, String keywords, int start, int end)
58          throws Exception;
59  
60      public abstract String getSearchPath();
61  
62      public abstract String getTitle(String keywords);
63  
64      public String search(
65              HttpServletRequest request, String keywords, int startPage,
66              int itemsPerPage)
67          throws SearchException {
68  
69          try {
70              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71                  WebKeys.THEME_DISPLAY);
72  
73              int start = (startPage * itemsPerPage) - itemsPerPage;
74              int end = startPage * itemsPerPage;
75  
76              Hits results = getHits(
77                  themeDisplay.getCompanyId(), keywords, start, end);
78  
79              int total = results.getLength();
80  
81              Object[] values = addSearchResults(
82                  keywords, startPage, itemsPerPage, total, start,
83                  getTitle(keywords), getSearchPath(), themeDisplay);
84  
85              com.liferay.portal.kernel.xml.Document doc =
86                  (com.liferay.portal.kernel.xml.Document)values[0];
87              Element root = (Element)values[1];
88  
89              for (int i = 0; i < results.getDocs().length; i++) {
90                  Document result = results.doc(i);
91  
92                  String portletId = result.get(Field.PORTLET_ID);
93  
94                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
95                      themeDisplay.getCompanyId(), portletId);
96  
97                  //String portletTitle = PortalUtil.getPortletTitle(
98                  //  portletId, themeDisplay.getUser());
99  
100                 long groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
101 
102                 PortletURL portletURL = getPortletURL(
103                     request, portletId, groupId);
104 
105                 Indexer indexer = (Indexer)InstancePool.get(
106                     portlet.getIndexerClass());
107 
108                 DocumentSummary docSummary = indexer.getDocumentSummary(
109                     result, portletURL);
110 
111                 String title = docSummary.getTitle();
112                 String url = getURL(themeDisplay, groupId, result, portletURL);
113                 Date modifedDate = result.getDate(Field.MODIFIED);
114                 String content = docSummary.getContent();
115 
116                 String[] tags = new String[0];
117 
118                 Field tagsEntriesField = result.getFields().get(
119                     Field.TAGS_ENTRIES);
120 
121                 if (tagsEntriesField != null) {
122                     tags = tagsEntriesField.getValues();
123                 }
124 
125                 double ratings = 0.0;
126 
127                 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
128                 long entryClassPK = GetterUtil.getLong(
129                     result.get(Field.ENTRY_CLASS_PK));
130 
131                 if ((Validator.isNotNull(entryClassName)) &&
132                     (entryClassPK > 0)) {
133 
134                     RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
135                         entryClassName, entryClassPK);
136 
137                     ratings = stats.getTotalScore();
138                 }
139 
140                 double score = results.score(i);
141 
142                 addSearchResult(
143                     root, title, url, modifedDate, content, tags, ratings,
144                     score);
145             }
146 
147             if (_log.isDebugEnabled()) {
148                 _log.debug("Return\n" + doc.asXML());
149             }
150 
151             return doc.asXML();
152         }
153         catch (Exception e) {
154             throw new SearchException(e);
155         }
156     }
157 
158     protected String getURL(
159             ThemeDisplay themeDisplay, long groupId, Document result,
160             PortletURL portletURL)
161         throws Exception {
162 
163         return portletURL.toString();
164     }
165 
166     private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
167 
168 }