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.documentlibrary.trash;
016    
017    import com.liferay.portal.InvalidRepositoryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.Repository;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.trash.TrashActionKeys;
023    import com.liferay.portal.kernel.trash.TrashRenderer;
024    import com.liferay.portal.model.ContainerModel;
025    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.service.RepositoryServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portlet.documentlibrary.model.DLFolder;
031    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
035    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
036    import com.liferay.portlet.documentlibrary.util.DLUtil;
037    import com.liferay.portlet.trash.DuplicateEntryException;
038    import com.liferay.portlet.trash.TrashEntryConstants;
039    import com.liferay.portlet.trash.model.TrashEntry;
040    
041    import javax.portlet.PortletRequest;
042    
043    /**
044     * Implements trash handling for the folder entity.
045     *
046     * @author Alexander Chow
047     * @author Zsolt Berentey
048     */
049    public class DLFolderTrashHandler extends DLBaseTrashHandler {
050    
051            public static final String CLASS_NAME = DLFolder.class.getName();
052    
053            @Override
054            public void checkDuplicateTrashEntry(
055                            TrashEntry trashEntry, long containerModelId, String newName)
056                    throws PortalException, SystemException {
057    
058                    DLFolder dlFolder = getDLFolder(trashEntry.getClassPK());
059    
060                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
061                            containerModelId = dlFolder.getParentFolderId();
062                    }
063    
064                    String originalTitle = trashEntry.getTypeSettingsProperty("title");
065    
066                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
067                            dlFolder.getGroupId(), dlFolder.getParentFolderId(), originalTitle);
068    
069                    if (duplicateDLFolder != null) {
070                            DuplicateEntryException dee = new DuplicateEntryException();
071    
072                            dee.setDuplicateEntryId(duplicateDLFolder.getFolderId());
073                            dee.setOldName(duplicateDLFolder.getName());
074                            dee.setTrashEntryId(trashEntry.getEntryId());
075    
076                            throw dee;
077                    }
078            }
079    
080            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
081                    throws PortalException, SystemException {
082    
083                    for (long classPK : classPKs) {
084                            if (checkPermission) {
085                                    DLFolderServiceUtil.deleteFolder(classPK, false);
086                            }
087                            else {
088                                    DLFolderLocalServiceUtil.deleteFolder(classPK, false);
089                            }
090                    }
091            }
092    
093            public String getClassName() {
094                    return CLASS_NAME;
095            }
096    
097            @Override
098            public String getDeleteMessage() {
099                    return "found-in-deleted-folder-x";
100            }
101    
102            @Override
103            public ContainerModel getParentContainerModel(long classPK)
104                    throws PortalException, SystemException {
105    
106                    DLFolder dlFolder = getDLFolder(classPK);
107    
108                    long parentFolderId = dlFolder.getParentFolderId();
109    
110                    if (parentFolderId <= 0) {
111                            return null;
112                    }
113    
114                    return getContainerModel(parentFolderId);
115            }
116    
117            @Override
118            public String getRestoreLink(PortletRequest portletRequest, long classPK)
119                    throws PortalException, SystemException {
120    
121                    DLFolder dlFolder = getDLFolder(classPK);
122    
123                    return DLUtil.getDLControlPanelLink(
124                            portletRequest, dlFolder.getParentFolderId());
125            }
126    
127            @Override
128            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
129                    throws PortalException, SystemException {
130    
131                    DLFolder dlFolder = getDLFolder(classPK);
132    
133                    return DLUtil.getAbsolutePath(
134                            portletRequest, dlFolder.getParentFolderId());
135            }
136    
137            @Override
138            public TrashRenderer getTrashRenderer(long classPK)
139                    throws PortalException, SystemException {
140    
141                    Folder folder = DLAppLocalServiceUtil.getFolder(classPK);
142    
143                    return new DLFolderTrashRenderer(folder);
144            }
145    
146            @Override
147            public boolean hasTrashPermission(
148                            PermissionChecker permissionChecker, long groupId, long classPK,
149                            String trashActionId)
150                    throws PortalException, SystemException {
151    
152                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
153                            return DLFolderPermission.contains(
154                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
155                    }
156    
157                    return super.hasTrashPermission(
158                            permissionChecker, groupId, classPK, trashActionId);
159            }
160    
161            @Override
162            public boolean isContainerModel() {
163                    return true;
164            }
165    
166            public boolean isInTrash(long classPK)
167                    throws PortalException, SystemException {
168    
169                    try {
170                            DLFolder dlFolder = getDLFolder(classPK);
171    
172                            if (dlFolder.isInTrash() || dlFolder.isInTrashContainer()) {
173                                    return true;
174                            }
175    
176                            return false;
177                    }
178                    catch (InvalidRepositoryException ire) {
179                            return false;
180                    }
181            }
182    
183            @Override
184            public boolean isInTrashContainer(long classPK)
185                    throws PortalException, SystemException {
186    
187                    DLFolder dlFolder = getDLFolder(classPK);
188    
189                    return dlFolder.isInTrashContainer();
190            }
191    
192            @Override
193            public boolean isRestorable(long classPK)
194                    throws PortalException, SystemException {
195    
196                    try {
197                            DLFolder dlFolder = getDLFolder(classPK);
198    
199                            return !dlFolder.isInTrashContainer();
200                    }
201                    catch (InvalidRepositoryException ire) {
202                            return false;
203                    }
204            }
205    
206            @Override
207            public void moveEntry(
208                            long classPK, long containerModelId, ServiceContext serviceContext)
209                    throws PortalException, SystemException {
210    
211                    DLAppServiceUtil.moveFolder(classPK, containerModelId, serviceContext);
212            }
213    
214            @Override
215            public void moveTrashEntry(
216                            long classPK, long containerModelId, ServiceContext serviceContext)
217                    throws PortalException, SystemException {
218    
219                    DLAppServiceUtil.moveFolderFromTrash(
220                            classPK, containerModelId, serviceContext);
221            }
222    
223            public void restoreTrashEntries(long[] classPKs)
224                    throws PortalException, SystemException {
225    
226                    for (long classPK : classPKs) {
227                            DLAppServiceUtil.restoreFolderFromTrash(classPK);
228                    }
229            }
230    
231            @Override
232            public void updateTitle(long classPK, String name)
233                    throws PortalException, SystemException {
234    
235                    DLFolder dlFolder = getDLFolder(classPK);
236    
237                    dlFolder.setName(name);
238    
239                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
240            }
241    
242            @Override
243            protected Repository getRepository(long classPK)
244                    throws PortalException, SystemException {
245    
246                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
247                            classPK, 0, 0);
248    
249                    if (!(repository instanceof LiferayRepository)) {
250                            throw new InvalidRepositoryException(
251                                    "Repository " + repository.getRepositoryId() +
252                                            " does not support trash operations");
253                    }
254    
255                    return repository;
256            }
257    
258            @Override
259            protected boolean hasPermission(
260                            PermissionChecker permissionChecker, long classPK, String actionId)
261                    throws PortalException, SystemException {
262    
263                    DLFolder dlFolder = getDLFolder(classPK);
264    
265                    return DLFolderPermission.contains(
266                            permissionChecker, dlFolder, actionId);
267            }
268    
269    }