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.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.util.PortalUtil;
020    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
021    import com.liferay.portlet.asset.model.AssetRenderer;
022    import com.liferay.portlet.asset.model.AssetRendererFactory;
023    
024    import java.util.Locale;
025    
026    import javax.portlet.PortletRequest;
027    import javax.portlet.PortletResponse;
028    
029    /**
030     * @author Adolfo P??rez
031     */
032    public abstract class BaseSearchResultManager implements SearchResultManager {
033    
034            public static final int SUMMARY_MAX_CONTENT_LENGTH = 200;
035    
036            @Override
037            public SearchResult createSearchResult(Document document) {
038                    long classNameId = GetterUtil.getLong(
039                            document.get(Field.CLASS_NAME_ID));
040                    long classPK = GetterUtil.getLong(document.get(Field.CLASS_PK));
041    
042                    if ((classPK > 0) && (classNameId > 0)) {
043                            String className = PortalUtil.getClassName(classNameId);
044    
045                            return new SearchResult(className, classPK);
046                    }
047    
048                    String entryClassName = GetterUtil.getString(
049                            document.get(Field.ENTRY_CLASS_NAME));
050                    long entryClassPK = GetterUtil.getLong(
051                            document.get(Field.ENTRY_CLASS_PK));
052    
053                    return new SearchResult(entryClassName, entryClassPK);
054            }
055    
056            @Override
057            public void updateSearchResult(
058                            SearchResult searchResult, Document document, Locale locale,
059                            PortletRequest portletRequest, PortletResponse portletResponse)
060                    throws PortalException {
061    
062                    long classNameId = GetterUtil.getLong(
063                            document.get(Field.CLASS_NAME_ID));
064                    long classPK = GetterUtil.getLong(document.get(Field.CLASS_PK));
065    
066                    if ((classPK > 0) && (classNameId > 0)) {
067                            addRelatedModel(
068                                    searchResult, document, locale, portletRequest,
069                                    portletResponse);
070    
071                            if (searchResult.getSummary() == null) {
072                                    Summary summary = getSummary(
073                                            searchResult.getClassName(), searchResult.getClassPK(),
074                                            locale);
075    
076                                    searchResult.setSummary(summary);
077                            }
078                    }
079                    else {
080                            String entryClassName = GetterUtil.getString(
081                                    document.get(Field.ENTRY_CLASS_NAME));
082                            long entryClassPK = GetterUtil.getLong(
083                                    document.get(Field.ENTRY_CLASS_PK));
084    
085                            searchResult.setSummary(
086                                    getSummary(
087                                            document, entryClassName, entryClassPK, locale,
088                                            portletRequest, portletResponse));
089                    }
090            }
091    
092            protected static Summary getSummary(
093                            Document document, String className, long classPK, Locale locale,
094                            PortletRequest portletRequest, PortletResponse portletResponse)
095                    throws PortalException {
096    
097                    Indexer indexer = IndexerRegistryUtil.getIndexer(className);
098    
099                    if (indexer != null) {
100                            String snippet = document.get(Field.SNIPPET);
101    
102                            return indexer.getSummary(
103                                    document, snippet, portletRequest, portletResponse);
104                    }
105    
106                    return getSummary(className, classPK, locale);
107            }
108    
109            protected static Summary getSummary(
110                            String className, long classPK, Locale locale)
111                    throws PortalException {
112    
113                    AssetRendererFactory assetRendererFactory =
114                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
115                                    className);
116    
117                    if (assetRendererFactory == null) {
118                            return null;
119                    }
120    
121                    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
122                            classPK);
123    
124                    if (assetRenderer == null) {
125                            return null;
126                    }
127    
128                    Summary summary = new Summary(
129                            assetRenderer.getTitle(locale),
130                            assetRenderer.getSearchSummary(locale));
131    
132                    summary.setMaxContentLength(SUMMARY_MAX_CONTENT_LENGTH);
133    
134                    return summary;
135            }
136    
137            /**
138             * @throws PortalException
139             */
140            protected void addRelatedModel(
141                            SearchResult searchResult, Document document, Locale locale,
142                            PortletRequest portletRequest, PortletResponse portletResponse)
143                    throws PortalException {
144            }
145    
146    }