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