001    /**
002     * Copyright (c) 2000-present 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.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.search.BaseIndexer;
025    import com.liferay.portal.kernel.search.Document;
026    import com.liferay.portal.kernel.search.DocumentImpl;
027    import com.liferay.portal.kernel.search.Field;
028    import com.liferay.portal.kernel.search.FolderIndexer;
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.search.filter.BooleanFilter;
033    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
034    import com.liferay.portal.kernel.util.CharPool;
035    import com.liferay.portal.kernel.util.GetterUtil;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.security.permission.ActionKeys;
038    import com.liferay.portal.security.permission.PermissionChecker;
039    import com.liferay.portlet.documentlibrary.model.DLFolder;
040    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
042    
043    import java.util.Locale;
044    
045    import javax.portlet.PortletRequest;
046    import javax.portlet.PortletResponse;
047    
048    /**
049     * @author Alexander Chow
050     */
051    @OSGiBeanProperties
052    public class DLFolderIndexer
053            extends BaseIndexer<DLFolder> implements FolderIndexer {
054    
055            public static final String CLASS_NAME = DLFolder.class.getName();
056    
057            public DLFolderIndexer() {
058                    setDefaultSelectedFieldNames(
059                            Field.COMPANY_ID, Field.DESCRIPTION, Field.ENTRY_CLASS_NAME,
060                            Field.ENTRY_CLASS_PK, Field.TITLE, Field.UID);
061                    setFilterSearch(true);
062                    setPermissionAware(true);
063            }
064    
065            @Override
066            public String getClassName() {
067                    return CLASS_NAME;
068            }
069    
070            @Override
071            public String[] getFolderClassNames() {
072                    return new String[] {CLASS_NAME};
073            }
074    
075            @Override
076            public boolean hasPermission(
077                            PermissionChecker permissionChecker, String entryClassName,
078                            long entryClassPK, String actionId)
079                    throws Exception {
080    
081                    DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(entryClassPK);
082    
083                    return DLFolderPermission.contains(
084                            permissionChecker, dlFolder, ActionKeys.VIEW);
085            }
086    
087            @Override
088            public void postProcessContextBooleanFilter(
089                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
090                    throws Exception {
091    
092                    addStatus(contextBooleanFilter, searchContext);
093    
094                    contextBooleanFilter.addRequiredTerm(Field.HIDDEN, false);
095            }
096    
097            @Override
098            protected void doDelete(DLFolder dlFolder) throws Exception {
099                    Document document = new DocumentImpl();
100    
101                    document.addUID(CLASS_NAME, dlFolder.getFolderId());
102    
103                    SearchEngineUtil.deleteDocument(
104                            getSearchEngineId(), dlFolder.getCompanyId(),
105                            document.get(Field.UID), isCommitImmediately());
106            }
107    
108            @Override
109            protected Document doGetDocument(DLFolder dlFolder) throws Exception {
110                    if (_log.isDebugEnabled()) {
111                            _log.debug("Indexing folder " + dlFolder);
112                    }
113    
114                    Document document = getBaseModelDocument(CLASS_NAME, dlFolder);
115    
116                    document.addText(Field.DESCRIPTION, dlFolder.getDescription());
117                    document.addKeyword(Field.FOLDER_ID, dlFolder.getParentFolderId());
118                    document.addKeyword(
119                            Field.HIDDEN, (dlFolder.isHidden() || dlFolder.isInHiddenFolder()));
120                    document.addText(Field.TITLE, dlFolder.getName());
121                    document.addKeyword(Field.TREE_PATH, dlFolder.getTreePath());
122                    document.addKeyword(
123                            Field.TREE_PATH,
124                            StringUtil.split(dlFolder.getTreePath(), CharPool.SLASH));
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug("Document " + dlFolder + " indexed successfully");
128                    }
129    
130                    return document;
131            }
132    
133            @Override
134            protected Summary doGetSummary(
135                    Document document, Locale locale, String snippet,
136                    PortletRequest portletRequest, PortletResponse portletResponse) {
137    
138                    Summary summary = createSummary(
139                            document, Field.TITLE, Field.DESCRIPTION);
140    
141                    summary.setMaxContentLength(200);
142    
143                    return summary;
144            }
145    
146            @Override
147            protected void doReindex(DLFolder dlFolder) throws Exception {
148                    if (!dlFolder.isApproved() && !dlFolder.isInTrash()) {
149                            return;
150                    }
151    
152                    Document document = getDocument(dlFolder);
153    
154                    if (document != null) {
155                            SearchEngineUtil.updateDocument(
156                                    getSearchEngineId(), dlFolder.getCompanyId(), document,
157                                    isCommitImmediately());
158                    }
159            }
160    
161            @Override
162            protected void doReindex(String className, long classPK) throws Exception {
163                    DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(classPK);
164    
165                    doReindex(dlFolder);
166            }
167    
168            @Override
169            protected void doReindex(String[] ids) throws Exception {
170                    long companyId = GetterUtil.getLong(ids[0]);
171    
172                    reindexFolders(companyId);
173            }
174    
175            protected void reindexFolders(final long companyId) throws PortalException {
176                    final ActionableDynamicQuery actionableDynamicQuery =
177                            DLFolderLocalServiceUtil.getActionableDynamicQuery();
178    
179                    actionableDynamicQuery.setAddCriteriaMethod(
180                            new ActionableDynamicQuery.AddCriteriaMethod() {
181    
182                                    @Override
183                                    public void addCriteria(DynamicQuery dynamicQuery) {
184                                            Property property = PropertyFactoryUtil.forName(
185                                                    "mountPoint");
186    
187                                            dynamicQuery.add(property.eq(false));
188                                    }
189    
190                            });
191                    actionableDynamicQuery.setCompanyId(companyId);
192                    actionableDynamicQuery.setPerformActionMethod(
193                            new ActionableDynamicQuery.PerformActionMethod<DLFolder>() {
194    
195                                    @Override
196                                    public void performAction(DLFolder dlFolder) {
197                                            try {
198                                                    Document document = getDocument(dlFolder);
199    
200                                                    if (document != null) {
201                                                            actionableDynamicQuery.addDocument(document);
202                                                    }
203                                            }
204                                            catch (PortalException pe) {
205                                                    if (_log.isWarnEnabled()) {
206                                                            _log.warn(
207                                                                    "Unable to index document library folder " +
208                                                                            dlFolder.getFolderId(),
209                                                                    pe);
210                                                    }
211                                            }
212                                    }
213    
214                            });
215                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
216    
217                    actionableDynamicQuery.performActions();
218            }
219    
220            private static final Log _log = LogFactoryUtil.getLog(
221                    DLFolderIndexer.class);
222    
223    }