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