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.service.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.repository.Repository;
022    import com.liferay.portal.kernel.repository.RepositoryProvider;
023    import com.liferay.portal.kernel.repository.capabilities.TrashCapability;
024    import com.liferay.portal.kernel.repository.model.FileEntry;
025    import com.liferay.portal.kernel.repository.model.FileShortcut;
026    import com.liferay.portal.kernel.repository.model.Folder;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
030    import com.liferay.portlet.documentlibrary.service.base.DLTrashServiceBaseImpl;
031    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
032    import com.liferay.portlet.documentlibrary.service.permission.DLFileShortcutPermission;
033    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
034    
035    /**
036     * @author Adolfo P??rez
037     */
038    @ProviderType
039    public class DLTrashServiceImpl extends DLTrashServiceBaseImpl {
040    
041            /**
042             * Moves the file entry from a trashed folder to the new folder.
043             *
044             * @param  fileEntryId the primary key of the file entry
045             * @param  newFolderId the primary key of the new folder
046             * @param  serviceContext the service context to be applied
047             * @return the file entry
048             */
049            @Override
050            public FileEntry moveFileEntryFromTrash(
051                            long fileEntryId, long newFolderId, ServiceContext serviceContext)
052                    throws PortalException {
053    
054                    Repository repository = repositoryProvider.getFileEntryRepository(
055                            fileEntryId);
056    
057                    FileEntry fileEntry = repository.getFileEntry(fileEntryId);
058    
059                    DLFileEntryPermission.check(
060                            getPermissionChecker(), fileEntry, ActionKeys.UPDATE);
061    
062                    Folder destinationFolder = null;
063    
064                    if (newFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
065                            destinationFolder = repository.getFolder(newFolderId);
066                    }
067    
068                    TrashCapability trashCapability = repository.getCapability(
069                            TrashCapability.class);
070    
071                    return trashCapability.moveFileEntryFromTrash(
072                            getUserId(), fileEntry, destinationFolder, serviceContext);
073            }
074    
075            /**
076             * Moves the file entry with the primary key to the trash portlet.
077             *
078             * @param  fileEntryId the primary key of the file entry
079             * @return the file entry
080             */
081            @Override
082            public FileEntry moveFileEntryToTrash(long fileEntryId)
083                    throws PortalException {
084    
085                    Repository repository = repositoryProvider.getFileEntryRepository(
086                            fileEntryId);
087    
088                    FileEntry fileEntry = repository.getFileEntry(fileEntryId);
089    
090                    DLFileEntryPermission.check(
091                            getPermissionChecker(), fileEntry, ActionKeys.DELETE);
092    
093                    TrashCapability trashCapability = repository.getCapability(
094                            TrashCapability.class);
095    
096                    return trashCapability.moveFileEntryToTrash(getUserId(), fileEntry);
097            }
098    
099            /**
100             * Moves the file shortcut from a trashed folder to the new folder.
101             *
102             * @param  fileShortcutId the primary key of the file shortcut
103             * @param  newFolderId the primary key of the new folder
104             * @param  serviceContext the service context to be applied
105             * @return the file shortcut
106             */
107            @Override
108            public FileShortcut moveFileShortcutFromTrash(
109                            long fileShortcutId, long newFolderId,
110                            ServiceContext serviceContext)
111                    throws PortalException {
112    
113                    Repository repository = repositoryProvider.getFileShortcutRepository(
114                            fileShortcutId);
115    
116                    FileShortcut fileShortcut = repository.getFileShortcut(fileShortcutId);
117    
118                    DLFileShortcutPermission.check(
119                            getPermissionChecker(), fileShortcut, ActionKeys.UPDATE);
120    
121                    Folder destinationFolder = null;
122    
123                    if (newFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
124                            destinationFolder = repository.getFolder(newFolderId);
125                    }
126    
127                    TrashCapability trashCapability = repository.getCapability(
128                            TrashCapability.class);
129    
130                    return trashCapability.moveFileShortcutFromTrash(
131                            getUserId(), fileShortcut, destinationFolder, serviceContext);
132            }
133    
134            /**
135             * Moves the file shortcut with the primary key to the trash portlet.
136             *
137             * @param  fileShortcutId the primary key of the file shortcut
138             * @return the file shortcut
139             */
140            @Override
141            public FileShortcut moveFileShortcutToTrash(long fileShortcutId)
142                    throws PortalException {
143    
144                    Repository repository = repositoryProvider.getFileShortcutRepository(
145                            fileShortcutId);
146    
147                    FileShortcut fileShortcut = repository.getFileShortcut(fileShortcutId);
148    
149                    DLFileShortcutPermission.check(
150                            getPermissionChecker(), fileShortcut, ActionKeys.DELETE);
151    
152                    TrashCapability trashCapability = repository.getCapability(
153                            TrashCapability.class);
154    
155                    return trashCapability.moveFileShortcutToTrash(
156                            getUserId(), fileShortcut);
157            }
158    
159            /**
160             * Moves the folder with the primary key from the trash portlet to the new
161             * parent folder with the primary key.
162             *
163             * @param  folderId the primary key of the folder
164             * @param  parentFolderId the primary key of the new parent folder
165             * @param  serviceContext the service context to be applied
166             * @return the file entry
167             */
168            @Override
169            public Folder moveFolderFromTrash(
170                            long folderId, long parentFolderId, ServiceContext serviceContext)
171                    throws PortalException {
172    
173                    Repository repository = repositoryProvider.getFolderRepository(
174                            folderId);
175    
176                    Folder folder = repository.getFolder(folderId);
177    
178                    DLFolderPermission.check(
179                            getPermissionChecker(), folder, ActionKeys.UPDATE);
180    
181                    Folder destinationFolder = null;
182    
183                    if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
184                            destinationFolder = repository.getFolder(parentFolderId);
185                    }
186    
187                    TrashCapability trashCapability = repository.getCapability(
188                            TrashCapability.class);
189    
190                    return trashCapability.moveFolderFromTrash(
191                            getUserId(), folder, destinationFolder, serviceContext);
192            }
193    
194            /**
195             * Moves the folder with the primary key to the trash portlet.
196             *
197             * @param  folderId the primary key of the folder
198             * @return the file entry
199             */
200            @Override
201            public Folder moveFolderToTrash(long folderId) throws PortalException {
202                    Repository repository = repositoryProvider.getFolderRepository(
203                            folderId);
204    
205                    Folder folder = repository.getFolder(folderId);
206    
207                    DLFolderPermission.check(
208                            getPermissionChecker(), folder, ActionKeys.DELETE);
209    
210                    TrashCapability trashCapability = repository.getCapability(
211                            TrashCapability.class);
212    
213                    return trashCapability.moveFolderToTrash(getUserId(), folder);
214            }
215    
216            /**
217             * Restores the file entry with the primary key from the trash portlet.
218             *
219             * @param fileEntryId the primary key of the file entry
220             */
221            @Override
222            public void restoreFileEntryFromTrash(long fileEntryId)
223                    throws PortalException {
224    
225                    Repository repository = repositoryProvider.getFileEntryRepository(
226                            fileEntryId);
227    
228                    FileEntry fileEntry = repository.getFileEntry(fileEntryId);
229    
230                    DLFileEntryPermission.check(
231                            getPermissionChecker(), fileEntry, ActionKeys.DELETE);
232    
233                    TrashCapability trashCapability = repository.getCapability(
234                            TrashCapability.class);
235    
236                    trashCapability.restoreFileEntryFromTrash(getUserId(), fileEntry);
237            }
238    
239            /**
240             * Restores the file shortcut with the primary key from the trash portlet.
241             *
242             * @param fileShortcutId the primary key of the file shortcut
243             */
244            @Override
245            public void restoreFileShortcutFromTrash(long fileShortcutId)
246                    throws PortalException {
247    
248                    Repository repository = repositoryProvider.getFileShortcutRepository(
249                            fileShortcutId);
250    
251                    FileShortcut fileShortcut = repository.getFileShortcut(fileShortcutId);
252    
253                    DLFileShortcutPermission.check(
254                            getPermissionChecker(), fileShortcut, ActionKeys.DELETE);
255    
256                    TrashCapability trashCapability = repository.getCapability(
257                            TrashCapability.class);
258    
259                    trashCapability.restoreFileShortcutFromTrash(getUserId(), fileShortcut);
260            }
261    
262            /**
263             * Restores the folder with the primary key from the trash portlet.
264             *
265             * @param folderId the primary key of the folder
266             */
267            @Override
268            public void restoreFolderFromTrash(long folderId) throws PortalException {
269                    Repository repository = repositoryProvider.getFolderRepository(
270                            folderId);
271    
272                    Folder folder = repository.getFolder(folderId);
273    
274                    DLFolderPermission.check(
275                            getPermissionChecker(), folder, ActionKeys.DELETE);
276    
277                    TrashCapability trashCapability = repository.getCapability(
278                            TrashCapability.class);
279    
280                    trashCapability.restoreFolderFromTrash(getUserId(), folder);
281            }
282    
283            @BeanReference(type = RepositoryProvider.class)
284            protected RepositoryProvider repositoryProvider;
285    
286    }