001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.search.BaseIndexer;
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.GetterUtil;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.security.permission.ActionKeys;
035    import com.liferay.portal.security.permission.PermissionChecker;
036    import com.liferay.portal.util.PortletKeys;
037    import com.liferay.portlet.trash.util.TrashUtil;
038    import com.liferay.portlet.wiki.model.WikiNode;
039    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
040    import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
041    import com.liferay.portlet.wiki.service.persistence.WikiNodeActionableDynamicQuery;
042    
043    import java.util.Locale;
044    
045    import javax.portlet.PortletURL;
046    
047    /**
048     * @author Eudaldo Alonso
049     */
050    public class WikiNodeIndexer extends BaseIndexer {
051    
052            public static final String[] CLASS_NAMES = {WikiNode.class.getName()};
053    
054            public static final String PORTLET_ID = PortletKeys.WIKI;
055    
056            public WikiNodeIndexer() {
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());
090    
091                    SearchEngineUtil.deleteDocument(
092                            getSearchEngineId(), node.getCompanyId(), document.get(Field.UID));
093            }
094    
095            @Override
096            protected Document doGetDocument(Object obj) throws Exception {
097                    WikiNode node = (WikiNode)obj;
098    
099                    Document document = getBaseModelDocument(PORTLET_ID, node);
100    
101                    document.addUID(PORTLET_ID, node.getNodeId(), node.getName());
102    
103                    document.addText(Field.DESCRIPTION, node.getDescription());
104    
105                    String title = node.getName();
106    
107                    if (node.isInTrash()) {
108                            title = TrashUtil.getOriginalTitle(title);
109                    }
110    
111                    document.addText(Field.TITLE, title);
112    
113                    return document;
114            }
115    
116            @Override
117            protected Summary doGetSummary(
118                            Document document, Locale locale, String snippet,
119                            PortletURL portletURL)
120                    throws Exception {
121    
122                    return null;
123            }
124    
125            @Override
126            protected void doReindex(Object obj) throws Exception {
127                    WikiNode node = (WikiNode)obj;
128    
129                    Document document = getDocument(obj);
130    
131                    if (!node.isInTrash()) {
132                            SearchEngineUtil.deleteDocument(
133                                    getSearchEngineId(), node.getCompanyId(),
134                                    document.get(Field.UID));
135    
136                            return;
137                    }
138    
139                    SearchEngineUtil.updateDocument(
140                            getSearchEngineId(), node.getCompanyId(), document);
141            }
142    
143            @Override
144            protected void doReindex(String className, long classPK) throws Exception {
145                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
146    
147                    doReindex(node);
148            }
149    
150            @Override
151            protected void doReindex(String[] ids) throws Exception {
152                    long companyId = GetterUtil.getLong(ids[0]);
153    
154                    reindexEntries(companyId);
155            }
156    
157            @Override
158            protected String getPortletId(SearchContext searchContext) {
159                    return PORTLET_ID;
160            }
161    
162            protected void reindexEntries(long companyId)
163                    throws PortalException, SystemException {
164    
165                    ActionableDynamicQuery actionableDynamicQuery =
166                            new WikiNodeActionableDynamicQuery() {
167    
168                            @Override
169                            protected void addCriteria(DynamicQuery dynamicQuery) {
170                                    Property property = PropertyFactoryUtil.forName("status");
171    
172                                    dynamicQuery.add(
173                                            property.eq(WorkflowConstants.STATUS_IN_TRASH));
174                            }
175    
176                            @Override
177                            protected void performAction(Object object) {
178                                    WikiNode node = (WikiNode)object;
179    
180                                    try {
181                                            Document document = getDocument(node);
182    
183                                            addDocument(document);
184                                    }
185                                    catch (PortalException pe) {
186                                            if (_log.isWarnEnabled()) {
187                                                    _log.warn(
188                                                            "Unable to index wiki node " + node.getNodeId(),
189                                                            pe);
190                                            }
191                                    }
192                            }
193    
194                    };
195    
196                    actionableDynamicQuery.setCompanyId(companyId);
197                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
198    
199                    actionableDynamicQuery.performActions();
200            }
201    
202            private static Log _log = LogFactoryUtil.getLog(WikiNodeIndexer.class);
203    
204    }