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 TrashEntry getTrashEntry(long classPK)
156 throws PortalException, SystemException {
157
158 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
159
160 return dlFileEntry.getTrashEntry();
161 }
162
163 @Override
164 public boolean hasTrashPermission(
165 PermissionChecker permissionChecker, long groupId, long classPK,
166 String trashActionId)
167 throws PortalException, SystemException {
168
169 if (trashActionId.equals(TrashActionKeys.MOVE)) {
170 return DLFolderPermission.contains(
171 permissionChecker, groupId, classPK, ActionKeys.ADD_DOCUMENT);
172 }
173
174 return super.hasTrashPermission(
175 permissionChecker, groupId, classPK, trashActionId);
176 }
177
178 @Override
179 public boolean isInTrash(long classPK)
180 throws PortalException, SystemException {
181
182 try {
183 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
184
185 return dlFileEntry.isInTrash();
186 }
187 catch (InvalidRepositoryException ire) {
188 return false;
189 }
190 }
191
192 @Override
193 public boolean isInTrashContainer(long classPK)
194 throws PortalException, SystemException {
195
196 try {
197 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
198
199 return dlFileEntry.isInTrashContainer();
200 }
201 catch (InvalidRepositoryException ire) {
202 return false;
203 }
204 }
205
206 @Override
207 public boolean isRestorable(long classPK)
208 throws PortalException, SystemException {
209
210 DLFileEntry dlFileEntry = fetchDLFileEntry(classPK);
211
212 if ((dlFileEntry == null) ||
213 ((dlFileEntry.getFolderId() > 0) &&
214 (DLFolderLocalServiceUtil.fetchFolder(
215 dlFileEntry.getFolderId()) == null))) {
216
217 return false;
218 }
219
220 return !dlFileEntry.isInTrashContainer();
221 }
222
223 @Override
224 public void moveEntry(
225 long userId, long classPK, long containerModelId,
226 ServiceContext serviceContext)
227 throws PortalException, SystemException {
228
229 DLAppLocalServiceUtil.moveFileEntry(
230 userId, classPK, containerModelId, serviceContext);
231 }
232
233 @Override
234 public void moveTrashEntry(
235 long userId, long classPK, long containerModelId,
236 ServiceContext serviceContext)
237 throws PortalException, SystemException {
238
239 Repository repository = getRepository(classPK);
240
241 DLAppHelperLocalServiceUtil.moveFileEntryFromTrash(
242 userId, repository.getFileEntry(classPK), containerModelId,
243 serviceContext);
244 }
245
246 @Override
247 public void restoreTrashEntry(long userId, long classPK)
248 throws PortalException, SystemException {
249
250 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
251
252 if ((dlFileEntry.getClassNameId() > 0) &&
253 (dlFileEntry.getClassPK() > 0)) {
254
255 TrashHandler trashHandler =
256 TrashHandlerRegistryUtil.getTrashHandler(
257 dlFileEntry.getClassName());
258
259 trashHandler.restoreRelatedTrashEntry(getClassName(), classPK);
260
261 return;
262 }
263
264 DLAppLocalServiceUtil.restoreFileEntryFromTrash(userId, classPK);
265 }
266
267 @Override
268 public void updateTitle(long classPK, String name)
269 throws PortalException, SystemException {
270
271 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
272
273 dlFileEntry.setTitle(name);
274
275 DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
276
277 DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
278
279 dlFileVersion.setTitle(name);
280
281 DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);
282 }
283
284 protected void checkDuplicateEntry(
285 long classPK, long entryId, long containerModelId,
286 String originalTitle, String newName)
287 throws PortalException, SystemException {
288
289 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
290
291 if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
292 containerModelId = dlFileEntry.getFolderId();
293 }
294
295 if (Validator.isNotNull(newName)) {
296 originalTitle = newName;
297 }
298
299 DLFileEntry duplicateDLFileEntry =
300 DLFileEntryLocalServiceUtil.fetchFileEntry(
301 dlFileEntry.getGroupId(), containerModelId, originalTitle);
302
303 if (duplicateDLFileEntry != null) {
304 DuplicateEntryException dee = new DuplicateEntryException();
305
306 dee.setDuplicateEntryId(duplicateDLFileEntry.getFileEntryId());
307 dee.setOldName(duplicateDLFileEntry.getTitle());
308 dee.setTrashEntryId(entryId);
309
310 throw dee;
311 }
312 }
313
314 protected DLFileEntry fetchDLFileEntry(long classPK)
315 throws PortalException, SystemException {
316
317 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
318 0, classPK, 0);
319
320 if (!(repository instanceof LiferayRepository)) {
321 return null;
322 }
323
324 FileEntry fileEntry = repository.getFileEntry(classPK);
325
326 return (DLFileEntry)fileEntry.getModel();
327 }
328
329 protected DLFileEntry getDLFileEntry(long classPK)
330 throws PortalException, SystemException {
331
332 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
333 0, classPK, 0);
334
335 if (!(repository instanceof LiferayRepository)) {
336 throw new InvalidRepositoryException(
337 "Repository " + repository.getRepositoryId() +
338 " does not support trash operations");
339 }
340
341 FileEntry fileEntry = repository.getFileEntry(classPK);
342
343 return (DLFileEntry)fileEntry.getModel();
344 }
345
346 @Override
347 protected Repository getRepository(long classPK)
348 throws PortalException, SystemException {
349
350 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
351 0, classPK, 0);
352
353 if (!(repository instanceof LiferayRepository)) {
354 throw new InvalidRepositoryException(
355 "Repository " + repository.getRepositoryId() +
356 " does not support trash operations");
357 }
358
359 return repository;
360 }
361
362 @Override
363 protected boolean hasPermission(
364 PermissionChecker permissionChecker, long classPK, String actionId)
365 throws PortalException, SystemException {
366
367 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
368
369 if (dlFileEntry.isInHiddenFolder() &&
370 actionId.equals(ActionKeys.VIEW)) {
371
372 return false;
373 }
374
375 return DLFileEntryPermission.contains(
376 permissionChecker, classPK, actionId);
377 }
378
379 }