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            @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                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
083                            entryClassPK);
084    
085                    return BookmarksFolderPermission.contains(
086                            permissionChecker, folder, 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    
097            @Override
098            protected void doDelete(Object obj) throws Exception {
099                    BookmarksFolder folder = (BookmarksFolder)obj;
100    
101                    Document document = new DocumentImpl();
102    
103                    document.addUID(PORTLET_ID, folder.getFolderId(), folder.getName());
104    
105                    SearchEngineUtil.deleteDocument(
106                            getSearchEngineId(), folder.getCompanyId(),
107                            document.get(Field.UID));
108            }
109    
110            @Override
111            protected Document doGetDocument(Object obj) throws Exception {
112                    BookmarksFolder folder = (BookmarksFolder)obj;
113    
114                    if (_log.isDebugEnabled()) {
115                            _log.debug("Indexing folder " + folder);
116                    }
117    
118                    Document document = getBaseModelDocument(PORTLET_ID, folder);
119    
120                    document.addText(Field.DESCRIPTION, folder.getDescription());
121                    document.addKeyword(Field.FOLDER_ID, folder.getParentFolderId());
122                    document.addText(Field.TITLE, folder.getName());
123    
124                    if (!folder.isInTrash() && folder.isInTrashContainer()) {
125                            BookmarksFolder trashedFolder = folder.getTrashContainer();
126    
127                            if (trashedFolder != null) {
128                                    addTrashFields(
129                                            document, BookmarksFolder.class.getName(),
130                                            trashedFolder.getFolderId(), null, null,
131                                            BookmarksFolderAssetRendererFactory.TYPE);
132    
133                                    document.addKeyword(
134                                            Field.ROOT_ENTRY_CLASS_NAME,
135                                            BookmarksFolder.class.getName());
136                                    document.addKeyword(
137                                            Field.ROOT_ENTRY_CLASS_PK, trashedFolder.getFolderId());
138                                    document.addKeyword(
139                                            Field.STATUS, WorkflowConstants.STATUS_IN_TRASH);
140                            }
141                    }
142    
143                    if (_log.isDebugEnabled()) {
144                            _log.debug("Document " + folder + " indexed successfully");
145                    }
146    
147                    return document;
148            }
149    
150            @Override
151            protected Summary doGetSummary(
152                    Document document, Locale locale, String snippet,
153                    PortletURL portletURL) {
154    
155                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
156    
157                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
158    
159                    try {
160                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
161                    }
162                    catch (WindowStateException wse) {
163                    }
164    
165                    String folderId = document.get(Field.ENTRY_CLASS_PK);
166    
167                    portletURL.setParameter("struts_action", "/bookmarks/view");
168                    portletURL.setParameter("folderId", folderId);
169    
170                    Summary summary = createSummary(
171                            document, Field.TITLE, Field.DESCRIPTION);
172    
173                    summary.setMaxContentLength(200);
174                    summary.setPortletURL(portletURL);
175    
176                    return summary;
177            }
178    
179            @Override
180            protected void doReindex(Object obj) throws Exception {
181                    BookmarksFolder folder = (BookmarksFolder)obj;
182    
183                    Document document = getDocument(folder);
184    
185                    if (!folder.isApproved() && !folder.isInTrash()) {
186                            return;
187                    }
188    
189                    if (document != null) {
190                            SearchEngineUtil.updateDocument(
191                                    getSearchEngineId(), folder.getCompanyId(), document);
192                    }
193    
194                    SearchEngineUtil.updateDocument(
195                            getSearchEngineId(), folder.getCompanyId(), document);
196            }
197    
198            @Override
199            protected void doReindex(String className, long classPK) throws Exception {
200                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
201                            classPK);
202    
203                    doReindex(folder);
204            }
205    
206            @Override
207            protected void doReindex(String[] ids) throws Exception {
208                    long companyId = GetterUtil.getLong(ids[0]);
209    
210                    reindexFolders(companyId);
211            }
212    
213            @Override
214            protected String getPortletId(SearchContext searchContext) {
215                    return PORTLET_ID;
216            }
217    
218            protected void reindexFolders(long companyId)
219                    throws PortalException, SystemException {
220    
221                    final Collection<Document> documents = new ArrayList<Document>();
222    
223                    ActionableDynamicQuery actionableDynamicQuery =
224                            new BookmarksFolderActionableDynamicQuery() {
225    
226                            @Override
227                            protected void performAction(Object object) throws PortalException {
228                                    BookmarksFolder folder = (BookmarksFolder)object;
229    
230                                    Document document = getDocument(folder);
231    
232                                    documents.add(document);
233                            }
234    
235                    };
236    
237                    actionableDynamicQuery.setCompanyId(companyId);
238    
239                    actionableDynamicQuery.performActions();
240    
241                    SearchEngineUtil.updateDocuments(
242                            getSearchEngineId(), companyId, documents);
243            }
244    
245            private static Log _log = LogFactoryUtil.getLog(
246                    BookmarksFolderIndexer.class);
247    
248    }