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