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            @Override
066            public String[] getClassNames() {
067                    return CLASS_NAMES;
068            }
069    
070            @Override
071            public String getPortletId() {
072                    return PORTLET_ID;
073            }
074    
075            @Override
076            public boolean hasPermission(
077                            PermissionChecker permissionChecker, String entryClassName,
078                            long entryClassPK, String actionId)
079                    throws Exception {
080    
081                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(
082                            entryClassPK);
083    
084                    return JournalFolderPermission.contains(
085                            permissionChecker, folder, ActionKeys.VIEW);
086            }
087    
088            @Override
089            public void postProcessContextQuery(
090                            BooleanQuery contextQuery, SearchContext searchContext)
091                    throws Exception {
092    
093                    addStatus(contextQuery, searchContext);
094            }
095    
096            @Override
097            protected void doDelete(Object obj) throws Exception {
098                    JournalFolder folder = (JournalFolder)obj;
099    
100                    Document document = new DocumentImpl();
101    
102                    document.addUID(PORTLET_ID, folder.getFolderId());
103    
104                    SearchEngineUtil.deleteDocument(
105                            getSearchEngineId(), folder.getCompanyId(),
106                            document.get(Field.UID));
107            }
108    
109            @Override
110            protected Document doGetDocument(Object obj) throws Exception {
111                    JournalFolder folder = (JournalFolder)obj;
112    
113                    if (_log.isDebugEnabled()) {
114                            _log.debug("Indexing folder " + folder);
115                    }
116    
117                    Document document = getBaseModelDocument(PORTLET_ID, folder);
118    
119                    document.addText(Field.DESCRIPTION, folder.getDescription());
120                    document.addKeyword(Field.FOLDER_ID, folder.getParentFolderId());
121                    document.addText(Field.TITLE, folder.getName());
122    
123                    if (!folder.isInTrash() && folder.isInTrashContainer()) {
124                            JournalFolder trashedFolder = folder.getTrashContainer();
125    
126                            if (trashedFolder != null) {
127                                    addTrashFields(
128                                            document, JournalFolder.class.getName(),
129                                            trashedFolder.getFolderId(), null, null,
130                                            JournalFolderAssetRendererFactory.TYPE);
131    
132                                    document.addKeyword(
133                                            Field.ROOT_ENTRY_CLASS_NAME, JournalFolder.class.getName());
134                                    document.addKeyword(
135                                            Field.ROOT_ENTRY_CLASS_PK, trashedFolder.getFolderId());
136                                    document.addKeyword(
137                                            Field.STATUS, WorkflowConstants.STATUS_IN_TRASH);
138                            }
139                    }
140    
141                    if (_log.isDebugEnabled()) {
142                            _log.debug("Document " + folder + " indexed successfully");
143                    }
144    
145                    return document;
146            }
147    
148            @Override
149            protected Summary doGetSummary(
150                    Document document, Locale locale, String snippet,
151                    PortletURL portletURL) {
152    
153                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
154    
155                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
156    
157                    try {
158                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
159                    }
160                    catch (WindowStateException wse) {
161                    }
162    
163                    String folderId = document.get(Field.ENTRY_CLASS_PK);
164    
165                    portletURL.setParameter("struts_action", "/journal/view");
166                    portletURL.setParameter("folderId", folderId);
167    
168                    Summary summary = createSummary(
169                            document, Field.TITLE, Field.DESCRIPTION);
170    
171                    summary.setMaxContentLength(200);
172                    summary.setPortletURL(portletURL);
173    
174                    return summary;
175            }
176    
177            @Override
178            protected void doReindex(Object obj) throws Exception {
179                    JournalFolder folder = (JournalFolder)obj;
180    
181                    Document document = getDocument(folder);
182    
183                    SearchEngineUtil.updateDocument(
184                            getSearchEngineId(), folder.getCompanyId(), document);
185            }
186    
187            @Override
188            protected void doReindex(String className, long classPK) throws Exception {
189                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
190    
191                    doReindex(folder);
192            }
193    
194            @Override
195            protected void doReindex(String[] ids) throws Exception {
196                    long companyId = GetterUtil.getLong(ids[0]);
197    
198                    reindexFolders(companyId);
199            }
200    
201            @Override
202            protected String getPortletId(SearchContext searchContext) {
203                    return PORTLET_ID;
204            }
205    
206            protected void reindexFolders(long companyId)
207                    throws PortalException, SystemException {
208    
209                    final Collection<Document> documents = new ArrayList<Document>();
210    
211                    ActionableDynamicQuery actionableDynamicQuery =
212                            new JournalFolderActionableDynamicQuery() {
213    
214                            @Override
215                            protected void performAction(Object object) throws PortalException {
216                                    JournalFolder folder = (JournalFolder)object;
217    
218                                    Document document = getDocument(folder);
219    
220                                    if (document != null) {
221                                            documents.add(document);
222                                    }
223                            }
224    
225                    };
226    
227                    actionableDynamicQuery.setCompanyId(companyId);
228    
229                    actionableDynamicQuery.performActions();
230    
231                    SearchEngineUtil.updateDocuments(
232                            getSearchEngineId(), companyId, documents);
233            }
234    
235            private static Log _log = LogFactoryUtil.getLog(JournalFolderIndexer.class);
236    
237    }