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.wiki.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.search.BaseIndexer;
024    import com.liferay.portal.kernel.search.Document;
025    import com.liferay.portal.kernel.search.DocumentImpl;
026    import com.liferay.portal.kernel.search.Field;
027    import com.liferay.portal.kernel.search.SearchContext;
028    import com.liferay.portal.kernel.search.SearchEngineUtil;
029    import com.liferay.portal.kernel.search.Summary;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.workflow.WorkflowConstants;
032    import com.liferay.portal.security.permission.ActionKeys;
033    import com.liferay.portal.security.permission.PermissionChecker;
034    import com.liferay.portal.util.PortletKeys;
035    import com.liferay.portlet.wiki.model.WikiNode;
036    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
037    import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
038    import com.liferay.portlet.wiki.service.persistence.WikiNodeActionableDynamicQuery;
039    
040    import java.util.ArrayList;
041    import java.util.Collection;
042    import java.util.Locale;
043    
044    import javax.portlet.PortletURL;
045    
046    /**
047     * @author Eudaldo Alonso
048     */
049    public class WikiNodeIndexer extends BaseIndexer {
050    
051            public static final String[] CLASS_NAMES = {WikiNode.class.getName()};
052    
053            public static final String PORTLET_ID = PortletKeys.WIKI;
054    
055            public WikiNodeIndexer() {
056                    setFilterSearch(false);
057                    setPermissionAware(false);
058            }
059    
060            @Override
061            public String[] getClassNames() {
062                    return CLASS_NAMES;
063            }
064    
065            @Override
066            public String getPortletId() {
067                    return PORTLET_ID;
068            }
069    
070            @Override
071            public boolean hasPermission(
072                            PermissionChecker permissionChecker, String entryClassName,
073                            long entryClassPK, String actionId)
074                    throws Exception {
075    
076                    WikiNode node = WikiNodeLocalServiceUtil.getNode(entryClassPK);
077    
078                    return WikiNodePermission.contains(
079                            permissionChecker, node, ActionKeys.VIEW);
080            }
081    
082            @Override
083            protected void doDelete(Object obj) throws Exception {
084                    WikiNode node = (WikiNode)obj;
085    
086                    Document document = new DocumentImpl();
087    
088                    document.addUID(PORTLET_ID, node.getNodeId(), node.getName());
089    
090                    SearchEngineUtil.deleteDocument(
091                            getSearchEngineId(), node.getCompanyId(), document.get(Field.UID));
092            }
093    
094            @Override
095            protected Document doGetDocument(Object obj) throws Exception {
096                    WikiNode node = (WikiNode)obj;
097    
098                    Document document = getBaseModelDocument(PORTLET_ID, node);
099    
100                    document.addUID(PORTLET_ID, node.getNodeId(), node.getName());
101    
102                    document.addText(Field.DESCRIPTION, node.getDescription());
103                    document.addText(Field.TITLE, node.getName());
104    
105                    return document;
106            }
107    
108            @Override
109            protected Summary doGetSummary(
110                            Document document, Locale locale, String snippet,
111                            PortletURL portletURL)
112                    throws Exception {
113    
114                    return null;
115            }
116    
117            @Override
118            protected void doReindex(Object obj) throws Exception {
119                    WikiNode node = (WikiNode)obj;
120    
121                    Document document = getDocument(obj);
122    
123                    SearchEngineUtil.updateDocument(
124                            getSearchEngineId(), node.getCompanyId(), document);
125            }
126    
127            @Override
128            protected void doReindex(String className, long classPK) throws Exception {
129                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
130    
131                    doReindex(node);
132            }
133    
134            @Override
135            protected void doReindex(String[] ids) throws Exception {
136                    long companyId = GetterUtil.getLong(ids[0]);
137    
138                    reindexEntries(companyId);
139            }
140    
141            @Override
142            protected String getPortletId(SearchContext searchContext) {
143                    return PORTLET_ID;
144            }
145    
146            protected void reindexEntries(long companyId)
147                    throws PortalException, SystemException {
148    
149                    final Collection<Document> documents = new ArrayList<Document>();
150    
151                    ActionableDynamicQuery actionableDynamicQuery =
152                            new WikiNodeActionableDynamicQuery() {
153    
154                            @Override
155                            protected void addCriteria(DynamicQuery dynamicQuery) {
156                                    Property property = PropertyFactoryUtil.forName("status");
157    
158                                    dynamicQuery.add(
159                                            property.eq(WorkflowConstants.STATUS_IN_TRASH));
160                            }
161    
162                            @Override
163                            protected void performAction(Object object) throws PortalException {
164                                    WikiNode node = (WikiNode)object;
165    
166                                    Document document = getDocument(node);
167    
168                                    documents.add(document);
169                            }
170    
171                    };
172    
173                    actionableDynamicQuery.setCompanyId(companyId);
174    
175                    actionableDynamicQuery.performActions();
176    
177                    SearchEngineUtil.updateDocuments(
178                            getSearchEngineId(), companyId, documents);
179            }
180    
181    }