/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portlet.messageboards.util;
import com.liferay.message.boards.kernel.model.MBCategory;
import com.liferay.message.boards.kernel.model.MBCategoryConstants;
import com.liferay.message.boards.kernel.model.MBDiscussion;
import com.liferay.message.boards.kernel.model.MBThread;
import com.liferay.message.boards.kernel.service.MBCategoryLocalServiceUtil;
import com.liferay.message.boards.kernel.service.MBDiscussionLocalServiceUtil;
import com.liferay.message.boards.kernel.service.MBThreadLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.Property;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.search.BaseIndexer;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.search.filter.BooleanFilter;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import java.util.Locale;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
/**
* @author Eudaldo Alonso
*/
@OSGiBeanProperties
public class MBThreadIndexer extends BaseIndexer<MBThread> {
public static final String CLASS_NAME = MBThread.class.getName();
public MBThreadIndexer() {
setDefaultSelectedFieldNames(
Field.CLASS_NAME_ID, Field.CLASS_PK, Field.COMPANY_ID,
Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK, Field.UID);
setFilterSearch(true);
setPermissionAware(true);
}
@Override
public String getClassName() {
return CLASS_NAME;
}
@Override
public boolean hasPermission(
PermissionChecker permissionChecker, String entryClassName,
long entryClassPK, String actionId)
throws Exception {
return true;
}
@Override
public void postProcessContextBooleanFilter(
BooleanFilter contextBooleanFilter, SearchContext searchContext)
throws Exception {
addStatus(contextBooleanFilter, searchContext);
boolean discussion = GetterUtil.getBoolean(
searchContext.getAttribute("discussion"));
contextBooleanFilter.addRequiredTerm("discussion", discussion);
long endDate = GetterUtil.getLong(
searchContext.getAttribute("endDate"));
long startDate = GetterUtil.getLong(
searchContext.getAttribute("startDate"));
if ((endDate > 0) && (startDate > 0)) {
contextBooleanFilter.addRangeTerm(
"lastPostDate", startDate, endDate);
}
long participantUserId = GetterUtil.getLong(
searchContext.getAttribute("participantUserId"));
if (participantUserId > 0) {
contextBooleanFilter.addRequiredTerm(
"participantUserIds", participantUserId);
}
}
@Override
protected void doDelete(MBThread mbThread) throws Exception {
SearchContext searchContext = new SearchContext();
searchContext.setSearchEngineId(getSearchEngineId());
deleteDocument(mbThread.getCompanyId(), mbThread.getThreadId());
}
@Override
protected Document doGetDocument(MBThread mbThread) throws Exception {
Document document = getBaseModelDocument(CLASS_NAME, mbThread);
MBDiscussion discussion =
MBDiscussionLocalServiceUtil.fetchThreadDiscussion(
mbThread.getThreadId());
if (discussion == null) {
document.addKeyword("discussion", false);
}
else {
document.addKeyword("discussion", true);
}
document.addKeyword(
"lastPostDate", mbThread.getLastPostDate().getTime());
document.addKeyword(
"participantUserIds", mbThread.getParticipantUserIds());
return document;
}
@Override
protected Summary doGetSummary(
Document document, Locale locale, String snippet,
PortletRequest portletRequest, PortletResponse portletResponse) {
return null;
}
@Override
protected void doReindex(MBThread mbThread) throws Exception {
Document document = getDocument(mbThread);
IndexWriterHelperUtil.updateDocument(
getSearchEngineId(), mbThread.getCompanyId(), document,
isCommitImmediately());
}
@Override
protected void doReindex(String className, long classPK) throws Exception {
MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
doReindex(thread);
}
@Override
protected void doReindex(String[] ids) throws Exception {
long companyId = GetterUtil.getLong(ids[0]);
reindexCategories(companyId);
reindexDiscussions(companyId);
reindexRoot(companyId);
}
protected void reindexCategories(final long companyId)
throws PortalException {
ActionableDynamicQuery actionableDynamicQuery =
MBCategoryLocalServiceUtil.getActionableDynamicQuery();
actionableDynamicQuery.setCompanyId(companyId);
actionableDynamicQuery.setPerformActionMethod(
new ActionableDynamicQuery.PerformActionMethod<MBCategory>() {
@Override
public void performAction(MBCategory category)
throws PortalException {
reindexThreads(
companyId, category.getGroupId(),
category.getCategoryId());
}
});
actionableDynamicQuery.performActions();
}
protected void reindexDiscussions(final long companyId)
throws PortalException {
ActionableDynamicQuery actionableDynamicQuery =
GroupLocalServiceUtil.getActionableDynamicQuery();
actionableDynamicQuery.setCompanyId(companyId);
actionableDynamicQuery.setPerformActionMethod(
new ActionableDynamicQuery.PerformActionMethod<Group>() {
@Override
public void performAction(Group group) throws PortalException {
reindexThreads(
companyId, group.getGroupId(),
MBCategoryConstants.DISCUSSION_CATEGORY_ID);
}
});
actionableDynamicQuery.performActions();
}
protected void reindexRoot(final long companyId) throws PortalException {
ActionableDynamicQuery actionableDynamicQuery =
GroupLocalServiceUtil.getActionableDynamicQuery();
actionableDynamicQuery.setCompanyId(companyId);
actionableDynamicQuery.setPerformActionMethod(
new ActionableDynamicQuery.PerformActionMethod<Group>() {
@Override
public void performAction(Group group) throws PortalException {
reindexThreads(
companyId, group.getGroupId(),
MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
}
});
actionableDynamicQuery.performActions();
}
protected void reindexThreads(
long companyId, long groupId, final long categoryId)
throws PortalException {
final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
MBThreadLocalServiceUtil.getIndexableActionableDynamicQuery();
indexableActionableDynamicQuery.setAddCriteriaMethod(
new ActionableDynamicQuery.AddCriteriaMethod() {
@Override
public void addCriteria(DynamicQuery dynamicQuery) {
Property categoryIdProperty = PropertyFactoryUtil.forName(
"categoryId");
dynamicQuery.add(categoryIdProperty.eq(categoryId));
Property statusProperty = PropertyFactoryUtil.forName(
"status");
dynamicQuery.add(
statusProperty.eq(WorkflowConstants.STATUS_APPROVED));
}
});
indexableActionableDynamicQuery.setCompanyId(companyId);
indexableActionableDynamicQuery.setGroupId(groupId);
indexableActionableDynamicQuery.setPerformActionMethod(
new ActionableDynamicQuery.PerformActionMethod<MBThread>() {
@Override
public void performAction(MBThread thread) {
try {
Document document = getDocument(thread);
indexableActionableDynamicQuery.addDocuments(document);
}
catch (PortalException pe) {
if (_log.isWarnEnabled()) {
_log.warn(
"Unable to index message boards thread " +
thread.getThreadId(),
pe);
}
}
}
});
indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
indexableActionableDynamicQuery.performActions();
}
private static final Log _log = LogFactoryUtil.getLog(
MBThreadIndexer.class);
}