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.Document;
021    import com.liferay.portal.kernel.search.Field;
022    import com.liferay.portal.kernel.search.Query;
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.search.filter.BooleanFilter;
027    import com.liferay.portal.kernel.search.filter.Filter;
028    import com.liferay.portal.kernel.search.filter.QueryFilter;
029    import com.liferay.portal.kernel.search.filter.TermsFilter;
030    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
031    import com.liferay.portal.kernel.trash.TrashHandler;
032    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
033    import com.liferay.portal.kernel.util.ArrayUtil;
034    import com.liferay.portal.kernel.workflow.WorkflowConstants;
035    import com.liferay.portal.security.permission.PermissionChecker;
036    import com.liferay.portlet.trash.model.TrashEntry;
037    
038    import java.util.List;
039    import java.util.Locale;
040    
041    import javax.portlet.PortletRequest;
042    import javax.portlet.PortletResponse;
043    
044    /**
045     * @author Julio Camarero
046     * @author Zsolt Berentey
047     */
048    @OSGiBeanProperties
049    public class TrashIndexer extends BaseIndexer<TrashEntry> {
050    
051            public static final String CLASS_NAME = TrashEntry.class.getName();
052    
053            public TrashIndexer() {
054                    setDefaultSelectedFieldNames(
055                            Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK,
056                            Field.REMOVED_BY_USER_NAME, Field.REMOVED_DATE,
057                            Field.ROOT_ENTRY_CLASS_NAME, Field.ROOT_ENTRY_CLASS_PK, Field.UID);
058                    setFilterSearch(true);
059                    setPermissionAware(true);
060            }
061    
062            @Override
063            public String getClassName() {
064                    return CLASS_NAME;
065            }
066    
067            @Override
068            public BooleanQuery getFullQuery(SearchContext searchContext)
069                    throws SearchException {
070    
071                    try {
072                            BooleanFilter fullQueryBooleanFilter = new BooleanFilter();
073    
074                            fullQueryBooleanFilter.addRequiredTerm(
075                                    Field.COMPANY_ID, searchContext.getCompanyId());
076    
077                            List<TrashHandler> trashHandlers =
078                                    TrashHandlerRegistryUtil.getTrashHandlers();
079    
080                            for (TrashHandler trashHandler : trashHandlers) {
081                                    Filter filter = trashHandler.getExcludeFilter(searchContext);
082    
083                                    if (filter != null) {
084                                            fullQueryBooleanFilter.add(
085                                                    filter, BooleanClauseOccur.MUST_NOT);
086                                    }
087    
088                                    processTrashHandlerExcludeQuery(
089                                            searchContext, fullQueryBooleanFilter, trashHandler);
090                            }
091    
092                            long[] groupIds = searchContext.getGroupIds();
093    
094                            if (ArrayUtil.isNotEmpty(groupIds)) {
095                                    TermsFilter groupTermsFilter = new TermsFilter(Field.GROUP_ID);
096    
097                                    groupTermsFilter.addValues(ArrayUtil.toStringArray(groupIds));
098    
099                                    fullQueryBooleanFilter.add(
100                                            groupTermsFilter, BooleanClauseOccur.MUST);
101                            }
102    
103                            fullQueryBooleanFilter.addRequiredTerm(
104                                    Field.STATUS, WorkflowConstants.STATUS_IN_TRASH);
105    
106                            BooleanQuery fullQuery = createFullQuery(
107                                    fullQueryBooleanFilter, 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 boolean hasPermission(
121                            PermissionChecker permissionChecker, String entryClassName,
122                            long entryClassPK, String actionId)
123                    throws Exception {
124    
125                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
126                            entryClassName);
127    
128                    return trashHandler.hasTrashPermission(
129                            permissionChecker, 0, entryClassPK, actionId);
130            }
131    
132            @Override
133            public void postProcessSearchQuery(
134                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
135                            SearchContext searchContext)
136                    throws Exception {
137    
138                    if (searchContext.getAttributes() == null) {
139                            return;
140                    }
141    
142                    addSearchTerm(searchQuery, searchContext, Field.CONTENT, true);
143                    addSearchTerm(
144                            searchQuery, searchContext, Field.REMOVED_BY_USER_NAME, true);
145                    addSearchTerm(searchQuery, searchContext, Field.TITLE, true);
146                    addSearchTerm(searchQuery, searchContext, Field.TYPE, false);
147                    addSearchTerm(searchQuery, searchContext, Field.USER_NAME, true);
148            }
149    
150            @Override
151            protected void doDelete(TrashEntry trashEntry) {
152            }
153    
154            @Override
155            protected Document doGetDocument(TrashEntry trashEntry) {
156                    return null;
157            }
158    
159            @Override
160            protected String doGetSortField(String orderByCol) {
161                    if (orderByCol.equals("removed-date")) {
162                            return Field.REMOVED_DATE;
163                    }
164                    else if (orderByCol.equals("removed-by")) {
165                            return Field.REMOVED_BY_USER_NAME;
166                    }
167                    else {
168                            return orderByCol;
169                    }
170            }
171    
172            @Override
173            protected Summary doGetSummary(
174                    Document document, Locale locale, String snippet,
175                    PortletRequest portletRequest, PortletResponse portletResponse) {
176    
177                    return null;
178            }
179    
180            @Override
181            protected void doReindex(String className, long classPK) {
182            }
183    
184            @Override
185            protected void doReindex(String[] ids) {
186            }
187    
188            @Override
189            protected void doReindex(TrashEntry trashEntry) {
190            }
191    
192            /**
193             * @deprecated As of 7.0.0, added strictly to support backwards
194             *             compatibility of {@link
195             *             TrashHandler#getExcludeQuery(SearchContext)}
196             */
197            @Deprecated
198            protected void processTrashHandlerExcludeQuery(
199                    SearchContext searchContext, BooleanFilter fullQueryBooleanFilter,
200                    TrashHandler trashHandler) {
201    
202                    Query query = trashHandler.getExcludeQuery(searchContext);
203    
204                    if (query != null) {
205                            fullQueryBooleanFilter.add(
206                                    new QueryFilter(query), BooleanClauseOccur.MUST_NOT);
207                    }
208            }
209    
210    }