001    /**
002     * Copyright (c) 2000-2013 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.journal.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
023    import com.liferay.portal.kernel.portlet.LiferayWindowState;
024    import com.liferay.portal.kernel.search.BaseIndexer;
025    import com.liferay.portal.kernel.search.BooleanQuery;
026    import com.liferay.portal.kernel.search.Document;
027    import com.liferay.portal.kernel.search.DocumentImpl;
028    import com.liferay.portal.kernel.search.Field;
029    import com.liferay.portal.kernel.search.SearchContext;
030    import com.liferay.portal.kernel.search.SearchEngineUtil;
031    import com.liferay.portal.kernel.search.Summary;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.security.permission.ActionKeys;
035    import com.liferay.portal.security.permission.PermissionChecker;
036    import com.liferay.portal.util.PortletKeys;
037    import com.liferay.portlet.journal.asset.JournalFolderAssetRendererFactory;
038    import com.liferay.portlet.journal.model.JournalFolder;
039    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
040    import com.liferay.portlet.journal.service.permission.JournalFolderPermission;
041    import com.liferay.portlet.journal.service.persistence.JournalFolderActionableDynamicQuery;
042    
043    import java.util.ArrayList;
044    import java.util.Collection;
045    import java.util.Locale;
046    
047    import javax.portlet.PortletRequest;
048    import javax.portlet.PortletURL;
049    import javax.portlet.WindowStateException;
050    
051    /**
052     * @author Eduardo Garcia
053     */
054    public class JournalFolderIndexer extends BaseIndexer {
055    
056            public static final String[] CLASS_NAMES = {JournalFolder.class.getName()};
057    
058            public static final String PORTLET_ID = PortletKeys.JOURNAL;
059    
060            public JournalFolderIndexer() {
061                    setFilterSearch(true);
062                    setPermissionAware(true);
063            }
064    
065            public String[] getClassNames() {
066                    return CLASS_NAMES;
067            }
068    
069            public String getPortletId() {
070                    return PORTLET_ID;
071            }
072    
073            @Override
074            public boolean hasPermission(
075                            PermissionChecker permissionChecker, String entryClassName,
076                            long entryClassPK, String actionId)
077                    throws Exception {
078    
079                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(
080                            entryClassPK);
081    
082                    return JournalFolderPermission.contains(
083                            permissionChecker, folder, ActionKeys.VIEW);
084            }
085    
086            @Override
087            public void postProcessContextQuery(
088                            BooleanQuery contextQuery, SearchContext searchContext)
089                    throws Exception {
090    
091                    int status = GetterUtil.getInteger(
092                                    searchContext.getAttribute(Field.STATUS),
093                                    WorkflowConstants.STATUS_APPROVED);
094    
095                    if (status != WorkflowConstants.STATUS_ANY) {
096                            contextQuery.addRequiredTerm(Field.STATUS, status);
097                    }
098            }
099    
100            @Override
101            protected void doDelete(Object obj) throws Exception {
102                    JournalFolder folder = (JournalFolder)obj;
103    
104                    Document document = new DocumentImpl();
105    
106                    document.addUID(PORTLET_ID, folder.getFolderId());
107    
108                    SearchEngineUtil.deleteDocument(
109                            getSearchEngineId(), folder.getCompanyId(),
110                            document.get(Field.UID));
111            }
112    
113            @Override
114            protected Document doGetDocument(Object obj) throws Exception {
115                    JournalFolder folder = (JournalFolder)obj;
116    
117                    if (_log.isDebugEnabled()) {
118                            _log.debug("Indexing folder " + folder);
119                    }
120    
121                    Document document = getBaseModelDocument(PORTLET_ID, folder);
122    
123                    document.addText(Field.DESCRIPTION, folder.getDescription());
124                    document.addKeyword(Field.FOLDER_ID, folder.getParentFolderId());
125                    document.addText(Field.TITLE, folder.getName());
126    
127                    if (!folder.isInTrash() && folder.isInTrashContainer()) {
128                            JournalFolder trashedFolder = folder.getTrashContainer();
129    
130                            if (trashedFolder != null) {
131                                    addTrashFields(
132                                            document, JournalFolder.class.getName(),
133                                            trashedFolder.getFolderId(), null, null,
134                                            JournalFolderAssetRendererFactory.TYPE);
135    
136                                    document.addKeyword(
137                                            Field.ROOT_ENTRY_CLASS_NAME, JournalFolder.class.getName());
138                                    document.addKeyword(
139                                            Field.ROOT_ENTRY_CLASS_PK, trashedFolder.getFolderId());
140                                    document.addKeyword(
141                                            Field.STATUS, WorkflowConstants.STATUS_IN_TRASH);
142                            }
143                    }
144    
145                    if (_log.isDebugEnabled()) {
146                            _log.debug("Document " + folder + " indexed successfully");
147                    }
148    
149                    return document;
150            }
151    
152            @Override
153            protected Summary doGetSummary(
154                    Document document, Locale locale, String snippet,
155                    PortletURL portletURL) {
156    
157                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
158    
159                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
160    
161                    try {
162                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
163                    }
164                    catch (WindowStateException wse) {
165                    }
166    
167                    String folderId = document.get(Field.ENTRY_CLASS_PK);
168    
169                    portletURL.setParameter("struts_action", "/journal/view");
170                    portletURL.setParameter("folderId", folderId);
171    
172                    Summary summary = createSummary(
173                            document, Field.TITLE, Field.DESCRIPTION);
174    
175                    summary.setMaxContentLength(200);
176                    summary.setPortletURL(portletURL);
177    
178                    return summary;
179            }
180    
181            @Override
182            protected void doReindex(Object obj) throws Exception {
183                    JournalFolder folder = (JournalFolder)obj;
184    
185                    Document document = getDocument(folder);
186    
187                    SearchEngineUtil.updateDocument(
188                            getSearchEngineId(), folder.getCompanyId(), document);
189            }
190    
191            @Override
192            protected void doReindex(String className, long classPK) throws Exception {
193                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
194    
195                    doReindex(folder);
196            }
197    
198            @Override
199            protected void doReindex(String[] ids) throws Exception {
200                    long companyId = GetterUtil.getLong(ids[0]);
201    
202                    reindexFolders(companyId);
203            }
204    
205            @Override
206            protected String getPortletId(SearchContext searchContext) {
207                    return PORTLET_ID;
208            }
209    
210            protected void reindexFolders(long companyId)
211                    throws PortalException, SystemException {
212    
213                    final Collection<Document> documents = new ArrayList<Document>();
214    
215                    ActionableDynamicQuery actionableDynamicQuery =
216                            new JournalFolderActionableDynamicQuery() {
217    
218                            @Override
219                            protected void performAction(Object object) throws PortalException {
220                                    JournalFolder folder = (JournalFolder)object;
221    
222                                    Document document = getDocument(folder);
223    
224                                    if (document != null) {
225                                            documents.add(document);
226                                    }
227                            }
228    
229                    };
230    
231                    actionableDynamicQuery.setCompanyId(companyId);
232    
233                    actionableDynamicQuery.performActions();
234    
235                    SearchEngineUtil.updateDocuments(
236                            getSearchEngineId(), companyId, documents);
237            }
238    
239            private static Log _log = LogFactoryUtil.getLog(JournalFolderIndexer.class);
240    
241    }