001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.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.PortletURL;
037    
038    /**
039     * @author Eudaldo Alonso
040     */
041    public class SearchResultUtil {
042    
043            public static List<SearchResult> getSearchResults(
044                    Hits hits, Locale locale, PortletURL portletURL) {
045    
046                    List<SearchResult> searchResults = new ArrayList<SearchResult>();
047    
048                    for (Document document : hits.getDocs()) {
049                            String entryClassName = GetterUtil.getString(
050                                    document.get(Field.ENTRY_CLASS_NAME));
051                            long entryClassPK = GetterUtil.getLong(
052                                    document.get(Field.ENTRY_CLASS_PK));
053    
054                            try {
055                                    String className = entryClassName;
056                                    long classPK = entryClassPK;
057    
058                                    FileEntry fileEntry = null;
059                                    MBMessage mbMessage = null;
060    
061                                    if (entryClassName.equals(DLFileEntry.class.getName()) ||
062                                            entryClassName.equals(MBMessage.class.getName())) {
063    
064                                            classPK = GetterUtil.getLong(document.get(Field.CLASS_PK));
065                                            long classNameId = GetterUtil.getLong(
066                                                    document.get(Field.CLASS_NAME_ID));
067    
068                                            if ((classPK > 0) && (classNameId > 0)) {
069                                                    className = PortalUtil.getClassName(classNameId);
070    
071                                                    if (entryClassName.equals(
072                                                                    DLFileEntry.class.getName())) {
073    
074                                                            fileEntry = DLAppLocalServiceUtil.getFileEntry(
075                                                                    entryClassPK);
076                                                    }
077                                                    else if (entryClassName.equals(
078                                                                            MBMessage.class.getName())) {
079    
080                                                            mbMessage = MBMessageLocalServiceUtil.getMessage(
081                                                                    entryClassPK);
082                                                    }
083                                            }
084                                            else {
085                                                    className = entryClassName;
086                                                    classPK = entryClassPK;
087                                            }
088                                    }
089    
090                                    SearchResult searchResult = new SearchResult(
091                                            className, classPK);
092    
093                                    int index = searchResults.indexOf(searchResult);
094    
095                                    if (index < 0) {
096                                            searchResults.add(searchResult);
097                                    }
098                                    else {
099                                            searchResult = searchResults.get(index);
100                                    }
101    
102                                    if (fileEntry != null) {
103                                            Summary summary = getSummary(
104                                                    document, DLFileEntry.class.getName(),
105                                                    fileEntry.getFileEntryId(), locale, portletURL);
106    
107                                            searchResult.addFileEntry(fileEntry, summary);
108                                    }
109    
110                                    if (mbMessage != null) {
111                                            searchResult.addMBMessage(mbMessage);
112                                    }
113    
114                                    if ((mbMessage == null) && (fileEntry == null)) {
115                                            Summary summary = getSummary(
116                                                    document, className, classPK, locale, portletURL);
117    
118                                            searchResult.setSummary(summary);
119                                    }
120                                    else {
121                                            if (searchResult.getSummary() == null) {
122                                                    Summary summary = getSummary(
123                                                            className, classPK, locale, portletURL);
124    
125                                                    searchResult.setSummary(summary);
126                                            }
127                                    }
128                            }
129                            catch (Exception e) {
130                                    if (_log.isWarnEnabled()) {
131                                            _log.warn(
132                                                    "Search index is stale and contains entry {" +
133                                                            entryClassPK + "}");
134                                    }
135                            }
136                    }
137    
138                    return searchResults;
139            }
140    
141            protected static Summary getSummary(
142                            Document document, String className, long classPK, Locale locale,
143                            PortletURL portletURL)
144                    throws PortalException, SystemException {
145    
146                    Indexer indexer = IndexerRegistryUtil.getIndexer(className);
147    
148                    if (indexer != null) {
149                            String snippet = document.get(Field.SNIPPET);
150    
151                            return indexer.getSummary(document, locale, snippet, portletURL);
152                    }
153    
154                    return getSummary(className, classPK, locale, portletURL);
155            }
156    
157            protected static Summary getSummary(
158                            String className, long classPK, Locale locale,
159                            PortletURL portletURL)
160                    throws PortalException, SystemException {
161    
162                    AssetRendererFactory assetRendererFactory =
163                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
164                                    className);
165    
166                    if (assetRendererFactory == null) {
167                            return null;
168                    }
169    
170                    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
171                            classPK);
172    
173                    if (assetRenderer == null) {
174                            return null;
175                    }
176    
177                    Summary summary = new Summary(
178                            assetRenderer.getTitle(locale),
179                            assetRenderer.getSearchSummary(locale), portletURL);
180    
181                    summary.setMaxContentLength(200);
182                    summary.setPortletURL(portletURL);
183    
184                    return summary;
185            }
186    
187            private static Log _log = LogFactoryUtil.getLog(SearchResultUtil.class);
188    
189    }