/** * 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.journal.search; import com.liferay.journal.model.JournalFolder; import com.liferay.journal.service.JournalFolderLocalService; import com.liferay.journal.service.permission.JournalFolderPermission; import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery; import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery; 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.FolderIndexer; 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.CharPool; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.trash.TrashHelper; 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 Eduardo Garcia */ @Component(immediate = true, service = Indexer.class) public class JournalFolderIndexer extends BaseIndexer<JournalFolder> implements FolderIndexer { public static final String CLASS_NAME = JournalFolder.class.getName(); public JournalFolderIndexer() { setDefaultSelectedFieldNames( Field.COMPANY_ID, Field.DESCRIPTION, Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK, Field.TITLE, Field.UID); setFilterSearch(true); setPermissionAware(true); } @Override public String getClassName() { return CLASS_NAME; } @Override public String[] getFolderClassNames() { return new String[] {CLASS_NAME}; } @Override public boolean hasPermission( PermissionChecker permissionChecker, String entryClassName, long entryClassPK, String actionId) throws Exception { JournalFolder folder = _journalFolderLocalService.getFolder( entryClassPK); return JournalFolderPermission.contains( permissionChecker, folder, ActionKeys.VIEW); } @Override public void postProcessContextBooleanFilter( BooleanFilter contextBooleanFilter, SearchContext searchContext) throws Exception { addStatus(contextBooleanFilter, searchContext); } @Override protected void doDelete(JournalFolder journalFolder) throws Exception { deleteDocument( journalFolder.getCompanyId(), journalFolder.getFolderId()); } @Override protected Document doGetDocument(JournalFolder journalFolder) throws Exception { if (_log.isDebugEnabled()) { _log.debug("Indexing journalFolder " + journalFolder); } Document document = getBaseModelDocument(CLASS_NAME, journalFolder); document.addText(Field.DESCRIPTION, journalFolder.getDescription()); document.addKeyword(Field.FOLDER_ID, journalFolder.getParentFolderId()); String title = journalFolder.getName(); if (journalFolder.isInTrash()) { title = _trashHelper.getOriginalTitle(title); } document.addText(Field.TITLE, title); document.addKeyword( Field.TREE_PATH, StringUtil.split(journalFolder.getTreePath(), CharPool.SLASH)); if (_log.isDebugEnabled()) { _log.debug("Document " + journalFolder + " indexed successfully"); } return document; } @Override protected Summary doGetSummary( Document document, Locale locale, String snippet, PortletRequest portletRequest, PortletResponse portletResponse) { Summary summary = createSummary( document, Field.TITLE, Field.DESCRIPTION); summary.setMaxContentLength(200); return summary; } @Override protected void doReindex(JournalFolder journalFolder) throws Exception { Document document = getDocument(journalFolder); _indexWriterHelper.updateDocument( getSearchEngineId(), journalFolder.getCompanyId(), document, isCommitImmediately()); } @Override protected void doReindex(String className, long classPK) throws Exception { JournalFolder folder = _journalFolderLocalService.getFolder(classPK); doReindex(folder); } @Override protected void doReindex(String[] ids) throws Exception { long companyId = GetterUtil.getLong(ids[0]); reindexFolders(companyId); } protected void reindexFolders(long companyId) throws PortalException { final IndexableActionableDynamicQuery indexableActionableDynamicQuery = _journalFolderLocalService.getIndexableActionableDynamicQuery(); indexableActionableDynamicQuery.setCompanyId(companyId); indexableActionableDynamicQuery.setPerformActionMethod( new ActionableDynamicQuery.PerformActionMethod<JournalFolder>() { @Override public void performAction(JournalFolder folder) { try { Document document = getDocument(folder); if (document != null) { indexableActionableDynamicQuery.addDocuments( document); } } catch (PortalException pe) { if (_log.isWarnEnabled()) { _log.warn( "Unable to index journal folder " + folder.getFolderId(), pe); } } } }); indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId()); indexableActionableDynamicQuery.performActions(); } @Reference(unbind = "-") protected void setJournalFolderLocalService( JournalFolderLocalService journalFolderLocalService) { _journalFolderLocalService = journalFolderLocalService; } private static final Log _log = LogFactoryUtil.getLog( JournalFolderIndexer.class); @Reference private IndexWriterHelper _indexWriterHelper; private JournalFolderLocalService _journalFolderLocalService; @Reference private TrashHelper _trashHelper; }