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.Collection;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.hibernate.Criteria;
|
21
|
import org.hibernate.Query;
|
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
|
/**
|
44
|
* @param type
|
45
|
*/
|
46
|
public TermVocabularyDaoImpl() {
|
47
|
super(TermVocabulary.class);
|
48
|
indexedClasses = new Class[2];
|
49
|
indexedClasses[0] = TermVocabulary.class;
|
50
|
indexedClasses[1] = OrderedTermVocabulary.class;
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public int countTerms(TermVocabulary termVocabulary) {
|
55
|
AuditEvent auditEvent = getAuditEventFromContext();
|
56
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
57
|
Query query = getSession().createQuery("select count(term) from DefinedTermBase term where term.vocabulary = :vocabulary");
|
58
|
query.setParameter("vocabulary", termVocabulary);
|
59
|
|
60
|
return ((Long)query.uniqueResult()).intValue();
|
61
|
} else {
|
62
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
63
|
query.addProjection(AuditEntity.id().count());
|
64
|
query.add(AuditEntity.relatedId("vocabulary").eq(termVocabulary.getId()));
|
65
|
return ((Number)query.getSingleResult()).intValue();
|
66
|
}
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public <T extends DefinedTermBase> List<T> getTerms(TermVocabulary<T> vocabulary,Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,List<String> propertyPaths) {
|
71
|
AuditEvent auditEvent = getAuditEventFromContext();
|
72
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
73
|
Criteria criteria = getSession().createCriteria(DefinedTermBase.class);
|
74
|
criteria.createCriteria("vocabulary").add(Restrictions.idEq(vocabulary.getId()));
|
75
|
|
76
|
if(pageSize != null) {
|
77
|
criteria.setMaxResults(pageSize);
|
78
|
if(pageNumber != null) {
|
79
|
criteria.setFirstResult(pageNumber * pageSize);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
this.addOrder(criteria, orderHints);
|
84
|
@SuppressWarnings("unchecked")
|
85
|
List<T> result = criteria.list();
|
86
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
87
|
return result;
|
88
|
} else {
|
89
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
90
|
query.add(AuditEntity.relatedId("vocabulary").eq(vocabulary.getId()));
|
91
|
|
92
|
if(pageSize != null) {
|
93
|
query.setMaxResults(pageSize);
|
94
|
if(pageNumber != null) {
|
95
|
query.setFirstResult(pageNumber * pageSize);
|
96
|
}
|
97
|
}
|
98
|
|
99
|
@SuppressWarnings("unchecked")
|
100
|
List<T> result = query.getResultList();
|
101
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
102
|
return result;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public <T extends DefinedTermBase> TermVocabulary<T> findByUri(String termSourceUri, Class<T> clazz) {
|
108
|
AuditEvent auditEvent = getAuditEventFromContext();
|
109
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
110
|
Query query = getSession().createQuery("select vocabulary from TermVocabulary vocabulary where vocabulary.termSourceUri= :termSourceUri");
|
111
|
query.setParameter("termSourceUri", termSourceUri);
|
112
|
|
113
|
return (TermVocabulary<T>)query.uniqueResult();
|
114
|
} else {
|
115
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(type,auditEvent.getRevisionNumber());
|
116
|
query.add(AuditEntity.property("termSourceUri").eq(termSourceUri));
|
117
|
|
118
|
return (TermVocabulary<T>)query.getSingleResult();
|
119
|
}
|
120
|
}
|
121
|
|
122
|
|
123
|
@Override
|
124
|
public <T extends DefinedTermBase> List<T> getTerms(TermVocabulary<T> termVocabulary, Integer pageSize, Integer pageNumber) {
|
125
|
return getTerms(termVocabulary, pageSize, pageNumber, null, null);
|
126
|
}
|
127
|
|
128
|
|
129
|
@Override
|
130
|
public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths) {
|
131
|
|
132
|
Criteria criteria = getSession().createCriteria(type);
|
133
|
criteria.add(Restrictions.eq("termType", termType));
|
134
|
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
|
135
|
//this.addOrder(criteria, orderHints);
|
136
|
|
137
|
@SuppressWarnings("unchecked")
|
138
|
List<TermVocabulary<T>> result = criteria.list();
|
139
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
140
|
return result;
|
141
|
}
|
142
|
|
143
|
@Override
|
144
|
public List<TermVocabulary> listByTermType(TermType termType, boolean includeSubTypes, Integer limit, Integer start,List<OrderHint> orderHints, List<String> propertyPaths) {
|
145
|
checkNotInPriorView("TermVocabularyDao.listByTermType(TermType termType, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths)");
|
146
|
|
147
|
Set<TermType> allTermTypes = new HashSet<TermType>();
|
148
|
allTermTypes.add(termType);
|
149
|
if (includeSubTypes){
|
150
|
allTermTypes.addAll(termType.getGeneralizationOf(true));
|
151
|
}
|
152
|
|
153
|
Criteria criteria = getSession().createCriteria(type);
|
154
|
criteria.add(Restrictions.in("termType", allTermTypes));
|
155
|
|
156
|
if(limit != null) {
|
157
|
criteria.setMaxResults(limit);
|
158
|
if(start != null) {
|
159
|
criteria.setFirstResult(start);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
this.addOrder(criteria, orderHints);
|
164
|
|
165
|
@SuppressWarnings("unchecked")
|
166
|
List<TermVocabulary> result = criteria.list();
|
167
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
168
|
return result;
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public void missingTermUuids(
|
173
|
Map<UUID, Set<UUID>> uuidsRequested,
|
174
|
Map<UUID, Set<UUID>> uuidMissingTermsRepsonse,
|
175
|
Map<UUID, TermVocabulary<?>> vocabularyResponse){
|
176
|
|
177
|
Set<UUID> missingTermCandidateUuids = new HashSet<>();
|
178
|
|
179
|
for (Set<UUID> uuidsPerVocSet : uuidsRequested.values()){
|
180
|
missingTermCandidateUuids.addAll(uuidsPerVocSet);
|
181
|
}
|
182
|
|
183
|
//search persisted subset of required (usually all)
|
184
|
String hql = " SELECT terms.uuid " +
|
185
|
" FROM TermVocabulary voc join voc.terms terms " +
|
186
|
" WHERE terms.uuid IN (:uuids) " +
|
187
|
" ORDER BY voc.uuid ";
|
188
|
Query query = getSession().createQuery(hql);
|
189
|
|
190
|
int splitSize = 2000;
|
191
|
List<Collection<UUID>> missingTermCandidates = splitCollection(missingTermCandidateUuids, splitSize);
|
192
|
List<UUID> persistedUuids = new ArrayList<>();
|
193
|
|
194
|
for (Collection<UUID> uuids : missingTermCandidates){
|
195
|
query.setParameterList("uuids", uuids);
|
196
|
@SuppressWarnings("unchecked")
|
197
|
List<UUID> list = query.list();
|
198
|
persistedUuids.addAll(list);
|
199
|
}
|
200
|
|
201
|
|
202
|
//fully load and initialize vocabularies if required
|
203
|
if (vocabularyResponse != null){
|
204
|
String hql2 = " SELECT DISTINCT voc " +
|
205
|
" FROM TermVocabulary voc " +
|
206
|
" LEFT JOIN FETCH voc.terms terms " +
|
207
|
" LEFT JOIN FETCH terms.representations representations " +
|
208
|
" LEFT JOIN FETCH voc.representations vocReps " +
|
209
|
" 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
|
210
|
// " WHERE voc.uuid IN (:vocUuids) AND voc.terms is empty " +
|
211
|
" ORDER BY voc.uuid ";
|
212
|
query = getSession().createQuery(hql2);
|
213
|
query.setParameterList("termUuids", missingTermCandidateUuids);
|
214
|
query.setParameterList("vocUuids", uuidsRequested.keySet());
|
215
|
|
216
|
for (Collection<UUID> uuids : missingTermCandidates){
|
217
|
query.setParameterList("termUuids", uuids);
|
218
|
@SuppressWarnings("unchecked")
|
219
|
List<TermVocabulary<?>> o = query.list();
|
220
|
for (TermVocabulary<?> voc : o){
|
221
|
vocabularyResponse.put(voc.getUuid(), voc);
|
222
|
}
|
223
|
}
|
224
|
}
|
225
|
|
226
|
//compute missing terms
|
227
|
if (missingTermCandidateUuids.size() == persistedUuids.size()){
|
228
|
missingTermCandidateUuids.clear();
|
229
|
}else{
|
230
|
missingTermCandidateUuids.removeAll(persistedUuids);
|
231
|
//add missing terms to response
|
232
|
for (UUID vocUUID : uuidsRequested.keySet()){
|
233
|
for (UUID termUuid : uuidsRequested.get(vocUUID)){
|
234
|
if (missingTermCandidateUuids.contains(termUuid)){
|
235
|
Set<UUID> r = uuidMissingTermsRepsonse.get(vocUUID);
|
236
|
if (r == null){
|
237
|
r = new HashSet<>();
|
238
|
uuidMissingTermsRepsonse.put(vocUUID, r);
|
239
|
}
|
240
|
r.add(termUuid);
|
241
|
}
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
|
246
|
return;
|
247
|
}
|
248
|
|
249
|
}
|