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