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