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