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.documentlibrary.trash;
016    
017    import com.liferay.portal.InvalidRepositoryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.Repository;
020    import com.liferay.portal.kernel.repository.capabilities.TrashCapability;
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.kernel.util.Validator;
025    import com.liferay.portal.model.ContainerModel;
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.asset.DLFolderAssetRenderer;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
033    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
035    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
036    import com.liferay.portlet.documentlibrary.util.DLUtil;
037    import com.liferay.portlet.trash.RestoreEntryException;
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            @Override
052            public void checkRestorableEntry(
053                            long classPK, long containerModelId, String newName)
054                    throws PortalException {
055    
056                    DLFolder dlFolder = getDLFolder(classPK);
057    
058                    checkRestorableEntry(
059                            classPK, 0, containerModelId, dlFolder.getName(), newName);
060            }
061    
062            @Override
063            public void checkRestorableEntry(
064                            TrashEntry trashEntry, long containerModelId, String newName)
065                    throws PortalException {
066    
067                    checkRestorableEntry(
068                            trashEntry.getClassPK(), trashEntry.getEntryId(), containerModelId,
069                            trashEntry.getTypeSettingsProperty("title"), newName);
070            }
071    
072            @Override
073            public void deleteTrashEntry(long classPK) throws PortalException {
074                    Repository repository = getRepository(classPK);
075    
076                    TrashCapability trashCapability = repository.getCapability(
077                            TrashCapability.class);
078    
079                    Folder folder = repository.getFolder(classPK);
080    
081                    trashCapability.deleteFolder(folder);
082            }
083    
084            @Override
085            public String getClassName() {
086                    return DLFolder.class.getName();
087            }
088    
089            @Override
090            public String getDeleteMessage() {
091                    return "found-in-deleted-folder-x";
092            }
093    
094            @Override
095            public ContainerModel getParentContainerModel(long classPK)
096                    throws PortalException {
097    
098                    DLFolder dlFolder = getDLFolder(classPK);
099    
100                    long parentFolderId = dlFolder.getParentFolderId();
101    
102                    if (parentFolderId <= 0) {
103                            return null;
104                    }
105    
106                    return getContainerModel(parentFolderId);
107            }
108    
109            @Override
110            public String getRestoreContainedModelLink(
111                            PortletRequest portletRequest, long classPK)
112                    throws PortalException {
113    
114                    DLFolder dlFolder = getDLFolder(classPK);
115    
116                    return DLUtil.getDLFolderControlPanelLink(
117                            portletRequest, dlFolder.getFolderId());
118            }
119    
120            @Override
121            public String getRestoreContainerModelLink(
122                            PortletRequest portletRequest, long classPK)
123                    throws PortalException {
124    
125                    DLFolder dlFolder = getDLFolder(classPK);
126    
127                    return DLUtil.getDLFolderControlPanelLink(
128                            portletRequest, dlFolder.getParentFolderId());
129            }
130    
131            @Override
132            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
133                    throws PortalException {
134    
135                    DLFolder dlFolder = getDLFolder(classPK);
136    
137                    return DLUtil.getAbsolutePath(
138                            portletRequest, dlFolder.getParentFolderId());
139            }
140    
141            @Override
142            public String getSubcontainerModelName() {
143                    return "folder";
144            }
145    
146            @Override
147            public String getSystemEventClassName() {
148                    return DLFolderConstants.getClassName();
149            }
150    
151            @Override
152            public TrashEntry getTrashEntry(long classPK) throws PortalException {
153                    DLFolder dlFolder = getDLFolder(classPK);
154    
155                    return dlFolder.getTrashEntry();
156            }
157    
158            @Override
159            public TrashRenderer getTrashRenderer(long classPK) throws PortalException {
160                    Folder folder = DLAppLocalServiceUtil.getFolder(classPK);
161    
162                    return new DLFolderAssetRenderer(folder);
163            }
164    
165            @Override
166            public boolean hasTrashPermission(
167                            PermissionChecker permissionChecker, long groupId, long classPK,
168                            String trashActionId)
169                    throws PortalException {
170    
171                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
172                            return DLFolderPermission.contains(
173                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
174                    }
175    
176                    return super.hasTrashPermission(
177                            permissionChecker, groupId, classPK, trashActionId);
178            }
179    
180            @Override
181            public boolean isContainerModel() {
182                    return true;
183            }
184    
185            @Override
186            public boolean isInTrash(long classPK) throws PortalException {
187                    try {
188                            DLFolder dlFolder = getDLFolder(classPK);
189    
190                            return dlFolder.isInTrash();
191                    }
192                    catch (InvalidRepositoryException ire) {
193                            return false;
194                    }
195            }
196    
197            @Override
198            public boolean isInTrashContainer(long classPK) throws PortalException {
199                    try {
200                            DLFolder dlFolder = getDLFolder(classPK);
201    
202                            return dlFolder.isInTrashContainer();
203                    }
204                    catch (InvalidRepositoryException ire) {
205                            return false;
206                    }
207            }
208    
209            @Override
210            public boolean isRestorable(long classPK) throws PortalException {
211                    DLFolder dlFolder = fetchDLFolder(classPK);
212    
213                    if ((dlFolder == null) ||
214                            ((dlFolder.getParentFolderId() > 0) &&
215                             (DLFolderLocalServiceUtil.fetchFolder(
216                                    dlFolder.getParentFolderId()) == null))) {
217    
218                            return false;
219                    }
220    
221                    return !dlFolder.isInTrashContainer();
222            }
223    
224            @Override
225            public void moveEntry(
226                            long userId, long classPK, long containerModelId,
227                            ServiceContext serviceContext)
228                    throws PortalException {
229    
230                    DLAppLocalServiceUtil.moveFolder(
231                            userId, classPK, containerModelId, serviceContext);
232            }
233    
234            @Override
235            public void moveTrashEntry(
236                            long userId, long classPK, long containerModelId,
237                            ServiceContext serviceContext)
238                    throws PortalException {
239    
240                    Repository repository = getRepository(classPK);
241    
242                    TrashCapability trashCapability = repository.getCapability(
243                            TrashCapability.class);
244    
245                    Folder folder = repository.getFolder(classPK);
246                    Folder destinationFolder = repository.getFolder(containerModelId);
247    
248                    trashCapability.moveFolderFromTrash(
249                            userId, folder, destinationFolder, serviceContext);
250            }
251    
252            @Override
253            public void restoreTrashEntry(long userId, long classPK)
254                    throws PortalException {
255    
256                    Repository repository = getRepository(classPK);
257    
258                    TrashCapability trashCapability = repository.getCapability(
259                            TrashCapability.class);
260    
261                    Folder folder = repository.getFolder(classPK);
262    
263                    trashCapability.restoreFolderFromTrash(userId, folder);
264            }
265    
266            @Override
267            public void updateTitle(long classPK, String name) throws PortalException {
268                    DLFolder dlFolder = getDLFolder(classPK);
269    
270                    dlFolder.setName(name);
271    
272                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
273            }
274    
275            protected void checkRestorableEntry(
276                            long classPK, long trashEntryId, long containerModelId,
277                            String originalTitle, String newName)
278                    throws PortalException {
279    
280                    DLFolder dlFolder = getDLFolder(classPK);
281    
282                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
283                            containerModelId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
284                    }
285    
286                    if (Validator.isNotNull(newName)) {
287                            originalTitle = newName;
288                    }
289    
290                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
291                            dlFolder.getGroupId(), containerModelId, originalTitle);
292    
293                    if (duplicateDLFolder != null) {
294                            RestoreEntryException ree = new RestoreEntryException(
295                                    RestoreEntryException.DUPLICATE);
296    
297                            ree.setDuplicateEntryId(duplicateDLFolder.getFolderId());
298                            ree.setOldName(duplicateDLFolder.getName());
299                            ree.setTrashEntryId(trashEntryId);
300    
301                            throw ree;
302                    }
303            }
304    
305            @Override
306            protected Repository getRepository(long classPK) throws PortalException {
307                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
308                            classPK, 0, 0);
309    
310                    if (!repository.isCapabilityProvided(TrashCapability.class)) {
311                            throw new InvalidRepositoryException(
312                                    "Repository " + repository.getRepositoryId() +
313                                            " does not support trash operations");
314                    }
315    
316                    return repository;
317            }
318    
319            @Override
320            protected boolean hasPermission(
321                            PermissionChecker permissionChecker, long classPK, String actionId)
322                    throws PortalException {
323    
324                    DLFolder dlFolder = getDLFolder(classPK);
325    
326                    if (dlFolder.isInHiddenFolder() && actionId.equals(ActionKeys.VIEW)) {
327                            return false;
328                    }
329    
330                    return DLFolderPermission.contains(
331                            permissionChecker, dlFolder, actionId);
332            }
333    
334    }