2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.api
.service
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Iterator
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.hibernate
.envers
.query
.criteria
.AuditCriterion
;
18 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
19 import org
.springframework
.transaction
.annotation
.Transactional
;
21 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
22 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
23 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
24 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.VersionableEntity
;
27 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
28 import eu
.etaxonomy
.cdm
.model
.view
.AuditEventRecord
;
29 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.AuditEventSort
;
30 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IVersionableDao
;
32 public abstract class VersionableServiceBase
<T
extends VersionableEntity
, DAO
extends IVersionableDao
<T
>> extends ServiceBase
<T
,DAO
> implements IVersionableService
<T
> {
34 protected ICommonService commonService
;
36 @Transactional(readOnly
= true)
37 public Pager
<AuditEventRecord
<T
>> pageAuditEvents(T t
, Integer pageSize
, Integer pageNumber
, AuditEventSort sort
, List
<String
> propertyPaths
) {
38 long numberOfResults
= dao
.countAuditEvents(t
, sort
);
40 List
<AuditEventRecord
<T
>> results
= new ArrayList
<>();
41 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
42 results
= dao
.getAuditEvents(t
, pageSize
, pageNumber
, sort
,propertyPaths
);
45 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
49 @Transactional(readOnly
= true)
50 public AuditEventRecord
<T
> getNextAuditEvent(T t
) {
51 return dao
.getNextAuditEvent(t
);
55 @Transactional(readOnly
= true)
56 public AuditEventRecord
<T
> getPreviousAuditEvent(T t
) {
57 return dao
.getPreviousAuditEvent(t
);
61 @Transactional(readOnly
= true)
62 public Pager
<AuditEventRecord
<T
>> pageAuditEvents(Class
<?
extends T
> clazz
,AuditEvent from
,AuditEvent to
, List
<AuditCriterion
> criteria
, Integer pageSize
, Integer pageNumber
, AuditEventSort sort
,List
<String
> propertyPaths
) {
63 long numberOfResults
= dao
.countAuditEvents(clazz
, from
, to
, criteria
);
65 List
<AuditEventRecord
<T
>> results
= new ArrayList
<>();
66 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
67 results
= dao
.getAuditEvents(clazz
,from
,to
,criteria
, pageSize
, pageNumber
, sort
,propertyPaths
);
70 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
74 * the basic isDeletable method return false if the object is referenced from any other object.
78 public DeleteResult
isDeletable(UUID baseUUID
, DeleteConfiguratorBase config
){
79 DeleteResult result
= new DeleteResult();
80 T base
= this.find(baseUUID
);
81 Set
<CdmBase
> references
= commonService
.getReferencingObjectsForDeletion(base
);
82 if (references
!= null){
83 result
.addRelatedObjects(references
);
84 Iterator
<CdmBase
> iterator
= references
.iterator();
86 while (iterator
.hasNext()){
87 ref
= iterator
.next();
88 String message
= "An object of " + ref
.getClass().getName() + " with ID " + ref
.getId() + " is referencing the object" ;
89 result
.addException(new ReferencedObjectUndeletableException(message
));