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.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
061 @Override
062 @Transactional(noRollbackFor = {TrashPermissionException.class})
063 public void deleteEntries(long groupId) throws PortalException {
064 boolean throwTrashPermissionException = false;
065
066 List<TrashEntry> entries = trashEntryPersistence.findByGroupId(groupId);
067
068 PermissionChecker permissionChecker = getPermissionChecker();
069
070 for (TrashEntry entry : entries) {
071 entry = trashEntryPersistence.fetchByPrimaryKey(entry.getEntryId());
072
073 if (entry == null) {
074 continue;
075 }
076
077 try {
078 TrashHandler trashHandler =
079 TrashHandlerRegistryUtil.getTrashHandler(
080 entry.getClassName());
081
082 if (!trashHandler.hasTrashPermission(
083 permissionChecker, 0, entry.getClassPK(),
084 ActionKeys.VIEW)) {
085
086 continue;
087 }
088
089 deleteEntry(entry);
090 }
091 catch (TrashPermissionException tpe) {
092 throwTrashPermissionException = true;
093 }
094 catch (Exception e) {
095 _log.error(e, e);
096 }
097 }
098
099 if (throwTrashPermissionException) {
100 throw new TrashPermissionException(
101 TrashPermissionException.EMPTY_TRASH);
102 }
103 }
104
105
113 @Override
114 @Transactional(noRollbackFor = {TrashPermissionException.class})
115 public void deleteEntries(long[] entryIds) throws PortalException {
116 boolean throwTrashPermissionException = false;
117
118 for (long entryId : entryIds) {
119 try {
120 deleteEntry(entryId);
121 }
122 catch (TrashPermissionException tpe) {
123 throwTrashPermissionException = true;
124 }
125 }
126
127 if (throwTrashPermissionException) {
128 throw new TrashPermissionException(
129 TrashPermissionException.EMPTY_TRASH);
130 }
131 }
132
133
147 @Override
148 public void deleteEntry(long entryId) throws PortalException {
149 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
150
151 deleteEntry(entry);
152 }
153
154
169 @Override
170 public void deleteEntry(String className, long classPK)
171 throws PortalException {
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
193 @Override
194 public TrashEntryList getEntries(long groupId) throws PrincipalException {
195 return getEntries(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
196 }
197
198
211 @Override
212 public TrashEntryList getEntries(
213 long groupId, int start, int end, OrderByComparator<TrashEntry> obc)
214 throws PrincipalException {
215
216 TrashEntryList trashEntriesList = new TrashEntryList();
217
218 int entriesCount = trashEntryPersistence.countByGroupId(groupId);
219
220 boolean approximate = entriesCount > PropsValues.TRASH_SEARCH_LIMIT;
221
222 trashEntriesList.setApproximate(approximate);
223
224 List<TrashEntry> entries = trashEntryPersistence.findByGroupId(
225 groupId, 0, end + PropsValues.TRASH_SEARCH_LIMIT, obc);
226
227 List<TrashEntry> filteredEntries = new ArrayList<>();
228
229 PermissionChecker permissionChecker = getPermissionChecker();
230
231 for (TrashEntry entry : entries) {
232 String className = entry.getClassName();
233 long classPK = entry.getClassPK();
234
235 try {
236 TrashHandler trashHandler =
237 TrashHandlerRegistryUtil.getTrashHandler(className);
238
239 if (trashHandler.hasTrashPermission(
240 permissionChecker, 0, classPK, ActionKeys.VIEW)) {
241
242 filteredEntries.add(entry);
243 }
244 }
245 catch (Exception e) {
246 _log.error(e, e);
247 }
248 }
249
250 int total = filteredEntries.size();
251
252 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
253 start = 0;
254 end = total;
255 }
256
257 int[] startAndEnd = SearchPaginationUtil.calculateStartAndEnd(
258 start, end, total);
259
260 start = startAndEnd[0];
261 end = startAndEnd[1];
262
263 filteredEntries = filteredEntries.subList(start, end);
264
265 trashEntriesList.setArray(TrashEntrySoap.toSoapModels(filteredEntries));
266 trashEntriesList.setCount(total);
267
268 return trashEntriesList;
269 }
270
271
305 @Override
306 public void moveEntry(
307 String className, long classPK, long destinationContainerModelId,
308 ServiceContext serviceContext)
309 throws PortalException {
310
311 PermissionChecker permissionChecker = getPermissionChecker();
312
313 long scopeGroupId = 0;
314
315 if (serviceContext != null) {
316 scopeGroupId = serviceContext.getScopeGroupId();
317 }
318
319 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
320 className);
321
322 destinationContainerModelId =
323 trashHandler.getDestinationContainerModelId(
324 classPK, destinationContainerModelId);
325
326 if (!trashHandler.hasTrashPermission(
327 permissionChecker, scopeGroupId, destinationContainerModelId,
328 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.checkRestorableEntry(
345 trashEntry, destinationContainerModelId, StringPool.BLANK);
346 }
347 else {
348 trashHandler.checkRestorableEntry(
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) throws PortalException {
358 return restoreEntry(entryId, 0, null);
359 }
360
361
399 @Override
400 public TrashEntry restoreEntry(
401 long entryId, long overrideClassPK, String name)
402 throws PortalException {
403
404 PermissionChecker permissionChecker = getPermissionChecker();
405
406 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
407
408 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
409 entry.getClassName());
410
411 if (!trashHandler.hasTrashPermission(
412 permissionChecker, 0, entry.getClassPK(),
413 TrashActionKeys.RESTORE)) {
414
415 throw new TrashPermissionException(
416 TrashPermissionException.RESTORE);
417 }
418
419 if (overrideClassPK > 0) {
420 if (!trashHandler.hasTrashPermission(
421 permissionChecker, 0, overrideClassPK,
422 TrashActionKeys.OVERWRITE)) {
423
424 throw new TrashPermissionException(
425 TrashPermissionException.RESTORE_OVERWRITE);
426 }
427
428 trashHandler.deleteTrashEntry(overrideClassPK);
429
430 trashHandler.checkRestorableEntry(
431 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, null);
432 }
433 else if (name != null) {
434 if (!trashHandler.hasTrashPermission(
435 permissionChecker, 0, entry.getClassPK(),
436 TrashActionKeys.RENAME)) {
437
438 throw new TrashPermissionException(
439 TrashPermissionException.RESTORE_RENAME);
440 }
441
442 trashHandler.checkRestorableEntry(
443 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, name);
444
445 trashHandler.updateTitle(entry.getClassPK(), name);
446 }
447
448 trashHandler.restoreTrashEntry(getUserId(), entry.getClassPK());
449
450 return entry;
451 }
452
453 @Override
454 public TrashEntry restoreEntry(String className, long classPK)
455 throws PortalException {
456
457 return restoreEntry(className, classPK, 0, null);
458 }
459
460 @Override
461 public TrashEntry restoreEntry(
462 String className, long classPK, long overrideClassPK, String name)
463 throws PortalException {
464
465 TrashEntry trashEntry = trashEntryPersistence.fetchByC_C(
466 classNameLocalService.getClassNameId(className), classPK);
467
468 if (trashEntry != null) {
469 return restoreEntry(trashEntry.getEntryId(), overrideClassPK, name);
470 }
471
472 return null;
473 }
474
475 protected void deleteEntry(TrashEntry entry) throws PortalException {
476 PermissionChecker permissionChecker = getPermissionChecker();
477
478 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
479 entry.getClassName());
480
481 if (!trashHandler.hasTrashPermission(
482 permissionChecker, 0, entry.getClassPK(), ActionKeys.DELETE)) {
483
484 throw new TrashPermissionException(TrashPermissionException.DELETE);
485 }
486
487 trashHandler.deleteTrashEntry(entry.getClassPK());
488 }
489
490 private static final Log _log = LogFactoryUtil.getLog(
491 TrashEntryServiceImpl.class);
492
493 }