1
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
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
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 }