001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portlet.trash.util.TrashUtil;
046    
047    import java.util.Locale;
048    
049    import javax.portlet.PortletRequest;
050    import javax.portlet.PortletURL;
051    import javax.portlet.WindowStateException;
052    
053    /**
054     * @author Alexander Chow
055     */
056    public class DLFolderIndexer extends BaseIndexer {
057    
058            public static final String[] CLASS_NAMES = {DLFolder.class.getName()};
059    
060            public static final String PORTLET_ID = PortletKeys.DOCUMENT_LIBRARY;
061    
062            public DLFolderIndexer() {
063                    setFilterSearch(true);
064                    setPermissionAware(true);
065            }
066    
067            @Override
068            public String[] getClassNames() {
069                    return CLASS_NAMES;
070            }
071    
072            @Override
073            public String getPortletId() {
074                    return PORTLET_ID;
075            }
076    
077            @Override
078            public boolean hasPermission(
079                            PermissionChecker permissionChecker, String entryClassName,
080                            long entryClassPK, String actionId)
081                    throws Exception {
082    
083                    DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(entryClassPK);
084    
085                    return DLFolderPermission.contains(
086                            permissionChecker, dlFolder, ActionKeys.VIEW);
087            }
088    
089            @Override
090            public void postProcessContextQuery(
091                            BooleanQuery contextQuery, SearchContext searchContext)
092                    throws Exception {
093    
094                    addStatus(contextQuery, searchContext);
095    
096                    contextQuery.addRequiredTerm(Field.HIDDEN, false);
097            }
098    
099            @Override
100            protected void doDelete(Object obj) throws Exception {
101                    DLFolder dlFolder = (DLFolder)obj;
102    
103                    Document document = new DocumentImpl();
104    
105                    document.addUID(PORTLET_ID, dlFolder.getFolderId());
106    
107                    SearchEngineUtil.deleteDocument(
108                            getSearchEngineId(), dlFolder.getCompanyId(),
109                            document.get(Field.UID));
110            }
111    
112            @Override
113            protected Document doGetDocument(Object obj) throws Exception {
114                    DLFolder dlFolder = (DLFolder)obj;
115    
116                    if (_log.isDebugEnabled()) {
117                            _log.debug("Indexing folder " + dlFolder);
118                    }
119    
120                    Document document = getBaseModelDocument(PORTLET_ID, dlFolder);
121    
122                    document.addText(Field.DESCRIPTION, dlFolder.getDescription());
123                    document.addKeyword(Field.FOLDER_ID, dlFolder.getParentFolderId());
124                    document.addKeyword(
125                            Field.HIDDEN, (dlFolder.isHidden() || dlFolder.isInHiddenFolder()));
126    
127                    String title = dlFolder.getName();
128    
129                    if (dlFolder.isInTrash()) {
130                            title = TrashUtil.getOriginalTitle(title);
131                    }
132    
133                    document.addText(Field.TITLE, title);
134    
135                    document.addKeyword(Field.TREE_PATH, dlFolder.getTreePath());
136                    document.addKeyword(
137                            Field.TREE_PATH,
138                            StringUtil.split(dlFolder.getTreePath(), CharPool.SLASH));
139    
140                    if (_log.isDebugEnabled()) {
141                            _log.debug("Document " + dlFolder + " indexed successfully");
142                    }
143    
144                    return document;
145            }
146    
147            @Override
148            protected Summary doGetSummary(
149                    Document document, Locale locale, String snippet,
150                    PortletURL portletURL) {
151    
152                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
153    
154                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
155    
156                    try {
157                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
158                    }
159                    catch (WindowStateException wse) {
160                    }
161    
162                    String folderId = document.get(Field.ENTRY_CLASS_PK);
163    
164                    portletURL.setParameter("struts_action", "/document_library/view");
165                    portletURL.setParameter("folderId", folderId);
166    
167                    Summary summary = createSummary(
168                            document, Field.TITLE, Field.DESCRIPTION);
169    
170                    summary.setMaxContentLength(200);
171                    summary.setPortletURL(portletURL);
172    
173                    return summary;
174            }
175    
176            @Override
177            protected void doReindex(Object obj) throws Exception {
178                    DLFolder dlFolder = (DLFolder)obj;
179    
180                    if (!dlFolder.isApproved() && !dlFolder.isInTrash()) {
181                            return;
182                    }
183    
184                    Document document = getDocument(dlFolder);
185    
186                    if (document != null) {
187                            SearchEngineUtil.updateDocument(
188                                    getSearchEngineId(), dlFolder.getCompanyId(), document);
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)
212                    throws PortalException, SystemException {
213    
214                    ActionableDynamicQuery actionableDynamicQuery =
215                            new DLFolderActionableDynamicQuery() {
216    
217                            @Override
218                            protected void addCriteria(DynamicQuery dynamicQuery) {
219                                    Property property = PropertyFactoryUtil.forName("mountPoint");
220    
221                                    dynamicQuery.add(property.eq(false));
222                            }
223    
224                            @Override
225                            protected void performAction(Object object) {
226                                    DLFolder dlFolder = (DLFolder)object;
227    
228                                    try {
229                                            Document document = getDocument(dlFolder);
230    
231                                            if (document != null) {
232                                                    addDocument(document);
233                                            }
234                                    }
235                                    catch (PortalException pe) {
236                                            if (_log.isWarnEnabled()) {
237                                                    _log.warn(
238                                                            "Unable to index document library folder " +
239                                                                    dlFolder.getFolderId(),
240                                                            pe);
241                                            }
242                                    }
243                            }
244    
245                    };
246    
247                    actionableDynamicQuery.setCompanyId(companyId);
248                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
249    
250                    actionableDynamicQuery.performActions();
251            }
252    
253            private static Log _log = LogFactoryUtil.getLog(DLFolderIndexer.class);
254    
255    }