001    /**
002     * Copyright (c) 2000-present 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    import java.util.Locale;
025    
026    import javax.portlet.PortletRequest;
027    import javax.portlet.PortletResponse;
028    
029    /**
030     * @author Eudaldo Alonso
031     */
032    public class SearchResultUtil {
033    
034            public static List<SearchResult> getSearchResults(
035                    Hits hits, Locale locale) {
036    
037                    return getSearchResults(hits, locale, null, null);
038            }
039    
040            public static List<SearchResult> getSearchResults(
041                    Hits hits, Locale locale, PortletRequest portletRequest,
042                    PortletResponse portletResponse) {
043    
044                    List<SearchResult> searchResults = new ArrayList<>();
045    
046                    for (Document document : hits.getDocs()) {
047                            try {
048                                    SearchResult searchResult =
049                                            SearchResultManagerUtil.createSearchResult(document);
050    
051                                    int index = searchResults.indexOf(searchResult);
052    
053                                    if (index < 0) {
054                                            searchResults.add(searchResult);
055                                    }
056                                    else {
057                                            searchResult = searchResults.get(index);
058                                    }
059    
060                                    String version = document.get(Field.VERSION);
061    
062                                    if (Validator.isNotNull(version)) {
063                                            searchResult.addVersion(version);
064                                    }
065    
066                                    SearchResultManagerUtil.updateSearchResult(
067                                            searchResult, document, locale, portletRequest,
068                                            portletResponse);
069                            }
070                            catch (Exception e) {
071                                    if (_log.isWarnEnabled()) {
072                                            long entryClassPK = GetterUtil.getLong(
073                                                    document.get(Field.ENTRY_CLASS_PK));
074    
075                                            _log.warn(
076                                                    "Search index is stale and contains entry {" +
077                                                            entryClassPK + "}");
078                                    }
079                            }
080                    }
081    
082                    return searchResults;
083            }
084    
085            private static final Log _log = LogFactoryUtil.getLog(
086                    SearchResultUtil.class);
087    
088    }