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.PortletURL;
038
039
042 public class SearchResultUtil {
043
044 public static List<SearchResult> getSearchResults(
045 Hits hits, Locale locale, PortletURL portletURL) {
046
047 List<SearchResult> searchResults = new ArrayList<SearchResult>();
048
049 for (Document document : hits.getDocs()) {
050 String entryClassName = GetterUtil.getString(
051 document.get(Field.ENTRY_CLASS_NAME));
052 long entryClassPK = GetterUtil.getLong(
053 document.get(Field.ENTRY_CLASS_PK));
054
055 try {
056 String className = entryClassName;
057 long classPK = entryClassPK;
058
059 FileEntry fileEntry = null;
060 MBMessage mbMessage = null;
061
062 if (entryClassName.equals(DLFileEntry.class.getName()) ||
063 entryClassName.equals(MBMessage.class.getName())) {
064
065 classPK = GetterUtil.getLong(document.get(Field.CLASS_PK));
066 long classNameId = GetterUtil.getLong(
067 document.get(Field.CLASS_NAME_ID));
068
069 if ((classPK > 0) && (classNameId > 0)) {
070 className = PortalUtil.getClassName(classNameId);
071
072 if (entryClassName.equals(
073 DLFileEntry.class.getName())) {
074
075 fileEntry = DLAppLocalServiceUtil.getFileEntry(
076 entryClassPK);
077 }
078 else if (entryClassName.equals(
079 MBMessage.class.getName())) {
080
081 mbMessage = MBMessageLocalServiceUtil.getMessage(
082 entryClassPK);
083 }
084 }
085 else {
086 className = entryClassName;
087 classPK = entryClassPK;
088 }
089 }
090
091 SearchResult searchResult = new SearchResult(
092 className, classPK);
093
094 int index = searchResults.indexOf(searchResult);
095
096 if (index < 0) {
097 searchResults.add(searchResult);
098 }
099 else {
100 searchResult = searchResults.get(index);
101 }
102
103 if (fileEntry != null) {
104 Summary summary = getSummary(
105 document, DLFileEntry.class.getName(),
106 fileEntry.getFileEntryId(), locale, portletURL);
107
108 searchResult.addFileEntry(fileEntry, summary);
109 }
110
111 if (mbMessage != null) {
112 searchResult.addMBMessage(mbMessage);
113 }
114
115 if (entryClassName.equals(JournalArticle.class.getName())) {
116 String version = document.get(Field.VERSION);
117
118 searchResult.addVersion(version);
119 }
120
121 if ((mbMessage == null) && (fileEntry == null)) {
122 Summary summary = getSummary(
123 document, className, classPK, locale, portletURL);
124
125 searchResult.setSummary(summary);
126 }
127 else {
128 if (searchResult.getSummary() == null) {
129 Summary summary = getSummary(
130 className, classPK, locale, portletURL);
131
132 searchResult.setSummary(summary);
133 }
134 }
135 }
136 catch (Exception e) {
137 if (_log.isWarnEnabled()) {
138 _log.warn(
139 "Search index is stale and contains entry {" +
140 entryClassPK + "}");
141 }
142 }
143 }
144
145 return searchResults;
146 }
147
148 protected static Summary getSummary(
149 Document document, String className, long classPK, Locale locale,
150 PortletURL portletURL)
151 throws PortalException, SystemException {
152
153 Indexer indexer = IndexerRegistryUtil.getIndexer(className);
154
155 if (indexer != null) {
156 String snippet = document.get(Field.SNIPPET);
157
158 return indexer.getSummary(document, locale, snippet, portletURL);
159 }
160
161 return getSummary(className, classPK, locale, portletURL);
162 }
163
164 protected static Summary getSummary(
165 String className, long classPK, Locale locale,
166 PortletURL portletURL)
167 throws PortalException, SystemException {
168
169 AssetRendererFactory assetRendererFactory =
170 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
171 className);
172
173 if (assetRendererFactory == null) {
174 return null;
175 }
176
177 AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
178 classPK);
179
180 if (assetRenderer == null) {
181 return null;
182 }
183
184 Summary summary = new Summary(
185 assetRenderer.getTitle(locale),
186 assetRenderer.getSearchSummary(locale), portletURL);
187
188 summary.setMaxContentLength(200);
189 summary.setPortletURL(portletURL);
190
191 return summary;
192 }
193
194 private static Log _log = LogFactoryUtil.getLog(SearchResultUtil.class);
195
196 }