1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.api.service;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collection;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.hibernate.LockOptions;
|
21
|
import org.hibernate.Session;
|
22
|
import org.hibernate.criterion.Criterion;
|
23
|
import org.springframework.context.ApplicationContext;
|
24
|
import org.springframework.context.ApplicationContextAware;
|
25
|
import org.springframework.dao.DataAccessException;
|
26
|
import org.springframework.transaction.annotation.Transactional;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
29
|
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
|
30
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
31
|
import eu.etaxonomy.cdm.exception.UnpublishedException;
|
32
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
33
|
import eu.etaxonomy.cdm.model.common.IPublishable;
|
34
|
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
|
35
|
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
|
36
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.DaoBase;
|
37
|
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
|
38
|
import eu.etaxonomy.cdm.persistence.query.Grouping;
|
39
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
40
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
41
|
|
42
|
public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T>>
|
43
|
implements IService<T>, ApplicationContextAware {
|
44
|
|
45
|
@SuppressWarnings("unused")
|
46
|
private static final Logger logger = Logger.getLogger(ServiceBase.class);
|
47
|
|
48
|
protected ApplicationContext appContext;
|
49
|
|
50
|
public final static boolean NO_UNPUBLISHED = DaoBase.NO_UNPUBLISHED; //constant for unpublished
|
51
|
public final static boolean INCLUDE_UNPUBLISHED = DaoBase.INCLUDE_UNPUBLISHED; //constant for unpublished
|
52
|
|
53
|
protected DAO dao;
|
54
|
|
55
|
@Override
|
56
|
@Transactional(readOnly = true)
|
57
|
public void lock(T t, LockOptions lockOptions) {
|
58
|
dao.lock(t, lockOptions);
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
@Transactional(readOnly = true)
|
63
|
public void refresh(T t, LockOptions lockOptions, List<String> propertyPaths) {
|
64
|
dao.refresh(t, lockOptions, propertyPaths);
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
@Transactional(readOnly = false)
|
69
|
public void clear() {
|
70
|
dao.clear();
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
@Transactional(readOnly = true)
|
75
|
public int count(Class<? extends T> clazz) {
|
76
|
return Long.valueOf(dao.count(clazz)).intValue();
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
@Transactional(readOnly = false)
|
81
|
public DeleteResult delete(UUID persistentObjectUUID) {
|
82
|
T persistentObject = dao.findByUuid(persistentObjectUUID);
|
83
|
return delete(persistentObject);
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
@Transactional(readOnly = false)
|
88
|
public DeleteResult delete(Collection<UUID> persistentObjectUUIDs) {
|
89
|
DeleteResult result = new DeleteResult();
|
90
|
for(UUID persistentObjectUUID : persistentObjectUUIDs) {
|
91
|
T persistentObject = dao.findByUuid(persistentObjectUUID);
|
92
|
DeleteResult dr = delete(persistentObject);
|
93
|
result.includeResult(dr);
|
94
|
}
|
95
|
return result;
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
@Transactional(readOnly = false)
|
100
|
public DeleteResult delete(T persistentObject) {
|
101
|
DeleteResult result = new DeleteResult();
|
102
|
try{
|
103
|
dao.delete(persistentObject);
|
104
|
result.addDeletedObject(persistentObject);
|
105
|
} catch(DataAccessException e){
|
106
|
result.setError();
|
107
|
result.addException(e);
|
108
|
}
|
109
|
return result;
|
110
|
}
|
111
|
|
112
|
|
113
|
|
114
|
@Override
|
115
|
@Transactional(readOnly = true)
|
116
|
public boolean exists(UUID uuid) {
|
117
|
return dao.exists(uuid);
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
@Transactional(readOnly = true)
|
122
|
public List<T> find(Set<UUID> uuidSet) {
|
123
|
return dao.list(uuidSet, null, null, null, null);
|
124
|
}
|
125
|
|
126
|
|
127
|
@Override
|
128
|
@Transactional(readOnly = true)
|
129
|
public <S extends T> List<S> find(Class<S> clazz, Set<UUID> uuidSet) {
|
130
|
return dao.list(clazz, uuidSet, null, null, null, null);
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
@Transactional(readOnly = true)
|
135
|
public List<T> findById(Set<Integer> idSet) { //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
|
136
|
return dao.loadList(idSet, null);
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
@Transactional(readOnly = true)
|
141
|
public List<T> loadByIds(List<Integer> idList, List<String> propertyPaths){
|
142
|
return dao.loadList(idList, propertyPaths);
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
@Transactional(readOnly = true)
|
147
|
public T find(UUID uuid) {
|
148
|
return uuid == null ? null : dao.findByUuid(uuid);
|
149
|
}
|
150
|
|
151
|
@Override
|
152
|
@Transactional(readOnly = true)
|
153
|
public T findWithoutFlush(UUID uuid) {
|
154
|
return uuid == null ? null : dao.findByUuidWithoutFlush(uuid);
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
@Transactional(readOnly = true)
|
159
|
public T find(int id) {
|
160
|
return dao.findById(id);
|
161
|
}
|
162
|
|
163
|
@Override
|
164
|
@Transactional(readOnly = true)
|
165
|
public Session getSession() {
|
166
|
return dao.getSession();
|
167
|
}
|
168
|
|
169
|
@Override
|
170
|
@Transactional(readOnly = true)
|
171
|
public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
|
172
|
return dao.group(clazz, limit, start, groups, propertyPaths);
|
173
|
}
|
174
|
|
175
|
@Override
|
176
|
@Transactional(readOnly = true)
|
177
|
public <S extends T> List<S> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
|
178
|
return dao.list(type,limit, start, orderHints, propertyPaths);
|
179
|
}
|
180
|
|
181
|
@Override
|
182
|
@Transactional(readOnly = true)
|
183
|
public T load(UUID uuid) {
|
184
|
return uuid == null ? null : dao.load(uuid);
|
185
|
}
|
186
|
|
187
|
@Override
|
188
|
@Transactional(readOnly = true)
|
189
|
public T loadWithUpdate(UUID uuid) {
|
190
|
return load(uuid);
|
191
|
}
|
192
|
|
193
|
@Override
|
194
|
@Transactional(readOnly = true)
|
195
|
public T load(int id, List<String> propertyPaths) {
|
196
|
return dao.load(id, propertyPaths);
|
197
|
}
|
198
|
|
199
|
@Override
|
200
|
@Transactional(readOnly = true)
|
201
|
public T load(UUID uuid, List<String> propertyPaths){
|
202
|
return uuid == null ? null : dao.load(uuid, propertyPaths);
|
203
|
}
|
204
|
|
205
|
@Override
|
206
|
@Transactional(readOnly = true)
|
207
|
public List<T> load(List<UUID> uuids, List<String> propertyPaths){
|
208
|
if(uuids == null) {
|
209
|
return null;
|
210
|
}
|
211
|
|
212
|
List<T> entities = new ArrayList<>();
|
213
|
for(UUID uuid : uuids) {
|
214
|
entities.add(uuid == null ? null : dao.load(uuid, propertyPaths));
|
215
|
}
|
216
|
return entities;
|
217
|
}
|
218
|
|
219
|
@Override
|
220
|
@Transactional(readOnly = false)
|
221
|
public T merge(T newInstance) {
|
222
|
return dao.merge(newInstance);
|
223
|
}
|
224
|
|
225
|
@Override
|
226
|
@Transactional(readOnly = false)
|
227
|
public MergeResult<T> merge(T newInstance, boolean returnTransientEntity) {
|
228
|
return dao.merge(newInstance, returnTransientEntity);
|
229
|
}
|
230
|
|
231
|
@Override
|
232
|
@Transactional(readOnly = false)
|
233
|
public List<T> merge(List<T> detachedObjects) {
|
234
|
List<T> mergedObjects = new ArrayList<T>();
|
235
|
for(T obj : detachedObjects) {
|
236
|
mergedObjects.add(dao.merge(obj));
|
237
|
}
|
238
|
return mergedObjects;
|
239
|
}
|
240
|
|
241
|
@Override
|
242
|
@Transactional(readOnly = false)
|
243
|
public List<MergeResult<T>> merge(List<T> detachedObjects, boolean returnTransientEntity) {
|
244
|
List<MergeResult<T>> mergedObjects = new ArrayList<MergeResult<T>>();
|
245
|
for(T obj : detachedObjects) {
|
246
|
mergedObjects.add(dao.merge(obj, returnTransientEntity));
|
247
|
}
|
248
|
return mergedObjects;
|
249
|
}
|
250
|
|
251
|
@Override
|
252
|
@Transactional(readOnly = true)
|
253
|
public <S extends T> Pager<S> page(Class<S> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
|
254
|
Long numberOfResults = dao.count(type);
|
255
|
List<S> results = new ArrayList<>();
|
256
|
pageNumber = pageNumber == null ? 0 : pageNumber;
|
257
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
258
|
Integer start = pageSize == null ? 0 : pageSize * pageNumber;
|
259
|
results = dao.list(type, pageSize, start, orderHints, propertyPaths);
|
260
|
}
|
261
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
262
|
}
|
263
|
|
264
|
@Override
|
265
|
@Transactional(readOnly = true)
|
266
|
public UUID refresh(T persistentObject) {
|
267
|
return dao.refresh(persistentObject);
|
268
|
}
|
269
|
|
270
|
@Override
|
271
|
@Transactional(readOnly = false)
|
272
|
public Map<UUID, T> save(Collection<T> newInstances) {
|
273
|
return dao.saveAll(newInstances);
|
274
|
}
|
275
|
|
276
|
@Override
|
277
|
@Transactional(readOnly = false)
|
278
|
public T save(T newInstance) {
|
279
|
return dao.save(newInstance);
|
280
|
}
|
281
|
|
282
|
@Override
|
283
|
@Transactional(readOnly = false)
|
284
|
public UUID saveOrUpdate(T transientObject) {
|
285
|
return dao.saveOrUpdate(transientObject);
|
286
|
}
|
287
|
|
288
|
@Override
|
289
|
@Transactional(readOnly = false)
|
290
|
public Map<UUID, T> saveOrUpdate(Collection<T> transientInstances) {
|
291
|
return dao.saveOrUpdateAll(transientInstances);
|
292
|
}
|
293
|
|
294
|
@Override
|
295
|
public void setApplicationContext(ApplicationContext appContext){
|
296
|
this.appContext = appContext;
|
297
|
}
|
298
|
|
299
|
|
300
|
protected abstract void setDao(DAO dao);
|
301
|
|
302
|
@Override
|
303
|
@Transactional(readOnly = false)
|
304
|
public UUID update(T transientObject) {
|
305
|
return dao.update(transientObject);
|
306
|
}
|
307
|
|
308
|
@Override
|
309
|
@Transactional(readOnly = true)
|
310
|
public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
311
|
return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
|
312
|
}
|
313
|
|
314
|
@Override
|
315
|
@Transactional(readOnly = true)
|
316
|
public <S extends T> Pager<S> page(Class<S> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths){
|
317
|
|
318
|
List<S> records;
|
319
|
long resultSize = dao.countByParam(clazz, param, queryString, matchmode, criteria);
|
320
|
if(AbstractPagerImpl.hasResultsInRange(resultSize, pageIndex, pageSize)){
|
321
|
records = dao.findByParam(clazz, param, queryString, matchmode, criteria, pageSize, pageIndex, orderHints, propertyPaths);
|
322
|
} else {
|
323
|
records = new ArrayList<>();
|
324
|
}
|
325
|
return new DefaultPagerImpl<>(pageIndex, resultSize, pageSize, records);
|
326
|
}
|
327
|
|
328
|
|
329
|
@Override
|
330
|
@Transactional(readOnly = true)
|
331
|
public <S extends T> Pager<S> pageByRestrictions(Class<S> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths){
|
332
|
|
333
|
List<S> records;
|
334
|
long resultSize = dao.countByParamWithRestrictions(clazz, param, queryString, matchmode, restrictions);
|
335
|
if(AbstractPagerImpl.hasResultsInRange(resultSize, pageIndex, pageSize)){
|
336
|
records = dao.findByParamWithRestrictions(clazz, param, queryString, matchmode, restrictions, pageSize, pageIndex, orderHints, propertyPaths);
|
337
|
} else {
|
338
|
records = new ArrayList<>();
|
339
|
}
|
340
|
Pager<S> pager = new DefaultPagerImpl<>(pageIndex, resultSize, pageSize, records);
|
341
|
return pager;
|
342
|
}
|
343
|
|
344
|
|
345
|
/**
|
346
|
* Throws an exception if the publishable entity should not be published.
|
347
|
* @param publishable the publishable entity
|
348
|
* @param includeUnpublished should publish be checked
|
349
|
* @param message the error message to include
|
350
|
* @throws UnpublishedException thrown if entity is not public and unpublished should not be included
|
351
|
*/
|
352
|
protected void checkPublished(IPublishable publishable, boolean includeUnpublished, String message) throws UnpublishedException {
|
353
|
if (!(includeUnpublished || publishable.isPublish())){
|
354
|
throw new UnpublishedException("Access denied. "+ message);
|
355
|
}
|
356
|
}
|
357
|
|
358
|
|
359
|
|
360
|
}
|