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.portal.repository.util;
016    
017    import com.liferay.portal.InvalidRepositoryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.LocalRepository;
020    import com.liferay.portal.kernel.repository.capabilities.TrashCapability;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.repository.util.RepositoryTrash;
024    import com.liferay.portal.service.RepositoryLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
027    
028    /**
029     * @author Adolfo P??rez
030     */
031    public class RepositoryTrashImpl implements RepositoryTrash {
032    
033            @Override
034            public FileEntry moveFileEntryFromTrash(
035                            long userId, long repositoryId, long fileEntryId, long newFolderId,
036                            ServiceContext serviceContext)
037                    throws PortalException {
038    
039                    LocalRepository localRepository =
040                            RepositoryLocalServiceUtil.getLocalRepositoryImpl(repositoryId);
041    
042                    validateCapability(localRepository);
043    
044                    TrashCapability trashCapability = localRepository.getCapability(
045                            TrashCapability.class);
046    
047                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
048    
049                    Folder newFolder = null;
050    
051                    if (newFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
052                            newFolder = localRepository.getFolder(newFolderId);
053                    }
054    
055                    return trashCapability.moveFileEntryFromTrash(
056                            userId, fileEntry, newFolder, serviceContext);
057            }
058    
059            @Override
060            public FileEntry moveFileEntryToTrash(
061                            long userId, long repositoryId, long fileEntryId)
062                    throws PortalException {
063    
064                    LocalRepository localRepository =
065                            RepositoryLocalServiceUtil.getLocalRepositoryImpl(repositoryId);
066    
067                    validateCapability(localRepository);
068    
069                    TrashCapability trashCapability = localRepository.getCapability(
070                            TrashCapability.class);
071    
072                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
073    
074                    return trashCapability.moveFileEntryToTrash(userId, fileEntry);
075            }
076    
077            @Override
078            public void restoreFileEntryFromTrash(
079                            long userId, long repositoryId, long fileEntryId)
080                    throws PortalException {
081    
082                    LocalRepository localRepository =
083                            RepositoryLocalServiceUtil.getLocalRepositoryImpl(repositoryId);
084    
085                    validateCapability(localRepository);
086    
087                    TrashCapability trashCapability = localRepository.getCapability(
088                            TrashCapability.class);
089    
090                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
091    
092                    trashCapability.restoreFileEntryFromTrash(userId, fileEntry);
093            }
094    
095            protected void validateCapability(LocalRepository localRepository)
096                    throws InvalidRepositoryException {
097    
098                    if (!localRepository.isCapabilityProvided(TrashCapability.class)) {
099                            throw new InvalidRepositoryException(
100                                    "Repository " + localRepository.getRepositoryId() +
101                                            " does not support trash operations");
102                    }
103            }
104    
105    }