Java tutorial
/** * Copyright (c) 2000-2013 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.documentlibrary.util; import java.io.FileWriter; import java.io.IOException; import java.util.Locale; import javax.portlet.PortletRequest; import javax.portlet.PortletURL; import javax.portlet.WindowStateException; import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery; import com.liferay.portal.kernel.dao.orm.DynamicQuery; 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.exception.SystemException; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.portlet.LiferayPortletURL; import com.liferay.portal.kernel.portlet.LiferayWindowState; import com.liferay.portal.kernel.search.BaseIndexer; import com.liferay.portal.kernel.search.BooleanQuery; import com.liferay.portal.kernel.search.Document; import com.liferay.portal.kernel.search.DocumentImpl; import com.liferay.portal.kernel.search.Field; import com.liferay.portal.kernel.search.SearchContext; import com.liferay.portal.kernel.search.SearchEngineUtil; import com.liferay.portal.kernel.search.Summary; import com.liferay.portal.kernel.util.CharPool; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.PropsUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.security.permission.ActionKeys; import com.liferay.portal.security.permission.PermissionChecker; import com.liferay.portal.util.PortletKeys; import com.liferay.portlet.documentlibrary.model.DLFolder; import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil; import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission; import com.liferay.portlet.documentlibrary.service.persistence.DLFolderActionableDynamicQuery; import com.stoxx.folderindexer.logger.FolderIndexer; import com.stoxx.indexer.logger.IndexerLogger; /** * @author Alexander Chow */ public class DLFolderIndexer extends BaseIndexer { private static final String NEW_LINE_SEPARATOR = "\n"; String liferayHome = PropsUtil.get("liferay.home"); String fileName = "ReIndexingFolderException.csv"; String fileCsvpath = liferayHome + StringPool.SLASH + fileName; FileWriter fileWriter = null; public static final String[] CLASS_NAMES = { DLFolder.class.getName() }; public static final String PORTLET_ID = PortletKeys.DOCUMENT_LIBRARY; public DLFolderIndexer() { setFilterSearch(true); setPermissionAware(true); } @Override public String[] getClassNames() { return CLASS_NAMES; } @Override public String getPortletId() { return PORTLET_ID; } @Override public boolean hasPermission(PermissionChecker permissionChecker, String entryClassName, long entryClassPK, String actionId) throws Exception { DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(entryClassPK); return DLFolderPermission.contains(permissionChecker, dlFolder, ActionKeys.VIEW); } @Override public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext) throws Exception { addStatus(contextQuery, searchContext); contextQuery.addRequiredTerm(Field.HIDDEN, false); } @Override protected void doDelete(Object obj) throws Exception { DLFolder dlFolder = (DLFolder) obj; Document document = new DocumentImpl(); document.addUID(PORTLET_ID, dlFolder.getFolderId()); SearchEngineUtil.deleteDocument(getSearchEngineId(), dlFolder.getCompanyId(), document.get(Field.UID)); } @Override protected Document doGetDocument(Object obj) throws Exception { DLFolder dlFolder = (DLFolder) obj; _log.info("COGNIZANT@@@@doGetDocument@@@@@DLFolderIndexer:::::Folder Id::::" + dlFolder.getFolderId() + "Folder Name:::::" + dlFolder.getName()); if (_log.isDebugEnabled()) { _log.debug("Indexing folder " + dlFolder); } Document document = null; try { document = getBaseModelDocument(PORTLET_ID, dlFolder); document.addText(Field.DESCRIPTION, dlFolder.getDescription()); document.addKeyword(Field.FOLDER_ID, dlFolder.getParentFolderId()); document.addKeyword(Field.HIDDEN, (dlFolder.isHidden() || dlFolder.isInHiddenFolder())); document.addText(Field.TITLE, dlFolder.getName()); document.addKeyword(Field.TREE_PATH, dlFolder.getTreePath()); document.addKeyword(Field.TREE_PATH, StringUtil.split(dlFolder.getTreePath(), CharPool.SLASH)); _log.info("Document " + dlFolder + " indexed successfully"); if (_log.isDebugEnabled()) { _log.debug("Document " + dlFolder + " indexed successfully"); } } catch (Exception e) { _log.error(" >>>>>> Exception in DLFolderIndexer.doGetDocument() for " + dlFolder.getFolderId(), e); folderLog.error("Folder Id " + dlFolder.getFolderId()); try { fileWriter = generateCsvFile(dlFolder.getFolderId(), fileCsvpath); } catch (IOException e1) { _log.error("Error Generating CSV", e1); } } return document; } @Override protected Summary doGetSummary(Document document, Locale locale, String snippet, PortletURL portletURL) { LiferayPortletURL liferayPortletURL = (LiferayPortletURL) portletURL; liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE); try { liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE); } catch (WindowStateException wse) { } String folderId = document.get(Field.ENTRY_CLASS_PK); portletURL.setParameter("struts_action", "/document_library/view"); portletURL.setParameter("folderId", folderId); Summary summary = createSummary(document, Field.TITLE, Field.DESCRIPTION); summary.setMaxContentLength(200); summary.setPortletURL(portletURL); return summary; } @Override protected void doReindex(Object obj) throws Exception { DLFolder dlFolder = (DLFolder) obj; try { if (!dlFolder.isApproved() && !dlFolder.isInTrash()) { return; } Document document = getDocument(dlFolder); if (document != null) { SearchEngineUtil.updateDocument(getSearchEngineId(), dlFolder.getCompanyId(), document); } } catch (Exception e) { _log.error("COGNIZANT@@@@doReindex@@@@@DLFolderIndexer:::::@@@@@@" + dlFolder.getFolderId() + "@@@@@@", e); folderLog.error("Folder Id " + dlFolder.getFolderId()); try { fileWriter = generateCsvFile(dlFolder.getFolderId(), fileCsvpath); } catch (IOException e1) { _log.error("Error Generating CSV", e1); } } } @Override protected void doReindex(String className, long classPK) throws Exception { DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(classPK); doReindex(dlFolder); } @Override protected void doReindex(String[] ids) throws Exception { long companyId = GetterUtil.getLong(ids[0]); reindexFolders(companyId); } @Override protected String getPortletId(SearchContext searchContext) { return PORTLET_ID; } protected void reindexFolders(final long companyId) throws PortalException, SystemException { try { ActionableDynamicQuery actionableDynamicQuery = new DLFolderActionableDynamicQuery() { @Override protected void addCriteria(DynamicQuery dynamicQuery) { Property property = PropertyFactoryUtil.forName("mountPoint"); dynamicQuery.add(property.eq(false)); } @Override protected void performAction(Object object) throws PortalException { DLFolder dlFolder = null; try { dlFolder = (DLFolder) object; Document document = getDocument(dlFolder); if (document != null) { addDocument(document); } } catch (Exception e) { _log.error("COGNIZANT@@@@performAction@@@@@DLFolderIndexer:::::" + dlFolder.getFolderId(), e); folderLog.error("Folder Id " + dlFolder.getFolderId()); try { fileWriter = generateCsvFile(dlFolder.getFolderId(), fileCsvpath); } catch (IOException e1) { _log.error("Error Generating CSV", e1); } } } }; actionableDynamicQuery.setCompanyId(companyId); actionableDynamicQuery.setSearchEngineId(getSearchEngineId()); actionableDynamicQuery.performActions(); } catch (Exception e) { _log.error("COGNIZANT@@@@reindexFolders@@@@@DLFolderIndexer:::::" + e.getMessage()); } } private FileWriter generateCsvFile(long folderId, String sFileName) throws IOException { try { fileWriter = new FileWriter(sFileName, Boolean.TRUE); folderLog.error( "generateCsvFile with fileEntryId as " + folderId + " and the sFileName as " + sFileName); fileWriter.append(String.valueOf(folderId)); fileWriter.append(NEW_LINE_SEPARATOR); } catch (IOException e) { folderLog.error("Error in generateCsvFile method", e); } finally { fileWriter.flush(); fileWriter.close(); } return fileWriter; } private static Log _log = LogFactoryUtil.getLog(IndexerLogger.class); private static Log folderLog = LogFactoryUtil.getLog(FolderIndexer.class); }