1
|
/**
|
2
|
* Copyright (C) 2009 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.persistence.dao.hibernate.common;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.hibernate.criterion.Criterion;
|
19
|
import org.hibernate.envers.AuditReader;
|
20
|
import org.hibernate.envers.AuditReaderFactory;
|
21
|
import org.hibernate.envers.query.AuditEntity;
|
22
|
import org.hibernate.envers.query.AuditQuery;
|
23
|
import org.hibernate.envers.query.criteria.AuditCriterion;
|
24
|
|
25
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
26
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
27
|
import eu.etaxonomy.cdm.model.view.AuditEventRecord;
|
28
|
import eu.etaxonomy.cdm.model.view.AuditEventRecordImpl;
|
29
|
import eu.etaxonomy.cdm.model.view.context.AuditEventContext;
|
30
|
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.common.AuditEventSort;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.common.IVersionableDao;
|
33
|
import eu.etaxonomy.cdm.persistence.dao.common.OperationNotSupportedInPriorViewException;
|
34
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
35
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
36
|
|
37
|
public abstract class VersionableDaoBase<T extends VersionableEntity> extends CdmEntityDaoBase<T> implements IVersionableDao<T> {
|
38
|
@SuppressWarnings("unused")
|
39
|
private static final Logger logger = Logger.getLogger(VersionableDaoBase.class);
|
40
|
|
41
|
protected AuditReader getAuditReader() {
|
42
|
return AuditReaderFactory.get(getSession());
|
43
|
}
|
44
|
|
45
|
public VersionableDaoBase(Class<T> type) {
|
46
|
super(type);
|
47
|
}
|
48
|
|
49
|
protected AuditEvent getAuditEventFromContext() {
|
50
|
AuditEventContext auditEventContext = AuditEventContextHolder.getContext();
|
51
|
|
52
|
AuditEvent auditEvent = auditEventContext.getAuditEvent();
|
53
|
if(auditEvent != null) {
|
54
|
return auditEvent;
|
55
|
} else {
|
56
|
return AuditEvent.CURRENT_VIEW;
|
57
|
}
|
58
|
}
|
59
|
|
60
|
protected void checkNotInPriorView(String message) {
|
61
|
AuditEvent auditEvent = getAuditEventFromContext();
|
62
|
if(!auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
63
|
throw new OperationNotSupportedInPriorViewException(message);
|
64
|
}
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public T findByUuid(UUID uuid) {
|
69
|
AuditEvent auditEvent = getAuditEventFromContext();
|
70
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
71
|
return super.findByUuid(uuid);
|
72
|
} else {
|
73
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
74
|
query.add(AuditEntity.property("uuid").eq(uuid));
|
75
|
// TODO initialize bits
|
76
|
return (T)query.getSingleResult();
|
77
|
}
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
protected List<T> findByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
82
|
checkNotInPriorView("IdentifiableDaoBase.findByParam(Class<? extends T> clazz, String queryString, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
83
|
return super.findByParam(clazz, param, queryString, matchmode, criterion, pageSize, pageNumber, orderHints, propertyPaths);
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
public T load(UUID uuid) {
|
88
|
AuditEvent auditEvent = getAuditEventFromContext();
|
89
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
90
|
return super.load(uuid);
|
91
|
} else {
|
92
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
93
|
query.add(AuditEntity.property("uuid").eq(uuid));
|
94
|
T t = (T)query.getSingleResult();
|
95
|
defaultBeanInitializer.load(t);
|
96
|
return t;
|
97
|
}
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public T load(UUID uuid, List<String> propertyPaths) {
|
102
|
AuditEvent auditEvent = getAuditEventFromContext();
|
103
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
104
|
return super.load(uuid,propertyPaths);
|
105
|
} else {
|
106
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
107
|
query.add(AuditEntity.property("uuid").eq(uuid));
|
108
|
T t = (T)query.getSingleResult();
|
109
|
defaultBeanInitializer.initialize(t, propertyPaths);
|
110
|
return t;
|
111
|
}
|
112
|
}
|
113
|
|
114
|
|
115
|
@Override
|
116
|
public Boolean exists(UUID uuid) {
|
117
|
AuditEvent auditEvent = getAuditEventFromContext();
|
118
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
119
|
return super.exists(uuid);
|
120
|
} else {
|
121
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
122
|
query.add(AuditEntity.property("uuid").eq(uuid));
|
123
|
return null != (T)query.getSingleResult();
|
124
|
}
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public int count() {
|
129
|
AuditEvent auditEvent = getAuditEventFromContext();
|
130
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
131
|
return super.count();
|
132
|
} else {
|
133
|
return this.count(null);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
public int count(Class<? extends T> clazz) {
|
139
|
AuditEvent auditEvent = getAuditEventFromContext();
|
140
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
141
|
return super.count(clazz);
|
142
|
} else {
|
143
|
AuditQuery query = null;
|
144
|
if(clazz == null) {
|
145
|
query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
146
|
} else {
|
147
|
query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
|
148
|
}
|
149
|
|
150
|
query.addProjection(AuditEntity.id().count("id"));
|
151
|
return ((Long)query.getSingleResult()).intValue();
|
152
|
}
|
153
|
}
|
154
|
|
155
|
@Override
|
156
|
public List<T> list(Integer limit, Integer start) {
|
157
|
AuditEvent auditEvent = getAuditEventFromContext();
|
158
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
159
|
return super.list(limit, start);
|
160
|
} else {
|
161
|
return this.list(null, limit, start);
|
162
|
}
|
163
|
}
|
164
|
|
165
|
@Override
|
166
|
public List<T> list(Class<? extends T> type, Integer limit, Integer start) {
|
167
|
AuditEvent auditEvent = getAuditEventFromContext();
|
168
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
169
|
return super.list(type,limit, start);
|
170
|
} else {
|
171
|
return this.list(type, limit, start, null,null);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
@Override
|
176
|
public List<T> list(Class<? extends T> clazz, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
177
|
AuditEvent auditEvent = getAuditEventFromContext();
|
178
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
179
|
return super.list(clazz, limit, start, orderHints, propertyPaths);
|
180
|
} else {
|
181
|
AuditQuery query = null;
|
182
|
|
183
|
if(clazz == null) {
|
184
|
query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
185
|
} else {
|
186
|
query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
|
187
|
}
|
188
|
|
189
|
addOrder(query,orderHints);
|
190
|
|
191
|
if(limit != null) {
|
192
|
query.setMaxResults(limit);
|
193
|
query.setFirstResult(start);
|
194
|
}
|
195
|
|
196
|
List<T> result = (List<T>)query.getResultList();
|
197
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
198
|
return result;
|
199
|
}
|
200
|
}
|
201
|
|
202
|
protected void addOrder(AuditQuery query, List<OrderHint> orderHints) {
|
203
|
if(orderHints != null && !orderHints.isEmpty()) {
|
204
|
for(OrderHint orderHint : orderHints) {
|
205
|
orderHint.add(query);
|
206
|
}
|
207
|
}
|
208
|
}
|
209
|
|
210
|
public List<AuditEventRecord<T>> getAuditEvents(T t, Integer pageSize, Integer pageNumber, AuditEventSort sort, List<String> propertyPaths) {
|
211
|
AuditEvent auditEvent = getAuditEventFromContext();
|
212
|
|
213
|
AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity(type, false, true);
|
214
|
query.add(AuditEntity.id().eq(t.getId()));
|
215
|
if(sort == null) {
|
216
|
sort = AuditEventSort.BACKWARDS;
|
217
|
}
|
218
|
|
219
|
if(sort.equals(AuditEventSort.BACKWARDS)) {
|
220
|
if(!auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
221
|
query.add(AuditEntity.revisionNumber().lt(auditEvent.getRevisionNumber()));
|
222
|
}
|
223
|
query.addOrder(AuditEntity.revisionNumber().desc());
|
224
|
} else {
|
225
|
if(!auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
226
|
query.add(AuditEntity.revisionNumber().gt(auditEvent.getRevisionNumber()));
|
227
|
}
|
228
|
query.addOrder(AuditEntity.revisionNumber().asc());
|
229
|
}
|
230
|
|
231
|
if(pageSize != null) {
|
232
|
query.setMaxResults(pageSize);
|
233
|
if(pageNumber != null) {
|
234
|
query.setFirstResult(pageNumber * pageSize);
|
235
|
} else {
|
236
|
query.setFirstResult(0);
|
237
|
}
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* At the moment we need to transform the data manually
|
242
|
*/
|
243
|
List<Object[]> objs = (List<Object[]>)query.getResultList();
|
244
|
List<AuditEventRecord<T>> records = new ArrayList<AuditEventRecord<T>>();
|
245
|
|
246
|
for(Object[] obj : objs) {
|
247
|
records.add(new AuditEventRecordImpl<T>(obj));
|
248
|
}
|
249
|
|
250
|
for(AuditEventRecord<T> record : records) {
|
251
|
defaultBeanInitializer.initialize(record.getAuditableObject(), propertyPaths);
|
252
|
}
|
253
|
return records;
|
254
|
}
|
255
|
|
256
|
public int countAuditEvents(T t, AuditEventSort sort) {
|
257
|
AuditEvent auditEvent = getAuditEventFromContext();
|
258
|
|
259
|
AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity(type, false, true);
|
260
|
query.add(AuditEntity.id().eq(t.getId()));
|
261
|
|
262
|
if(!auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
263
|
if(sort == null) {
|
264
|
sort = AuditEventSort.BACKWARDS;
|
265
|
}
|
266
|
|
267
|
if(sort.equals(AuditEventSort.BACKWARDS)) {
|
268
|
query.add(AuditEntity.revisionNumber().lt(auditEvent.getRevisionNumber()));
|
269
|
} else {
|
270
|
query.add(AuditEntity.revisionNumber().gt(auditEvent.getRevisionNumber()));
|
271
|
}
|
272
|
}
|
273
|
|
274
|
query.addProjection(AuditEntity.revisionNumber().count());
|
275
|
|
276
|
return ((Long)query.getSingleResult()).intValue();
|
277
|
}
|
278
|
|
279
|
public AuditEventRecord<T> getNextAuditEvent(T t) {
|
280
|
List<AuditEventRecord<T>> auditEvents = getAuditEvents(t,1,0,AuditEventSort.FORWARDS, null);
|
281
|
if(auditEvents.isEmpty()) {
|
282
|
return null;
|
283
|
} else {
|
284
|
return auditEvents.get(0);
|
285
|
}
|
286
|
}
|
287
|
|
288
|
public AuditEventRecord<T> getPreviousAuditEvent(T t) {
|
289
|
List<AuditEventRecord<T>> auditEvents = getAuditEvents(t,1,0,AuditEventSort.BACKWARDS, null);
|
290
|
if(auditEvents.isEmpty()) {
|
291
|
return null;
|
292
|
} else {
|
293
|
return auditEvents.get(0);
|
294
|
}
|
295
|
}
|
296
|
|
297
|
public int countAuditEvents(Class<? extends T> clazz, AuditEvent from, AuditEvent to, List<AuditCriterion> criteria) {
|
298
|
AuditQuery query = null;
|
299
|
|
300
|
if(clazz == null) {
|
301
|
query = getAuditReader().createQuery().forRevisionsOfEntity(type, false, true);
|
302
|
} else {
|
303
|
query = getAuditReader().createQuery().forRevisionsOfEntity(clazz, false, true);
|
304
|
}
|
305
|
|
306
|
if(from != null) {
|
307
|
query.add(AuditEntity.revisionNumber().ge(from.getRevisionNumber()));
|
308
|
}
|
309
|
|
310
|
if(to != null && !to.equals(AuditEvent.CURRENT_VIEW)) {
|
311
|
query.add(AuditEntity.revisionNumber().lt(to.getRevisionNumber()));
|
312
|
}
|
313
|
|
314
|
addCriteria(query,criteria);
|
315
|
|
316
|
query.addProjection(AuditEntity.revisionNumber().count());
|
317
|
|
318
|
return ((Long)query.getSingleResult()).intValue();
|
319
|
}
|
320
|
|
321
|
protected void addCriteria(AuditQuery query, List<AuditCriterion> criteria) {
|
322
|
if(criteria != null) {
|
323
|
for(AuditCriterion c : criteria) {
|
324
|
query.add(c);
|
325
|
}
|
326
|
}
|
327
|
}
|
328
|
|
329
|
public List<AuditEventRecord<T>> getAuditEvents(Class<? extends T> clazz,AuditEvent from, AuditEvent to, List<AuditCriterion> criteria, Integer pageSize, Integer pageNumber, AuditEventSort sort, List<String> propertyPaths) {
|
330
|
AuditQuery query = null;
|
331
|
|
332
|
if(clazz == null) {
|
333
|
query = getAuditReader().createQuery().forRevisionsOfEntity(type, false, true);
|
334
|
} else {
|
335
|
query = getAuditReader().createQuery().forRevisionsOfEntity(clazz, false, true);
|
336
|
}
|
337
|
|
338
|
if(from != null) {
|
339
|
query.add(AuditEntity.revisionNumber().ge(from.getRevisionNumber()));
|
340
|
}
|
341
|
|
342
|
if(to != null && !to.equals(AuditEvent.CURRENT_VIEW)) {
|
343
|
query.add(AuditEntity.revisionNumber().lt(to.getRevisionNumber()));
|
344
|
}
|
345
|
|
346
|
if(sort.equals(AuditEventSort.BACKWARDS)) {
|
347
|
query.addOrder(AuditEntity.revisionNumber().desc());
|
348
|
} else {
|
349
|
query.addOrder(AuditEntity.revisionNumber().asc());
|
350
|
}
|
351
|
|
352
|
addCriteria(query,criteria);
|
353
|
|
354
|
if(pageSize != null) {
|
355
|
query.setMaxResults(pageSize);
|
356
|
if(pageNumber != null) {
|
357
|
query.setFirstResult(pageNumber * pageSize);
|
358
|
} else {
|
359
|
query.setFirstResult(0);
|
360
|
}
|
361
|
}
|
362
|
|
363
|
/**
|
364
|
* At the moment we need to transform the data manually
|
365
|
*/
|
366
|
List<Object[]> objs = (List<Object[]>)query.getResultList();
|
367
|
List<AuditEventRecord<T>> records = new ArrayList<AuditEventRecord<T>>();
|
368
|
|
369
|
for(Object[] obj : objs) {
|
370
|
records.add(new AuditEventRecordImpl<T>(obj));
|
371
|
}
|
372
|
|
373
|
for(AuditEventRecord<T> record : records) {
|
374
|
defaultBeanInitializer.initialize(record.getAuditableObject(), propertyPaths);
|
375
|
}
|
376
|
return records;
|
377
|
}
|
378
|
|
379
|
@Override
|
380
|
protected int countByParam(Class<? extends T> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criterion) {
|
381
|
checkNotInPriorView("IdentifiableDaoBase.findByParam(Class<? extends T> clazz, String queryString, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
382
|
return super.countByParam(clazz, param, queryString, matchmode, criterion);
|
383
|
}
|
384
|
|
385
|
@Override
|
386
|
public int count(T example, Set<String> includeProperties) {
|
387
|
this.checkNotInPriorView("count(T example, Set<String> includeProperties)");
|
388
|
return super.count(example, includeProperties);
|
389
|
}
|
390
|
|
391
|
public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
392
|
this.checkNotInPriorView("list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {");
|
393
|
return super.list(example, includeProperties, limit, start, orderHints, propertyPaths);
|
394
|
}
|
395
|
}
|