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