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 long scopeGroupId = 0;
324
325 if (serviceContext != null) {
326 scopeGroupId = serviceContext.getScopeGroupId();
327 }
328
329 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
330 className);
331
332 if (!trashHandler.hasTrashPermission(
333 permissionChecker, scopeGroupId, destinationContainerModelId,
334 TrashActionKeys.MOVE)) {
335
336 throw new TrashPermissionException(TrashPermissionException.MOVE);
337 }
338
339 if (trashHandler.isInTrash(classPK) &&
340 !trashHandler.hasTrashPermission(
341 permissionChecker, 0, classPK, TrashActionKeys.RESTORE)) {
342
343 throw new TrashPermissionException(
344 TrashPermissionException.RESTORE);
345 }
346
347 TrashEntry trashEntry = trashHandler.getTrashEntry(classPK);
348
349 if (trashEntry.isTrashEntry(className, classPK)) {
350 trashHandler.checkDuplicateTrashEntry(
351 trashEntry, destinationContainerModelId, StringPool.BLANK);
352 }
353 else {
354 trashHandler.checkDuplicateEntry(
355 classPK, destinationContainerModelId, StringPool.BLANK);
356 }
357
358 trashHandler.moveTrashEntry(
359 getUserId(), classPK, destinationContainerModelId, serviceContext);
360 }
361
362 @Override
363 public TrashEntry restoreEntry(long entryId)
364 throws PortalException, SystemException {
365
366 return restoreEntry(entryId, 0, null);
367 }
368
369
408 @Override
409 public TrashEntry restoreEntry(
410 long entryId, long overrideClassPK, String name)
411 throws PortalException, SystemException {
412
413 PermissionChecker permissionChecker = getPermissionChecker();
414
415 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
416
417 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
418 entry.getClassName());
419
420 if (!trashHandler.hasTrashPermission(
421 permissionChecker, 0, entry.getClassPK(),
422 TrashActionKeys.RESTORE)) {
423
424 throw new TrashPermissionException(
425 TrashPermissionException.RESTORE);
426 }
427
428 if (overrideClassPK > 0) {
429 if (!trashHandler.hasTrashPermission(
430 permissionChecker, 0, overrideClassPK,
431 TrashActionKeys.OVERWRITE)) {
432
433 throw new TrashPermissionException(
434 TrashPermissionException.RESTORE_OVERWRITE);
435 }
436
437 trashHandler.deleteTrashEntry(overrideClassPK);
438
439 trashHandler.checkDuplicateTrashEntry(
440 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, null);
441 }
442 else if (name != null) {
443 if (!trashHandler.hasTrashPermission(
444 permissionChecker, 0, entry.getClassPK(),
445 TrashActionKeys.RENAME)) {
446
447 throw new TrashPermissionException(
448 TrashPermissionException.RESTORE_RENAME);
449 }
450
451 trashHandler.checkDuplicateTrashEntry(
452 entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, name);
453
454 trashHandler.updateTitle(entry.getClassPK(), name);
455 }
456
457 trashHandler.restoreTrashEntry(getUserId(), entry.getClassPK());
458
459 return entry;
460 }
461
462 protected void deleteEntry(TrashEntry entry)
463 throws PortalException, SystemException {
464
465 PermissionChecker permissionChecker = getPermissionChecker();
466
467 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
468 entry.getClassName());
469
470 if (!trashHandler.hasTrashPermission(
471 permissionChecker, 0, entry.getClassPK(), ActionKeys.DELETE)) {
472
473 throw new TrashPermissionException(TrashPermissionException.DELETE);
474 }
475
476 trashHandler.deleteTrashEntry(entry.getClassPK());
477 }
478
479 private static Log _log = LogFactoryUtil.getLog(
480 TrashEntryServiceImpl.class);
481
482 }