1
14
15 package com.liferay.portal.search;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.search.BaseOpenSearchImpl;
20 import com.liferay.portal.kernel.search.Document;
21 import com.liferay.portal.kernel.search.Field;
22 import com.liferay.portal.kernel.search.Hits;
23 import com.liferay.portal.kernel.search.Indexer;
24 import com.liferay.portal.kernel.search.SearchException;
25 import com.liferay.portal.kernel.search.Summary;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.InstancePool;
28 import com.liferay.portal.kernel.util.StringBundler;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.kernel.xml.Element;
32 import com.liferay.portal.model.Layout;
33 import com.liferay.portal.model.Portlet;
34 import com.liferay.portal.service.CompanyLocalServiceUtil;
35 import com.liferay.portal.service.LayoutLocalServiceUtil;
36 import com.liferay.portal.service.PortletLocalServiceUtil;
37 import com.liferay.portal.theme.ThemeDisplay;
38 import com.liferay.portal.util.PortalUtil;
39 import com.liferay.portal.util.PortletKeys;
40 import com.liferay.portal.util.WebKeys;
41 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
42
43 import java.util.Date;
44 import java.util.List;
45
46 import javax.portlet.PortletURL;
47
48 import javax.servlet.http.HttpServletRequest;
49
50
56 public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
57
58 public static final String SEARCH_PATH = "/c/search/open_search";
59
60 public String search(
61 HttpServletRequest request, long groupId, long userId,
62 String keywords, int startPage, int itemsPerPage, String format)
63 throws SearchException {
64
65 try {
66 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
67 WebKeys.THEME_DISPLAY);
68
69 int start = (startPage * itemsPerPage) - itemsPerPage;
70 int end = startPage * itemsPerPage;
71
72 Hits results = CompanyLocalServiceUtil.search(
73 themeDisplay.getCompanyId(), userId, keywords, start, end);
74
75 String[] queryTerms = results.getQueryTerms();
76
77 int total = results.getLength();
78
79 Object[] values = addSearchResults(
80 queryTerms, keywords, startPage, itemsPerPage, total, start,
81 "Liferay Portal Search: " + keywords, SEARCH_PATH, format,
82 themeDisplay);
83
84 com.liferay.portal.kernel.xml.Document doc =
85 (com.liferay.portal.kernel.xml.Document)values[0];
86 Element root = (Element)values[1];
87
88 for (int i = 0; i < results.getDocs().length; i++) {
89 Document result = results.doc(i);
90
91 String portletId = result.get(Field.PORTLET_ID);
92
93 Portlet portlet = PortletLocalServiceUtil.getPortletById(
94 themeDisplay.getCompanyId(), portletId);
95
96 if (portlet == null) {
97 continue;
98 }
99
100 String portletTitle = PortalUtil.getPortletTitle(
101 portletId, themeDisplay.getUser());
102
103 long resultGroupId = GetterUtil.getLong(
104 result.get(Field.GROUP_ID));
105
106 String title = StringPool.BLANK;
107
108 PortletURL portletURL = getPortletURL(
109 request, portletId, resultGroupId);
110
111 String url = portletURL.toString();
112
113 Date modifedDate = result.getDate(Field.MODIFIED);
114
115 String content = StringPool.BLANK;
116
117 if (Validator.isNotNull(portlet.getIndexerClass())) {
118 Indexer indexer = (Indexer)InstancePool.get(
119 portlet.getIndexerClass());
120
121 String snippet = results.snippet(i);
122
123 Summary summary = indexer.getSummary(
124 result, snippet, portletURL);
125
126 title = summary.getTitle();
127 url = portletURL.toString();
128 content = summary.getContent();
129
130 if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
131 url = getJournalURL(
132 themeDisplay, resultGroupId, result);
133 }
134 }
135
136 double score = results.score(i);
137
138 addSearchResult(
139 root, groupId, portletTitle + " » " + title, url,
140 modifedDate, content, score, format);
141 }
142
143 if (_log.isDebugEnabled()) {
144 _log.debug("Return\n" + doc.asXML());
145 }
146
147 return doc.asXML();
148
149 }
150 catch (Exception e) {
151 throw new SearchException(e);
152 }
153 }
154
155 protected String getJournalURL(
156 ThemeDisplay themeDisplay, long groupId, Document result)
157 throws Exception {
158
159 Layout layout = themeDisplay.getLayout();
160
161 String articleId = result.get(Field.ENTRY_CLASS_PK);
162 String version = result.get("version");
163
164 List<Long> hitLayoutIds =
165 JournalContentSearchLocalServiceUtil.getLayoutIds(
166 layout.getGroupId(), layout.isPrivateLayout(), articleId);
167
168 if (hitLayoutIds.size() > 0) {
169 Long hitLayoutId = hitLayoutIds.get(0);
170
171 Layout hitLayout = LayoutLocalServiceUtil.getLayout(
172 layout.getGroupId(), layout.isPrivateLayout(),
173 hitLayoutId.longValue());
174
175 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
176 }
177 else {
178 StringBundler sb = new StringBundler(7);
179
180 sb.append(themeDisplay.getPathMain());
181 sb.append("/journal/view_article_content?groupId=");
182 sb.append(groupId);
183 sb.append("&articleId=");
184 sb.append(articleId);
185 sb.append("&version=");
186 sb.append(version);
187
188 return sb.toString();
189 }
190 }
191
192 private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
193
194 }