001
014
015 package com.liferay.portlet.trash.service.impl;
016
017 import com.liferay.portal.TrashPermissionException;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.transaction.Transactional;
024 import com.liferay.portal.kernel.trash.TrashActionKeys;
025 import com.liferay.portal.kernel.trash.TrashHandler;
026 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
027 import com.liferay.portal.kernel.util.OrderByComparator;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.security.auth.PrincipalException;
030 import com.liferay.portal.security.permission.ActionKeys;
031 import com.liferay.portal.security.permission.PermissionChecker;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portal.util.PropsValues;
034 import com.liferay.portlet.trash.TrashEntryConstants;
035 import com.liferay.portlet.trash.model.TrashEntry;
036 import com.liferay.portlet.trash.model.TrashEntryList;
037 import com.liferay.portlet.trash.model.TrashEntrySoap;
038 import com.liferay.portlet.trash.model.impl.TrashEntryImpl;
039 import com.liferay.portlet.trash.service.base.TrashEntryServiceBaseImpl;
040
041 import java.util.ArrayList;
042 import java.util.List;
043
044
052 public class TrashEntryServiceImpl extends TrashEntryServiceBaseImpl {
053
054
062 @Transactional(noRollbackFor = {TrashPermissionException.class})
063 public void deleteEntries(long groupId)
064 throws PortalException, SystemException {
065
066 boolean throwTrashPermissionException = false;
067
068 List<TrashEntry> entries = trashEntryPersistence.findByGroupId(groupId);
069
070 PermissionChecker permissionChecker = getPermissionChecker();
071
072 for (TrashEntry entry : entries) {
073 try {
074 TrashHandler trashHandler =
075 TrashHandlerRegistryUtil.getTrashHandler(
076 entry.getClassName());
077
078 if (!trashHandler.hasTrashPermission(
079 permissionChecker, 0, entry.getClassPK(),
080 ActionKeys.VIEW)) {
081
082 continue;
083 }
084
085 deleteEntry(entry);
086 }
087 catch (TrashPermissionException tpe) {
088 throwTrashPermissionException = true;
089 }
090 catch (Exception e) {
091 _log.error(e, e);
092 }
093 }
094
095 if (throwTrashPermissionException) {
096 throw new TrashPermissionException(
097 TrashPermissionException.EMPTY_TRASH);
098 }
099 }
100
101
110 @Transactional(noRollbackFor = {TrashPermissionException.class})
111 public void deleteEntries(long[] entryIds)
112 throws PortalException, SystemException {
113
114 boolean throwTrashPermissionException = false;
115
116 for (long entryId : entryIds) {
117 try {
118 deleteEntry(entryId);
119 }
120 catch (TrashPermissionException tpe) {
121 throwTrashPermissionException = true;
122 }
123 }
124
125 if (throwTrashPermissionException) {
126 throw new TrashPermissionException(
127 TrashPermissionException.EMPTY_TRASH);
128 }
129 }
130
131
146 public void deleteEntry(long entryId)
147 throws PortalException, SystemException {
148
149 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
150
151 deleteEntry(entry);
152 }
153
154
170 public void deleteEntry(String className, long classPK)
171 throws PortalException, SystemException {
172
173 TrashEntry entry = trashEntryLocalService.fetchEntry(
174 className, classPK);
175
176 if (entry == null) {
177 entry = new TrashEntryImpl();
178
179 entry.setClassName(className);
180 entry.setClassPK(classPK);
181 }
182
183 deleteEntry(entry);
184 }
185
186
194 public TrashEntryList getEntries(long groupId)
195 throws PrincipalException, SystemException {
196
197 return getEntries(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
198 }
199
200
214 public TrashEntryList getEntries(
215 long groupId, int start, int end, OrderByComparator obc)
216 throws PrincipalException, SystemException {
217
218 TrashEntryList trashEntriesList = new TrashEntryList();
219
220 int entriesCount = trashEntryPersistence.countByGroupId(groupId);
221
222 boolean approximate = entriesCount > PropsValues.TRASH_SEARCH_LIMIT;
223
224 trashEntriesList.setApproximate(approximate);
225
226 List<TrashEntry> entries = trashEntryPersistence.findByGroupId(
227 groupId, 0, end + PropsValues.TRASH_SEARCH_LIMIT, obc);
228
229 List<TrashEntry> filteredEntries = new ArrayList<TrashEntry>();
230
231 PermissionChecker permissionChecker = getPermissionChecker();
232
233 for (TrashEntry entry : entries) {
234 String className = entry.getClassName();
235 long classPK = entry.getClassPK();
236
237 try {
238 TrashHandler trashHandler =
239 TrashHandlerRegistryUtil.getTrashHandler(className);
240
241 if (trashHandler.hasTrashPermission(
242 permissionChecker, 0, classPK, ActionKeys.VIEW)) {
243
244 filteredEntries.add(entry);
245 }
246 }
247 catch (Exception e) {
248 _log.error(e, e);
249 }
250 }
251
252 int filteredEntriesCount = filteredEntries.size();
253
254 if ((end != QueryUtil.ALL_POS) && (start != QueryUtil.ALL_POS)) {
255 if (end > filteredEntriesCount) {
256 end = filteredEntriesCount;
257 }
258
259 if (start > filteredEntriesCount) {
260 start = filteredEntriesCount;
261 }
262
263 filteredEntries = filteredEntries.subList(start, end);
264 }
265
266 trashEntriesList.setArray(TrashEntrySoap.toSoapModels(filteredEntries));
267 trashEntriesList.setCount(filteredEntriesCount);
268
269 return trashEntriesList;
270 }
271
272
307 public void moveEntry(
308 String className, long classPK, long destinationContainerModelId,
309 ServiceContext serviceContext)
310 throws PortalException, SystemException {
311
312 PermissionChecker permissionChecker = getPermissionChecker();
313
314 TrashEntry entry = trashEntryLocalService.getEntry(className, classPK);
315
316 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
317 className);
318
319 if (!trashHandler.hasTrashPermission(
320 permissionChecker, entry.getGroupId(),
321 destinationContainerModelId, TrashActionKeys.MOVE)) {
322
323 throw new TrashPermissionException(TrashPermissionException.MOVE);
324 }
325
326 if (trashHandler.isInTrash(classPK) &&
327 !trashHandler.hasTrashPermission(
328 permissionChecker, 0, classPK, TrashActionKeys.RESTORE)) {
329
330 throw new TrashPermissionException(
331 TrashPermissionException.RESTORE);
332 }
333
334 trashHandler.checkDuplicateTrashEntry(
335 entry, destinationContainerModelId, StringPool.BLANK);
336
337 if (trashHandler.isInTrash(classPK)) {
338 trashHandler.moveTrashEntry(
339 getUserId(), classPK, destinationContainerModelId,
340 serviceContext);
341 }
342 else {
343 trashHandler.moveEntry(
344 getUserId(), classPK, destinationContainerModelId,
345 serviceContext);
346 }
347 }
348
349 public TrashEntry restoreEntry(long entryId)
350 throws PortalException, SystemException {
351
352 return restoreEntry(entryId, 0, null);
353 }
354
355
394 public TrashEntry restoreEntry(
395 long entryId, long overrideClassPK, String name)
396 throws PortalException, SystemException {
397
398 PermissionChecker permissionChecker = getPermissionChecker();
399
400 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
401
402 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
403 entry.getClassName());
404
405 if (!trashHandler.hasTrashPermission(
406 permissionChecker, 0, entry.getClassPK(),
407 TrashActionKeys.RESTORE)) {
408
409 throw new TrashPermissionException(
410 TrashPermissionException.RESTORE);
411 }
412
413 if (overrideClassPK > 0) {
414 if (!trashHandler.hasTrashPermission(
415 permissionChecker, 0, overrideClassPK,
416 TrashActionKeys.OVERWRITE)) {
417
418 throw new TrashPermissionException(
419 TrashPermissionException.RESTORE_OVERWRITE);
420 }
421
422 trashHandler.deleteTrashEntry(overrideClassPK);
423
424 trashHandler.checkDuplicateTrashEntry(
425 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, null);
426 }
427 else if (name != null) {
428 if (!trashHandler.hasTrashPermission(
429 permissionChecker, 0, entry.getClassPK(),
430 TrashActionKeys.RENAME)) {
431
432 throw new TrashPermissionException(
433 TrashPermissionException.RESTORE_RENAME);
434 }
435
436 trashHandler.checkDuplicateTrashEntry(
437 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, name);
438
439 trashHandler.updateTitle(entry.getClassPK(), name);
440 }
441
442 trashHandler.restoreTrashEntry(getUserId(), entry.getClassPK());
443
444 return entry;
445 }
446
447 protected void deleteEntry(TrashEntry entry)
448 throws PortalException, SystemException {
449
450 PermissionChecker permissionChecker = getPermissionChecker();
451
452 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
453 entry.getClassName());
454
455 if (!trashHandler.hasTrashPermission(
456 permissionChecker, 0, entry.getClassPK(), ActionKeys.DELETE)) {
457
458 throw new TrashPermissionException(TrashPermissionException.DELETE);
459 }
460
461 trashHandler.deleteTrashEntry(entry.getClassPK());
462 }
463
464 private static Log _log = LogFactoryUtil.getLog(
465 TrashEntryServiceImpl.class);
466
467 }