001
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
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 if (!dlFileEntry.isInTrash()) {
278 return;
279 }
280
281 if (dlFileEntry.isInTrashExplicitly()) {
282 _trashEntryLocalService.deleteEntry(
283 DLFileEntryConstants.getClassName(),
284 dlFileEntry.getFileEntryId());
285 }
286 else {
287 List<DLFileVersion> dlFileVersions = dlFileEntry.getFileVersions(
288 WorkflowConstants.STATUS_ANY);
289
290 for (DLFileVersion dlFileVersion : dlFileVersions) {
291 _trashVersionLocalService.deleteTrashVersion(
292 DLFileVersion.class.getName(),
293 dlFileVersion.getFileVersionId());
294 }
295 }
296 }
297
298 protected void deleteTrashEntry(DLFolder dlFolder) {
299 if (!dlFolder.isInTrash()) {
300 return;
301 }
302
303 if (dlFolder.isInTrashExplicitly()) {
304 _trashEntryLocalService.deleteEntry(
305 DLFolderConstants.getClassName(), dlFolder.getFolderId());
306 }
307 else {
308 _trashVersionLocalService.deleteTrashVersion(
309 DLFolderConstants.getClassName(), dlFolder.getFolderId());
310 }
311 }
312
313 protected void deleteTrashEntry(FileEntry fileEntry) {
314 deleteTrashEntry((DLFileEntry)fileEntry.getModel());
315 }
316
317 protected void deleteTrashEntry(Folder folder) {
318 deleteTrashEntry((DLFolder)folder.getModel());
319 }
320
321 private final DLAppHelperLocalService _dlAppHelperLocalService;
322 private final DLAppServiceAdapter _dlAppServiceAdapter;
323 private final DLFileEntryServiceAdapter _dlFileEntryServiceAdapter;
324 private final DLFolderServiceAdapter _dlFolderServiceAdapter;
325 private final RepositoryServiceAdapter _repositoryServiceAdapter;
326 private final TrashEntryLocalService _trashEntryLocalService;
327 private final TrashVersionLocalService _trashVersionLocalService;
328
329 private class DeleteFileEntryRepositoryEventListener
330 implements RepositoryEventListener
331 <RepositoryEventType.Delete, FileEntry> {
332
333 @Override
334 public void execute(FileEntry fileEntry) {
335 LiferayTrashCapability.this.deleteTrashEntry(fileEntry);
336 }
337
338 }
339
340 private class DeleteFolderRepositoryEventListener
341 implements RepositoryEventListener<RepositoryEventType.Delete, Folder> {
342
343 @Override
344 public void execute(Folder folder) {
345 LiferayTrashCapability.this.deleteTrashEntry(folder);
346 }
347
348 }
349
350 private class DeleteLocalRepositoryEventListener
351 implements RepositoryEventListener
352 <RepositoryEventType.Delete, LocalRepository> {
353
354 @Override
355 public void execute(LocalRepository localRepository)
356 throws PortalException {
357
358 LiferayTrashCapability.this.deleteTrashEntries(
359 localRepository.getRepositoryId());
360 }
361
362 }
363
364 }