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