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 try {
072 TrashHandler trashHandler =
073 TrashHandlerRegistryUtil.getTrashHandler(
074 entry.getClassName());
075
076 if (!trashHandler.hasTrashPermission(
077 permissionChecker, 0, entry.getClassPK(),
078 ActionKeys.VIEW)) {
079
080 continue;
081 }
082
083 deleteEntry(entry);
084 }
085 catch (TrashPermissionException tpe) {
086 throwTrashPermissionException = true;
087 }
088 catch (Exception e) {
089 _log.error(e, e);
090 }
091 }
092
093 if (throwTrashPermissionException) {
094 throw new TrashPermissionException(
095 TrashPermissionException.EMPTY_TRASH);
096 }
097 }
098
099
107 @Override
108 @Transactional(noRollbackFor = {TrashPermissionException.class})
109 public void deleteEntries(long[] entryIds) throws PortalException {
110 boolean throwTrashPermissionException = false;
111
112 for (long entryId : entryIds) {
113 try {
114 deleteEntry(entryId);
115 }
116 catch (TrashPermissionException tpe) {
117 throwTrashPermissionException = true;
118 }
119 }
120
121 if (throwTrashPermissionException) {
122 throw new TrashPermissionException(
123 TrashPermissionException.EMPTY_TRASH);
124 }
125 }
126
127
141 @Override
142 public void deleteEntry(long entryId) throws PortalException {
143 TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);
144
145 deleteEntry(entry);
146 }
147
148
163 @Override
164 public void deleteEntry(String className, long classPK)
165 throws PortalException {
166
167 TrashEntry entry = trashEntryLocalService.fetchEntry(
168 className, classPK);
169
170 if (entry == null) {
171 entry = new TrashEntryImpl();
172
173 entry.setClassName(className);
174 entry.setClassPK(classPK);
175 }
176
177 deleteEntry(entry);
178 }
179
180
187 @Override
188 public TrashEntryList getEntries(long groupId) throws PrincipalException {
189 return getEntries(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
190 }
191
192
205 @Override
206 public TrashEntryList getEntries(
207 long groupId, int start, int end, OrderByComparator<TrashEntry> obc)
208 throws PrincipalException {
209
210 TrashEntryList trashEntriesList = new TrashEntryList();
211
212 int entriesCount = trashEntryPersistence.countByGroupId(groupId);
213
214 boolean approximate = entriesCount > PropsValues.TRASH_SEARCH_LIMIT;
215
216 trashEntriesList.setApproximate(approximate);
217
218 List<TrashEntry> entries = trashEntryPersistence.findByGroupId(
219 groupId, 0, end + PropsValues.TRASH_SEARCH_LIMIT, obc);
220
221 List<TrashEntry> filteredEntries = new ArrayList<TrashEntry>();
222
223 PermissionChecker permissionChecker = getPermissionChecker();
224
225 for (TrashEntry entry : entries) {
226 String className = entry.getClassName();
227 long classPK = entry.getClassPK();
228
229 try {
230 TrashHandler trashHandler =
231 TrashHandlerRegistryUtil.getTrashHandler(className);
232
233 if (trashHandler.hasTrashPermission(
234 permissionChecker, 0, classPK, ActionKeys.VIEW)) {
235
236 filteredEntries.add(entry);
237 }
238 }
239 catch (Exception e) {
240 _log.error(e, e);
241 }
242 }
243
244 int total = filteredEntries.size();
245
246 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
247 start = 0;
248 end = total;
249 }
250
251 int[] startAndEnd = SearchPaginationUtil.calculateStartAndEnd(
252 start, end, total);
253
254 start = startAndEnd[0];
255 end = startAndEnd[1];
256
257 filteredEntries = filteredEntries.subList(start, end);
258
259 trashEntriesList.setArray(TrashEntrySoap.toSoapModels(filteredEntries));
260 trashEntriesList.setCount(total);
261
262 return trashEntriesList;
263 }
264
265
299 @Override
300 public void moveEntry(
301 String className, long classPK, long destinationContainerModelId,
302 ServiceContext serviceContext)
303 throws PortalException {
304
305 PermissionChecker permissionChecker = getPermissionChecker();
306
307 long scopeGroupId = 0;
308
309 if (serviceContext != null) {
310 scopeGroupId = serviceContext.getScopeGroupId();
311 }
312
313 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
314 className);
315
316 destinationContainerModelId =
317 trashHandler.getDestinationContainerModelId(
318 classPK, destinationContainerModelId);
319
320 if (!trashHandler.hasTrashPermission(
321 permissionChecker, scopeGroupId, destinationContainerModelId,
322 TrashActionKeys.MOVE)) {
323
324 throw new TrashPermissionException(TrashPermissionException.MOVE);
325 }
326
327 if (trashHandler.isInTrash(classPK) &&
328 !trashHandler.hasTrashPermission(
329 permissionChecker, 0, classPK, TrashActionKeys.RESTORE)) {
330
331 throw new TrashPermissionException(
332 TrashPermissionException.RESTORE);
333 }
334
335 TrashEntry trashEntry = trashHandler.getTrashEntry(classPK);
336
337 if (trashEntry.isTrashEntry(className, classPK)) {
338 trashHandler.checkRestorableEntry(
339 trashEntry, destinationContainerModelId, StringPool.BLANK);
340 }
341 else {
342 trashHandler.checkRestorableEntry(
343 classPK, destinationContainerModelId, StringPool.BLANK);
344 }
345
346 trashHandler.moveTrashEntry(
347 getUserId(), classPK, destinationContainerModelId, serviceContext);
348 }
349
350 @Override
351 public TrashEntry restoreEntry(long entryId) throws PortalException {
352 return restoreEntry(entryId, 0, null);
353 }
354
355
393 @Override
394 public TrashEntry restoreEntry(
395 long entryId, long overrideClassPK, String name)
396 throws PortalException {
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.checkRestorableEntry(
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.checkRestorableEntry(
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 @Override
448 public TrashEntry restoreEntry(String className, long classPK)
449 throws PortalException {
450
451 return restoreEntry(className, classPK, 0, null);
452 }
453
454 @Override
455 public TrashEntry restoreEntry(
456 String className, long classPK, long overrideClassPK, String name)
457 throws PortalException {
458
459 TrashEntry trashEntry = trashEntryPersistence.fetchByC_C(
460 classNameLocalService.getClassNameId(className), classPK);
461
462 if (trashEntry != null) {
463 return restoreEntry(trashEntry.getEntryId(), overrideClassPK, name);
464 }
465
466 return null;
467 }
468
469 protected void deleteEntry(TrashEntry entry) throws PortalException {
470 PermissionChecker permissionChecker = getPermissionChecker();
471
472 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
473 entry.getClassName());
474
475 if (!trashHandler.hasTrashPermission(
476 permissionChecker, 0, entry.getClassPK(), ActionKeys.DELETE)) {
477
478 throw new TrashPermissionException(TrashPermissionException.DELETE);
479 }
480
481 trashHandler.deleteTrashEntry(entry.getClassPK());
482 }
483
484 private static final Log _log = LogFactoryUtil.getLog(
485 TrashEntryServiceImpl.class);
486
487 }