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