/** * 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.blogs.internal.search; import com.liferay.blogs.model.BlogsEntry; import com.liferay.blogs.service.BlogsEntryLocalService; import com.liferay.blogs.service.permission.BlogsEntryPermission; 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.search.BaseIndexer; import com.liferay.portal.kernel.search.Document; import com.liferay.portal.kernel.search.Field; import com.liferay.portal.kernel.search.IndexWriterHelper; import com.liferay.portal.kernel.search.Indexer; 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.ActionKeys; import com.liferay.portal.kernel.security.permission.PermissionChecker; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.HtmlUtil; import com.liferay.portal.kernel.workflow.WorkflowConstants; import java.util.Date; import java.util.Locale; import javax.portlet.PortletRequest; import javax.portlet.PortletResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Brian Wing Shun Chan * @author Harry Mark * @author Bruno Farache * @author Raymond Augé */ @Component(immediate = true, service = Indexer.class) public class BlogsEntryIndexer extends BaseIndexer<BlogsEntry> { public static final String CLASS_NAME = BlogsEntry.class.getName(); public BlogsEntryIndexer() { setDefaultSelectedFieldNames( Field.ASSET_TAG_NAMES, Field.COMPANY_ID, Field.CONTENT, Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK, Field.GROUP_ID, Field.MODIFIED_DATE, Field.SCOPE_GROUP_ID, Field.TITLE, 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 BlogsEntryPermission.contains( permissionChecker, entryClassPK, ActionKeys.VIEW); } @Override public boolean isVisible(long classPK, int status) throws Exception { BlogsEntry entry = blogsEntryLocalService.getEntry(classPK); return isVisible(entry.getStatus(), status); } @Override public void postProcessContextBooleanFilter( BooleanFilter contextBooleanFilter, SearchContext searchContext) throws Exception { addStatus(contextBooleanFilter, searchContext); } @Override protected void doDelete(BlogsEntry blogsEntry) throws Exception { deleteDocument(blogsEntry.getCompanyId(), blogsEntry.getEntryId()); } @Override protected Document doGetDocument(BlogsEntry blogsEntry) throws Exception { Document document = getBaseModelDocument(CLASS_NAME, blogsEntry); document.addText(Field.CAPTION, blogsEntry.getCoverImageCaption()); document.addText( Field.CONTENT, HtmlUtil.extractText(blogsEntry.getContent())); document.addText(Field.DESCRIPTION, blogsEntry.getDescription()); document.addDate(Field.MODIFIED_DATE, blogsEntry.getModifiedDate()); document.addText(Field.SUBTITLE, blogsEntry.getSubtitle()); document.addText(Field.TITLE, blogsEntry.getTitle()); return document; } @Override protected Summary doGetSummary( Document document, Locale locale, String snippet, PortletRequest portletRequest, PortletResponse portletResponse) { Summary summary = createSummary(document); summary.setMaxContentLength(200); return summary; } @Override protected void doReindex(BlogsEntry blogsEntry) throws Exception { Document document = getDocument(blogsEntry); indexWriterHelper.updateDocument( getSearchEngineId(), blogsEntry.getCompanyId(), document, isCommitImmediately()); } @Override protected void doReindex(String className, long classPK) throws Exception { BlogsEntry entry = blogsEntryLocalService.getEntry(classPK); doReindex(entry); } @Override protected void doReindex(String[] ids) throws Exception { long companyId = GetterUtil.getLong(ids[0]); reindexEntries(companyId); } protected void reindexEntries(long companyId) throws PortalException { final IndexableActionableDynamicQuery indexableActionableDynamicQuery = blogsEntryLocalService.getIndexableActionableDynamicQuery(); indexableActionableDynamicQuery.setAddCriteriaMethod( new ActionableDynamicQuery.AddCriteriaMethod() { @Override public void addCriteria(DynamicQuery dynamicQuery) { Property displayDateProperty = PropertyFactoryUtil.forName( "displayDate"); dynamicQuery.add(displayDateProperty.lt(new Date())); Property statusProperty = PropertyFactoryUtil.forName( "status"); Integer[] statuses = { WorkflowConstants.STATUS_APPROVED, WorkflowConstants.STATUS_IN_TRASH }; dynamicQuery.add(statusProperty.in(statuses)); } }); indexableActionableDynamicQuery.setCompanyId(companyId); indexableActionableDynamicQuery.setPerformActionMethod( new ActionableDynamicQuery.PerformActionMethod<BlogsEntry>() { @Override public void performAction(BlogsEntry entry) { try { Document document = getDocument(entry); indexableActionableDynamicQuery.addDocuments(document); } catch (PortalException pe) { if (_log.isWarnEnabled()) { _log.warn( "Unable to index blogs entry " + entry.getEntryId(), pe); } } } }); indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId()); indexableActionableDynamicQuery.performActions(); } @Reference protected BlogsEntryLocalService blogsEntryLocalService; @Reference protected IndexWriterHelper indexWriterHelper; private static final Log _log = LogFactoryUtil.getLog( BlogsEntryIndexer.class); }