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