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