1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
57  
58      public abstract Hits getHits(
59              long companyId, String keywords, int start, int end)
60          throws Exception;
61  
62      public abstract String getSearchPath();
63  
64      public abstract String getTitle(String keywords);
65  
66      public String search(
67              HttpServletRequest request, String keywords, int startPage,
68              int itemsPerPage)
69          throws SearchException {
70  
71          try {
72              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73                  WebKeys.THEME_DISPLAY);
74  
75              int start = (startPage * itemsPerPage) - itemsPerPage;
76              int end = startPage * itemsPerPage;
77  
78              Hits results = getHits(
79                  themeDisplay.getCompanyId(), keywords, start, end);
80  
81              int total = results.getLength();
82  
83              Object[] values = addSearchResults(
84                  keywords, startPage, itemsPerPage, total, start,
85                  getTitle(keywords), getSearchPath(), 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 groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
103 
104                 PortletURL portletURL = getPortletURL(
105                     request, portletId, groupId);
106 
107                 Indexer indexer = (Indexer)InstancePool.get(
108                     portlet.getIndexerClass());
109 
110                 DocumentSummary docSummary = indexer.getDocumentSummary(
111                     result, portletURL);
112 
113                 String title = docSummary.getTitle();
114                 String url = getURL(themeDisplay, groupId, result, portletURL);
115                 Date modifedDate = result.getDate(Field.MODIFIED);
116                 String content = docSummary.getContent();
117 
118                 String[] tags = new String[0];
119 
120                 Field tagsEntriesField = result.getFields().get(
121                     Field.TAGS_ENTRIES);
122 
123                 if (tagsEntriesField != null) {
124                     tags = tagsEntriesField.getValues();
125                 }
126 
127                 double ratings = 0.0;
128 
129                 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
130                 long entryClassPK = GetterUtil.getLong(
131                     result.get(Field.ENTRY_CLASS_PK));
132 
133                 if ((Validator.isNotNull(entryClassName)) &&
134                     (entryClassPK > 0)) {
135 
136                     RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
137                         entryClassName, entryClassPK);
138 
139                     ratings = stats.getTotalScore();
140                 }
141 
142                 double score = results.score(i);
143 
144                 addSearchResult(
145                     root, title, url, modifedDate, content, tags, ratings,
146                     score);
147             }
148 
149             if (_log.isDebugEnabled()) {
150                 _log.debug("Return\n" + doc.asXML());
151             }
152 
153             return doc.asXML();
154         }
155         catch (Exception e) {
156             throw new SearchException(e);
157         }
158     }
159 
160     protected String getURL(
161             ThemeDisplay themeDisplay, long groupId, Document result,
162             PortletURL portletURL)
163         throws Exception {
164 
165         return portletURL.toString();
166     }
167 
168     private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
169 
170 }