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