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