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.Locale;
044    
045    import javax.portlet.PortletRequest;
046    import javax.portlet.PortletURL;
047    import javax.portlet.WindowStateException;
048    
049    /**
050     * @author Eduardo Garcia
051     */
052    public class BookmarksFolderIndexer extends BaseIndexer {
053    
054            public static final String[] CLASS_NAMES =
055                    {BookmarksFolder.class.getName()};
056    
057            public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
058    
059            public BookmarksFolderIndexer() {
060                    setFilterSearch(true);
061                    setPermissionAware(true);
062            }
063    
064            @Override
065            public String[] getClassNames() {
066                    return CLASS_NAMES;
067            }
068    
069            @Override
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                    addStatus(contextQuery, searchContext);
093            }
094    
095            @Override
096            protected void doDelete(Object obj) throws Exception {
097                    BookmarksFolder folder = (BookmarksFolder)obj;
098    
099                    Document document = new DocumentImpl();
100    
101                    document.addUID(PORTLET_ID, folder.getFolderId());
102    
103                    SearchEngineUtil.deleteDocument(
104                            getSearchEngineId(), folder.getCompanyId(),
105                            document.get(Field.UID));
106            }
107    
108            @Override
109            protected Document doGetDocument(Object obj) throws Exception {
110                    BookmarksFolder folder = (BookmarksFolder)obj;
111    
112                    if (_log.isDebugEnabled()) {
113                            _log.debug("Indexing folder " + folder);
114                    }
115    
116                    Document document = getBaseModelDocument(PORTLET_ID, folder);
117    
118                    document.addText(Field.DESCRIPTION, folder.getDescription());
119                    document.addKeyword(Field.FOLDER_ID, folder.getParentFolderId());
120                    document.addText(Field.TITLE, folder.getName());
121                    document.addKeyword(
122                            Field.TREE_PATH,
123                            StringUtil.split(folder.getTreePath(), CharPool.SLASH));
124    
125                    if (_log.isDebugEnabled()) {
126                            _log.debug("Document " + folder + " indexed successfully");
127                    }
128    
129                    return document;
130            }
131    
132            @Override
133            protected Summary doGetSummary(
134                    Document document, Locale locale, String snippet,
135                    PortletURL portletURL) {
136    
137                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
138    
139                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
140    
141                    try {
142                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
143                    }
144                    catch (WindowStateException wse) {
145                    }
146    
147                    String folderId = document.get(Field.ENTRY_CLASS_PK);
148    
149                    portletURL.setParameter("struts_action", "/bookmarks/view");
150                    portletURL.setParameter("folderId", folderId);
151    
152                    Summary summary = createSummary(
153                            document, Field.TITLE, Field.DESCRIPTION);
154    
155                    summary.setMaxContentLength(200);
156                    summary.setPortletURL(portletURL);
157    
158                    return summary;
159            }
160    
161            @Override
162            protected void doReindex(Object obj) throws Exception {
163                    BookmarksFolder folder = (BookmarksFolder)obj;
164    
165                    Document document = getDocument(folder);
166    
167                    if (!folder.isApproved() && !folder.isInTrash()) {
168                            return;
169                    }
170    
171                    if (document != null) {
172                            SearchEngineUtil.updateDocument(
173                                    getSearchEngineId(), folder.getCompanyId(), document);
174                    }
175    
176                    SearchEngineUtil.updateDocument(
177                            getSearchEngineId(), folder.getCompanyId(), document);
178            }
179    
180            @Override
181            protected void doReindex(String className, long classPK) throws Exception {
182                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
183                            classPK);
184    
185                    doReindex(folder);
186            }
187    
188            @Override
189            protected void doReindex(String[] ids) throws Exception {
190                    long companyId = GetterUtil.getLong(ids[0]);
191    
192                    reindexFolders(companyId);
193            }
194    
195            @Override
196            protected String getPortletId(SearchContext searchContext) {
197                    return PORTLET_ID;
198            }
199    
200            protected void reindexFolders(long companyId)
201                    throws PortalException, SystemException {
202    
203                    ActionableDynamicQuery actionableDynamicQuery =
204                            new BookmarksFolderActionableDynamicQuery() {
205    
206                            @Override
207                            protected void performAction(Object object) throws PortalException {
208                                    BookmarksFolder folder = (BookmarksFolder)object;
209    
210                                    Document document = getDocument(folder);
211    
212                                    addDocument(document);
213                            }
214    
215                    };
216    
217                    actionableDynamicQuery.setCompanyId(companyId);
218                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
219    
220                    actionableDynamicQuery.performActions();
221            }
222    
223            private static Log _log = LogFactoryUtil.getLog(
224                    BookmarksFolderIndexer.class);
225    
226    }