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, String keywords, int startPage,
69 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(), 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 groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
109
110 String title = StringPool.BLANK;
111
112 PortletURL portletURL = getPortletURL(
113 request, portletId, groupId);
114
115 String url = portletURL.toString();
116
117 Date modifedDate = result.getDate(Field.MODIFIED);
118
119 String content = StringPool.BLANK;
120
121 if (Validator.isNotNull(portlet.getIndexerClass())) {
122 Indexer indexer = (Indexer)InstancePool.get(
123 portlet.getIndexerClass());
124
125 DocumentSummary docSummary = indexer.getDocumentSummary(
126 result, portletURL);
127
128 title = docSummary.getTitle();
129 url = portletURL.toString();
130 content = docSummary.getContent();
131
132 if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
133 url = getJournalURL(themeDisplay, groupId, result);
134 }
135 }
136
137 double score = results.score(i);
138
139 addSearchResult(
140 root, portletTitle + " » " + title, url, modifedDate,
141 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 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
177 }
178 else {
179 StringBuilder sb = new StringBuilder();
180
181 sb.append(themeDisplay.getPathMain());
182 sb.append("/journal/view_article_content?groupId=");
183 sb.append(groupId);
184 sb.append("&articleId=");
185 sb.append(articleId);
186 sb.append("&version=");
187 sb.append(version);
188
189 return sb.toString();
190 }
191 }
192
193 private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
194
195 }