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.portlet.messageboards.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
020    import com.liferay.portal.kernel.dao.orm.Property;
021    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.search.BaseIndexer;
026    import com.liferay.portal.kernel.search.Document;
027    import com.liferay.portal.kernel.search.DocumentImpl;
028    import com.liferay.portal.kernel.search.Field;
029    import com.liferay.portal.kernel.search.SearchContext;
030    import com.liferay.portal.kernel.search.SearchEngineUtil;
031    import com.liferay.portal.kernel.search.Summary;
032    import com.liferay.portal.kernel.search.filter.BooleanFilter;
033    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
034    import com.liferay.portal.kernel.util.GetterUtil;
035    import com.liferay.portal.kernel.workflow.WorkflowConstants;
036    import com.liferay.portal.model.Group;
037    import com.liferay.portal.security.permission.PermissionChecker;
038    import com.liferay.portal.service.GroupLocalServiceUtil;
039    import com.liferay.portlet.messageboards.model.MBCategory;
040    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
041    import com.liferay.portlet.messageboards.model.MBDiscussion;
042    import com.liferay.portlet.messageboards.model.MBThread;
043    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
044    import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
045    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
046    
047    import java.util.Locale;
048    
049    import javax.portlet.PortletRequest;
050    import javax.portlet.PortletResponse;
051    
052    /**
053     * @author Eudaldo Alonso
054     */
055    @OSGiBeanProperties
056    public class MBThreadIndexer extends BaseIndexer<MBThread> {
057    
058            public static final String CLASS_NAME = MBThread.class.getName();
059    
060            public MBThreadIndexer() {
061                    setDefaultSelectedFieldNames(
062                            Field.CLASS_NAME_ID, Field.CLASS_PK, Field.COMPANY_ID,
063                            Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK, Field.UID);
064                    setFilterSearch(true);
065                    setPermissionAware(true);
066            }
067    
068            @Override
069            public String getClassName() {
070                    return CLASS_NAME;
071            }
072    
073            @Override
074            public boolean hasPermission(
075                            PermissionChecker permissionChecker, String entryClassName,
076                            long entryClassPK, String actionId)
077                    throws Exception {
078    
079                    return true;
080            }
081    
082            @Override
083            public void postProcessContextBooleanFilter(
084                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
085                    throws Exception {
086    
087                    addStatus(contextBooleanFilter, searchContext);
088    
089                    boolean discussion = GetterUtil.getBoolean(
090                            searchContext.getAttribute("discussion"), false);
091    
092                    contextBooleanFilter.addRequiredTerm("discussion", discussion);
093    
094                    long endDate = GetterUtil.getLong(
095                            searchContext.getAttribute("endDate"));
096                    long startDate = GetterUtil.getLong(
097                            searchContext.getAttribute("startDate"));
098    
099                    if ((endDate > 0) && (startDate > 0)) {
100                            contextBooleanFilter.addRangeTerm(
101                                    "lastPostDate", startDate, endDate);
102                    }
103    
104                    long participantUserId = GetterUtil.getLong(
105                            searchContext.getAttribute("participantUserId"));
106    
107                    if (participantUserId > 0) {
108                            contextBooleanFilter.addRequiredTerm(
109                                    "participantUserIds", participantUserId);
110                    }
111            }
112    
113            @Override
114            protected void doDelete(MBThread mbThread) throws Exception {
115                    SearchContext searchContext = new SearchContext();
116    
117                    searchContext.setSearchEngineId(getSearchEngineId());
118    
119                    Document document = new DocumentImpl();
120    
121                    document.addUID(CLASS_NAME, mbThread.getThreadId());
122    
123                    SearchEngineUtil.deleteDocument(
124                            getSearchEngineId(), mbThread.getCompanyId(),
125                            document.get(Field.UID), isCommitImmediately());
126            }
127    
128            @Override
129            protected Document doGetDocument(MBThread mbThread) throws Exception {
130                    Document document = getBaseModelDocument(CLASS_NAME, mbThread);
131    
132                    MBDiscussion discussion =
133                            MBDiscussionLocalServiceUtil.fetchThreadDiscussion(
134                                    mbThread.getThreadId());
135    
136                    if (discussion == null) {
137                            document.addKeyword("discussion", false);
138                    }
139                    else {
140                            document.addKeyword("discussion", true);
141                    }
142    
143                    document.addKeyword(
144                            "lastPostDate", mbThread.getLastPostDate().getTime());
145                    document.addKeyword(
146                            "participantUserIds", mbThread.getParticipantUserIds());
147    
148                    return document;
149            }
150    
151            @Override
152            protected Summary doGetSummary(
153                    Document document, Locale locale, String snippet,
154                    PortletRequest portletRequest, PortletResponse portletResponse) {
155    
156                    return null;
157            }
158    
159            @Override
160            protected void doReindex(MBThread mbThread) throws Exception {
161                    Document document = getDocument(mbThread);
162    
163                    SearchEngineUtil.updateDocument(
164                            getSearchEngineId(), mbThread.getCompanyId(), document,
165                            isCommitImmediately());
166            }
167    
168            @Override
169            protected void doReindex(String className, long classPK) throws Exception {
170                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
171    
172                    doReindex(thread);
173            }
174    
175            @Override
176            protected void doReindex(String[] ids) throws Exception {
177                    long companyId = GetterUtil.getLong(ids[0]);
178    
179                    reindexCategories(companyId);
180                    reindexDiscussions(companyId);
181                    reindexRoot(companyId);
182            }
183    
184            protected void reindexCategories(final long companyId)
185                    throws PortalException {
186    
187                    ActionableDynamicQuery actionableDynamicQuery =
188                            MBCategoryLocalServiceUtil.getActionableDynamicQuery();
189    
190                    actionableDynamicQuery.setCompanyId(companyId);
191                    actionableDynamicQuery.setPerformActionMethod(
192                            new ActionableDynamicQuery.PerformActionMethod<MBCategory>() {
193    
194                                    @Override
195                                    public void performAction(MBCategory category)
196                                            throws PortalException {
197    
198                                            reindexThreads(
199                                                    companyId, category.getGroupId(),
200                                                    category.getCategoryId());
201                                    }
202    
203                            });
204    
205                    actionableDynamicQuery.performActions();
206            }
207    
208            protected void reindexDiscussions(final long companyId)
209                    throws PortalException {
210    
211                    ActionableDynamicQuery actionableDynamicQuery =
212                            GroupLocalServiceUtil.getActionableDynamicQuery();
213    
214                    actionableDynamicQuery.setCompanyId(companyId);
215                    actionableDynamicQuery.setPerformActionMethod(
216                            new ActionableDynamicQuery.PerformActionMethod<Group>() {
217    
218                                    @Override
219                                    public void performAction(Group group) throws PortalException {
220                                            reindexThreads(
221                                                    companyId, group.getGroupId(),
222                                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID);
223                                    }
224    
225                            });
226    
227                    actionableDynamicQuery.performActions();
228            }
229    
230            protected void reindexRoot(final long companyId) throws PortalException {
231                    ActionableDynamicQuery actionableDynamicQuery =
232                            GroupLocalServiceUtil.getActionableDynamicQuery();
233    
234                    actionableDynamicQuery.setCompanyId(companyId);
235                    actionableDynamicQuery.setPerformActionMethod(
236                            new ActionableDynamicQuery.PerformActionMethod<Group>() {
237    
238                                    @Override
239                                    public void performAction(Group group) throws PortalException {
240                                            reindexThreads(
241                                                    companyId, group.getGroupId(),
242                                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
243                                    }
244    
245                            });
246    
247                    actionableDynamicQuery.performActions();
248            }
249    
250            protected void reindexThreads(
251                            long companyId, long groupId, final long categoryId)
252                    throws PortalException {
253    
254                    final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
255                            MBThreadLocalServiceUtil.getIndexableActionableDynamicQuery();
256    
257                    indexableActionableDynamicQuery.setAddCriteriaMethod(
258                            new ActionableDynamicQuery.AddCriteriaMethod() {
259    
260                                    @Override
261                                    public void addCriteria(DynamicQuery dynamicQuery) {
262                                            Property categoryIdProperty = PropertyFactoryUtil.forName(
263                                                    "categoryId");
264    
265                                            dynamicQuery.add(categoryIdProperty.eq(categoryId));
266    
267                                            Property statusProperty = PropertyFactoryUtil.forName(
268                                                    "status");
269    
270                                            dynamicQuery.add(
271                                                    statusProperty.eq(WorkflowConstants.STATUS_APPROVED));
272                                    }
273    
274                            });
275                    indexableActionableDynamicQuery.setCompanyId(companyId);
276                    indexableActionableDynamicQuery.setGroupId(groupId);
277                    indexableActionableDynamicQuery.setPerformActionMethod(
278                            new ActionableDynamicQuery.PerformActionMethod<MBThread>() {
279    
280                                    @Override
281                                    public void performAction(MBThread thread) {
282                                            try {
283                                                    Document document = getDocument(thread);
284    
285                                                    indexableActionableDynamicQuery.addDocuments(document);
286                                            }
287                                            catch (PortalException pe) {
288                                                    if (_log.isWarnEnabled()) {
289                                                            _log.warn(
290                                                                    "Unable to index message boards thread " +
291                                                                            thread.getThreadId(),
292                                                                    pe);
293                                                    }
294                                            }
295                                    }
296    
297                            });
298                    indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
299    
300                    indexableActionableDynamicQuery.performActions();
301            }
302    
303            private static final Log _log = LogFactoryUtil.getLog(
304                    MBThreadIndexer.class);
305    
306    }