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