001    /**
002     * Copyright (c) 2000-present 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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
022    import com.liferay.portal.kernel.portlet.LiferayWindowState;
023    import com.liferay.portal.kernel.search.BaseIndexer;
024    import com.liferay.portal.kernel.search.BooleanQuery;
025    import com.liferay.portal.kernel.search.Document;
026    import com.liferay.portal.kernel.search.DocumentImpl;
027    import com.liferay.portal.kernel.search.Field;
028    import com.liferay.portal.kernel.search.SearchContext;
029    import com.liferay.portal.kernel.search.SearchEngineUtil;
030    import com.liferay.portal.kernel.search.Summary;
031    import com.liferay.portal.kernel.util.CharPool;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.StringUtil;
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.model.BookmarksFolder;
038    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
039    import com.liferay.portlet.bookmarks.service.permission.BookmarksFolderPermission;
040    
041    import java.util.Locale;
042    
043    import javax.portlet.PortletRequest;
044    import javax.portlet.PortletResponse;
045    import javax.portlet.PortletURL;
046    import javax.portlet.WindowStateException;
047    
048    /**
049     * @author Eduardo Garcia
050     */
051    public class BookmarksFolderIndexer extends BaseIndexer {
052    
053            public static final String[] CLASS_NAMES =
054                    {BookmarksFolder.class.getName()};
055    
056            public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
057    
058            public BookmarksFolderIndexer() {
059                    setDefaultSelectedFieldNames(
060                            Field.COMPANY_ID, Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK,
061                            Field.TITLE, Field.UID);
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(), document.get(Field.UID),
107                            isCommitImmediately());
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                    document.addKeyword(
124                            Field.TREE_PATH,
125                            StringUtil.split(folder.getTreePath(), CharPool.SLASH));
126    
127                    if (_log.isDebugEnabled()) {
128                            _log.debug("Document " + folder + " indexed successfully");
129                    }
130    
131                    return document;
132            }
133    
134            @Override
135            protected Summary doGetSummary(
136                    Document document, Locale locale, String snippet, PortletURL portletURL,
137                    PortletRequest portletRequest, PortletResponse portletResponse) {
138    
139                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
140    
141                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
142    
143                    try {
144                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
145                    }
146                    catch (WindowStateException wse) {
147                    }
148    
149                    String folderId = document.get(Field.ENTRY_CLASS_PK);
150    
151                    portletURL.setParameter("struts_action", "/bookmarks/view");
152                    portletURL.setParameter("folderId", folderId);
153    
154                    Summary summary = createSummary(
155                            document, Field.TITLE, Field.DESCRIPTION);
156    
157                    summary.setMaxContentLength(200);
158                    summary.setPortletURL(portletURL);
159    
160                    return summary;
161            }
162    
163            @Override
164            protected void doReindex(Object obj) throws Exception {
165                    BookmarksFolder folder = (BookmarksFolder)obj;
166    
167                    Document document = getDocument(folder);
168    
169                    if (!folder.isApproved() && !folder.isInTrash()) {
170                            return;
171                    }
172    
173                    if (document != null) {
174                            SearchEngineUtil.updateDocument(
175                                    getSearchEngineId(), folder.getCompanyId(), document,
176                                    isCommitImmediately());
177                    }
178    
179                    SearchEngineUtil.updateDocument(
180                            getSearchEngineId(), folder.getCompanyId(), document,
181                            isCommitImmediately());
182            }
183    
184            @Override
185            protected void doReindex(String className, long classPK) throws Exception {
186                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
187                            classPK);
188    
189                    doReindex(folder);
190            }
191    
192            @Override
193            protected void doReindex(String[] ids) throws Exception {
194                    long companyId = GetterUtil.getLong(ids[0]);
195    
196                    reindexFolders(companyId);
197            }
198    
199            @Override
200            protected String getPortletId(SearchContext searchContext) {
201                    return PORTLET_ID;
202            }
203    
204            protected void reindexFolders(long companyId) throws PortalException {
205                    final ActionableDynamicQuery actionableDynamicQuery =
206                            BookmarksFolderLocalServiceUtil.getActionableDynamicQuery();
207    
208                    actionableDynamicQuery.setCompanyId(companyId);
209                    actionableDynamicQuery.setPerformActionMethod(
210                            new ActionableDynamicQuery.PerformActionMethod() {
211    
212                                    @Override
213                                    public void performAction(Object object)
214                                            throws PortalException {
215    
216                                            BookmarksFolder folder = (BookmarksFolder)object;
217    
218                                            Document document = getDocument(folder);
219    
220                                            actionableDynamicQuery.addDocument(document);
221                                    }
222    
223                            });
224                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
225    
226                    actionableDynamicQuery.performActions();
227            }
228    
229            private static final Log _log = LogFactoryUtil.getLog(
230                    BookmarksFolderIndexer.class);
231    
232    }