1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.api.service;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Iterator;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.hibernate.envers.query.criteria.AuditCriterion;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.transaction.annotation.Transactional;
|
20
|
|
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.agent.AgentBase;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
28
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
29
|
import eu.etaxonomy.cdm.model.view.AuditEventRecord;
|
30
|
import eu.etaxonomy.cdm.persistence.dao.common.AuditEventSort;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.common.IVersionableDao;
|
32
|
|
33
|
public abstract class VersionableServiceBase<T extends VersionableEntity, DAO extends IVersionableDao<T>> extends ServiceBase<T,DAO> implements IVersionableService<T> {
|
34
|
@Autowired
|
35
|
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
|
Integer numberOfResults = dao.countAuditEvents(t, sort);
|
39
|
|
40
|
List<AuditEventRecord<T>> results = new ArrayList<AuditEventRecord<T>>();
|
41
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
42
|
results = dao.getAuditEvents(t, pageSize, pageNumber, sort,propertyPaths);
|
43
|
}
|
44
|
|
45
|
return new DefaultPagerImpl<AuditEventRecord<T>>(pageNumber, numberOfResults, pageSize, results);
|
46
|
}
|
47
|
|
48
|
@Transactional(readOnly = true)
|
49
|
public AuditEventRecord<T> getNextAuditEvent(T t) {
|
50
|
return dao.getNextAuditEvent(t);
|
51
|
}
|
52
|
|
53
|
@Transactional(readOnly = true)
|
54
|
public AuditEventRecord<T> getPreviousAuditEvent(T t) {
|
55
|
return dao.getPreviousAuditEvent(t);
|
56
|
}
|
57
|
|
58
|
@Transactional(readOnly = true)
|
59
|
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) {
|
60
|
Integer numberOfResults = dao.countAuditEvents(clazz, from, to, criteria);
|
61
|
|
62
|
List<AuditEventRecord<T>> results = new ArrayList<AuditEventRecord<T>>();
|
63
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
64
|
results = dao.getAuditEvents(clazz,from,to,criteria, pageSize, pageNumber, sort,propertyPaths);
|
65
|
}
|
66
|
|
67
|
return new DefaultPagerImpl<AuditEventRecord<T>>(pageNumber, numberOfResults, pageSize, results);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* the basic isDeletable method return false if the object is referenced from any other object.
|
72
|
*/
|
73
|
|
74
|
@Override
|
75
|
public DeleteResult isDeletable(T base, DeleteConfiguratorBase config){
|
76
|
DeleteResult result = new DeleteResult();
|
77
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(base);
|
78
|
if (references != null){
|
79
|
result.addRelatedObjects(references);
|
80
|
Iterator<CdmBase> iterator = references.iterator();
|
81
|
CdmBase ref;
|
82
|
while (iterator.hasNext()){
|
83
|
ref = iterator.next();
|
84
|
String message = "An object of " + ref.getClass().getName() + " with ID " + ref.getId() + " is referencing the object" ;
|
85
|
result.addException(new ReferencedObjectUndeletableException(message));
|
86
|
result.setAbort();
|
87
|
}
|
88
|
}
|
89
|
return result;
|
90
|
}
|
91
|
|
92
|
|
93
|
}
|