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