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