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