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