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