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.dao.search.SearchPaginationUtil;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.transaction.Transactional;
025 import com.liferay.portal.kernel.trash.TrashActionKeys;
026 import com.liferay.portal.kernel.trash.TrashHandler;
027 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
028 import com.liferay.portal.kernel.util.OrderByComparator;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.security.auth.PrincipalException;
031 import com.liferay.portal.security.permission.ActionKeys;
032 import com.liferay.portal.security.permission.PermissionChecker;
033 import com.liferay.portal.service.ServiceContext;
034 import com.liferay.portal.util.PropsValues;
035 import com.liferay.portlet.trash.TrashEntryConstants;
036 import com.liferay.portlet.trash.model.TrashEntry;
037 import com.liferay.portlet.trash.model.TrashEntryList;
038 import com.liferay.portlet.trash.model.TrashEntrySoap;
039 import com.liferay.portlet.trash.model.impl.TrashEntryImpl;
040 import com.liferay.portlet.trash.service.base.TrashEntryServiceBaseImpl;
041
042 import java.util.ArrayList;
043 import java.util.List;
044
045
053 public class TrashEntryServiceImpl extends TrashEntryServiceBaseImpl {
054
055
063 @Override
064 @Transactional(noRollbackFor = {TrashPermissionException.class})
065 public void deleteEntries(long groupId)
066 throws PortalException, SystemException {
067
068 boolean throwTrashPermissionException = false;
069
070 List<TrashEntry> entries = trashEntryPersistence.findByGroupId(groupId);
071
072 PermissionChecker permissionChecker = getPermissionChecker();
073
074 for (TrashEntry entry : entries) {
075 try {
076 TrashHandler trashHandler =
077 TrashHandlerRegistryUtil.getTrashHandler(
078 entry.getClassName());
079
080 if (!trashHandler.hasTrashPermission(
081 permissionChecker, 0, entry.getClassPK(),
082 ActionKeys.VIEW)) {
083
084 continue;
085 }
086
087 deleteEntry(entry);
088 }
089 catch (TrashPermissionException tpe) {
090 throwTrashPermissionException = true;
091 }
092 catch (Exception e) {
093 _log.error(e, e);
094 }
095 }
096
097 if (throwTrashPermissionException) {
098 throw new TrashPermissionException(
099 TrashPermissionException.EMPTY_TRASH);
100 }
101 }
102
103
112 @Override
113 @Transactional(noRollbackFor = {TrashPermissionException.class})
114 public void deleteEntries(long[] entryIds)
115 throws PortalException, SystemException {
116
117 boolean throwTrashPermissionException = false;
118
119 for (long entryId : entryIds) {
120 try {
121 deleteEntry(entryId);
122 }
123 catch (TrashPermissionException tpe) {
124 throwTrashPermissionException = true;
125 }
126 }
127
128 if (throwTrashPermissionException) {
129 throw new TrashPermissionException(
130 TrashPermissionException.EMPTY_TRASH);
131 }
132 }
133
134
149 @Override
150 public void deleteEntry(long entryId)
151 throws PortalException, SystemException {
152
153 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
154
155 deleteEntry(entry);
156 }
157
158
174 @Override
175 public void deleteEntry(String className, long classPK)
176 throws PortalException, SystemException {
177
178 TrashEntry entry = trashEntryLocalService.fetchEntry(
179 className, classPK);
180
181 if (entry == null) {
182 entry = new TrashEntryImpl();
183
184 entry.setClassName(className);
185 entry.setClassPK(classPK);
186 }
187
188 deleteEntry(entry);
189 }
190
191
199 @Override
200 public TrashEntryList getEntries(long groupId)
201 throws PrincipalException, SystemException {
202
203 return getEntries(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
204 }
205
206
220 @Override
221 public TrashEntryList getEntries(
222 long groupId, int start, int end, OrderByComparator obc)
223 throws PrincipalException, SystemException {
224
225 TrashEntryList trashEntriesList = new TrashEntryList();
226
227 int entriesCount = trashEntryPersistence.countByGroupId(groupId);
228
229 boolean approximate = entriesCount > PropsValues.TRASH_SEARCH_LIMIT;
230
231 trashEntriesList.setApproximate(approximate);
232
233 List<TrashEntry> entries = trashEntryPersistence.findByGroupId(
234 groupId, 0, end + PropsValues.TRASH_SEARCH_LIMIT, obc);
235
236 List<TrashEntry> filteredEntries = new ArrayList<TrashEntry>();
237
238 PermissionChecker permissionChecker = getPermissionChecker();
239
240 for (TrashEntry entry : entries) {
241 String className = entry.getClassName();
242 long classPK = entry.getClassPK();
243
244 try {
245 TrashHandler trashHandler =
246 TrashHandlerRegistryUtil.getTrashHandler(className);
247
248 if (trashHandler.hasTrashPermission(
249 permissionChecker, 0, classPK, ActionKeys.VIEW)) {
250
251 filteredEntries.add(entry);
252 }
253 }
254 catch (Exception e) {
255 _log.error(e, e);
256 }
257 }
258
259 int total = filteredEntries.size();
260
261 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
262 start = 0;
263 end = total;
264 }
265
266 int[] startAndEnd = SearchPaginationUtil.calculateStartAndEnd(
267 start, end, total);
268
269 start = startAndEnd[0];
270 end = startAndEnd[1];
271
272 filteredEntries = filteredEntries.subList(start, end);
273
274 trashEntriesList.setArray(TrashEntrySoap.toSoapModels(filteredEntries));
275 trashEntriesList.setCount(total);
276
277 return trashEntriesList;
278 }
279
280
315 @Override
316 public void moveEntry(
317 String className, long classPK, long destinationContainerModelId,
318 ServiceContext serviceContext)
319 throws PortalException, SystemException {
320
321 PermissionChecker permissionChecker = getPermissionChecker();
322
323 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
324 className);
325
326 if (!trashHandler.hasTrashPermission(
327 permissionChecker, serviceContext.getScopeGroupId(),
328 destinationContainerModelId, TrashActionKeys.MOVE)) {
329
330 throw new TrashPermissionException(TrashPermissionException.MOVE);
331 }
332
333 if (trashHandler.isInTrash(classPK) &&
334 !trashHandler.hasTrashPermission(
335 permissionChecker, 0, classPK, TrashActionKeys.RESTORE)) {
336
337 throw new TrashPermissionException(
338 TrashPermissionException.RESTORE);
339 }
340
341 TrashEntry trashEntry = trashHandler.getTrashEntry(classPK);
342
343 if (trashEntry.isTrashEntry(className, classPK)) {
344 trashHandler.checkDuplicateTrashEntry(
345 trashEntry, destinationContainerModelId, StringPool.BLANK);
346 }
347 else {
348 trashHandler.checkDuplicateEntry(
349 classPK, destinationContainerModelId, StringPool.BLANK);
350 }
351
352 trashHandler.moveTrashEntry(
353 getUserId(), classPK, destinationContainerModelId, serviceContext);
354 }
355
356 @Override
357 public TrashEntry restoreEntry(long entryId)
358 throws PortalException, SystemException {
359
360 return restoreEntry(entryId, 0, null);
361 }
362
363
402 @Override
403 public TrashEntry restoreEntry(
404 long entryId, long overrideClassPK, String name)
405 throws PortalException, SystemException {
406
407 PermissionChecker permissionChecker = getPermissionChecker();
408
409 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
410
411 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
412 entry.getClassName());
413
414 if (!trashHandler.hasTrashPermission(
415 permissionChecker, 0, entry.getClassPK(),
416 TrashActionKeys.RESTORE)) {
417
418 throw new TrashPermissionException(
419 TrashPermissionException.RESTORE);
420 }
421
422 if (overrideClassPK > 0) {
423 if (!trashHandler.hasTrashPermission(
424 permissionChecker, 0, overrideClassPK,
425 TrashActionKeys.OVERWRITE)) {
426
427 throw new TrashPermissionException(
428 TrashPermissionException.RESTORE_OVERWRITE);
429 }
430
431 trashHandler.deleteTrashEntry(overrideClassPK);
432
433 trashHandler.checkDuplicateTrashEntry(
434 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, null);
435 }
436 else if (name != null) {
437 if (!trashHandler.hasTrashPermission(
438 permissionChecker, 0, entry.getClassPK(),
439 TrashActionKeys.RENAME)) {
440
441 throw new TrashPermissionException(
442 TrashPermissionException.RESTORE_RENAME);
443 }
444
445 trashHandler.checkDuplicateTrashEntry(
446 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, name);
447
448 trashHandler.updateTitle(entry.getClassPK(), name);
449 }
450
451 trashHandler.restoreTrashEntry(getUserId(), entry.getClassPK());
452
453 return entry;
454 }
455
456 protected void deleteEntry(TrashEntry entry)
457 throws PortalException, SystemException {
458
459 PermissionChecker permissionChecker = getPermissionChecker();
460
461 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
462 entry.getClassName());
463
464 if (!trashHandler.hasTrashPermission(
465 permissionChecker, 0, entry.getClassPK(), ActionKeys.DELETE)) {
466
467 throw new TrashPermissionException(TrashPermissionException.DELETE);
468 }
469
470 trashHandler.deleteTrashEntry(entry.getClassPK());
471 }
472
473 private static Log _log = LogFactoryUtil.getLog(
474 TrashEntryServiceImpl.class);
475
476 }