1
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
57 public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
58
59 public abstract Hits getHits(
60 long companyId, long groupId, long userId, String keywords,
61 int start, int end)
62 throws Exception;
63
64 public abstract String getSearchPath();
65
66 public abstract String getTitle(String keywords);
67
68 public String search(
69 HttpServletRequest request, long groupId, long userId,
70 String keywords, int startPage, int itemsPerPage, String format)
71 throws SearchException {
72
73 try {
74 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
75 WebKeys.THEME_DISPLAY);
76
77 int start = (startPage * itemsPerPage) - itemsPerPage;
78 int end = startPage * itemsPerPage;
79
80 Hits results = getHits(
81 themeDisplay.getCompanyId(), groupId, userId, keywords, start,
82 end);
83
84 int total = results.getLength();
85
86 Object[] values = addSearchResults(
87 keywords, startPage, itemsPerPage, total, start,
88 getTitle(keywords), getSearchPath(), format, themeDisplay);
89
90 com.liferay.portal.kernel.xml.Document doc =
91 (com.liferay.portal.kernel.xml.Document)values[0];
92 Element root = (Element)values[1];
93
94 for (int i = 0; i < results.getDocs().length; i++) {
95 Document result = results.doc(i);
96
97 String portletId = result.get(Field.PORTLET_ID);
98
99 Portlet portlet = PortletLocalServiceUtil.getPortletById(
100 themeDisplay.getCompanyId(), portletId);
101
102
105 long resultGroupId = GetterUtil.getLong(
106 result.get(Field.GROUP_ID));
107
108 PortletURL portletURL = getPortletURL(
109 request, portletId, resultGroupId);
110
111 Indexer indexer = (Indexer)InstancePool.get(
112 portlet.getIndexerClass());
113
114 DocumentSummary docSummary = indexer.getDocumentSummary(
115 result, portletURL);
116
117 String title = docSummary.getTitle();
118 String url = getURL(
119 themeDisplay, resultGroupId, result, portletURL);
120 Date modifedDate = result.getDate(Field.MODIFIED);
121 String content = docSummary.getContent();
122
123 String[] tags = new String[0];
124
125 Field tagsEntriesField = result.getFields().get(
126 Field.TAGS_ENTRIES);
127
128 if (tagsEntriesField != null) {
129 tags = tagsEntriesField.getValues();
130 }
131
132 double ratings = 0.0;
133
134 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
135 long entryClassPK = GetterUtil.getLong(
136 result.get(Field.ENTRY_CLASS_PK));
137
138 if ((Validator.isNotNull(entryClassName)) &&
139 (entryClassPK > 0)) {
140
141 RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
142 entryClassName, entryClassPK);
143
144 ratings = stats.getTotalScore();
145 }
146
147 double score = results.score(i);
148
149 addSearchResult(
150 root, title, url, modifedDate, content, tags, ratings,
151 score, format);
152 }
153
154 if (_log.isDebugEnabled()) {
155 _log.debug("Return\n" + doc.asXML());
156 }
157
158 return doc.asXML();
159 }
160 catch (Exception e) {
161 throw new SearchException(e);
162 }
163 }
164
165 protected String getURL(
166 ThemeDisplay themeDisplay, long groupId, Document result,
167 PortletURL portletURL)
168 throws Exception {
169
170 return portletURL.toString();
171 }
172
173 private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
174
175 }