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.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.LocalRepository;
019    import com.liferay.portal.kernel.repository.RepositoryProviderUtil;
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.ServiceContext;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    
027    /**
028     * @author Adolfo P??rez
029     */
030    public class RepositoryTrashImpl implements RepositoryTrash {
031    
032            @Override
033            public FileEntry moveFileEntryFromTrash(
034                            long userId, long repositoryId, long fileEntryId, long newFolderId,
035                            ServiceContext serviceContext)
036                    throws PortalException {
037    
038                    LocalRepository localRepository =
039                            RepositoryProviderUtil.getLocalRepository(repositoryId);
040    
041                    TrashCapability trashCapability = localRepository.getCapability(
042                            TrashCapability.class);
043    
044                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
045    
046                    Folder newFolder = null;
047    
048                    if (newFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
049                            newFolder = localRepository.getFolder(newFolderId);
050                    }
051    
052                    return trashCapability.moveFileEntryFromTrash(
053                            userId, fileEntry, newFolder, serviceContext);
054            }
055    
056            @Override
057            public FileEntry moveFileEntryToTrash(
058                            long userId, long repositoryId, long fileEntryId)
059                    throws PortalException {
060    
061                    LocalRepository localRepository =
062                            RepositoryProviderUtil.getLocalRepository(repositoryId);
063    
064                    TrashCapability trashCapability = localRepository.getCapability(
065                            TrashCapability.class);
066    
067                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
068    
069                    return trashCapability.moveFileEntryToTrash(userId, fileEntry);
070            }
071    
072            @Override
073            public void restoreFileEntryFromTrash(
074                            long userId, long repositoryId, long fileEntryId)
075                    throws PortalException {
076    
077                    LocalRepository localRepository =
078                            RepositoryProviderUtil.getLocalRepository(repositoryId);
079    
080                    TrashCapability trashCapability = localRepository.getCapability(
081                            TrashCapability.class);
082    
083                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
084    
085                    trashCapability.restoreFileEntryFromTrash(userId, fileEntry);
086            }
087    
088    }