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