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.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                    document.addKeyword(Field.TREE_PATH, dlFolder.getTreePath());
129                    document.addKeyword(
130                            Field.TREE_PATH,
131                            StringUtil.split(dlFolder.getTreePath(), CharPool.SLASH));
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                    ActionableDynamicQuery actionableDynamicQuery =
226                            new DLFolderActionableDynamicQuery() {
227    
228                            @Override
229                            protected void addCriteria(DynamicQuery dynamicQuery) {
230                                    Property property = PropertyFactoryUtil.forName("mountPoint");
231    
232                                    dynamicQuery.add(property.eq(false));
233                            }
234    
235                            @Override
236                            protected void performAction(Object object) throws PortalException {
237                                    DLFolder dlFolder = (DLFolder)object;
238    
239                                    Document document = getDocument(dlFolder);
240    
241                                    if (document != null) {
242                                            addDocument(document);
243                                    }
244                            }
245    
246                    };
247    
248                    actionableDynamicQuery.setCompanyId(companyId);
249                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
250    
251                    actionableDynamicQuery.performActions();
252            }
253    
254            private static Log _log = LogFactoryUtil.getLog(DLFolderIndexer.class);
255    
256    }