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

https://github.com/kiyoshilee/liferay-portal · Java · 371 lines · 277 code · 78 blank · 16 comment · 22 complexity · b28bc605cfd0564f625d8266760d3a01 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. protected 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. protected void deleteTrashEntries(long repositoryId)
  189. throws PortalException {
  190. Repository repository = _repositoryServiceAdapter.fetchRepository(
  191. repositoryId);
  192. if (repository == null) {
  193. deleteRepositoryTrashEntries(
  194. repositoryId, DLFileEntry.class.getName());
  195. deleteRepositoryTrashEntries(
  196. repositoryId, DLFolder.class.getName());
  197. }
  198. else {
  199. deleteTrashEntries(
  200. repository.getGroupId(), repository.getDlFolderId());
  201. }
  202. }
  203. protected void deleteTrashEntries(long groupId, long dlFolderId)
  204. throws PortalException {
  205. QueryDefinition<Object> queryDefinition = new QueryDefinition<>();
  206. queryDefinition.setStatus(WorkflowConstants.STATUS_ANY);
  207. List<Object> foldersAndFileEntriesAndFileShortcuts =
  208. _dlFolderServiceAdapter.getFoldersAndFileEntriesAndFileShortcuts(
  209. groupId, dlFolderId, null, true, queryDefinition);
  210. for (Object folderFileEntryOrFileShortcut :
  211. foldersAndFileEntriesAndFileShortcuts) {
  212. if (folderFileEntryOrFileShortcut instanceof DLFileEntry) {
  213. deleteTrashEntry((DLFileEntry)folderFileEntryOrFileShortcut);
  214. }
  215. else if (folderFileEntryOrFileShortcut instanceof DLFolder) {
  216. DLFolder dlFolder = (DLFolder)folderFileEntryOrFileShortcut;
  217. deleteTrashEntries(
  218. dlFolder.getGroupId(), dlFolder.getFolderId());
  219. deleteTrashEntry(dlFolder);
  220. }
  221. }
  222. }
  223. protected void deleteTrashEntry(DLFileEntry dlFileEntry) {
  224. if (!dlFileEntry.isInTrash()) {
  225. return;
  226. }
  227. if (dlFileEntry.isInTrashExplicitly()) {
  228. _trashEntryLocalService.deleteEntry(
  229. DLFileEntryConstants.getClassName(),
  230. dlFileEntry.getFileEntryId());
  231. }
  232. else {
  233. List<DLFileVersion> dlFileVersions = dlFileEntry.getFileVersions(
  234. WorkflowConstants.STATUS_ANY);
  235. for (DLFileVersion dlFileVersion : dlFileVersions) {
  236. _trashVersionLocalService.deleteTrashVersion(
  237. DLFileVersion.class.getName(),
  238. dlFileVersion.getFileVersionId());
  239. }
  240. }
  241. }
  242. protected void deleteTrashEntry(DLFolder dlFolder) {
  243. if (!dlFolder.isInTrash()) {
  244. return;
  245. }
  246. if (dlFolder.isInTrashExplicitly()) {
  247. _trashEntryLocalService.deleteEntry(
  248. DLFolderConstants.getClassName(), dlFolder.getFolderId());
  249. }
  250. else {
  251. _trashVersionLocalService.deleteTrashVersion(
  252. DLFolderConstants.getClassName(), dlFolder.getFolderId());
  253. }
  254. }
  255. protected void deleteTrashEntry(FileEntry fileEntry) {
  256. deleteTrashEntry((DLFileEntry)fileEntry.getModel());
  257. }
  258. protected void deleteTrashEntry(Folder folder) {
  259. deleteTrashEntry((DLFolder)folder.getModel());
  260. }
  261. private final DLAppHelperLocalService _dlAppHelperLocalService;
  262. private final DLAppServiceAdapter _dlAppServiceAdapter;
  263. private final DLFileEntryServiceAdapter _dlFileEntryServiceAdapter;
  264. private final DLFolderServiceAdapter _dlFolderServiceAdapter;
  265. private final RepositoryServiceAdapter _repositoryServiceAdapter;
  266. private final TrashEntryLocalService _trashEntryLocalService;
  267. private final TrashVersionLocalService _trashVersionLocalService;
  268. private class DeleteFileEntryRepositoryEventListener
  269. implements RepositoryEventListener
  270. <RepositoryEventType.Delete, FileEntry> {
  271. @Override
  272. public void execute(FileEntry fileEntry) {
  273. LiferayTrashCapability.this.deleteTrashEntry(fileEntry);
  274. }
  275. }
  276. private class DeleteFolderRepositoryEventListener
  277. implements RepositoryEventListener<RepositoryEventType.Delete, Folder> {
  278. @Override
  279. public void execute(Folder folder) {
  280. LiferayTrashCapability.this.deleteTrashEntry(folder);
  281. }
  282. }
  283. private class DeleteLocalRepositoryEventListener
  284. implements RepositoryEventListener
  285. <RepositoryEventType.Delete, LocalRepository> {
  286. @Override
  287. public void execute(LocalRepository localRepository)
  288. throws PortalException {
  289. LiferayTrashCapability.this.deleteTrashEntries(
  290. localRepository.getRepositoryId());
  291. }
  292. }
  293. }