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