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