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