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