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