001
014
015 package com.liferay.portal.search;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.search.BaseOpenSearchImpl;
020 import com.liferay.portal.kernel.search.Document;
021 import com.liferay.portal.kernel.search.Field;
022 import com.liferay.portal.kernel.search.Hits;
023 import com.liferay.portal.kernel.search.Indexer;
024 import com.liferay.portal.kernel.search.SearchException;
025 import com.liferay.portal.kernel.search.Summary;
026 import com.liferay.portal.kernel.util.CharPool;
027 import com.liferay.portal.kernel.util.GetterUtil;
028 import com.liferay.portal.kernel.util.InstancePool;
029 import com.liferay.portal.kernel.util.StringBundler;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.xml.Element;
033 import com.liferay.portal.model.Layout;
034 import com.liferay.portal.model.Portlet;
035 import com.liferay.portal.service.CompanyLocalServiceUtil;
036 import com.liferay.portal.service.LayoutLocalServiceUtil;
037 import com.liferay.portal.service.PortletLocalServiceUtil;
038 import com.liferay.portal.theme.ThemeDisplay;
039 import com.liferay.portal.util.PortalUtil;
040 import com.liferay.portal.util.PortletKeys;
041 import com.liferay.portal.util.WebKeys;
042 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
043
044 import java.util.Date;
045 import java.util.List;
046
047 import javax.portlet.PortletURL;
048
049 import javax.servlet.http.HttpServletRequest;
050
051
055 public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
056
057 public static final String SEARCH_PATH = "/c/search/open_search";
058
059 public String search(
060 HttpServletRequest request, long groupId, long userId,
061 String keywords, int startPage, int itemsPerPage, String format)
062 throws SearchException {
063
064 try {
065 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
066 WebKeys.THEME_DISPLAY);
067
068 int start = (startPage * itemsPerPage) - itemsPerPage;
069 int end = startPage * itemsPerPage;
070
071 Hits results = CompanyLocalServiceUtil.search(
072 themeDisplay.getCompanyId(), userId, keywords, start, end);
073
074 String[] queryTerms = results.getQueryTerms();
075
076 int total = results.getLength();
077
078 Object[] values = addSearchResults(
079 queryTerms, keywords, startPage, itemsPerPage, total, start,
080 "Liferay Portal Search: " + keywords, SEARCH_PATH, format,
081 themeDisplay);
082
083 com.liferay.portal.kernel.xml.Document doc =
084 (com.liferay.portal.kernel.xml.Document)values[0];
085 Element root = (Element)values[1];
086
087 for (int i = 0; i < results.getDocs().length; i++) {
088 Document result = results.doc(i);
089
090 String portletId = result.get(Field.PORTLET_ID);
091
092 Portlet portlet = PortletLocalServiceUtil.getPortletById(
093 themeDisplay.getCompanyId(), portletId);
094
095 if (portlet == null) {
096 continue;
097 }
098
099 String portletTitle = PortalUtil.getPortletTitle(
100 portletId, themeDisplay.getUser());
101
102 long resultGroupId = GetterUtil.getLong(
103 result.get(Field.GROUP_ID));
104
105 long resultScopeGroupId = GetterUtil.getLong(
106 result.get(Field.SCOPE_GROUP_ID));
107
108 String entryClassName = GetterUtil.getString(
109 result.get(Field.ENTRY_CLASS_NAME));
110
111 long entryClassPK = GetterUtil.getLong(
112 result.get(Field.ENTRY_CLASS_PK));
113
114 String title = StringPool.BLANK;
115
116 PortletURL portletURL = getPortletURL(
117 request, portletId, resultGroupId);
118
119 String url = portletURL.toString();
120
121 Date modifedDate = result.getDate(Field.MODIFIED);
122
123 String content = StringPool.BLANK;
124
125 if (Validator.isNotNull(portlet.getIndexerClass())) {
126 Indexer indexer = (Indexer)InstancePool.get(
127 portlet.getIndexerClass());
128
129 String snippet = results.snippet(i);
130
131 Summary summary = indexer.getSummary(
132 result, snippet, portletURL);
133
134 title = summary.getTitle();
135 url = portletURL.toString();
136 content = summary.getContent();
137
138 if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
139 url = getJournalURL(
140 themeDisplay, resultGroupId, result);
141 }
142 }
143
144 double score = results.score(i);
145
146 addSearchResult(
147 root, resultGroupId, resultScopeGroupId, entryClassName,
148 entryClassPK,
149 portletTitle + " " + CharPool.RAQUO + " " + title, url,
150 modifedDate, content, score, format);
151 }
152
153 if (_log.isDebugEnabled()) {
154 _log.debug("Return\n" + doc.asXML());
155 }
156
157 return doc.asXML();
158
159 }
160 catch (Exception e) {
161 throw new SearchException(e);
162 }
163 }
164
165 protected String getJournalURL(
166 ThemeDisplay themeDisplay, long groupId, Document result)
167 throws Exception {
168
169 Layout layout = themeDisplay.getLayout();
170
171 String articleId = result.get(Field.ENTRY_CLASS_PK);
172 String version = result.get("version");
173
174 List<Long> hitLayoutIds =
175 JournalContentSearchLocalServiceUtil.getLayoutIds(
176 layout.getGroupId(), layout.isPrivateLayout(), articleId);
177
178 if (hitLayoutIds.size() > 0) {
179 Long hitLayoutId = hitLayoutIds.get(0);
180
181 Layout hitLayout = LayoutLocalServiceUtil.getLayout(
182 layout.getGroupId(), layout.isPrivateLayout(),
183 hitLayoutId.longValue());
184
185 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
186 }
187 else {
188 StringBundler sb = new StringBundler(7);
189
190 sb.append(themeDisplay.getPathMain());
191 sb.append("/journal/view_article_content?groupId=");
192 sb.append(groupId);
193 sb.append("&articleId=");
194 sb.append(articleId);
195 sb.append("&version=");
196 sb.append(version);
197
198 return sb.toString();
199 }
200 }
201
202 private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
203
204 }