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.Locale;
041    
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                    setFilterSearch(false);
055                    setPermissionAware(false);
056            }
057    
058            @Override
059            public String[] getClassNames() {
060                    return CLASS_NAMES;
061            }
062    
063            @Override
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());
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                    if (!node.isInTrash()) {
122                            SearchEngineUtil.deleteDocument(
123                                    getSearchEngineId(), node.getCompanyId(),
124                                    document.get(Field.UID));
125    
126                            return;
127                    }
128    
129                    SearchEngineUtil.updateDocument(
130                            getSearchEngineId(), node.getCompanyId(), document);
131            }
132    
133            @Override
134            protected void doReindex(String className, long classPK) throws Exception {
135                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
136    
137                    doReindex(node);
138            }
139    
140            @Override
141            protected void doReindex(String[] ids) throws Exception {
142                    long companyId = GetterUtil.getLong(ids[0]);
143    
144                    reindexEntries(companyId);
145            }
146    
147            @Override
148            protected String getPortletId(SearchContext searchContext) {
149                    return PORTLET_ID;
150            }
151    
152            protected void reindexEntries(long companyId)
153                    throws PortalException, SystemException {
154    
155                    ActionableDynamicQuery actionableDynamicQuery =
156                            new WikiNodeActionableDynamicQuery() {
157    
158                            @Override
159                            protected void addCriteria(DynamicQuery dynamicQuery) {
160                                    Property property = PropertyFactoryUtil.forName("status");
161    
162                                    dynamicQuery.add(
163                                            property.eq(WorkflowConstants.STATUS_IN_TRASH));
164                            }
165    
166                            @Override
167                            protected void performAction(Object object) throws PortalException {
168                                    WikiNode node = (WikiNode)object;
169    
170                                    Document document = getDocument(node);
171    
172                                    addDocument(document);
173                            }
174    
175                    };
176    
177                    actionableDynamicQuery.setCompanyId(companyId);
178                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
179    
180                    actionableDynamicQuery.performActions();
181            }
182    
183    }