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.security.permission.ActionKeys;
036    import com.liferay.portal.security.permission.PermissionChecker;
037    import com.liferay.portal.util.PortletKeys;
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                    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,
137                    PortletURL portletURL) {
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                    }
177    
178                    SearchEngineUtil.updateDocument(
179                            getSearchEngineId(), folder.getCompanyId(), document);
180            }
181    
182            @Override
183            protected void doReindex(String className, long classPK) throws Exception {
184                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
185                            classPK);
186    
187                    doReindex(folder);
188            }
189    
190            @Override
191            protected void doReindex(String[] ids) throws Exception {
192                    long companyId = GetterUtil.getLong(ids[0]);
193    
194                    reindexFolders(companyId);
195            }
196    
197            @Override
198            protected String getPortletId(SearchContext searchContext) {
199                    return PORTLET_ID;
200            }
201    
202            protected void reindexFolders(long companyId)
203                    throws PortalException, SystemException {
204    
205                    final Collection<Document> documents = new ArrayList<Document>();
206    
207                    ActionableDynamicQuery actionableDynamicQuery =
208                            new BookmarksFolderActionableDynamicQuery() {
209    
210                            @Override
211                            protected void performAction(Object object) throws PortalException {
212                                    BookmarksFolder folder = (BookmarksFolder)object;
213    
214                                    Document document = getDocument(folder);
215    
216                                    documents.add(document);
217                            }
218    
219                    };
220    
221                    actionableDynamicQuery.setCompanyId(companyId);
222    
223                    actionableDynamicQuery.performActions();
224    
225                    SearchEngineUtil.updateDocuments(
226                            getSearchEngineId(), companyId, documents);
227            }
228    
229            private static Log _log = LogFactoryUtil.getLog(
230                    BookmarksFolderIndexer.class);
231    
232    }