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