001
014
015 package com.liferay.portal.kernel.search;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.repository.model.FileEntry;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.util.PortalUtil;
023 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
024 import com.liferay.portlet.asset.model.AssetRenderer;
025 import com.liferay.portlet.asset.model.AssetRendererFactory;
026 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
027 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
028 import com.liferay.portlet.journal.model.JournalArticle;
029 import com.liferay.portlet.messageboards.model.MBMessage;
030 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
031
032 import java.util.ArrayList;
033 import java.util.List;
034 import java.util.Locale;
035
036 import javax.portlet.PortletRequest;
037 import javax.portlet.PortletResponse;
038 import javax.portlet.PortletURL;
039
040
043 public class SearchResultUtil {
044
045 public static List<SearchResult> getSearchResults(
046 Hits hits, Locale locale, PortletURL portletURL) {
047
048 return getSearchResults(hits, locale, portletURL, null, null);
049 }
050
051 public static List<SearchResult> getSearchResults(
052 Hits hits, Locale locale, PortletURL portletURL,
053 PortletRequest portletRequest, PortletResponse portletResponse) {
054
055 List<SearchResult> searchResults = new ArrayList<SearchResult>();
056
057 for (Document document : hits.getDocs()) {
058 String entryClassName = GetterUtil.getString(
059 document.get(Field.ENTRY_CLASS_NAME));
060 long entryClassPK = GetterUtil.getLong(
061 document.get(Field.ENTRY_CLASS_PK));
062
063 try {
064 String className = entryClassName;
065 long classPK = entryClassPK;
066
067 FileEntry fileEntry = null;
068 MBMessage mbMessage = null;
069
070 if (entryClassName.equals(DLFileEntry.class.getName()) ||
071 entryClassName.equals(MBMessage.class.getName())) {
072
073 classPK = GetterUtil.getLong(document.get(Field.CLASS_PK));
074 long classNameId = GetterUtil.getLong(
075 document.get(Field.CLASS_NAME_ID));
076
077 if ((classPK > 0) && (classNameId > 0)) {
078 className = PortalUtil.getClassName(classNameId);
079
080 if (entryClassName.equals(
081 DLFileEntry.class.getName())) {
082
083 fileEntry = DLAppLocalServiceUtil.getFileEntry(
084 entryClassPK);
085 }
086 else if (entryClassName.equals(
087 MBMessage.class.getName())) {
088
089 mbMessage = MBMessageLocalServiceUtil.getMessage(
090 entryClassPK);
091 }
092 }
093 else {
094 className = entryClassName;
095 classPK = entryClassPK;
096 }
097 }
098
099 SearchResult searchResult = new SearchResult(
100 className, classPK);
101
102 int index = searchResults.indexOf(searchResult);
103
104 if (index < 0) {
105 searchResults.add(searchResult);
106 }
107 else {
108 searchResult = searchResults.get(index);
109 }
110
111 if (fileEntry != null) {
112 Summary summary = getSummary(
113 document, DLFileEntry.class.getName(),
114 fileEntry.getFileEntryId(), locale, portletURL,
115 portletRequest, portletResponse);
116
117 searchResult.addFileEntry(fileEntry, summary);
118 }
119
120 if (mbMessage != null) {
121 searchResult.addMBMessage(mbMessage);
122 }
123
124 if (entryClassName.equals(JournalArticle.class.getName())) {
125 String version = document.get(Field.VERSION);
126
127 searchResult.addVersion(version);
128 }
129
130 if ((mbMessage == null) && (fileEntry == null)) {
131 Summary summary = getSummary(
132 document, className, classPK, locale, portletURL,
133 portletRequest, portletResponse);
134
135 searchResult.setSummary(summary);
136 }
137 else {
138 if (searchResult.getSummary() == null) {
139 Summary summary = getSummary(
140 className, classPK, locale, portletURL);
141
142 searchResult.setSummary(summary);
143 }
144 }
145 }
146 catch (Exception e) {
147 if (_log.isWarnEnabled()) {
148 _log.warn(
149 "Search index is stale and contains entry {" +
150 entryClassPK + "}");
151 }
152 }
153 }
154
155 return searchResults;
156 }
157
158 protected static Summary getSummary(
159 Document document, String className, long classPK, Locale locale,
160 PortletURL portletURL, PortletRequest portletRequest,
161 PortletResponse portletResponse)
162 throws PortalException {
163
164 Indexer indexer = IndexerRegistryUtil.getIndexer(className);
165
166 if (indexer != null) {
167 String snippet = document.get(Field.SNIPPET);
168
169 return indexer.getSummary(
170 document, snippet, portletURL, portletRequest, portletResponse);
171 }
172
173 return getSummary(className, classPK, locale, portletURL);
174 }
175
176 protected static Summary getSummary(
177 String className, long classPK, Locale locale,
178 PortletURL portletURL)
179 throws PortalException {
180
181 AssetRendererFactory assetRendererFactory =
182 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
183 className);
184
185 if (assetRendererFactory == null) {
186 return null;
187 }
188
189 AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
190 classPK);
191
192 if (assetRenderer == null) {
193 return null;
194 }
195
196 Summary summary = new Summary(
197 assetRenderer.getTitle(locale),
198 assetRenderer.getSearchSummary(locale), portletURL);
199
200 summary.setMaxContentLength(SUMMARY_MAX_CONTENT_LENGTH);
201 summary.setPortletURL(portletURL);
202
203 return summary;
204 }
205
206 protected static final int SUMMARY_MAX_CONTENT_LENGTH = 200;
207
208 private static final Log _log = LogFactoryUtil.getLog(
209 SearchResultUtil.class);
210
211 }