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.dao.orm.QueryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.search.BaseIndexer;
025    import com.liferay.portal.kernel.search.BooleanClauseOccur;
026    import com.liferay.portal.kernel.search.BooleanQuery;
027    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
028    import com.liferay.portal.kernel.search.Document;
029    import com.liferay.portal.kernel.search.DocumentImpl;
030    import com.liferay.portal.kernel.search.Field;
031    import com.liferay.portal.kernel.search.Hits;
032    import com.liferay.portal.kernel.search.SearchContext;
033    import com.liferay.portal.kernel.search.SearchEngineUtil;
034    import com.liferay.portal.kernel.search.Summary;
035    import com.liferay.portal.kernel.util.GetterUtil;
036    import com.liferay.portal.kernel.util.HtmlUtil;
037    import com.liferay.portal.kernel.util.StringUtil;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.kernel.workflow.WorkflowConstants;
040    import com.liferay.portal.security.permission.ActionKeys;
041    import com.liferay.portal.security.permission.PermissionChecker;
042    import com.liferay.portal.util.PortletKeys;
043    import com.liferay.portlet.wiki.model.WikiNode;
044    import com.liferay.portlet.wiki.model.WikiPage;
045    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
046    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
047    import com.liferay.portlet.wiki.service.permission.WikiPagePermission;
048    import com.liferay.portlet.wiki.service.persistence.WikiNodeActionableDynamicQuery;
049    import com.liferay.portlet.wiki.service.persistence.WikiPageActionableDynamicQuery;
050    
051    import java.util.ArrayList;
052    import java.util.Collection;
053    import java.util.Locale;
054    
055    import javax.portlet.PortletURL;
056    
057    /**
058     * @author Brian Wing Shun Chan
059     * @author Harry Mark
060     * @author Bruno Farache
061     * @author Raymond Aug??
062     */
063    public class WikiIndexer extends BaseIndexer {
064    
065            public static final String[] CLASS_NAMES = {WikiPage.class.getName()};
066    
067            public static final String PORTLET_ID = PortletKeys.WIKI;
068    
069            @Override
070            public String[] getClassNames() {
071                    return CLASS_NAMES;
072            }
073    
074            @Override
075            public String getPortletId() {
076                    return PORTLET_ID;
077            }
078    
079            @Override
080            public boolean hasPermission(
081                            PermissionChecker permissionChecker, long entryClassPK,
082                            String actionId)
083                    throws Exception {
084    
085                    WikiPage page = WikiPageLocalServiceUtil.getPage(entryClassPK);
086    
087                    return WikiPagePermission.contains(
088                            permissionChecker, page, ActionKeys.VIEW);
089            }
090    
091            @Override
092            public boolean isFilterSearch() {
093                    return _FILTER_SEARCH;
094            }
095    
096            @Override
097            public boolean isPermissionAware() {
098                    return _PERMISSION_AWARE;
099            }
100    
101            @Override
102            public void postProcessContextQuery(
103                            BooleanQuery contextQuery, SearchContext searchContext)
104                    throws Exception {
105    
106                    int status = GetterUtil.getInteger(
107                            searchContext.getAttribute(Field.STATUS),
108                            WorkflowConstants.STATUS_ANY);
109    
110                    if (status != WorkflowConstants.STATUS_ANY) {
111                            contextQuery.addRequiredTerm(Field.STATUS, status);
112                    }
113    
114                    long[] nodeIds = searchContext.getNodeIds();
115    
116                    if ((nodeIds != null) && (nodeIds.length > 0)) {
117                            BooleanQuery nodeIdsQuery = BooleanQueryFactoryUtil.create(
118                                    searchContext);
119    
120                            for (long nodeId : nodeIds) {
121                                    try {
122                                            WikiNodeServiceUtil.getNode(nodeId);
123                                    }
124                                    catch (Exception e) {
125                                            continue;
126                                    }
127    
128                                    nodeIdsQuery.addTerm(Field.NODE_ID, nodeId);
129                            }
130    
131                            contextQuery.add(nodeIdsQuery, BooleanClauseOccur.MUST);
132                    }
133            }
134    
135            @Override
136            protected void doDelete(Object obj) throws Exception {
137                    SearchContext searchContext = new SearchContext();
138    
139                    searchContext.setSearchEngineId(getSearchEngineId());
140    
141                    if (obj instanceof Object[]) {
142                            Object[] array = (Object[])obj;
143    
144                            long companyId = (Long)array[0];
145                            long nodeId = (Long)array[1];
146                            String title = (String)array[2];
147    
148                            Document document = new DocumentImpl();
149    
150                            document.addUID(PORTLET_ID, nodeId, title);
151    
152                            SearchEngineUtil.deleteDocument(
153                                    getSearchEngineId(), companyId, document.get(Field.UID));
154    
155                    }
156                    else if (obj instanceof WikiNode) {
157                            WikiNode node = (WikiNode)obj;
158    
159                            BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(
160                                    searchContext);
161    
162                            booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
163    
164                            booleanQuery.addRequiredTerm("nodeId", node.getNodeId());
165    
166                            Hits hits = SearchEngineUtil.search(
167                                    getSearchEngineId(), node.getCompanyId(), booleanQuery,
168                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
169    
170                            for (int i = 0; i < hits.getLength(); i++) {
171                                    Document document = hits.doc(i);
172    
173                                    SearchEngineUtil.deleteDocument(
174                                            getSearchEngineId(), node.getCompanyId(),
175                                            document.get(Field.UID));
176                            }
177                    }
178                    else if (obj instanceof WikiPage) {
179                            WikiPage page = (WikiPage)obj;
180    
181                            Document document = new DocumentImpl();
182    
183                            document.addUID(PORTLET_ID, page.getNodeId(), page.getTitle());
184    
185                            SearchEngineUtil.deleteDocument(
186                                    getSearchEngineId(), page.getCompanyId(),
187                                    document.get(Field.UID));
188                    }
189            }
190    
191            @Override
192            protected Document doGetDocument(Object obj) throws Exception {
193                    WikiPage page = (WikiPage)obj;
194    
195                    Document document = getBaseModelDocument(PORTLET_ID, page);
196    
197                    document.addUID(PORTLET_ID, page.getNodeId(), page.getTitle());
198    
199                    String content = HtmlUtil.extractText(
200                            WikiUtil.convert(page, null, null, null));
201    
202                    document.addText(Field.CONTENT, content);
203    
204                    document.addKeyword(Field.NODE_ID, page.getNodeId());
205                    document.addText(Field.TITLE, page.getTitle());
206    
207                    return document;
208            }
209    
210            @Override
211            protected Summary doGetSummary(
212                    Document document, Locale locale, String snippet,
213                    PortletURL portletURL) {
214    
215                    String title = document.get(Field.TITLE);
216    
217                    String content = snippet;
218    
219                    if (Validator.isNull(snippet)) {
220                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
221                    }
222    
223                    String nodeId = document.get("nodeId");
224    
225                    portletURL.setParameter("struts_action", "/wiki/view");
226                    portletURL.setParameter("nodeId", nodeId);
227                    portletURL.setParameter("title", title);
228    
229                    return new Summary(title, content, portletURL);
230            }
231    
232            @Override
233            protected void doReindex(Object obj) throws Exception {
234                    WikiPage page = (WikiPage)obj;
235    
236                    if (Validator.isNotNull(page.getRedirectTitle())) {
237                            return;
238                    }
239    
240                    Document document = getDocument(page);
241    
242                    SearchEngineUtil.updateDocument(
243                            getSearchEngineId(), page.getCompanyId(), document);
244            }
245    
246            @Override
247            protected void doReindex(String className, long classPK) throws Exception {
248                    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);
249    
250                    doReindex(page);
251            }
252    
253            @Override
254            protected void doReindex(String[] ids) throws Exception {
255                    long companyId = GetterUtil.getLong(ids[0]);
256    
257                    reindexNodes(companyId);
258            }
259    
260            @Override
261            protected String getPortletId(SearchContext searchContext) {
262                    return PORTLET_ID;
263            }
264    
265            protected void reindexNodes(final long companyId)
266                    throws PortalException, SystemException {
267    
268                    ActionableDynamicQuery actionableDynamicQuery =
269                            new WikiNodeActionableDynamicQuery() {
270    
271                            @Override
272                            protected void performAction(Object object)
273                                    throws PortalException, SystemException {
274    
275                                    WikiNode node = (WikiNode)object;
276    
277                                    reindexPages(companyId, node.getGroupId(), node.getNodeId());
278                            }
279    
280                    };
281    
282                    actionableDynamicQuery.setCompanyId(companyId);
283    
284                    actionableDynamicQuery.performActions();
285            }
286    
287            protected void reindexPages(long companyId, long groupId, final long nodeId)
288                    throws PortalException, SystemException {
289    
290                    final Collection<Document> documents = new ArrayList<Document>();
291    
292                    ActionableDynamicQuery actionableDynamicQuery =
293                            new WikiPageActionableDynamicQuery() {
294    
295                            @Override
296                            protected void addCriteria(DynamicQuery dynamicQuery) {
297                                    Property nodeIdProperty = PropertyFactoryUtil.forName("nodeId");
298    
299                                    dynamicQuery.add(nodeIdProperty.eq(nodeId));
300    
301                                    Property headProperty = PropertyFactoryUtil.forName("head");
302    
303                                    dynamicQuery.add(headProperty.eq(true));
304                            }
305    
306                            @Override
307                            protected void performAction(Object object) throws PortalException {
308                                    WikiPage page = (WikiPage)object;
309    
310                                    Document document = getDocument(page);
311    
312                                    documents.add(document);
313                            }
314    
315                    };
316    
317                    actionableDynamicQuery.setGroupId(groupId);
318    
319                    actionableDynamicQuery.performActions();
320    
321                    SearchEngineUtil.updateDocuments(
322                            getSearchEngineId(), companyId, documents);
323            }
324    
325            private static final boolean _FILTER_SEARCH = true;
326    
327            private static final boolean _PERMISSION_AWARE = true;
328    
329    }