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 String entryClassName = GetterUtil.getString(
106 result.get(Field.ENTRY_CLASS_NAME));
107
108 long entryClassPK = GetterUtil.getLong(
109 result.get(Field.ENTRY_CLASS_PK));
110
111 String title = StringPool.BLANK;
112
113 PortletURL portletURL = getPortletURL(
114 request, portletId, resultGroupId);
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 String snippet = results.snippet(i);
127
128 Summary summary = indexer.getSummary(
129 result, snippet, portletURL);
130
131 title = summary.getTitle();
132 url = portletURL.toString();
133 content = summary.getContent();
134
135 if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
136 url = getJournalURL(
137 themeDisplay, resultGroupId, result);
138 }
139 }
140
141 double score = results.score(i);
142
143 addSearchResult(
144 root, resultGroupId, entryClassName, entryClassPK,
145 portletTitle + " " + CharPool.RAQUO + " " + title, url,
146 modifedDate, content, score, format);
147 }
148
149 if (_log.isDebugEnabled()) {
150 _log.debug("Return\n" + doc.asXML());
151 }
152
153 return doc.asXML();
154
155 }
156 catch (Exception e) {
157 throw new SearchException(e);
158 }
159 }
160
161 protected String getJournalURL(
162 ThemeDisplay themeDisplay, long groupId, Document result)
163 throws Exception {
164
165 Layout layout = themeDisplay.getLayout();
166
167 String articleId = result.get(Field.ENTRY_CLASS_PK);
168 String version = result.get("version");
169
170 List<Long> hitLayoutIds =
171 JournalContentSearchLocalServiceUtil.getLayoutIds(
172 layout.getGroupId(), layout.isPrivateLayout(), articleId);
173
174 if (hitLayoutIds.size() > 0) {
175 Long hitLayoutId = hitLayoutIds.get(0);
176
177 Layout hitLayout = LayoutLocalServiceUtil.getLayout(
178 layout.getGroupId(), layout.isPrivateLayout(),
179 hitLayoutId.longValue());
180
181 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
182 }
183 else {
184 StringBundler sb = new StringBundler(7);
185
186 sb.append(themeDisplay.getPathMain());
187 sb.append("/journal/view_article_content?groupId=");
188 sb.append(groupId);
189 sb.append("&articleId=");
190 sb.append(articleId);
191 sb.append("&version=");
192 sb.append(version);
193
194 return sb.toString();
195 }
196 }
197
198 private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
199
200 }