001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
026    import com.liferay.portal.kernel.portlet.LiferayWindowState;
027    import com.liferay.portal.kernel.search.BaseIndexer;
028    import com.liferay.portal.kernel.search.BooleanQuery;
029    import com.liferay.portal.kernel.search.Document;
030    import com.liferay.portal.kernel.search.DocumentImpl;
031    import com.liferay.portal.kernel.search.Field;
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    import com.liferay.portlet.documentlibrary.service.persistence.DLFolderActionableDynamicQuery;
045    
046    import java.util.Locale;
047    
048    import javax.portlet.PortletRequest;
049    import javax.portlet.PortletURL;
050    import javax.portlet.WindowStateException;
051    
052    /**
053     * @author Alexander Chow
054     */
055    public class DLFolderIndexer extends BaseIndexer {
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                    setFilterSearch(true);
063                    setPermissionAware(true);
064            }
065    
066            @Override
067            public String[] getClassNames() {
068                    return CLASS_NAMES;
069            }
070    
071            @Override
072            public String getPortletId() {
073                    return PORTLET_ID;
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 postProcessContextQuery(
090                            BooleanQuery contextQuery, SearchContext searchContext)
091                    throws Exception {
092    
093                    addStatus(contextQuery, searchContext);
094    
095                    contextQuery.addRequiredTerm(Field.HIDDEN, false);
096            }
097    
098            @Override
099            protected void doDelete(Object obj) throws Exception {
100                    DLFolder dlFolder = (DLFolder)obj;
101    
102                    Document document = new DocumentImpl();
103    
104                    document.addUID(PORTLET_ID, dlFolder.getFolderId());
105    
106                    SearchEngineUtil.deleteDocument(
107                            getSearchEngineId(), dlFolder.getCompanyId(),
108                            document.get(Field.UID));
109            }
110    
111            @Override
112            protected Document doGetDocument(Object obj) throws Exception {
113                    DLFolder dlFolder = (DLFolder)obj;
114    
115                    if (_log.isDebugEnabled()) {
116                            _log.debug("Indexing folder " + dlFolder);
117                    }
118    
119                    Document document = getBaseModelDocument(PORTLET_ID, dlFolder);
120    
121                    document.addText(Field.DESCRIPTION, dlFolder.getDescription());
122                    document.addKeyword(Field.FOLDER_ID, dlFolder.getParentFolderId());
123                    document.addKeyword(
124                            Field.HIDDEN, (dlFolder.isHidden() || dlFolder.isInHiddenFolder()));
125                    document.addText(Field.TITLE, dlFolder.getName());
126                    document.addKeyword(Field.TREE_PATH, dlFolder.getTreePath());
127                    document.addKeyword(
128                            Field.TREE_PATH,
129                            StringUtil.split(dlFolder.getTreePath(), CharPool.SLASH));
130    
131                    if (_log.isDebugEnabled()) {
132                            _log.debug("Document " + dlFolder + " indexed successfully");
133                    }
134    
135                    return document;
136            }
137    
138            @Override
139            protected Summary doGetSummary(
140                    Document document, Locale locale, String snippet,
141                    PortletURL portletURL) {
142    
143                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
144    
145                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
146    
147                    try {
148                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
149                    }
150                    catch (WindowStateException wse) {
151                    }
152    
153                    String folderId = document.get(Field.ENTRY_CLASS_PK);
154    
155                    portletURL.setParameter("struts_action", "/document_library/view");
156                    portletURL.setParameter("folderId", folderId);
157    
158                    Summary summary = createSummary(
159                            document, Field.TITLE, Field.DESCRIPTION);
160    
161                    summary.setMaxContentLength(200);
162                    summary.setPortletURL(portletURL);
163    
164                    return summary;
165            }
166    
167            @Override
168            protected void doReindex(Object obj) throws Exception {
169                    DLFolder dlFolder = (DLFolder)obj;
170    
171                    if (!dlFolder.isApproved() && !dlFolder.isInTrash()) {
172                            return;
173                    }
174    
175                    Document document = getDocument(dlFolder);
176    
177                    if (document != null) {
178                            SearchEngineUtil.updateDocument(
179                                    getSearchEngineId(), dlFolder.getCompanyId(), document);
180                    }
181            }
182    
183            @Override
184            protected void doReindex(String className, long classPK) throws Exception {
185                    DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(classPK);
186    
187                    doReindex(dlFolder);
188            }
189    
190            @Override
191            protected void doReindex(String[] ids) throws Exception {
192                    long companyId = GetterUtil.getLong(ids[0]);
193    
194                    reindexFolders(companyId);
195            }
196    
197            @Override
198            protected String getPortletId(SearchContext searchContext) {
199                    return PORTLET_ID;
200            }
201    
202            protected void reindexFolders(final long companyId)
203                    throws PortalException, SystemException {
204    
205                    ActionableDynamicQuery actionableDynamicQuery =
206                            new DLFolderActionableDynamicQuery() {
207    
208                            @Override
209                            protected void addCriteria(DynamicQuery dynamicQuery) {
210                                    Property property = PropertyFactoryUtil.forName("mountPoint");
211    
212                                    dynamicQuery.add(property.eq(false));
213                            }
214    
215                            @Override
216                            protected void performAction(Object object) throws PortalException {
217                                    DLFolder dlFolder = (DLFolder)object;
218    
219                                    Document document = getDocument(dlFolder);
220    
221                                    if (document != null) {
222                                            addDocument(document);
223                                    }
224                            }
225    
226                    };
227    
228                    actionableDynamicQuery.setCompanyId(companyId);
229                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
230    
231                    actionableDynamicQuery.performActions();
232            }
233    
234            private static Log _log = LogFactoryUtil.getLog(DLFolderIndexer.class);
235    
236    }