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, long groupId, long userId, String keywords,
58              int start, int end)
59          throws Exception;
60  
61      public abstract String getSearchPath();
62  
63      public abstract String getTitle(String keywords);
64  
65      public String search(
66              HttpServletRequest request, long groupId, long userId,
67              String keywords, int startPage, int itemsPerPage, String format)
68          throws SearchException {
69  
70          try {
71              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
72                  WebKeys.THEME_DISPLAY);
73  
74              int start = (startPage * itemsPerPage) - itemsPerPage;
75              int end = startPage * itemsPerPage;
76  
77              Hits results = getHits(
78                  themeDisplay.getCompanyId(), groupId, userId, keywords, start,
79                  end);
80  
81              int total = results.getLength();
82  
83              Object[] values = addSearchResults(
84                  keywords, startPage, itemsPerPage, total, start,
85                  getTitle(keywords), getSearchPath(), format, themeDisplay);
86  
87              com.liferay.portal.kernel.xml.Document doc =
88                  (com.liferay.portal.kernel.xml.Document)values[0];
89              Element root = (Element)values[1];
90  
91              for (int i = 0; i < results.getDocs().length; i++) {
92                  Document result = results.doc(i);
93  
94                  String portletId = result.get(Field.PORTLET_ID);
95  
96                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
97                      themeDisplay.getCompanyId(), portletId);
98  
99                  //String portletTitle = PortalUtil.getPortletTitle(
100                 //  portletId, themeDisplay.getUser());
101 
102                 long resultGroupId = GetterUtil.getLong(
103                     result.get(Field.GROUP_ID));
104 
105                 PortletURL portletURL = getPortletURL(
106                     request, portletId, resultGroupId);
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(
116                     themeDisplay, resultGroupId, 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, format);
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 = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
171 
172 }