/modules/apps/document-library/document-library-service/src/main/java/com/liferay/document/library/internal/repository/capabilities/LiferayTrashCapability.java

https://github.com/danielreuther/liferay-portal · Java · 369 lines · 276 code · 77 blank · 16 comment · 22 complexity · 22be894d4e4a11de6987ccf371390c71 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.document.library.internal.repository.capabilities;
  15. import com.liferay.document.library.kernel.model.DLFileEntry;
  16. import com.liferay.document.library.kernel.model.DLFileEntryConstants;
  17. import com.liferay.document.library.kernel.model.DLFileVersion;
  18. import com.liferay.document.library.kernel.model.DLFolder;
  19. import com.liferay.document.library.kernel.model.DLFolderConstants;
  20. import com.liferay.document.library.kernel.service.DLAppHelperLocalService;
  21. import com.liferay.portal.kernel.dao.orm.QueryDefinition;
  22. import com.liferay.portal.kernel.dao.orm.QueryUtil;
  23. import com.liferay.portal.kernel.exception.PortalException;
  24. import com.liferay.portal.kernel.model.Repository;
  25. import com.liferay.portal.kernel.repository.LocalRepository;
  26. import com.liferay.portal.kernel.repository.capabilities.TrashCapability;
  27. import com.liferay.portal.kernel.repository.event.RepositoryEventAware;
  28. import com.liferay.portal.kernel.repository.event.RepositoryEventListener;
  29. import com.liferay.portal.kernel.repository.event.RepositoryEventType;
  30. import com.liferay.portal.kernel.repository.model.FileEntry;
  31. import com.liferay.portal.kernel.repository.model.FileShortcut;
  32. import com.liferay.portal.kernel.repository.model.Folder;
  33. import com.liferay.portal.kernel.repository.registry.RepositoryEventRegistry;
  34. import com.liferay.portal.kernel.service.ServiceContext;
  35. import com.liferay.portal.kernel.workflow.WorkflowConstants;
  36. import com.liferay.portal.repository.capabilities.util.DLAppServiceAdapter;
  37. import com.liferay.portal.repository.capabilities.util.DLFileEntryServiceAdapter;
  38. import com.liferay.portal.repository.capabilities.util.DLFolderServiceAdapter;
  39. import com.liferay.portal.repository.capabilities.util.RepositoryServiceAdapter;
  40. import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
  41. import com.liferay.trash.model.TrashEntry;
  42. import com.liferay.trash.service.TrashEntryLocalService;
  43. import com.liferay.trash.service.TrashVersionLocalService;
  44. import java.util.List;
  45. /**
  46. * @author Adolfo Pérez
  47. */
  48. public class LiferayTrashCapability
  49. implements RepositoryEventAware, TrashCapability {
  50. public LiferayTrashCapability(
  51. DLAppHelperLocalService dlAppHelperLocalService,
  52. DLAppServiceAdapter dlAppServiceAdapter,
  53. DLFileEntryServiceAdapter dlFileEntryServiceAdapter,
  54. DLFolderServiceAdapter dlFolderServiceAdapter,
  55. RepositoryServiceAdapter repositoryServiceAdapter,
  56. TrashEntryLocalService trashEntryLocalService,
  57. TrashVersionLocalService trashVersionLocalService) {
  58. _dlAppHelperLocalService = dlAppHelperLocalService;
  59. _dlAppServiceAdapter = dlAppServiceAdapter;
  60. _dlFileEntryServiceAdapter = dlFileEntryServiceAdapter;
  61. _dlFolderServiceAdapter = dlFolderServiceAdapter;
  62. _repositoryServiceAdapter = repositoryServiceAdapter;
  63. _trashEntryLocalService = trashEntryLocalService;
  64. _trashVersionLocalService = trashVersionLocalService;
  65. }
  66. @Override
  67. public void deleteFileEntry(FileEntry fileEntry) throws PortalException {
  68. _deleteTrashEntry(fileEntry);
  69. _dlAppServiceAdapter.deleteFileEntry(fileEntry.getFileEntryId());
  70. }
  71. @Override
  72. public void deleteFolder(Folder folder) throws PortalException {
  73. List<DLFileEntry> dlFileEntries =
  74. _dlFileEntryServiceAdapter.getGroupFileEntries(
  75. folder.getGroupId(), 0, folder.getRepositoryId(),
  76. folder.getFolderId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS,
  77. null);
  78. for (DLFileEntry dlFileEntry : dlFileEntries) {
  79. FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
  80. _dlAppHelperLocalService.deleteFileEntry(fileEntry);
  81. _deleteTrashEntry(fileEntry);
  82. }
  83. _dlAppHelperLocalService.deleteFolder(folder);
  84. _deleteTrashEntry(folder);
  85. _dlFolderServiceAdapter.deleteFolder(folder.getFolderId(), false);
  86. }
  87. @Override
  88. public boolean isInTrash(FileEntry fileEntry) throws PortalException {
  89. DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
  90. return dlFileEntry.isInTrash();
  91. }
  92. @Override
  93. public boolean isInTrash(Folder folder) {
  94. DLFolder dlFolder = (DLFolder)folder.getModel();
  95. return dlFolder.isInTrash();
  96. }
  97. @Override
  98. public FileEntry moveFileEntryFromTrash(
  99. long userId, FileEntry fileEntry, Folder newFolder,
  100. ServiceContext serviceContext)
  101. throws PortalException {
  102. long newFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
  103. if (newFolder != null) {
  104. newFolderId = newFolder.getFolderId();
  105. }
  106. return _dlAppHelperLocalService.moveFileEntryFromTrash(
  107. userId, fileEntry, newFolderId, serviceContext);
  108. }
  109. @Override
  110. public FileEntry moveFileEntryToTrash(long userId, FileEntry fileEntry)
  111. throws PortalException {
  112. return _dlAppHelperLocalService.moveFileEntryToTrash(userId, fileEntry);
  113. }
  114. @Override
  115. public FileShortcut moveFileShortcutFromTrash(
  116. long userId, FileShortcut fileShortcut, Folder newFolder,
  117. ServiceContext serviceContext)
  118. throws PortalException {
  119. long newFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
  120. if (newFolder != null) {
  121. newFolderId = newFolder.getFolderId();
  122. }
  123. return _dlAppHelperLocalService.moveFileShortcutFromTrash(
  124. userId, fileShortcut, newFolderId, serviceContext);
  125. }
  126. @Override
  127. public FileShortcut moveFileShortcutToTrash(
  128. long userId, FileShortcut fileShortcut)
  129. throws PortalException {
  130. return _dlAppHelperLocalService.moveFileShortcutToTrash(
  131. userId, fileShortcut);
  132. }
  133. @Override
  134. public Folder moveFolderFromTrash(
  135. long userId, Folder folder, Folder destinationFolder,
  136. ServiceContext serviceContext)
  137. throws PortalException {
  138. long destinationFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
  139. if (destinationFolder != null) {
  140. destinationFolderId = destinationFolder.getFolderId();
  141. }
  142. return _dlAppHelperLocalService.moveFolderFromTrash(
  143. userId, folder, destinationFolderId, serviceContext);
  144. }
  145. @Override
  146. public Folder moveFolderToTrash(long userId, Folder folder)
  147. throws PortalException {
  148. return _dlAppHelperLocalService.moveFolderToTrash(userId, folder);
  149. }
  150. @Override
  151. public void registerRepositoryEventListeners(
  152. RepositoryEventRegistry repositoryEventRegistry) {
  153. repositoryEventRegistry.registerRepositoryEventListener(
  154. RepositoryEventType.Delete.class, FileEntry.class,
  155. new DeleteFileEntryRepositoryEventListener());
  156. repositoryEventRegistry.registerRepositoryEventListener(
  157. RepositoryEventType.Delete.class, Folder.class,
  158. new DeleteFolderRepositoryEventListener());
  159. repositoryEventRegistry.registerRepositoryEventListener(
  160. RepositoryEventType.Delete.class, LocalRepository.class,
  161. new DeleteLocalRepositoryEventListener());
  162. }
  163. @Override
  164. public void restoreFileEntryFromTrash(long userId, FileEntry fileEntry)
  165. throws PortalException {
  166. _dlAppHelperLocalService.restoreFileEntryFromTrash(userId, fileEntry);
  167. }
  168. @Override
  169. public void restoreFileShortcutFromTrash(
  170. long userId, FileShortcut fileShortcut)
  171. throws PortalException {
  172. _dlAppHelperLocalService.restoreFileShortcutFromTrash(
  173. userId, fileShortcut);
  174. }
  175. @Override
  176. public void restoreFolderFromTrash(long userId, Folder folder)
  177. throws PortalException {
  178. _dlAppHelperLocalService.restoreFolderFromTrash(userId, folder);
  179. }
  180. private void _deleteRepositoryTrashEntries(
  181. long repositoryId, String className) {
  182. List<TrashEntry> trashEntries = _trashEntryLocalService.getEntries(
  183. repositoryId, className);
  184. for (TrashEntry trashEntry : trashEntries) {
  185. _trashEntryLocalService.deleteTrashEntry(trashEntry);
  186. }
  187. }
  188. private void _deleteTrashEntries(long repositoryId) throws PortalException {
  189. Repository repository = _repositoryServiceAdapter.fetchRepository(
  190. repositoryId);
  191. if (repository == null) {
  192. _deleteRepositoryTrashEntries(
  193. repositoryId, DLFileEntry.class.getName());
  194. _deleteRepositoryTrashEntries(
  195. repositoryId, DLFolder.class.getName());
  196. }
  197. else {
  198. _deleteTrashEntries(
  199. repository.getGroupId(), repository.getDlFolderId());
  200. }
  201. }
  202. private void _deleteTrashEntries(long groupId, long dlFolderId)
  203. throws PortalException {
  204. QueryDefinition<Object> queryDefinition = new QueryDefinition<>();
  205. queryDefinition.setStatus(WorkflowConstants.STATUS_ANY);
  206. List<Object> foldersAndFileEntriesAndFileShortcuts =
  207. _dlFolderServiceAdapter.getFoldersAndFileEntriesAndFileShortcuts(
  208. groupId, dlFolderId, null, true, queryDefinition);
  209. for (Object folderFileEntryOrFileShortcut :
  210. foldersAndFileEntriesAndFileShortcuts) {
  211. if (folderFileEntryOrFileShortcut instanceof DLFileEntry) {
  212. _deleteTrashEntry((DLFileEntry)folderFileEntryOrFileShortcut);
  213. }
  214. else if (folderFileEntryOrFileShortcut instanceof DLFolder) {
  215. DLFolder dlFolder = (DLFolder)folderFileEntryOrFileShortcut;
  216. _deleteTrashEntries(
  217. dlFolder.getGroupId(), dlFolder.getFolderId());
  218. _deleteTrashEntry(dlFolder);
  219. }
  220. }
  221. }
  222. private void _deleteTrashEntry(DLFileEntry dlFileEntry) {
  223. if (!dlFileEntry.isInTrash()) {
  224. return;
  225. }
  226. if (dlFileEntry.isInTrashExplicitly()) {
  227. _trashEntryLocalService.deleteEntry(
  228. DLFileEntryConstants.getClassName(),
  229. dlFileEntry.getFileEntryId());
  230. }
  231. else {
  232. List<DLFileVersion> dlFileVersions = dlFileEntry.getFileVersions(
  233. WorkflowConstants.STATUS_ANY);
  234. for (DLFileVersion dlFileVersion : dlFileVersions) {
  235. _trashVersionLocalService.deleteTrashVersion(
  236. DLFileVersion.class.getName(),
  237. dlFileVersion.getFileVersionId());
  238. }
  239. }
  240. }
  241. private void _deleteTrashEntry(DLFolder dlFolder) {
  242. if (!dlFolder.isInTrash()) {
  243. return;
  244. }
  245. if (dlFolder.isInTrashExplicitly()) {
  246. _trashEntryLocalService.deleteEntry(
  247. DLFolderConstants.getClassName(), dlFolder.getFolderId());
  248. }
  249. else {
  250. _trashVersionLocalService.deleteTrashVersion(
  251. DLFolderConstants.getClassName(), dlFolder.getFolderId());
  252. }
  253. }
  254. private void _deleteTrashEntry(FileEntry fileEntry) {
  255. _deleteTrashEntry((DLFileEntry)fileEntry.getModel());
  256. }
  257. private void _deleteTrashEntry(Folder folder) {
  258. _deleteTrashEntry((DLFolder)folder.getModel());
  259. }
  260. private final DLAppHelperLocalService _dlAppHelperLocalService;
  261. private final DLAppServiceAdapter _dlAppServiceAdapter;
  262. private final DLFileEntryServiceAdapter _dlFileEntryServiceAdapter;
  263. private final DLFolderServiceAdapter _dlFolderServiceAdapter;
  264. private final RepositoryServiceAdapter _repositoryServiceAdapter;
  265. private final TrashEntryLocalService _trashEntryLocalService;
  266. private final TrashVersionLocalService _trashVersionLocalService;
  267. private class DeleteFileEntryRepositoryEventListener
  268. implements RepositoryEventListener
  269. <RepositoryEventType.Delete, FileEntry> {
  270. @Override
  271. public void execute(FileEntry fileEntry) {
  272. LiferayTrashCapability.this._deleteTrashEntry(fileEntry);
  273. }
  274. }
  275. private class DeleteFolderRepositoryEventListener
  276. implements RepositoryEventListener<RepositoryEventType.Delete, Folder> {
  277. @Override
  278. public void execute(Folder folder) {
  279. LiferayTrashCapability.this._deleteTrashEntry(folder);
  280. }
  281. }
  282. private class DeleteLocalRepositoryEventListener
  283. implements RepositoryEventListener
  284. <RepositoryEventType.Delete, LocalRepository> {
  285. @Override
  286. public void execute(LocalRepository localRepository)
  287. throws PortalException {
  288. LiferayTrashCapability.this._deleteTrashEntries(
  289. localRepository.getRepositoryId());
  290. }
  291. }
  292. }