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