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.persistence.dao.hibernate.common;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.hibernate.Criteria;
|
20
|
import org.hibernate.Query;
|
21
|
import org.hibernate.criterion.Projections;
|
22
|
import org.hibernate.criterion.Restrictions;
|
23
|
import org.hibernate.envers.query.AuditEntity;
|
24
|
import org.hibernate.envers.query.AuditQuery;
|
25
|
import org.springframework.stereotype.Repository;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
28
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
29
|
import eu.etaxonomy.cdm.model.common.TermType;
|
30
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
31
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
|
33
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
34
|
|
35
|
/**
|
36
|
* @author a.mueller
|
37
|
*
|
38
|
*/
|
39
|
@Repository
|
40
|
public class TermVocabularyDaoImpl extends IdentifiableDaoBase<TermVocabulary> implements
|
41
|
ITermVocabularyDao {
|
42
|
/**
|
43
|
* @param type
|
44
|
*/
|
45
|
public TermVocabularyDaoImpl() {
|
46
|
super(TermVocabulary.class);
|
47
|
indexedClasses = new Class[2];
|
48
|
indexedClasses[0] = TermVocabulary.class;
|
49
|
indexedClasses[1] = OrderedTermVocabulary.class;
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
public int countTerms(TermVocabulary termVocabulary) {
|
54
|
AuditEvent auditEvent = getAuditEventFromContext();
|
55
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
56
|
Query query = getSession().createQuery("select count(term) from DefinedTermBase term where term.vocabulary = :vocabulary");
|
57
|
query.setParameter("vocabulary", termVocabulary);
|
58
|
|
59
|
return ((Long)query.uniqueResult()).intValue();
|
60
|
} else {
|
61
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
62
|
query.addProjection(AuditEntity.id().count("id"));
|
63
|
query.add(AuditEntity.relatedId("vocabulary").eq(termVocabulary.getId()));
|
64
|
return ((Number)query.getSingleResult()).intValue();
|
65
|
}
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public <T extends DefinedTermBase> List<T> getTerms(TermVocabulary<T> vocabulary,Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,List<String> propertyPaths) {
|
70
|
AuditEvent auditEvent = getAuditEventFromContext();
|
71
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
72
|
Criteria criteria = getSession().createCriteria(DefinedTermBase.class);
|
73
|
criteria.createCriteria("vocabulary").add(Restrictions.idEq(vocabulary.getId()));
|
74
|
|
75
|
if(pageSize != null) {
|
76
|
criteria.setMaxResults(pageSize);
|
77
|
if(pageNumber != null) {
|
78
|
criteria.setFirstResult(pageNumber * pageSize);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
this.addOrder(criteria, orderHints);
|
83
|
List<T> result = criteria.list();
|
84
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
85
|
return result;
|
86
|
} else {
|
87
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
88
|
query.add(AuditEntity.relatedId("vocabulary").eq(vocabulary.getId()));
|
89
|
|
90
|
if(pageSize != null) {
|
91
|
query.setMaxResults(pageSize);
|
92
|
if(pageNumber != null) {
|
93
|
query.setFirstResult(pageNumber * pageSize);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
List<T> result = query.getResultList();
|
98
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
99
|
return result;
|
100
|
}
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
public <T extends DefinedTermBase> TermVocabulary<T> findByUri(String termSourceUri, Class<T> clazz) {
|
105
|
AuditEvent auditEvent = getAuditEventFromContext();
|
106
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
107
|
Query query = getSession().createQuery("select vocabulary from TermVocabulary vocabulary where vocabulary.termSourceUri= :termSourceUri");
|
108
|
query.setParameter("termSourceUri", termSourceUri);
|
109
|
|
110
|
return (TermVocabulary<T>)query.uniqueResult();
|
111
|
} else {
|
112
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
113
|
query.add(AuditEntity.property("termSourceUri").eq(termSourceUri));
|
114
|
|
115
|
return (TermVocabulary<T>)query.getSingleResult();
|
116
|
}
|
117
|
}
|
118
|
|
119
|
|
120
|
@Override
|
121
|
public <T extends DefinedTermBase> List<T> getTerms(TermVocabulary<T> termVocabulary, Integer pageSize, Integer pageNumber) {
|
122
|
return getTerms(termVocabulary, pageSize, pageNumber, null, null);
|
123
|
}
|
124
|
|
125
|
@Override
|
126
|
@Deprecated
|
127
|
public <TERM extends DefinedTermBase> List<TermVocabulary<? extends TERM>> listByTermClass(Class<TERM> clazz, boolean includeSubclasses, boolean includeEmptyVocs, Integer limit, Integer start,List<OrderHint> orderHints, List<String> propertyPaths) {
|
128
|
checkNotInPriorView("TermVocabularyDao.listByTermClass2(Class<TERM> clazz, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths)");
|
129
|
List<Integer> intermediateResults;
|
130
|
|
131
|
if (includeSubclasses){
|
132
|
String hql = " SELECT DISTINCT trm.vocabulary.id " +
|
133
|
" FROM %s trm " +
|
134
|
" GROUP BY trm.vocabulary ";
|
135
|
hql = String.format(hql, clazz.getSimpleName());
|
136
|
Query query = getSession().createQuery(hql);
|
137
|
intermediateResults = query.list();
|
138
|
}else{
|
139
|
Criteria criteria = getSession().createCriteria(type);
|
140
|
criteria.createAlias("terms", "trms").add(Restrictions.eq("trms.class", clazz.getSimpleName()));
|
141
|
criteria.setProjection(Projections.id());
|
142
|
intermediateResults = criteria.list();
|
143
|
}
|
144
|
if (includeEmptyVocs){
|
145
|
intermediateResults.addAll(getEmptyVocIds());
|
146
|
}
|
147
|
|
148
|
if(intermediateResults.size() == 0) {
|
149
|
return new ArrayList<TermVocabulary<? extends TERM>>();
|
150
|
}
|
151
|
|
152
|
Criteria criteria = getSession().createCriteria(type);
|
153
|
criteria.add(Restrictions.in("id", intermediateResults));
|
154
|
|
155
|
if(limit != null) {
|
156
|
criteria.setMaxResults(limit);
|
157
|
if(start != null) {
|
158
|
criteria.setFirstResult(start);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
this.addOrder(criteria, orderHints);
|
163
|
|
164
|
List<TermVocabulary<? extends TERM>> result = criteria.list();
|
165
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
166
|
return result;
|
167
|
}
|
168
|
|
169
|
|
170
|
// public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType) {
|
171
|
//
|
172
|
// Query query = getSession().createQuery("select vocabulary from TermVocabulary vocabulary where vocabulary.termType= :termType");
|
173
|
// query.setParameter("termType", termType);
|
174
|
//
|
175
|
// return (List<TermVocabulary<T>>)query.list();
|
176
|
//
|
177
|
// }
|
178
|
|
179
|
@Override
|
180
|
public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType) {
|
181
|
|
182
|
Criteria criteria = getSession().createCriteria(type);
|
183
|
criteria.add(Restrictions.eq("termType", termType));
|
184
|
|
185
|
//this.addOrder(criteria, orderHints);
|
186
|
|
187
|
List<TermVocabulary<T>> result = criteria.list();
|
188
|
defaultBeanInitializer.initializeAll(result, null);
|
189
|
return result;
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
public List<TermVocabulary> listByTermType(TermType termType, boolean includeSubTypes, Integer limit, Integer start,List<OrderHint> orderHints, List<String> propertyPaths) {
|
194
|
checkNotInPriorView("TermVocabularyDao.listByTermType(TermType termType, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths)");
|
195
|
|
196
|
Set<TermType> allTermTypes = new HashSet<TermType>();
|
197
|
allTermTypes.add(termType);
|
198
|
if (includeSubTypes){
|
199
|
allTermTypes.addAll(termType.getGeneralizationOf(true));
|
200
|
}
|
201
|
|
202
|
Criteria criteria = getSession().createCriteria(type);
|
203
|
criteria.add(Restrictions.in("termType", allTermTypes));
|
204
|
|
205
|
if(limit != null) {
|
206
|
criteria.setMaxResults(limit);
|
207
|
if(start != null) {
|
208
|
criteria.setFirstResult(start);
|
209
|
}
|
210
|
}
|
211
|
|
212
|
this.addOrder(criteria, orderHints);
|
213
|
|
214
|
List<TermVocabulary> result = criteria.list();
|
215
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
216
|
return result;
|
217
|
}
|
218
|
|
219
|
|
220
|
@Override
|
221
|
public List<TermVocabulary> listEmpty(Integer limit, Integer start,List<OrderHint> orderHints, List<String> propertyPaths) {
|
222
|
checkNotInPriorView("TermVocabularyDao.listEmpty(Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths)");
|
223
|
List<Integer> intermediateResults;
|
224
|
|
225
|
intermediateResults = getEmptyVocIds();
|
226
|
|
227
|
Criteria criteria = getSession().createCriteria(type);
|
228
|
criteria.add(Restrictions.in("id", intermediateResults));
|
229
|
|
230
|
if(limit != null) {
|
231
|
criteria.setMaxResults(limit);
|
232
|
if(start != null) {
|
233
|
criteria.setFirstResult(start);
|
234
|
}
|
235
|
}
|
236
|
|
237
|
this.addOrder(criteria, orderHints);
|
238
|
|
239
|
List<TermVocabulary> result = criteria.list();
|
240
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
241
|
return result;
|
242
|
}
|
243
|
|
244
|
@Override
|
245
|
public void missingTermUuids(
|
246
|
Map<UUID, Set<UUID>> uuidsRequested,
|
247
|
Map<UUID, Set<UUID>> uuidMissingTermsRepsonse,
|
248
|
Map<UUID, TermVocabulary<?>> vocabularyResponse){
|
249
|
|
250
|
Set<UUID> missingTermCandidateUuids = new HashSet<UUID>();
|
251
|
|
252
|
for (Set<UUID> uuidsPerVocSet : uuidsRequested.values()){
|
253
|
missingTermCandidateUuids.addAll(uuidsPerVocSet);
|
254
|
}
|
255
|
|
256
|
//search persisted subset of required (usually all)
|
257
|
String hql = " SELECT terms.uuid " +
|
258
|
" FROM TermVocabulary voc join voc.terms terms " +
|
259
|
" WHERE terms.uuid IN (:uuids) " +
|
260
|
" ORDER BY voc.uuid ";
|
261
|
Query query = getSession().createQuery(hql);
|
262
|
query.setParameterList("uuids", missingTermCandidateUuids);
|
263
|
List<?> persistedUuids = query.list();
|
264
|
|
265
|
|
266
|
//fully load and initialize vocabularies if required
|
267
|
if (vocabularyResponse != null){
|
268
|
String hql2 = " SELECT DISTINCT voc " +
|
269
|
" FROM TermVocabulary voc " +
|
270
|
" LEFT JOIN FETCH voc.terms terms " +
|
271
|
" LEFT JOIN FETCH terms.representations representations " +
|
272
|
" LEFT JOIN FETCH voc.representations vocReps " +
|
273
|
" WHERE terms.uuid IN (:termUuids) OR ( voc.uuid IN (:vocUuids) ) " + //was: AND voc.terms is empty, but did not load originally empty vocabularies with user defined terms added
|
274
|
// " WHERE voc.uuid IN (:vocUuids) AND voc.terms is empty " +
|
275
|
" ORDER BY voc.uuid ";
|
276
|
query = getSession().createQuery(hql2);
|
277
|
query.setParameterList("termUuids",missingTermCandidateUuids);
|
278
|
query.setParameterList("vocUuids",uuidsRequested.keySet());
|
279
|
List<TermVocabulary> o = query.list();
|
280
|
for (TermVocabulary<?> voc : o){
|
281
|
vocabularyResponse.put(voc.getUuid(), voc);
|
282
|
}
|
283
|
}
|
284
|
|
285
|
//compute missing terms
|
286
|
if (missingTermCandidateUuids.size() == persistedUuids.size()){
|
287
|
missingTermCandidateUuids.clear();
|
288
|
}else{
|
289
|
missingTermCandidateUuids.removeAll(persistedUuids);
|
290
|
//add missing terms to response
|
291
|
for (UUID vocUUID : uuidsRequested.keySet()){
|
292
|
for (UUID termUuid : uuidsRequested.get(vocUUID)){
|
293
|
if (missingTermCandidateUuids.contains(termUuid)){
|
294
|
Set<UUID> r = uuidMissingTermsRepsonse.get(vocUUID);
|
295
|
if (r == null){
|
296
|
r = new HashSet<UUID>();
|
297
|
uuidMissingTermsRepsonse.put(vocUUID, r);
|
298
|
}
|
299
|
r.add(termUuid);
|
300
|
}
|
301
|
}
|
302
|
}
|
303
|
}
|
304
|
|
305
|
return;
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* @return
|
310
|
*/
|
311
|
private List<Integer> getEmptyVocIds() {
|
312
|
List<Integer> intermediateResults;
|
313
|
String hql = " SELECT voc.id " +
|
314
|
" FROM TermVocabulary voc " +
|
315
|
" WHERE voc.terms.size = 0 ";
|
316
|
Query query = getSession().createQuery(hql);
|
317
|
intermediateResults = query.list();
|
318
|
return intermediateResults;
|
319
|
}
|
320
|
}
|