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.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.trash.TrashHandler;
022    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portlet.trash.model.TrashEntry;
029    import com.liferay.portlet.trash.model.TrashEntryList;
030    import com.liferay.portlet.trash.model.TrashEntrySoap;
031    import com.liferay.portlet.trash.service.base.TrashEntryServiceBaseImpl;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    /**
037     * The trash entry remote service is responsible for returning trash entries.
038     * For more information on trash entries services and TrashEntry, see {@link
039     * com.liferay.portlet.trash.service.impl.TrashEntryLocalServiceImpl}.
040     *
041     * @author Julio Camarero
042     */
043    public class TrashEntryServiceImpl extends TrashEntryServiceBaseImpl {
044    
045            /**
046             * Deletes the trash entries with the matching group ID considering
047             * permissions.
048             *
049             * @param  groupId the primary key of the group
050             * @throws PrincipalException if a principal exception occurred
051             * @throws SystemException if a system exception occurred
052             */
053            public void deleteEntries(long groupId)
054                    throws PrincipalException, SystemException {
055    
056                    List<TrashEntry> entries = trashEntryLocalService.getEntries(groupId);
057    
058                    PermissionChecker permissionChecker = getPermissionChecker();
059    
060                    for (TrashEntry entry : entries) {
061                            String className = entry.getClassName();
062                            long classPK = entry.getClassPK();
063    
064                            try {
065                                    TrashHandler trashHandler =
066                                            TrashHandlerRegistryUtil.getTrashHandler(className);
067    
068                                    if (trashHandler.hasTrashPermission(
069                                                    permissionChecker, 0, classPK, ActionKeys.DELETE)) {
070    
071                                            trashHandler.deleteTrashEntry(classPK);
072                                    }
073                            }
074                            catch (Exception e) {
075                                    _log.error(e, e);
076                            }
077                    }
078            }
079    
080            /**
081             * Returns the trash entries with the matching group ID.
082             *
083             * @param  groupId the primary key of the group
084             * @return the matching trash entries
085             * @throws PrincipalException if a principal exception occurred
086             * @throws SystemException if a system exception occurred
087             */
088            public TrashEntryList getEntries(long groupId)
089                    throws PrincipalException, SystemException {
090    
091                    return getEntries(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
092            }
093    
094            /**
095             * Returns a range of all the trash entries matching the group ID.
096             *
097             * @param  groupId the primary key of the group
098             * @param  start the lower bound of the range of trash entries to return
099             * @param  end the upper bound of the range of trash entries to return (not
100             *         inclusive)
101             * @param  obc the comparator to order the trash entries (optionally
102             *         <code>null</code>)
103             * @return the range of matching trash entries ordered by comparator
104             *         <code>obc</code>
105             * @throws PrincipalException if a system exception occurred
106             * @throws SystemException if a system exception occurred
107             */
108            public TrashEntryList getEntries(
109                            long groupId, int start, int end, OrderByComparator obc)
110                    throws PrincipalException, SystemException {
111    
112                    TrashEntryList trashEntriesList = new TrashEntryList();
113    
114                    int entriesCount = trashEntryLocalService.getEntriesCount(groupId);
115    
116                    boolean approximate = entriesCount > PropsValues.TRASH_SEARCH_LIMIT;
117    
118                    trashEntriesList.setApproximate(approximate);
119    
120                    List<TrashEntry> entries = trashEntryLocalService.getEntries(
121                            groupId, 0, end + PropsValues.TRASH_SEARCH_LIMIT, obc);
122    
123                    List<TrashEntry> filteredEntries = new ArrayList<TrashEntry>();
124    
125                    PermissionChecker permissionChecker = getPermissionChecker();
126    
127                    for (TrashEntry entry : entries) {
128                            String className = entry.getClassName();
129                            long classPK = entry.getClassPK();
130    
131                            try {
132                                    TrashHandler trashHandler =
133                                            TrashHandlerRegistryUtil.getTrashHandler(className);
134    
135                                    if (trashHandler.hasTrashPermission(
136                                                    permissionChecker, 0, classPK, ActionKeys.VIEW)) {
137    
138                                            filteredEntries.add(entry);
139                                    }
140                            }
141                            catch (Exception e) {
142                                    _log.error(e, e);
143                            }
144                    }
145    
146                    int filteredEntriesCount = filteredEntries.size();
147    
148                    if ((end != QueryUtil.ALL_POS) && (start != QueryUtil.ALL_POS)) {
149                            if (end > filteredEntriesCount) {
150                                    end = filteredEntriesCount;
151                            }
152    
153                            if (start > filteredEntriesCount) {
154                                    start = filteredEntriesCount;
155                            }
156    
157                            filteredEntries = filteredEntries.subList(start, end);
158                    }
159    
160                    trashEntriesList.setArray(TrashEntrySoap.toSoapModels(filteredEntries));
161                    trashEntriesList.setCount(filteredEntriesCount);
162    
163                    return trashEntriesList;
164            }
165    
166            private static Log _log = LogFactoryUtil.getLog(
167                    TrashEntryServiceImpl.class);
168    
169    }