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.search;
016    
017    import com.liferay.portal.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.comment.Comment;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.search.BaseSearchResultManager;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.SearchResult;
025    import com.liferay.portal.kernel.search.SearchResultManager;
026    import com.liferay.portal.kernel.search.Summary;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.ClassName;
030    import com.liferay.portal.service.ClassNameLocalService;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
033    import com.liferay.portlet.documentlibrary.service.DLAppLocalService;
034    import com.liferay.portlet.messageboards.comment.MBCommentImpl;
035    import com.liferay.portlet.messageboards.model.MBMessage;
036    import com.liferay.portlet.messageboards.service.MBMessageLocalService;
037    import com.liferay.registry.ServiceReference;
038    import com.liferay.registry.collections.ServiceReferenceMapper;
039    import com.liferay.registry.collections.ServiceTrackerCollections;
040    import com.liferay.registry.collections.ServiceTrackerMap;
041    
042    import java.util.Locale;
043    
044    import javax.portlet.PortletRequest;
045    import javax.portlet.PortletResponse;
046    
047    /**
048     * @author Adolfo P??rez
049     */
050    public class SearchResultManagerImpl implements SearchResultManager {
051    
052            public SearchResultManagerImpl() {
053            }
054    
055            public SearchResultManagerImpl(
056                    ClassNameLocalService classNameLocalService,
057                    DLAppLocalService dlAppLocalService,
058                    MBMessageLocalService mbMessageLocalService) {
059    
060                    this.classNameLocalService = classNameLocalService;
061                    this.dlAppLocalService = dlAppLocalService;
062                    this.mbMessageLocalService = mbMessageLocalService;
063            }
064    
065            @Override
066            public SearchResult createSearchResult(Document document)
067                    throws PortalException {
068    
069                    SearchResultManager searchResultManager = _getSearchResultManager(
070                            document);
071    
072                    return searchResultManager.createSearchResult(document);
073            }
074    
075            @Override
076            public void updateSearchResult(
077                            SearchResult searchResult, Document document, Locale locale,
078                            PortletRequest portletRequest, PortletResponse portletResponse)
079                    throws PortalException {
080    
081                    SearchResultManager searchResultManager = _getSearchResultManager(
082                            document);
083    
084                    searchResultManager.updateSearchResult(
085                            searchResult, document, locale, portletRequest, portletResponse);
086            }
087    
088            @BeanReference(type = ClassNameLocalService.class)
089            protected ClassNameLocalService classNameLocalService;
090    
091            @BeanReference(type = DLAppLocalService.class)
092            protected DLAppLocalService dlAppLocalService;
093    
094            @BeanReference(type = MBMessageLocalService.class)
095            protected MBMessageLocalService mbMessageLocalService;
096    
097            private SearchResultManager _getSearchResultManager(Document document) {
098                    String entryClassName = GetterUtil.getString(
099                            document.get(Field.ENTRY_CLASS_NAME));
100    
101                    SearchResultManager searchResultManager = _serviceTrackerMap.getService(
102                            entryClassName);
103    
104                    if (searchResultManager == null) {
105                            if (entryClassName.equals(DLFileEntryConstants.getClassName())) {
106                                    return new DLFileEntrySearchResultManager();
107                            }
108                            else if (entryClassName.equals(MBMessage.class.getName())) {
109                                    return new MBMessageSearchResultManager();
110                            }
111                            else {
112                                    return new DefaultSearchResultManagerImpl();
113                            }
114                    }
115    
116                    return searchResultManager;
117            }
118    
119            private final ServiceTrackerMap<String, SearchResultManager>
120                    _serviceTrackerMap = ServiceTrackerCollections.singleValueMap(
121                            SearchResultManager.class, "(model.className=*)",
122                            new ServiceReferenceMapper<String, SearchResultManager>() {
123    
124                                    @Override
125                                    public void map(
126                                            ServiceReference<SearchResultManager> serviceReference,
127                                            Emitter<String> emitter) {
128    
129                                            Object modelClassName = serviceReference.getProperty(
130                                                    "model.className");
131    
132                                            emitter.emit((String)modelClassName);
133                                    }
134    
135                            });
136    
137            private static class DefaultSearchResultManagerImpl
138                    extends BaseSearchResultManager {
139    
140                    @Override
141                    public SearchResult createSearchResult(Document document) {
142                            String entryClassName = GetterUtil.getString(
143                                    document.get(Field.ENTRY_CLASS_NAME));
144                            long entryClassPK = GetterUtil.getLong(
145                                    document.get(Field.ENTRY_CLASS_PK));
146    
147                            return new SearchResult(entryClassName, entryClassPK);
148                    }
149    
150                    @Override
151                    public void updateSearchResult(
152                                    SearchResult searchResult, Document document, Locale locale,
153                                    PortletRequest portletRequest, PortletResponse portletResponse)
154                            throws PortalException {
155    
156                            String entryClassName = GetterUtil.getString(
157                                    document.get(Field.ENTRY_CLASS_NAME));
158                            long entryClassPK = GetterUtil.getLong(
159                                    document.get(Field.ENTRY_CLASS_PK));
160    
161                            searchResult.setSummary(
162                                    getSummary(
163                                            document, entryClassName, entryClassPK, locale,
164                                            portletRequest, portletResponse));
165                    }
166    
167            }
168    
169            private class DLFileEntrySearchResultManager
170                    extends BaseSearchResultManager {
171    
172                    @Override
173                    protected void addRelatedModel(
174                                    SearchResult searchResult, Document document, Locale locale,
175                                    PortletRequest portletRequest, PortletResponse portletResponse)
176                            throws PortalException {
177    
178                            long entryClassPK = GetterUtil.getLong(
179                                    document.get(Field.ENTRY_CLASS_PK));
180    
181                            FileEntry fileEntry = dlAppLocalService.getFileEntry(entryClassPK);
182    
183                            if (fileEntry != null) {
184                                    Summary summary = getSummary(
185                                            document, DLFileEntry.class.getName(),
186                                            fileEntry.getFileEntryId(), locale, portletRequest,
187                                            portletResponse);
188    
189                                    if (Validator.isNull(summary.getContent())) {
190                                            summary.setContent(fileEntry.getTitle());
191                                    }
192    
193                                    searchResult.addFileEntry(fileEntry, summary);
194                            }
195                            else {
196                                    long classNameId = GetterUtil.getLong(
197                                            document.get(Field.CLASS_NAME_ID));
198                                    long classPK = GetterUtil.getLong(document.get(Field.CLASS_PK));
199    
200                                    ClassName className = classNameLocalService.getClassName(
201                                            classNameId);
202    
203                                    Summary summary = getSummary(
204                                            document, className.getClassName(), classPK, locale,
205                                            portletRequest, portletResponse);
206    
207                                    searchResult.setSummary(summary);
208                            }
209                    }
210    
211            }
212    
213            private class MBMessageSearchResultManager extends BaseSearchResultManager {
214    
215                    @Override
216                    protected void addRelatedModel(
217                                    SearchResult searchResult, Document document, Locale locale,
218                                    PortletRequest portletRequest, PortletResponse portletResponse)
219                            throws PortalException {
220    
221                            long entryClassPK = GetterUtil.getLong(
222                                    document.get(Field.ENTRY_CLASS_PK));
223    
224                            MBMessage mbMessage = mbMessageLocalService.getMessage(
225                                    entryClassPK);
226    
227                            Comment comment = new MBCommentImpl(mbMessage);
228    
229                            Summary summary = new Summary(null, mbMessage.getBody());
230    
231                            summary.setEscape(false);
232    
233                            searchResult.addComment(comment, summary);
234                    }
235    
236            }
237    
238    }