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
|
|
11
|
package eu.etaxonomy.cdm.api.service;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collection;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.hibernate.LockMode;
|
22
|
import org.hibernate.Session;
|
23
|
import org.springframework.context.ApplicationContext;
|
24
|
import org.springframework.context.ApplicationContextAware;
|
25
|
import org.springframework.security.access.AccessDeniedException;
|
26
|
import org.springframework.security.access.prepost.PreAuthorize;
|
27
|
import org.springframework.security.core.Authentication;
|
28
|
import org.springframework.transaction.annotation.Transactional;
|
29
|
|
30
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
31
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
32
|
import eu.etaxonomy.cdm.database.EvaluationFailedException;
|
33
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
34
|
import eu.etaxonomy.cdm.permission.CdmPermission;
|
35
|
import eu.etaxonomy.cdm.permission.CdmPermissionEvaluator;
|
36
|
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
|
37
|
import eu.etaxonomy.cdm.persistence.query.Grouping;
|
38
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
39
|
|
40
|
public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T>> implements IService<T>, ApplicationContextAware {
|
41
|
@SuppressWarnings("unused")
|
42
|
private static final Logger logger = Logger.getLogger(ServiceBase.class);
|
43
|
|
44
|
//flush after saving this number of objects
|
45
|
int flushAfterNo = 2000;
|
46
|
protected ApplicationContext appContext;
|
47
|
|
48
|
protected DAO dao;
|
49
|
|
50
|
@Transactional(readOnly = true)
|
51
|
public void lock(T t, LockMode lockMode) {
|
52
|
dao.lock(t, lockMode);
|
53
|
}
|
54
|
|
55
|
@Transactional(readOnly = true)
|
56
|
public void refresh(T t, LockMode lockMode, List<String> propertyPaths) {
|
57
|
dao.refresh(t, lockMode, propertyPaths);
|
58
|
}
|
59
|
|
60
|
@Transactional(readOnly = false)
|
61
|
public void clear() {
|
62
|
dao.clear();
|
63
|
}
|
64
|
|
65
|
@Transactional(readOnly = true)
|
66
|
public int count(Class<? extends T> clazz) {
|
67
|
return dao.count(clazz);
|
68
|
}
|
69
|
|
70
|
@Transactional(readOnly = false)
|
71
|
@PreAuthorize("hasRole('ALL.ADMIN') or hasPermission(#persistentObject, 'DELETE')")
|
72
|
public UUID delete(T persistentObject) {
|
73
|
return dao.delete(persistentObject);
|
74
|
}
|
75
|
|
76
|
@Transactional(readOnly = true)
|
77
|
public boolean exists(UUID uuid) {
|
78
|
return dao.exists(uuid);
|
79
|
}
|
80
|
|
81
|
@Transactional(readOnly = true)
|
82
|
public List<T> find(Set<UUID> uuidSet) {
|
83
|
return dao.findByUuid(uuidSet);
|
84
|
}
|
85
|
|
86
|
@Transactional(readOnly = true)
|
87
|
public T find(UUID uuid) {
|
88
|
return dao.findByUuid(uuid);
|
89
|
}
|
90
|
|
91
|
@Transactional(readOnly = true)
|
92
|
public Session getSession() {
|
93
|
return dao.getSession();
|
94
|
}
|
95
|
|
96
|
@Transactional(readOnly = true)
|
97
|
public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
|
98
|
return dao.group(clazz, limit, start, groups, propertyPaths);
|
99
|
}
|
100
|
|
101
|
@Transactional(readOnly = true)
|
102
|
public List<T> list(Class<? extends T> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
|
103
|
return dao.list(type,limit, start, orderHints,propertyPaths);
|
104
|
}
|
105
|
|
106
|
@Transactional(readOnly = true)
|
107
|
public T load(UUID uuid) {
|
108
|
return dao.load(uuid);
|
109
|
}
|
110
|
|
111
|
@Transactional(readOnly = true)
|
112
|
public T load(UUID uuid, List<String> propertyPaths){
|
113
|
return dao.load(uuid, propertyPaths);
|
114
|
}
|
115
|
|
116
|
@Transactional(readOnly = false)
|
117
|
public UUID merge(T newInstance) {
|
118
|
return dao.merge(newInstance);
|
119
|
}
|
120
|
|
121
|
@Transactional(readOnly = true)
|
122
|
public Pager<T> page(Class<? extends T> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
|
123
|
Integer numberOfResults = dao.count(type);
|
124
|
List<T> results = new ArrayList<T>();
|
125
|
pageNumber = pageNumber == null ? 0 : pageNumber;
|
126
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
127
|
Integer start = pageSize == null ? 0 : pageSize * pageNumber;
|
128
|
results = dao.list(type, pageSize, start, orderHints,propertyPaths);
|
129
|
}
|
130
|
return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
|
131
|
}
|
132
|
|
133
|
@Transactional(readOnly = true)
|
134
|
public UUID refresh(T persistentObject) {
|
135
|
return dao.refresh(persistentObject);
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* FIXME Candidate for harmonization
|
140
|
* is this method used, and if so, should it be exposed in the service layer?
|
141
|
* it seems a bit incongruous that we use an ORM to hide the fact that there is a
|
142
|
* database, then expose a method that talks about "rows" . . .
|
143
|
*/
|
144
|
@Transactional(readOnly = true)
|
145
|
public List<T> rows(String tableName, int limit, int start) {
|
146
|
return dao.rows(tableName, limit, start);
|
147
|
}
|
148
|
|
149
|
@Transactional(readOnly = false)
|
150
|
public Map<UUID, T> save(Collection<T> newInstances) {
|
151
|
return dao.saveAll(newInstances);
|
152
|
}
|
153
|
|
154
|
@Transactional(readOnly = false)
|
155
|
@PreAuthorize("hasRole('ALL.ADMIN') or hasPermission(#newInstance, 'CREATE')" )
|
156
|
public UUID save(T newInstance) {
|
157
|
return dao.save(newInstance);
|
158
|
}
|
159
|
|
160
|
@Transactional(readOnly = false)
|
161
|
//@PreAuthorize("hasRole('ALL.ADMIN') or hasPermission(#transientObject, 'UPDATE')")
|
162
|
public UUID saveOrUpdate(T transientObject) {
|
163
|
return dao.saveOrUpdate(transientObject);
|
164
|
}
|
165
|
|
166
|
@Transactional(readOnly = false)
|
167
|
@PreAuthorize("hasRole('ALL.ADMIN') or hasPermission(#transientInstances, 'UPDATE')")
|
168
|
public Map<UUID, T> saveOrUpdate(Collection<T> transientInstances) {
|
169
|
try{
|
170
|
return dao.saveOrUpdateAll(transientInstances);
|
171
|
}catch(EvaluationFailedException e){
|
172
|
e.printStackTrace();
|
173
|
return null;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/* (non-Javadoc)
|
178
|
* @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
|
179
|
*/
|
180
|
public void setApplicationContext(ApplicationContext appContext){
|
181
|
this.appContext = appContext;
|
182
|
}
|
183
|
|
184
|
|
185
|
protected abstract void setDao(DAO dao);
|
186
|
|
187
|
@Transactional(readOnly = false)
|
188
|
@PreAuthorize("hasRole('ALL.ADMIN') or hasPermission(#transientObject, 'UPDATE')")
|
189
|
public UUID update(T transientObject) {
|
190
|
return dao.update(transientObject);
|
191
|
}
|
192
|
|
193
|
@Transactional(readOnly = true)
|
194
|
public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
195
|
return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
|
196
|
}
|
197
|
|
198
|
@Transactional(readOnly = true)
|
199
|
public boolean hasPermission(Authentication authentication, T target, CdmPermission permission) {
|
200
|
CdmPermissionEvaluator permissionEvaluator = new CdmPermissionEvaluator();
|
201
|
return permissionEvaluator.hasPermission(authentication, target, permission);
|
202
|
|
203
|
}
|
204
|
}
|