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