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.bookmarks.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
023    import com.liferay.portal.kernel.portlet.LiferayWindowState;
024    import com.liferay.portal.kernel.search.BaseIndexer;
025    import com.liferay.portal.kernel.search.BooleanQuery;
026    import com.liferay.portal.kernel.search.Document;
027    import com.liferay.portal.kernel.search.DocumentImpl;
028    import com.liferay.portal.kernel.search.Field;
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.util.GetterUtil;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.security.permission.ActionKeys;
035    import com.liferay.portal.security.permission.PermissionChecker;
036    import com.liferay.portal.util.PortletKeys;
037    import com.liferay.portlet.bookmarks.asset.BookmarksFolderAssetRendererFactory;
038    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
039    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
040    import com.liferay.portlet.bookmarks.service.permission.BookmarksFolderPermission;
041    import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderActionableDynamicQuery;
042    
043    import java.util.ArrayList;
044    import java.util.Collection;
045    import java.util.Locale;
046    
047    import javax.portlet.PortletRequest;
048    import javax.portlet.PortletURL;
049    import javax.portlet.WindowStateException;
050    
051    /**
052     * @author Eduardo Garcia
053     */
054    public class BookmarksFolderIndexer extends BaseIndexer {
055    
056            public static final String[] CLASS_NAMES =
057                    {BookmarksFolder.class.getName()};
058    
059            public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
060    
061            public BookmarksFolderIndexer() {
062                    setFilterSearch(true);
063                    setPermissionAware(true);
064            }
065    
066            public String[] getClassNames() {
067                    return CLASS_NAMES;
068            }
069    
070            public String getPortletId() {
071                    return PORTLET_ID;
072            }
073    
074            @Override
075            public boolean hasPermission(
076                            PermissionChecker permissionChecker, String entryClassName,
077                            long entryClassPK, String actionId)
078                    throws Exception {
079    
080                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
081                            entryClassPK);
082    
083                    return BookmarksFolderPermission.contains(
084                            permissionChecker, folder, ActionKeys.VIEW);
085            }
086    
087            @Override
088            public void postProcessContextQuery(
089                            BooleanQuery contextQuery, SearchContext searchContext)
090                    throws Exception {
091    
092                    int status = GetterUtil.getInteger(
093                            searchContext.getAttribute(Field.STATUS),
094                            WorkflowConstants.STATUS_APPROVED);
095    
096                    if (status != WorkflowConstants.STATUS_ANY) {
097                            contextQuery.addRequiredTerm(Field.STATUS, status);
098                    }
099            }
100    
101            @Override
102            protected void doDelete(Object obj) throws Exception {
103                    BookmarksFolder folder = (BookmarksFolder)obj;
104    
105                    Document document = new DocumentImpl();
106    
107                    document.addUID(PORTLET_ID, folder.getFolderId(), folder.getName());
108    
109                    SearchEngineUtil.deleteDocument(
110                            getSearchEngineId(), folder.getCompanyId(),
111                            document.get(Field.UID));
112            }
113    
114            @Override
115            protected Document doGetDocument(Object obj) throws Exception {
116                    BookmarksFolder folder = (BookmarksFolder)obj;
117    
118                    if (_log.isDebugEnabled()) {
119                            _log.debug("Indexing folder " + folder);
120                    }
121    
122                    Document document = getBaseModelDocument(PORTLET_ID, folder);
123    
124                    document.addText(Field.DESCRIPTION, folder.getDescription());
125                    document.addKeyword(Field.FOLDER_ID, folder.getParentFolderId());
126                    document.addText(Field.TITLE, folder.getName());
127    
128                    if (!folder.isInTrash() && folder.isInTrashContainer()) {
129                            BookmarksFolder trashedFolder = folder.getTrashContainer();
130    
131                            if (trashedFolder != null) {
132                                    addTrashFields(
133                                            document, BookmarksFolder.class.getName(),
134                                            trashedFolder.getFolderId(), null, null,
135                                            BookmarksFolderAssetRendererFactory.TYPE);
136    
137                                    document.addKeyword(
138                                            Field.ROOT_ENTRY_CLASS_NAME,
139                                            BookmarksFolder.class.getName());
140                                    document.addKeyword(
141                                            Field.ROOT_ENTRY_CLASS_PK, trashedFolder.getFolderId());
142                                    document.addKeyword(
143                                            Field.STATUS, WorkflowConstants.STATUS_IN_TRASH);
144                            }
145                    }
146    
147                    if (_log.isDebugEnabled()) {
148                            _log.debug("Document " + folder + " indexed successfully");
149                    }
150    
151                    return document;
152            }
153    
154            @Override
155            protected Summary doGetSummary(
156                    Document document, Locale locale, String snippet,
157                    PortletURL portletURL) {
158    
159                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
160    
161                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
162    
163                    try {
164                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
165                    }
166                    catch (WindowStateException wse) {
167                    }
168    
169                    String folderId = document.get(Field.ENTRY_CLASS_PK);
170    
171                    portletURL.setParameter("struts_action", "/bookmarks/view");
172                    portletURL.setParameter("folderId", folderId);
173    
174                    Summary summary = createSummary(
175                            document, Field.TITLE, Field.DESCRIPTION);
176    
177                    summary.setMaxContentLength(200);
178                    summary.setPortletURL(portletURL);
179    
180                    return summary;
181            }
182    
183            @Override
184            protected void doReindex(Object obj) throws Exception {
185                    BookmarksFolder folder = (BookmarksFolder)obj;
186    
187                    Document document = getDocument(folder);
188    
189                    if (!folder.isApproved() && !folder.isInTrash()) {
190                            return;
191                    }
192    
193                    if (document != null) {
194                            SearchEngineUtil.updateDocument(
195                                    getSearchEngineId(), folder.getCompanyId(), document);
196                    }
197    
198                    SearchEngineUtil.updateDocument(
199                            getSearchEngineId(), folder.getCompanyId(), document);
200            }
201    
202            @Override
203            protected void doReindex(String className, long classPK) throws Exception {
204                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
205                            classPK);
206    
207                    doReindex(folder);
208            }
209    
210            @Override
211            protected void doReindex(String[] ids) throws Exception {
212                    long companyId = GetterUtil.getLong(ids[0]);
213    
214                    reindexFolders(companyId);
215            }
216    
217            @Override
218            protected String getPortletId(SearchContext searchContext) {
219                    return PORTLET_ID;
220            }
221    
222            protected void reindexFolders(long companyId)
223                    throws PortalException, SystemException {
224    
225                    final Collection<Document> documents = new ArrayList<Document>();
226    
227                    ActionableDynamicQuery actionableDynamicQuery =
228                            new BookmarksFolderActionableDynamicQuery() {
229    
230                            @Override
231                            protected void performAction(Object object) throws PortalException {
232                                    BookmarksFolder folder = (BookmarksFolder)object;
233    
234                                    Document document = getDocument(folder);
235    
236                                    documents.add(document);
237                            }
238    
239                    };
240    
241                    actionableDynamicQuery.setCompanyId(companyId);
242    
243                    actionableDynamicQuery.performActions();
244    
245                    SearchEngineUtil.updateDocuments(
246                            getSearchEngineId(), companyId, documents);
247            }
248    
249            private static Log _log = LogFactoryUtil.getLog(
250                    BookmarksFolderIndexer.class);
251    
252    }