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    
247                    Folder destinationFolder = null;
248    
249                    if (containerModelId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
250                            destinationFolder = repository.getFolder(containerModelId);
251                    }
252    
253                    trashCapability.moveFolderFromTrash(
254                            userId, folder, destinationFolder, serviceContext);
255            }
256    
257            @Override
258            public void restoreTrashEntry(long userId, long classPK)
259                    throws PortalException {
260    
261                    Repository repository = getRepository(classPK);
262    
263                    TrashCapability trashCapability = repository.getCapability(
264                            TrashCapability.class);
265    
266                    Folder folder = repository.getFolder(classPK);
267    
268                    trashCapability.restoreFolderFromTrash(userId, folder);
269            }
270    
271            @Override
272            public void updateTitle(long classPK, String name) throws PortalException {
273                    DLFolder dlFolder = getDLFolder(classPK);
274    
275                    dlFolder.setName(name);
276    
277                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
278            }
279    
280            protected void checkRestorableEntry(
281                            long classPK, long trashEntryId, long containerModelId,
282                            String originalTitle, String newName)
283                    throws PortalException {
284    
285                    DLFolder dlFolder = getDLFolder(classPK);
286    
287                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
288                            containerModelId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
289                    }
290    
291                    if (Validator.isNotNull(newName)) {
292                            originalTitle = newName;
293                    }
294    
295                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
296                            dlFolder.getGroupId(), containerModelId, originalTitle);
297    
298                    if (duplicateDLFolder != null) {
299                            RestoreEntryException ree = new RestoreEntryException(
300                                    RestoreEntryException.DUPLICATE);
301    
302                            ree.setDuplicateEntryId(duplicateDLFolder.getFolderId());
303                            ree.setOldName(duplicateDLFolder.getName());
304                            ree.setTrashEntryId(trashEntryId);
305    
306                            throw ree;
307                    }
308            }
309    
310            @Override
311            protected Repository getRepository(long classPK) throws PortalException {
312                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
313                            classPK, 0, 0);
314    
315                    if (!repository.isCapabilityProvided(TrashCapability.class)) {
316                            throw new InvalidRepositoryException(
317                                    "Repository " + repository.getRepositoryId() +
318                                            " does not support trash operations");
319                    }
320    
321                    return repository;
322            }
323    
324            @Override
325            protected boolean hasPermission(
326                            PermissionChecker permissionChecker, long classPK, String actionId)
327                    throws PortalException {
328    
329                    DLFolder dlFolder = getDLFolder(classPK);
330    
331                    if (dlFolder.isInHiddenFolder() && actionId.equals(ActionKeys.VIEW)) {
332                            return false;
333                    }
334    
335                    return DLFolderPermission.contains(
336                            permissionChecker, dlFolder, actionId);
337            }
338    
339    }