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.Field;
028    import com.liferay.portal.kernel.search.FolderIndexer;
029    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
030    import com.liferay.portal.kernel.search.SearchContext;
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                    deleteDocument(dlFolder.getCompanyId(), dlFolder.getFolderId());
100            }
101    
102            @Override
103            protected Document doGetDocument(DLFolder dlFolder) throws Exception {
104                    if (_log.isDebugEnabled()) {
105                            _log.debug("Indexing folder " + dlFolder);
106                    }
107    
108                    Document document = getBaseModelDocument(CLASS_NAME, dlFolder);
109    
110                    document.addText(Field.DESCRIPTION, dlFolder.getDescription());
111                    document.addKeyword(Field.FOLDER_ID, dlFolder.getParentFolderId());
112                    document.addKeyword(
113                            Field.HIDDEN, (dlFolder.isHidden() || dlFolder.isInHiddenFolder()));
114                    document.addText(Field.TITLE, dlFolder.getName());
115                    document.addKeyword(Field.TREE_PATH, dlFolder.getTreePath());
116                    document.addKeyword(
117                            Field.TREE_PATH,
118                            StringUtil.split(dlFolder.getTreePath(), CharPool.SLASH));
119    
120                    if (_log.isDebugEnabled()) {
121                            _log.debug("Document " + dlFolder + " indexed successfully");
122                    }
123    
124                    return document;
125            }
126    
127            @Override
128            protected Summary doGetSummary(
129                    Document document, Locale locale, String snippet,
130                    PortletRequest portletRequest, PortletResponse portletResponse) {
131    
132                    Summary summary = createSummary(
133                            document, Field.TITLE, Field.DESCRIPTION);
134    
135                    summary.setMaxContentLength(200);
136    
137                    return summary;
138            }
139    
140            @Override
141            protected void doReindex(DLFolder dlFolder) throws Exception {
142                    if (!dlFolder.isApproved() && !dlFolder.isInTrash()) {
143                            return;
144                    }
145    
146                    Document document = getDocument(dlFolder);
147    
148                    IndexWriterHelperUtil.updateDocument(
149                            getSearchEngineId(), dlFolder.getCompanyId(), document,
150                            isCommitImmediately());
151            }
152    
153            @Override
154            protected void doReindex(String className, long classPK) throws Exception {
155                    DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(classPK);
156    
157                    doReindex(dlFolder);
158            }
159    
160            @Override
161            protected void doReindex(String[] ids) throws Exception {
162                    long companyId = GetterUtil.getLong(ids[0]);
163    
164                    reindexFolders(companyId);
165            }
166    
167            protected void reindexFolders(final long companyId) throws PortalException {
168                    final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
169                            DLFolderLocalServiceUtil.getIndexableActionableDynamicQuery();
170    
171                    indexableActionableDynamicQuery.setAddCriteriaMethod(
172                            new ActionableDynamicQuery.AddCriteriaMethod() {
173    
174                                    @Override
175                                    public void addCriteria(DynamicQuery dynamicQuery) {
176                                            Property property = PropertyFactoryUtil.forName(
177                                                    "mountPoint");
178    
179                                            dynamicQuery.add(property.eq(false));
180                                    }
181    
182                            });
183                    indexableActionableDynamicQuery.setCompanyId(companyId);
184                    indexableActionableDynamicQuery.setPerformActionMethod(
185                            new ActionableDynamicQuery.PerformActionMethod<DLFolder>() {
186    
187                                    @Override
188                                    public void performAction(DLFolder dlFolder) {
189                                            try {
190                                                    Document document = getDocument(dlFolder);
191    
192                                                    indexableActionableDynamicQuery.addDocuments(document);
193                                            }
194                                            catch (PortalException pe) {
195                                                    if (_log.isWarnEnabled()) {
196                                                            _log.warn(
197                                                                    "Unable to index document library folder " +
198                                                                            dlFolder.getFolderId(),
199                                                                    pe);
200                                                    }
201                                            }
202                                    }
203    
204                            });
205                    indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
206    
207                    indexableActionableDynamicQuery.performActions();
208            }
209    
210            private static final Log _log = LogFactoryUtil.getLog(
211                    DLFolderIndexer.class);
212    
213    }