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