001
014
015 package com.liferay.portlet.documentlibrary.trash;
016
017 import com.liferay.portal.InvalidRepositoryException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.repository.Repository;
021 import com.liferay.portal.kernel.repository.model.FileEntry;
022 import com.liferay.portal.kernel.trash.TrashActionKeys;
023 import com.liferay.portal.kernel.trash.TrashHandler;
024 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.ContainerModel;
027 import com.liferay.portal.model.TrashedModel;
028 import com.liferay.portal.repository.liferayrepository.LiferayRepository;
029 import com.liferay.portal.security.permission.ActionKeys;
030 import com.liferay.portal.security.permission.PermissionChecker;
031 import com.liferay.portal.service.RepositoryServiceUtil;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
035 import com.liferay.portlet.documentlibrary.model.DLFolder;
036 import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
037 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
038 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
039 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
040 import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
041 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
042 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
043 import com.liferay.portlet.documentlibrary.util.DLUtil;
044 import com.liferay.portlet.trash.DuplicateEntryException;
045 import com.liferay.portlet.trash.TrashEntryConstants;
046 import com.liferay.portlet.trash.model.TrashEntry;
047
048 import javax.portlet.PortletRequest;
049
050
057 public class DLFileEntryTrashHandler extends DLBaseTrashHandler {
058
059 @Override
060 public void checkDuplicateEntry(
061 long classPK, long containerModelId, String newName)
062 throws PortalException, SystemException {
063
064 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
065
066 checkDuplicateEntry(
067 classPK, 0, containerModelId, dlFileEntry.getTitle(), newName);
068 }
069
070 @Override
071 public void checkDuplicateTrashEntry(
072 TrashEntry trashEntry, long containerModelId, String newName)
073 throws PortalException, SystemException {
074
075 checkDuplicateEntry(
076 trashEntry.getClassPK(), trashEntry.getEntryId(), containerModelId,
077 trashEntry.getTypeSettingsProperty("title"), newName);
078 }
079
080 @Override
081 public void deleteTrashEntry(long classPK)
082 throws PortalException, SystemException {
083
084 DLAppLocalServiceUtil.deleteFileEntry(classPK);
085 }
086
087 @Override
088 public String getClassName() {
089 return DLFileEntry.class.getName();
090 }
091
092 @Override
093 public ContainerModel getParentContainerModel(long classPK)
094 throws PortalException, SystemException {
095
096 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
097
098 long parentFolderId = dlFileEntry.getFolderId();
099
100 if (parentFolderId <= 0) {
101 return null;
102 }
103
104 return getContainerModel(parentFolderId);
105 }
106
107 @Override
108 public ContainerModel getParentContainerModel(TrashedModel trashedModel)
109 throws PortalException, SystemException {
110
111 DLFileEntry dlFileEntry = (DLFileEntry)trashedModel;
112
113 return getContainerModel(dlFileEntry.getFolderId());
114 }
115
116 @Override
117 public String getRestoreContainedModelLink(
118 PortletRequest portletRequest, long classPK)
119 throws PortalException, SystemException {
120
121 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
122
123 return DLUtil.getDLFileEntryControlPanelLink(
124 portletRequest, dlFileEntry.getFileEntryId());
125 }
126
127 @Override
128 public String getRestoreContainerModelLink(
129 PortletRequest portletRequest, long classPK)
130 throws PortalException, SystemException {
131
132 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
133
134 return DLUtil.getDLFolderControlPanelLink(
135 portletRequest, dlFileEntry.getFolderId());
136 }
137
138 @Override
139 public String getRestoreMessage(PortletRequest portletRequest, long classPK)
140 throws PortalException, SystemException {
141
142 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
143
144 DLFolder dlFolder = dlFileEntry.getFolder();
145
146 return DLUtil.getAbsolutePath(portletRequest, dlFolder.getFolderId());
147 }
148
149 @Override
150 public String getSystemEventClassName() {
151 return FileEntry.class.getName();
152 }
153
154 @Override
155 public ContainerModel getTrashContainer(long classPK)
156 throws PortalException, SystemException {
157
158 try {
159 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
160
161 return dlFileEntry.getTrashContainer();
162 }
163 catch (InvalidRepositoryException ire) {
164 return null;
165 }
166 }
167
168 @Override
169 public TrashEntry getTrashEntry(long classPK)
170 throws PortalException, SystemException {
171
172 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
173
174 return dlFileEntry.getTrashEntry();
175 }
176
177 @Override
178 public boolean hasTrashPermission(
179 PermissionChecker permissionChecker, long groupId, long classPK,
180 String trashActionId)
181 throws PortalException, SystemException {
182
183 if (trashActionId.equals(TrashActionKeys.MOVE)) {
184 return DLFolderPermission.contains(
185 permissionChecker, groupId, classPK, ActionKeys.ADD_DOCUMENT);
186 }
187
188 return super.hasTrashPermission(
189 permissionChecker, groupId, classPK, trashActionId);
190 }
191
192 @Override
193 public boolean isInTrash(long classPK)
194 throws PortalException, SystemException {
195
196 try {
197 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
198
199 return dlFileEntry.isInTrash();
200 }
201 catch (InvalidRepositoryException ire) {
202 return false;
203 }
204 }
205
206 @Override
207 public boolean isInTrashContainer(long classPK)
208 throws PortalException, SystemException {
209
210 try {
211 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
212
213 return dlFileEntry.isInTrashContainer();
214 }
215 catch (InvalidRepositoryException ire) {
216 return false;
217 }
218 }
219
220 @Override
221 public boolean isRestorable(long classPK)
222 throws PortalException, SystemException {
223
224 DLFileEntry dlFileEntry = fetchDLFileEntry(classPK);
225
226 if ((dlFileEntry == null) ||
227 ((dlFileEntry.getFolderId() > 0) &&
228 (DLFolderLocalServiceUtil.fetchFolder(
229 dlFileEntry.getFolderId()) == null))) {
230
231 return false;
232 }
233
234 return !dlFileEntry.isInTrashContainer();
235 }
236
237 @Override
238 public void moveEntry(
239 long userId, long classPK, long containerModelId,
240 ServiceContext serviceContext)
241 throws PortalException, SystemException {
242
243 DLAppLocalServiceUtil.moveFileEntry(
244 userId, classPK, containerModelId, serviceContext);
245 }
246
247 @Override
248 public void moveTrashEntry(
249 long userId, long classPK, long containerModelId,
250 ServiceContext serviceContext)
251 throws PortalException, SystemException {
252
253 Repository repository = getRepository(classPK);
254
255 DLAppHelperLocalServiceUtil.moveFileEntryFromTrash(
256 userId, repository.getFileEntry(classPK), containerModelId,
257 serviceContext);
258 }
259
260 @Override
261 public void restoreTrashEntry(long userId, long classPK)
262 throws PortalException, SystemException {
263
264 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
265
266 if ((dlFileEntry.getClassNameId() > 0) &&
267 (dlFileEntry.getClassPK() > 0)) {
268
269 TrashHandler trashHandler =
270 TrashHandlerRegistryUtil.getTrashHandler(
271 dlFileEntry.getClassName());
272
273 trashHandler.restoreRelatedTrashEntry(getClassName(), classPK);
274
275 return;
276 }
277
278 DLAppLocalServiceUtil.restoreFileEntryFromTrash(userId, classPK);
279 }
280
281 @Override
282 public void updateTitle(long classPK, String name)
283 throws PortalException, SystemException {
284
285 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
286
287 dlFileEntry.setTitle(name);
288
289 DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
290
291 DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
292
293 dlFileVersion.setTitle(name);
294
295 DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);
296 }
297
298 protected void checkDuplicateEntry(
299 long classPK, long entryId, long containerModelId,
300 String originalTitle, String newName)
301 throws PortalException, SystemException {
302
303 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
304
305 if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
306 containerModelId = dlFileEntry.getFolderId();
307 }
308
309 if (Validator.isNotNull(newName)) {
310 originalTitle = newName;
311 }
312
313 DLFileEntry duplicateDLFileEntry =
314 DLFileEntryLocalServiceUtil.fetchFileEntry(
315 dlFileEntry.getGroupId(), containerModelId, originalTitle);
316
317 if (duplicateDLFileEntry != null) {
318 DuplicateEntryException dee = new DuplicateEntryException();
319
320 dee.setDuplicateEntryId(duplicateDLFileEntry.getFileEntryId());
321 dee.setOldName(duplicateDLFileEntry.getTitle());
322 dee.setTrashEntryId(entryId);
323
324 throw dee;
325 }
326 }
327
328 protected DLFileEntry fetchDLFileEntry(long classPK)
329 throws PortalException, SystemException {
330
331 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
332 0, classPK, 0);
333
334 if (!(repository instanceof LiferayRepository)) {
335 return null;
336 }
337
338 FileEntry fileEntry = repository.getFileEntry(classPK);
339
340 return (DLFileEntry)fileEntry.getModel();
341 }
342
343 protected DLFileEntry getDLFileEntry(long classPK)
344 throws PortalException, SystemException {
345
346 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
347 0, classPK, 0);
348
349 if (!(repository instanceof LiferayRepository)) {
350 throw new InvalidRepositoryException(
351 "Repository " + repository.getRepositoryId() +
352 " does not support trash operations");
353 }
354
355 FileEntry fileEntry = repository.getFileEntry(classPK);
356
357 return (DLFileEntry)fileEntry.getModel();
358 }
359
360 @Override
361 protected Repository getRepository(long classPK)
362 throws PortalException, SystemException {
363
364 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
365 0, classPK, 0);
366
367 if (!(repository instanceof LiferayRepository)) {
368 throw new InvalidRepositoryException(
369 "Repository " + repository.getRepositoryId() +
370 " does not support trash operations");
371 }
372
373 return repository;
374 }
375
376 @Override
377 protected boolean hasPermission(
378 PermissionChecker permissionChecker, long classPK, String actionId)
379 throws PortalException, SystemException {
380
381 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
382
383 if (dlFileEntry.isInHiddenFolder() &&
384 actionId.equals(ActionKeys.VIEW)) {
385
386 return false;
387 }
388
389 return DLFileEntryPermission.contains(
390 permissionChecker, classPK, actionId);
391 }
392
393 }