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.capabilities;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.repository.LocalRepository;
021    import com.liferay.portal.kernel.repository.capabilities.TrashCapability;
022    import com.liferay.portal.kernel.repository.event.RepositoryEventAware;
023    import com.liferay.portal.kernel.repository.event.RepositoryEventListener;
024    import com.liferay.portal.kernel.repository.event.RepositoryEventType;
025    import com.liferay.portal.kernel.repository.model.FileEntry;
026    import com.liferay.portal.kernel.repository.model.Folder;
027    import com.liferay.portal.kernel.repository.registry.RepositoryEventRegistry;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.model.Repository;
030    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
031    import com.liferay.portal.service.RepositoryLocalServiceUtil;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
035    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
036    import com.liferay.portlet.documentlibrary.model.DLFolder;
037    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
038    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
039    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
042    import com.liferay.portlet.trash.model.TrashEntry;
043    import com.liferay.portlet.trash.service.TrashEntryLocalServiceUtil;
044    import com.liferay.portlet.trash.service.TrashVersionLocalServiceUtil;
045    
046    import java.util.List;
047    
048    /**
049     * @author Adolfo P??rez
050     */
051    public class LiferayTrashCapability
052            implements RepositoryEventAware, TrashCapability {
053    
054            @Override
055            public void deleteFileEntry(FileEntry fileEntry) throws PortalException {
056                    deleteTrashEntry(fileEntry);
057    
058                    DLAppLocalServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());
059            }
060    
061            @Override
062            public void deleteFolder(Folder folder) throws PortalException {
063                    List<DLFileEntry> dlFileEntries =
064                            DLFileEntryLocalServiceUtil.getGroupFileEntries(
065                                    folder.getGroupId(), 0, folder.getRepositoryId(),
066                                    folder.getFolderId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS,
067                                    null);
068    
069                    for (DLFileEntry dlFileEntry : dlFileEntries) {
070                            FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
071    
072                            DLAppHelperLocalServiceUtil.deleteFileEntry(fileEntry);
073    
074                            deleteTrashEntry(fileEntry);
075                    }
076    
077                    DLAppHelperLocalServiceUtil.deleteFolder(folder);
078    
079                    deleteTrashEntry(folder);
080    
081                    DLFolderLocalServiceUtil.deleteFolder(folder.getFolderId(), false);
082            }
083    
084            @Override
085            public boolean isInTrash(Folder folder) {
086                    DLFolder dlFolder = (DLFolder)folder.getModel();
087    
088                    return dlFolder.isInTrash();
089            }
090    
091            @Override
092            public FileEntry moveFileEntryFromTrash(
093                            long userId, FileEntry fileEntry, Folder newFolder,
094                            ServiceContext serviceContext)
095                    throws PortalException {
096    
097                    long newFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
098    
099                    if (newFolder != null) {
100                            newFolderId = newFolder.getFolderId();
101                    }
102    
103                    return DLAppHelperLocalServiceUtil.moveFileEntryFromTrash(
104                            userId, fileEntry, newFolderId, serviceContext);
105            }
106    
107            @Override
108            public FileEntry moveFileEntryToTrash(long userId, FileEntry fileEntry)
109                    throws PortalException {
110    
111                    return DLAppHelperLocalServiceUtil.moveFileEntryToTrash(
112                            userId, fileEntry);
113            }
114    
115            @Override
116            public Folder moveFolderFromTrash(
117                            long userId, Folder folder, Folder destinationFolder,
118                            ServiceContext serviceContext)
119                    throws PortalException {
120    
121                    long destinationFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
122    
123                    if (destinationFolder != null) {
124                            destinationFolderId = destinationFolder.getFolderId();
125                    }
126    
127                    return DLAppHelperLocalServiceUtil.moveFolderFromTrash(
128                            userId, folder, destinationFolderId, serviceContext);
129            }
130    
131            @Override
132            public Folder moveFolderToTrash(long userId, Folder folder)
133                    throws PortalException {
134    
135                    return DLAppHelperLocalServiceUtil.moveFolderToTrash(userId, folder);
136            }
137    
138            @Override
139            public void registerRepositoryEventListeners(
140                    RepositoryEventRegistry repositoryEventRegistry) {
141    
142                    repositoryEventRegistry.registerRepositoryEventListener(
143                            RepositoryEventType.Delete.class, FileEntry.class,
144                            new DeleteFileEntryRepositoryEventListener());
145                    repositoryEventRegistry.registerRepositoryEventListener(
146                            RepositoryEventType.Delete.class, Folder.class,
147                            new DeleteFolderRepositoryEventListener());
148                    repositoryEventRegistry.registerRepositoryEventListener(
149                            RepositoryEventType.Delete.class, LocalRepository.class,
150                            new DeleteLocalRepositoryEventListener());
151            }
152    
153            @Override
154            public void restoreFileEntryFromTrash(long userId, FileEntry fileEntry)
155                    throws PortalException {
156    
157                    DLAppHelperLocalServiceUtil.restoreFileEntryFromTrash(
158                            userId, fileEntry);
159            }
160    
161            @Override
162            public void restoreFolderFromTrash(long userId, Folder folder)
163                    throws PortalException {
164    
165                    DLAppHelperLocalServiceUtil.restoreFolderFromTrash(userId, folder);
166            }
167    
168            protected void deleteRepositoryTrashEntries(
169                    long repositoryId, String className) {
170    
171                    List<TrashEntry> trashEntries = TrashEntryLocalServiceUtil.getEntries(
172                            repositoryId, className);
173    
174                    for (TrashEntry trashEntry : trashEntries) {
175                            TrashEntryLocalServiceUtil.deleteTrashEntry(trashEntry);
176                    }
177            }
178    
179            protected void deleteTrashEntries(long repositoryId)
180                    throws PortalException {
181    
182                    Repository repository = RepositoryLocalServiceUtil.fetchRepository(
183                            repositoryId);
184    
185                    if (repository == null) {
186                            deleteRepositoryTrashEntries(
187                                    repositoryId, DLFileEntry.class.getName());
188                            deleteRepositoryTrashEntries(
189                                    repositoryId, DLFolder.class.getName());
190                    }
191                    else {
192                            deleteTrashEntries(
193                                    repository.getGroupId(), repository.getDlFolderId());
194                    }
195            }
196    
197            protected void deleteTrashEntries(long groupId, long dlFolderId)
198                    throws PortalException {
199    
200                    QueryDefinition<Object> queryDefinition = new QueryDefinition<Object>();
201    
202                    queryDefinition.setStatus(WorkflowConstants.STATUS_ANY);
203    
204                    List<Object> foldersAndFileEntriesAndFileShortcuts =
205                            DLFolderLocalServiceUtil.getFoldersAndFileEntriesAndFileShortcuts(
206                                    groupId, dlFolderId, null, true, queryDefinition);
207    
208                    for (Object folderFileEntryOrFileShortcut :
209                                    foldersAndFileEntriesAndFileShortcuts) {
210    
211                            if (folderFileEntryOrFileShortcut instanceof DLFileEntry) {
212                                    deleteTrashEntry((DLFileEntry)folderFileEntryOrFileShortcut);
213                            }
214                            else if (folderFileEntryOrFileShortcut instanceof DLFolder) {
215                                    DLFolder dlFolder = (DLFolder)folderFileEntryOrFileShortcut;
216    
217                                    deleteTrashEntries(
218                                            dlFolder.getGroupId(), dlFolder.getFolderId());
219    
220                                    deleteTrashEntry(dlFolder);
221                            }
222                    }
223            }
224    
225            protected void deleteTrashEntry(DLFileEntry dlFileEntry)
226                    throws PortalException {
227    
228                    if (!dlFileEntry.isInTrash()) {
229                            return;
230                    }
231    
232                    if (dlFileEntry.isInTrashExplicitly()) {
233                            TrashEntryLocalServiceUtil.deleteEntry(
234                                    DLFileEntryConstants.getClassName(),
235                                    dlFileEntry.getFileEntryId());
236                    }
237                    else {
238                            List<DLFileVersion> dlFileVersions = dlFileEntry.getFileVersions(
239                                    WorkflowConstants.STATUS_ANY);
240    
241                            for (DLFileVersion dlFileVersion : dlFileVersions) {
242                                    TrashVersionLocalServiceUtil.deleteTrashVersion(
243                                            DLFileVersion.class.getName(),
244                                            dlFileVersion.getFileVersionId());
245                            }
246                    }
247            }
248    
249            protected void deleteTrashEntry(DLFolder dlFolder) throws PortalException {
250                    if (!dlFolder.isInTrash()) {
251                            return;
252                    }
253    
254                    if (dlFolder.isInTrashExplicitly()) {
255                            TrashEntryLocalServiceUtil.deleteEntry(
256                                    DLFolderConstants.getClassName(), dlFolder.getFolderId());
257                    }
258                    else {
259                            TrashVersionLocalServiceUtil.deleteTrashVersion(
260                                    DLFolderConstants.getClassName(), dlFolder.getFolderId());
261                    }
262            }
263    
264            protected void deleteTrashEntry(FileEntry fileEntry)
265                    throws PortalException {
266    
267                    deleteTrashEntry((DLFileEntry)fileEntry.getModel());
268            }
269    
270            protected void deleteTrashEntry(Folder folder) throws PortalException {
271                    deleteTrashEntry((DLFolder)folder.getModel());
272            }
273    
274            private class DeleteFileEntryRepositoryEventListener
275                    implements RepositoryEventListener
276                            <RepositoryEventType.Delete, FileEntry> {
277    
278                    @Override
279                    public void execute(FileEntry fileEntry) throws PortalException {
280                            LiferayTrashCapability.this.deleteTrashEntry(fileEntry);
281                    }
282    
283            }
284    
285            private class DeleteFolderRepositoryEventListener
286                    implements RepositoryEventListener<RepositoryEventType.Delete, Folder> {
287    
288                    @Override
289                    public void execute(Folder folder) throws PortalException {
290                            LiferayTrashCapability.this.deleteTrashEntry(folder);
291                    }
292    
293            }
294    
295            private class DeleteLocalRepositoryEventListener
296                    implements RepositoryEventListener
297                            <RepositoryEventType.Delete, LocalRepository> {
298    
299                    @Override
300                    public void execute(LocalRepository localRepository)
301                            throws PortalException {
302    
303                            LiferayTrashCapability.this.deleteTrashEntries(
304                                    localRepository.getRepositoryId());
305                    }
306    
307            }
308    
309    }