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