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
62 public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
63
64 public static final String SEARCH_PATH = "/c/search/open_search";
65
66 public String search(
67 HttpServletRequest request, String keywords, int startPage,
68 int itemsPerPage)
69 throws SearchException {
70
71 try {
72 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73 WebKeys.THEME_DISPLAY);
74
75 int start = (startPage * itemsPerPage) - itemsPerPage;
76 int end = startPage * itemsPerPage;
77
78 Hits results = CompanyLocalServiceUtil.search(
79 themeDisplay.getCompanyId(), keywords, start, end);
80
81 int total = results.getLength();
82
83 Object[] values = addSearchResults(
84 keywords, startPage, itemsPerPage, total, start,
85 "Liferay Portal Search: " + keywords, SEARCH_PATH,
86 themeDisplay);
87
88 com.liferay.portal.kernel.xml.Document doc =
89 (com.liferay.portal.kernel.xml.Document)values[0];
90 Element root = (Element)values[1];
91
92 for (int i = 0; i < results.getDocs().length; i++) {
93 Document result = results.doc(i);
94
95 String portletId = result.get(Field.PORTLET_ID);
96
97 Portlet portlet = PortletLocalServiceUtil.getPortletById(
98 themeDisplay.getCompanyId(), portletId);
99
100 if (portlet == null) {
101 continue;
102 }
103
104 String portletTitle = PortalUtil.getPortletTitle(
105 portletId, themeDisplay.getUser());
106
107 long groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
108
109 String title = StringPool.BLANK;
110
111 PortletURL portletURL = getPortletURL(
112 request, portletId, groupId);
113
114 String url = portletURL.toString();
115
116 Date modifedDate = result.getDate(Field.MODIFIED);
117
118 String content = StringPool.BLANK;
119
120 if (Validator.isNotNull(portlet.getIndexerClass())) {
121 Indexer indexer = (Indexer)InstancePool.get(
122 portlet.getIndexerClass());
123
124 DocumentSummary docSummary = indexer.getDocumentSummary(
125 result, 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(themeDisplay, groupId, result);
133 }
134 }
135
136 double score = results.score(i);
137
138 addSearchResult(
139 root, portletTitle + " » " + title, url, modifedDate,
140 content, score);
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 StringBuilder sb = new StringBuilder();
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 }