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.security.permission.ActionKeys;
036    import com.liferay.portal.security.permission.PermissionChecker;
037    import com.liferay.portal.util.PortletKeys;
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                    document.addKeyword(
123                            Field.TREE_PATH,
124                            StringUtil.split(folder.getTreePath(), CharPool.SLASH));
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug("Document " + folder + " indexed successfully");
128                    }
129    
130                    return document;
131            }
132    
133            @Override
134            protected Summary doGetSummary(
135                    Document document, Locale locale, String snippet,
136                    PortletURL portletURL) {
137    
138                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
139    
140                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
141    
142                    try {
143                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
144                    }
145                    catch (WindowStateException wse) {
146                    }
147    
148                    String folderId = document.get(Field.ENTRY_CLASS_PK);
149    
150                    portletURL.setParameter("struts_action", "/journal/view");
151                    portletURL.setParameter("folderId", folderId);
152    
153                    Summary summary = createSummary(
154                            document, Field.TITLE, Field.DESCRIPTION);
155    
156                    summary.setMaxContentLength(200);
157                    summary.setPortletURL(portletURL);
158    
159                    return summary;
160            }
161    
162            @Override
163            protected void doReindex(Object obj) throws Exception {
164                    JournalFolder folder = (JournalFolder)obj;
165    
166                    Document document = getDocument(folder);
167    
168                    SearchEngineUtil.updateDocument(
169                            getSearchEngineId(), folder.getCompanyId(), document);
170            }
171    
172            @Override
173            protected void doReindex(String className, long classPK) throws Exception {
174                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
175    
176                    doReindex(folder);
177            }
178    
179            @Override
180            protected void doReindex(String[] ids) throws Exception {
181                    long companyId = GetterUtil.getLong(ids[0]);
182    
183                    reindexFolders(companyId);
184            }
185    
186            @Override
187            protected String getPortletId(SearchContext searchContext) {
188                    return PORTLET_ID;
189            }
190    
191            protected void reindexFolders(long companyId)
192                    throws PortalException, SystemException {
193    
194                    final Collection<Document> documents = new ArrayList<Document>();
195    
196                    ActionableDynamicQuery actionableDynamicQuery =
197                            new JournalFolderActionableDynamicQuery() {
198    
199                            @Override
200                            protected void performAction(Object object) throws PortalException {
201                                    JournalFolder folder = (JournalFolder)object;
202    
203                                    Document document = getDocument(folder);
204    
205                                    if (document != null) {
206                                            documents.add(document);
207                                    }
208                            }
209    
210                    };
211    
212                    actionableDynamicQuery.setCompanyId(companyId);
213    
214                    actionableDynamicQuery.performActions();
215    
216                    SearchEngineUtil.updateDocuments(
217                            getSearchEngineId(), companyId, documents);
218            }
219    
220            private static Log _log = LogFactoryUtil.getLog(JournalFolderIndexer.class);
221    
222    }