001    /**
002     * Copyright (c) 2000-2012 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            public String[] getClassNames() {
061                    return CLASS_NAMES;
062            }
063    
064            public String getPortletId() {
065                    return PORTLET_ID;
066            }
067    
068            @Override
069            public boolean hasPermission(
070                            PermissionChecker permissionChecker, String entryClassName,
071                            long entryClassPK, String actionId)
072                    throws Exception {
073    
074                    WikiNode node = WikiNodeLocalServiceUtil.getNode(entryClassPK);
075    
076                    return WikiNodePermission.contains(
077                            permissionChecker, node, ActionKeys.VIEW);
078            }
079    
080            @Override
081            protected void doDelete(Object obj) throws Exception {
082                    WikiNode node = (WikiNode)obj;
083    
084                    Document document = new DocumentImpl();
085    
086                    document.addUID(PORTLET_ID, node.getNodeId(), node.getName());
087    
088                    SearchEngineUtil.deleteDocument(
089                            getSearchEngineId(), node.getCompanyId(), document.get(Field.UID));
090            }
091    
092            @Override
093            protected Document doGetDocument(Object obj) throws Exception {
094                    WikiNode node = (WikiNode)obj;
095    
096                    Document document = getBaseModelDocument(PORTLET_ID, node);
097    
098                    document.addUID(PORTLET_ID, node.getNodeId(), node.getName());
099    
100                    document.addText(Field.DESCRIPTION, node.getDescription());
101                    document.addText(Field.TITLE, node.getName());
102    
103                    return document;
104            }
105    
106            @Override
107            protected Summary doGetSummary(
108                            Document document, Locale locale, String snippet,
109                            PortletURL portletURL)
110                    throws Exception {
111    
112                    return null;
113            }
114    
115            @Override
116            protected void doReindex(Object obj) throws Exception {
117                    WikiNode node = (WikiNode)obj;
118    
119                    Document document = getDocument(obj);
120    
121                    SearchEngineUtil.updateDocument(
122                            getSearchEngineId(), node.getCompanyId(), document);
123            }
124    
125            @Override
126            protected void doReindex(String className, long classPK) throws Exception {
127                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
128    
129                    doReindex(node);
130            }
131    
132            @Override
133            protected void doReindex(String[] ids) throws Exception {
134                    long companyId = GetterUtil.getLong(ids[0]);
135    
136                    reindexEntries(companyId);
137            }
138    
139            @Override
140            protected String getPortletId(SearchContext searchContext) {
141                    return PORTLET_ID;
142            }
143    
144            protected void reindexEntries(long companyId)
145                    throws PortalException, SystemException {
146    
147                    final Collection<Document> documents = new ArrayList<Document>();
148    
149                    ActionableDynamicQuery actionableDynamicQuery =
150                            new WikiNodeActionableDynamicQuery() {
151    
152                            @Override
153                            protected void addCriteria(DynamicQuery dynamicQuery) {
154                                    Property property = PropertyFactoryUtil.forName("status");
155    
156                                    dynamicQuery.add(
157                                            property.eq(WorkflowConstants.STATUS_IN_TRASH));
158                            }
159    
160                            @Override
161                            protected void performAction(Object object) throws PortalException {
162                                    WikiNode node = (WikiNode)object;
163    
164                                    Document document = getDocument(node);
165    
166                                    documents.add(document);
167                            }
168    
169                    };
170    
171                    actionableDynamicQuery.setCompanyId(companyId);
172    
173                    actionableDynamicQuery.performActions();
174    
175                    SearchEngineUtil.updateDocuments(
176                            getSearchEngineId(), companyId, documents);
177            }
178    
179    }