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.trash.util;
016    
017    import com.liferay.portal.kernel.search.BaseIndexer;
018    import com.liferay.portal.kernel.search.BooleanClauseOccur;
019    import com.liferay.portal.kernel.search.BooleanQuery;
020    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
021    import com.liferay.portal.kernel.search.Document;
022    import com.liferay.portal.kernel.search.Field;
023    import com.liferay.portal.kernel.search.SearchContext;
024    import com.liferay.portal.kernel.search.SearchException;
025    import com.liferay.portal.kernel.search.Summary;
026    import com.liferay.portal.kernel.trash.TrashHandler;
027    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
032    import com.liferay.portlet.journal.model.JournalArticle;
033    import com.liferay.portlet.trash.model.TrashEntry;
034    
035    import java.util.Locale;
036    
037    import javax.portlet.PortletURL;
038    
039    /**
040     * @author Julio Camarero
041     * @author Zsolt Berentey
042     */
043    public class TrashIndexer extends BaseIndexer {
044    
045            public static final String[] CLASS_NAMES = {TrashEntry.class.getName()};
046    
047            public static final String PORTLET_ID = PortletKeys.TRASH;
048    
049            public TrashIndexer() {
050                    setFilterSearch(true);
051                    setPermissionAware(true);
052            }
053    
054            @Override
055            public String[] getClassNames() {
056                    return CLASS_NAMES;
057            }
058    
059            @Override
060            public BooleanQuery getFullQuery(SearchContext searchContext)
061                    throws SearchException {
062    
063                    try {
064                            BooleanQuery contextQuery = BooleanQueryFactoryUtil.create(
065                                    searchContext);
066    
067                            contextQuery.addRequiredTerm(
068                                    Field.COMPANY_ID, searchContext.getCompanyId());
069    
070                            BooleanQuery excludeAttachmentsQuery =
071                                    BooleanQueryFactoryUtil.create(searchContext);
072    
073                            excludeAttachmentsQuery.addRequiredTerm(
074                                    Field.ENTRY_CLASS_NAME, DLFileEntryConstants.getClassName());
075                            excludeAttachmentsQuery.addRequiredTerm(Field.HIDDEN, true);
076    
077                            contextQuery.add(
078                                    excludeAttachmentsQuery, BooleanClauseOccur.MUST_NOT);
079    
080                            BooleanQuery excludeJournalArticleVersionsQuery =
081                                    BooleanQueryFactoryUtil.create(searchContext);
082    
083                            excludeJournalArticleVersionsQuery.addRequiredTerm(
084                                    Field.ENTRY_CLASS_NAME, JournalArticle.class.getName());
085    
086                            excludeJournalArticleVersionsQuery.addRequiredTerm("head", false);
087    
088                            contextQuery.add(
089                                    excludeJournalArticleVersionsQuery,
090                                    BooleanClauseOccur.MUST_NOT);
091    
092                            BooleanQuery groupQuery = BooleanQueryFactoryUtil.create(
093                                    searchContext);
094    
095                            for (long groupId : searchContext.getGroupIds()) {
096                                    groupQuery.addTerm(
097                                            Field.GROUP_ID, String.valueOf(groupId), false,
098                                            BooleanClauseOccur.SHOULD);
099                            }
100    
101                            contextQuery.add(groupQuery, BooleanClauseOccur.MUST);
102    
103                            contextQuery.addRequiredTerm(
104                                    Field.STATUS, WorkflowConstants.STATUS_IN_TRASH);
105    
106                            BooleanQuery fullQuery = createFullQuery(
107                                    contextQuery, searchContext);
108    
109                            return fullQuery;
110                    }
111                    catch (SearchException se) {
112                            throw se;
113                    }
114                    catch (Exception e) {
115                            throw new SearchException(e);
116                    }
117            }
118    
119            @Override
120            public String getPortletId() {
121                    return PORTLET_ID;
122            }
123    
124            @Override
125            public boolean hasPermission(
126                            PermissionChecker permissionChecker, String entryClassName,
127                            long entryClassPK, String actionId)
128                    throws Exception {
129    
130                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
131                            entryClassName);
132    
133                    return trashHandler.hasTrashPermission(
134                            permissionChecker, 0, entryClassPK, actionId);
135            }
136    
137            @Override
138            public void postProcessSearchQuery(
139                            BooleanQuery searchQuery, SearchContext searchContext)
140                    throws Exception {
141    
142                    if (searchContext.getAttributes() == null) {
143                            return;
144                    }
145    
146                    addSearchTerm(searchQuery, searchContext, Field.CONTENT, true);
147                    addSearchTerm(
148                            searchQuery, searchContext, Field.REMOVED_BY_USER_NAME, true);
149                    addSearchTerm(searchQuery, searchContext, Field.TITLE, true);
150                    addSearchTerm(searchQuery, searchContext, Field.TYPE, false);
151                    addSearchTerm(searchQuery, searchContext, Field.USER_NAME, true);
152            }
153    
154            @Override
155            protected void doDelete(Object obj) {
156            }
157    
158            @Override
159            protected Document doGetDocument(Object obj) {
160                    return null;
161            }
162    
163            @Override
164            protected String doGetSortField(String orderByCol) {
165                    if (orderByCol.equals("removed-date")) {
166                            return Field.REMOVED_DATE;
167                    }
168                    else if (orderByCol.equals("removed-by")) {
169                            return Field.REMOVED_BY_USER_NAME;
170                    }
171                    else {
172                            return orderByCol;
173                    }
174            }
175    
176            @Override
177            protected Summary doGetSummary(
178                    Document document, Locale locale, String snippet,
179                    PortletURL portletURL) {
180    
181                    return null;
182            }
183    
184            @Override
185            protected void doReindex(Object obj) {
186            }
187    
188            @Override
189            protected void doReindex(String className, long classPK) {
190            }
191    
192            @Override
193            protected void doReindex(String[] ids) {
194            }
195    
196            @Override
197            protected String getPortletId(SearchContext searchContext) {
198                    return PORTLET_ID;
199            }
200    
201    }