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.description;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashMap;
|
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.apache.commons.lang.ArrayUtils;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.hibernate.Criteria;
|
23
|
import org.hibernate.Query;
|
24
|
import org.hibernate.criterion.ProjectionList;
|
25
|
import org.hibernate.criterion.Projections;
|
26
|
import org.hibernate.criterion.Restrictions;
|
27
|
import org.hibernate.envers.query.AuditEntity;
|
28
|
import org.hibernate.envers.query.AuditQuery;
|
29
|
import org.springframework.beans.factory.annotation.Qualifier;
|
30
|
import org.springframework.stereotype.Repository;
|
31
|
|
32
|
import eu.etaxonomy.cdm.model.common.DefinedTerm;
|
33
|
import eu.etaxonomy.cdm.model.common.LSID;
|
34
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
35
|
import eu.etaxonomy.cdm.model.common.Representation;
|
36
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
37
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
38
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
39
|
import eu.etaxonomy.cdm.model.description.Feature;
|
40
|
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
|
41
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
42
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
43
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
44
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
45
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
46
|
import eu.etaxonomy.cdm.model.media.Media;
|
47
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
48
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
49
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
50
|
import eu.etaxonomy.cdm.persistence.dao.common.OperationNotSupportedInPriorViewException;
|
51
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
|
52
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
|
53
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
54
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
55
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
56
|
|
57
|
@Repository
|
58
|
@Qualifier("descriptionDaoImpl")
|
59
|
public class DescriptionDaoImpl extends IdentifiableDaoBase<DescriptionBase> implements IDescriptionDao{
|
60
|
|
61
|
private static final Logger logger = Logger.getLogger(DescriptionDaoImpl.class);
|
62
|
|
63
|
public DescriptionDaoImpl() {
|
64
|
super(DescriptionBase.class);
|
65
|
indexedClasses = new Class[3];
|
66
|
indexedClasses[0] = TaxonDescription.class;
|
67
|
indexedClasses[1] = TaxonNameDescription.class;
|
68
|
indexedClasses[2] = SpecimenDescription.class;
|
69
|
}
|
70
|
|
71
|
// @Override //Override for testing
|
72
|
// public DescriptionBase load(UUID uuid, List<String> propertyPaths){
|
73
|
// DescriptionBase bean = findByUuid(uuid);
|
74
|
// if(bean == null){
|
75
|
// return bean;
|
76
|
// }
|
77
|
// defaultBeanInitializer.initialize(bean, propertyPaths);
|
78
|
//
|
79
|
// return bean;
|
80
|
// }
|
81
|
|
82
|
@Override
|
83
|
public int countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTerm status) {
|
84
|
checkNotInPriorView("DescriptionDaoImpl.countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status)");
|
85
|
Query query = null;
|
86
|
|
87
|
if(status == null) {
|
88
|
query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area where area in (:namedAreas)");
|
89
|
} else {
|
90
|
query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area join element.status status where area in (:namedAreas) and status = :status");
|
91
|
query.setParameter("status", status);
|
92
|
}
|
93
|
query.setParameterList("namedAreas", namedAreas);
|
94
|
|
95
|
return ((Long)query.uniqueResult()).intValue();
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public <T extends DescriptionElementBase> int countDescriptionElements(DescriptionBase description, Set<Feature> features, Class<T> clazz) {
|
100
|
return countDescriptionElements(description, null, features, clazz);
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
public <T extends DescriptionElementBase> int countDescriptionElements(DescriptionBase description, Class<? extends DescriptionBase> descriptionType,
|
105
|
Set<Feature> features, Class<T> clazz) {
|
106
|
AuditEvent auditEvent = getAuditEventFromContext();
|
107
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
108
|
Criteria criteria = null;
|
109
|
if(clazz == null) {
|
110
|
criteria = getSession().createCriteria(DescriptionElementBase.class);
|
111
|
} else {
|
112
|
criteria = getSession().createCriteria(clazz);
|
113
|
}
|
114
|
|
115
|
if(description != null) {
|
116
|
criteria.add(Restrictions.eq("inDescription", description));
|
117
|
}
|
118
|
|
119
|
if(descriptionType != null) {
|
120
|
criteria.createAlias("inDescription", "d").add(Restrictions.eq("d.class", descriptionType));
|
121
|
}
|
122
|
|
123
|
if(features != null && !features.isEmpty()) {
|
124
|
criteria.add(Restrictions.in("feature", features));
|
125
|
}
|
126
|
|
127
|
criteria.setProjection(Projections.rowCount());
|
128
|
|
129
|
return ((Number)criteria.uniqueResult()).intValue();
|
130
|
} else {
|
131
|
if(features != null && !features.isEmpty()) {
|
132
|
Integer count = 0;
|
133
|
for(Feature f : features) {
|
134
|
AuditQuery query = null;
|
135
|
if(clazz == null) {
|
136
|
query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
|
137
|
} else {
|
138
|
query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
|
139
|
}
|
140
|
|
141
|
if(description != null) {
|
142
|
query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
|
143
|
}
|
144
|
|
145
|
if(descriptionType != null) {
|
146
|
query.add(AuditEntity.property("inDescription.class").eq(descriptionType));
|
147
|
}
|
148
|
|
149
|
query.add(AuditEntity.relatedId("feature").eq(f.getId()));
|
150
|
query.addProjection(AuditEntity.id().count());
|
151
|
count += ((Long)query.getSingleResult()).intValue();
|
152
|
}
|
153
|
|
154
|
return count;
|
155
|
} else {
|
156
|
AuditQuery query = null;
|
157
|
if(clazz == null) {
|
158
|
query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
|
159
|
} else {
|
160
|
query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
|
161
|
}
|
162
|
|
163
|
if(description != null) {
|
164
|
query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
|
165
|
}
|
166
|
if(descriptionType != null) {
|
167
|
query.add(AuditEntity.property("inDescription.class").eq(descriptionType));
|
168
|
}
|
169
|
|
170
|
query.addProjection(AuditEntity.id().count());
|
171
|
return ((Long)query.getSingleResult()).intValue();
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public int countDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText, Set<Feature> features) {
|
178
|
checkNotInPriorView("DescriptionDaoImpl.countDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText, Set<Feature> features)");
|
179
|
Criteria inner = null;
|
180
|
|
181
|
if(clazz == null) {
|
182
|
inner = getSession().createCriteria(type);
|
183
|
} else {
|
184
|
inner = getSession().createCriteria(clazz);
|
185
|
}
|
186
|
|
187
|
Criteria elementsCriteria = inner.createCriteria("descriptionElements");
|
188
|
if(hasText != null) {
|
189
|
if(hasText) {
|
190
|
elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
|
191
|
} else {
|
192
|
elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
|
193
|
}
|
194
|
}
|
195
|
|
196
|
if(hasImages != null) {
|
197
|
if(hasImages) {
|
198
|
elementsCriteria.add(Restrictions.isNotEmpty("media"));
|
199
|
} else {
|
200
|
elementsCriteria.add(Restrictions.isEmpty("media"));
|
201
|
}
|
202
|
}
|
203
|
|
204
|
if(features != null && !features.isEmpty()) {
|
205
|
elementsCriteria.add(Restrictions.in("feature", features));
|
206
|
}
|
207
|
|
208
|
inner.setProjection(Projections.countDistinct("id"));
|
209
|
|
210
|
return ((Number) inner.uniqueResult()).intValue();
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public int countTaxonDescriptions(Taxon taxon, Set<DefinedTerm> scopes,Set<NamedArea> geographicalScopes, Set<MarkerType> markerTypes) {
|
215
|
AuditEvent auditEvent = getAuditEventFromContext();
|
216
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
217
|
Criteria criteria = getSession().createCriteria(TaxonDescription.class);
|
218
|
|
219
|
if(taxon != null) {
|
220
|
criteria.add(Restrictions.eq("taxon", taxon));
|
221
|
}
|
222
|
|
223
|
if(scopes != null && !scopes.isEmpty()) {
|
224
|
Set<Integer> scopeIds = new HashSet<Integer>();
|
225
|
for(DefinedTerm s : scopes) {
|
226
|
scopeIds.add(s.getId());
|
227
|
}
|
228
|
criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
|
229
|
}
|
230
|
|
231
|
if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
|
232
|
Set<Integer> geoScopeIds = new HashSet<Integer>();
|
233
|
for(NamedArea n : geographicalScopes) {
|
234
|
geoScopeIds.add(n.getId());
|
235
|
}
|
236
|
criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
|
237
|
}
|
238
|
|
239
|
|
240
|
addMarkerTypesCriterion(markerTypes, criteria);
|
241
|
|
242
|
|
243
|
criteria.setProjection(Projections.rowCount());
|
244
|
|
245
|
return ((Number)criteria.uniqueResult()).intValue();
|
246
|
} else {
|
247
|
if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty()) && (markerTypes == null || markerTypes.isEmpty())) {
|
248
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
|
249
|
if(taxon != null) {
|
250
|
query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
|
251
|
}
|
252
|
|
253
|
query.addProjection(AuditEntity.id().count());
|
254
|
|
255
|
return ((Long)query.getSingleResult()).intValue();
|
256
|
} else {
|
257
|
throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
|
258
|
}
|
259
|
}
|
260
|
}
|
261
|
|
262
|
/**
|
263
|
* @param markerTypes
|
264
|
* @param criteria
|
265
|
*
|
266
|
*/
|
267
|
//TODO move to AnnotatableEntityDao(?)
|
268
|
private void addMarkerTypesCriterion(Set<MarkerType> markerTypes, Criteria criteria) {
|
269
|
|
270
|
if(markerTypes != null && !markerTypes.isEmpty()) {
|
271
|
Set<Integer> markerTypeIds = new HashSet<Integer>();
|
272
|
for(MarkerType markerType : markerTypes) {
|
273
|
markerTypeIds.add(markerType.getId());
|
274
|
}
|
275
|
criteria.createCriteria("markers").add(Restrictions.eq("flag", true))
|
276
|
.createAlias("markerType", "mt")
|
277
|
.add(Restrictions.in("mt.id", markerTypeIds));
|
278
|
} else if (markerTypes != null && markerTypes.isEmpty()){
|
279
|
//AT: added in case the projects requires an third state description, An empty Marker type set
|
280
|
}
|
281
|
}
|
282
|
@Override
|
283
|
public <T extends DescriptionElementBase> List<T> getDescriptionElements(
|
284
|
DescriptionBase description, Set<Feature> features,
|
285
|
Class<T> clazz, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
286
|
return getDescriptionElements(description, null, features, clazz, pageSize, pageNumber, propertyPaths);
|
287
|
}
|
288
|
|
289
|
@Override
|
290
|
public <T extends DescriptionElementBase> List<T> getDescriptionElements(
|
291
|
DescriptionBase description, Class<? extends DescriptionBase> descriptionType,
|
292
|
Set<Feature> features,
|
293
|
Class<T> clazz,
|
294
|
Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
295
|
|
296
|
AuditEvent auditEvent = getAuditEventFromContext();
|
297
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
298
|
Criteria criteria = null;
|
299
|
if(clazz == null) {
|
300
|
criteria = getSession().createCriteria(DescriptionElementBase.class);
|
301
|
} else {
|
302
|
criteria = getSession().createCriteria(clazz);
|
303
|
}
|
304
|
|
305
|
if(description != null) {
|
306
|
criteria.add(Restrictions.eq("inDescription", description));
|
307
|
}
|
308
|
if(descriptionType != null) {
|
309
|
criteria.createAlias("inDescription", "d").add(Restrictions.eq("d.class", descriptionType));
|
310
|
}
|
311
|
|
312
|
if(features != null && !features.isEmpty()) {
|
313
|
criteria.add(Restrictions.in("feature", features));
|
314
|
}
|
315
|
|
316
|
if(pageSize != null) {
|
317
|
criteria.setMaxResults(pageSize);
|
318
|
if(pageNumber != null) {
|
319
|
criteria.setFirstResult(pageNumber * pageSize);
|
320
|
}
|
321
|
}
|
322
|
|
323
|
List<T> results = criteria.list();
|
324
|
|
325
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
326
|
|
327
|
return results;
|
328
|
} else {
|
329
|
List<T> result = new ArrayList<T>();
|
330
|
if(features != null && !features.isEmpty()) {
|
331
|
|
332
|
for(Feature f : features) {
|
333
|
AuditQuery query = null;
|
334
|
if(clazz == null) {
|
335
|
query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
|
336
|
} else {
|
337
|
query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
|
338
|
}
|
339
|
|
340
|
if(description != null) {
|
341
|
query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
|
342
|
}
|
343
|
|
344
|
if(descriptionType != null) {
|
345
|
query.add(AuditEntity.property("inDescription.class").eq(descriptionType));
|
346
|
}
|
347
|
|
348
|
query.add(AuditEntity.relatedId("feature").eq(f.getId()));
|
349
|
result.addAll(query.getResultList());
|
350
|
}
|
351
|
} else {
|
352
|
AuditQuery query = null;
|
353
|
if(clazz == null) {
|
354
|
query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
|
355
|
} else {
|
356
|
query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
|
357
|
}
|
358
|
|
359
|
if(description != null) {
|
360
|
query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
|
361
|
}
|
362
|
|
363
|
if(descriptionType != null) {
|
364
|
query.add(AuditEntity.property("inDescription.class").eq(descriptionType));
|
365
|
}
|
366
|
|
367
|
result = query.getResultList();
|
368
|
}
|
369
|
|
370
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
371
|
|
372
|
return result;
|
373
|
}
|
374
|
}
|
375
|
|
376
|
@Override
|
377
|
public List<TaxonDescription> listTaxonDescriptions(Taxon taxon, Set<DefinedTerm> scopes, Set<NamedArea> geographicalScopes, Set<MarkerType> markerTypes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
378
|
AuditEvent auditEvent = getAuditEventFromContext();
|
379
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
380
|
Criteria criteria = getSession().createCriteria(TaxonDescription.class);
|
381
|
|
382
|
if(taxon != null) {
|
383
|
criteria.add(Restrictions.eq("taxon", taxon));
|
384
|
}
|
385
|
|
386
|
if(scopes != null && !scopes.isEmpty()) {
|
387
|
Set<Integer> scopeIds = new HashSet<Integer>();
|
388
|
for(DefinedTerm s : scopes) {
|
389
|
scopeIds.add(s.getId());
|
390
|
}
|
391
|
criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
|
392
|
}
|
393
|
|
394
|
if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
|
395
|
Set<Integer> geoScopeIds = new HashSet<Integer>();
|
396
|
for(NamedArea n : geographicalScopes) {
|
397
|
geoScopeIds.add(n.getId());
|
398
|
}
|
399
|
criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
|
400
|
}
|
401
|
|
402
|
addMarkerTypesCriterion(markerTypes, criteria);
|
403
|
|
404
|
if(pageSize != null) {
|
405
|
criteria.setMaxResults(pageSize);
|
406
|
if(pageNumber != null) {
|
407
|
criteria.setFirstResult(pageNumber * pageSize);
|
408
|
}
|
409
|
}
|
410
|
|
411
|
List<TaxonDescription> results = criteria.list();
|
412
|
|
413
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
414
|
|
415
|
return results;
|
416
|
} else {
|
417
|
if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty())&& (markerTypes == null || markerTypes.isEmpty())) {
|
418
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
|
419
|
if(taxon != null) {
|
420
|
query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
|
421
|
}
|
422
|
|
423
|
if(pageSize != null) {
|
424
|
query.setMaxResults(pageSize);
|
425
|
if(pageNumber != null) {
|
426
|
query.setFirstResult(pageNumber * pageSize);
|
427
|
} else {
|
428
|
query.setFirstResult(0);
|
429
|
}
|
430
|
}
|
431
|
|
432
|
List<TaxonDescription> results = query.getResultList();
|
433
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
434
|
return results;
|
435
|
} else {
|
436
|
throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
|
437
|
}
|
438
|
}
|
439
|
}
|
440
|
|
441
|
@Override
|
442
|
public List<TaxonNameDescription> getTaxonNameDescriptions(TaxonName name, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
443
|
AuditEvent auditEvent = getAuditEventFromContext();
|
444
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
445
|
Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
|
446
|
|
447
|
if(name != null) {
|
448
|
criteria.add(Restrictions.eq("taxonName", name));
|
449
|
}
|
450
|
|
451
|
if(pageSize != null) {
|
452
|
criteria.setMaxResults(pageSize);
|
453
|
if(pageNumber != null) {
|
454
|
criteria.setFirstResult(pageNumber * pageSize);
|
455
|
}
|
456
|
}
|
457
|
|
458
|
List<TaxonNameDescription> results = criteria.list();
|
459
|
|
460
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
461
|
|
462
|
return results;
|
463
|
} else {
|
464
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameDescription.class,auditEvent.getRevisionNumber());
|
465
|
|
466
|
if(name != null) {
|
467
|
query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
|
468
|
}
|
469
|
|
470
|
if(pageSize != null) {
|
471
|
query.setMaxResults(pageSize);
|
472
|
if(pageNumber != null) {
|
473
|
query.setFirstResult(pageNumber * pageSize);
|
474
|
}
|
475
|
}
|
476
|
|
477
|
List<TaxonNameDescription> results = query.getResultList();
|
478
|
|
479
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
480
|
|
481
|
return results;
|
482
|
}
|
483
|
|
484
|
}
|
485
|
|
486
|
@Override
|
487
|
public int countTaxonNameDescriptions(TaxonName name) {
|
488
|
AuditEvent auditEvent = getAuditEventFromContext();
|
489
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
490
|
Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
|
491
|
|
492
|
if(name != null) {
|
493
|
criteria.add(Restrictions.eq("taxonName", name));
|
494
|
}
|
495
|
|
496
|
criteria.setProjection(Projections.rowCount());
|
497
|
|
498
|
return ((Number)criteria.uniqueResult()).intValue();
|
499
|
} else {
|
500
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameDescription.class,auditEvent.getRevisionNumber());
|
501
|
|
502
|
if(name != null) {
|
503
|
query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
|
504
|
}
|
505
|
|
506
|
query.addProjection(AuditEntity.id().count());
|
507
|
return ((Long)query.getSingleResult()).intValue();
|
508
|
}
|
509
|
}
|
510
|
|
511
|
/**
|
512
|
* Should use a DetachedCriteria & subquery, but HHH-158 prevents this, for now.
|
513
|
*
|
514
|
* e.g. DetachedCriteria inner = DestachedCriteria.forClass(type);
|
515
|
*
|
516
|
* outer.add(Subqueries.propertyIn("id", inner));
|
517
|
*/
|
518
|
@Override
|
519
|
public List<DescriptionBase> listDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText, Set<Feature> features, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
520
|
checkNotInPriorView("DescriptionDaoImpl.listDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText, Set<Feature> features, Integer pageSize, Integer pageNumber)");
|
521
|
Criteria inner = null;
|
522
|
|
523
|
if(clazz == null) {
|
524
|
inner = getSession().createCriteria(type);
|
525
|
} else {
|
526
|
inner = getSession().createCriteria(clazz);
|
527
|
}
|
528
|
|
529
|
Criteria elementsCriteria = inner.createCriteria("descriptionElements");
|
530
|
if(hasText != null) {
|
531
|
if(hasText) {
|
532
|
elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
|
533
|
} else {
|
534
|
elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
|
535
|
}
|
536
|
}
|
537
|
|
538
|
if(hasImages != null) {
|
539
|
if(hasImages) {
|
540
|
elementsCriteria.add(Restrictions.isNotEmpty("media"));
|
541
|
} else {
|
542
|
elementsCriteria.add(Restrictions.isEmpty("media"));
|
543
|
}
|
544
|
}
|
545
|
|
546
|
if(features != null && !features.isEmpty()) {
|
547
|
elementsCriteria.add(Restrictions.in("feature", features));
|
548
|
}
|
549
|
|
550
|
inner.setProjection(Projections.distinct(Projections.id()));
|
551
|
|
552
|
List<Object> intermediateResult = inner.list();
|
553
|
|
554
|
if(intermediateResult.isEmpty()) {
|
555
|
return new ArrayList<DescriptionBase>();
|
556
|
}
|
557
|
|
558
|
Integer[] resultIds = new Integer[intermediateResult.size()];
|
559
|
for(int i = 0; i < resultIds.length; i++) {
|
560
|
resultIds[i] = ((Number)intermediateResult.get(i)).intValue();
|
561
|
}
|
562
|
|
563
|
Criteria outer = null;
|
564
|
|
565
|
if(clazz == null) {
|
566
|
outer = getSession().createCriteria(type);
|
567
|
} else {
|
568
|
outer = getSession().createCriteria(clazz);
|
569
|
}
|
570
|
|
571
|
outer.add(Restrictions.in("id", resultIds));
|
572
|
addOrder(outer, orderHints);
|
573
|
|
574
|
if(pageSize != null) {
|
575
|
outer.setMaxResults(pageSize);
|
576
|
if(pageNumber != null) {
|
577
|
outer.setFirstResult(pageNumber * pageSize);
|
578
|
}
|
579
|
}
|
580
|
|
581
|
List<DescriptionBase> results = outer.list();
|
582
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
583
|
return results;
|
584
|
}
|
585
|
|
586
|
@Override
|
587
|
public List<TaxonDescription> searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTerm status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
588
|
checkNotInPriorView("DescriptionDaoImpl.searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
589
|
|
590
|
Criteria criteria = getSession().createCriteria(TaxonDescription.class);
|
591
|
Criteria elements = criteria.createCriteria("descriptionElements", "descriptionElement", Criteria.LEFT_JOIN);
|
592
|
elements.add(Restrictions.in("area", namedAreas.toArray()));
|
593
|
|
594
|
if(status != null) {
|
595
|
elements.add(Restrictions.eq("status", status));
|
596
|
}
|
597
|
|
598
|
ProjectionList projectionList = Projections.projectionList().add(Projections.id());
|
599
|
|
600
|
if(orderHints != null && !orderHints.isEmpty()) {
|
601
|
for(OrderHint orderHint : orderHints) {
|
602
|
projectionList = projectionList.add(Projections.property(orderHint.getPropertyName()));
|
603
|
}
|
604
|
}
|
605
|
|
606
|
criteria.setProjection(Projections.distinct(projectionList));
|
607
|
|
608
|
if(pageSize != null) {
|
609
|
criteria.setMaxResults(pageSize);
|
610
|
if(pageNumber != null) {
|
611
|
criteria.setFirstResult(pageNumber * pageSize);
|
612
|
}
|
613
|
}
|
614
|
|
615
|
addOrder(criteria,orderHints);
|
616
|
|
617
|
List<Object> intermediateResult = criteria.list();
|
618
|
|
619
|
if(intermediateResult.isEmpty()) {
|
620
|
return new ArrayList<TaxonDescription>();
|
621
|
}
|
622
|
|
623
|
Integer[] resultIds = new Integer[intermediateResult.size()];
|
624
|
for(int i = 0; i < resultIds.length; i++) {
|
625
|
if(orderHints == null || orderHints.isEmpty()) {
|
626
|
resultIds[i] = ((Number)intermediateResult.get(i)).intValue();
|
627
|
} else {
|
628
|
resultIds[i] = ((Number)((Object[])intermediateResult.get(i))[0]).intValue();
|
629
|
}
|
630
|
}
|
631
|
|
632
|
criteria = getSession().createCriteria(TaxonDescription.class);
|
633
|
criteria.add(Restrictions.in("id", resultIds));
|
634
|
addOrder(criteria,orderHints);
|
635
|
|
636
|
List<TaxonDescription> results = criteria.list();
|
637
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
638
|
return results;
|
639
|
}
|
640
|
|
641
|
@Override
|
642
|
public List<CommonTaxonName> searchDescriptionByCommonName(String queryString, MatchMode matchMode, Integer pageSize, Integer pageNumber) {
|
643
|
|
644
|
Criteria crit = getSession().createCriteria(CommonTaxonName.class);
|
645
|
if (matchMode == MatchMode.EXACT) {
|
646
|
crit.add(Restrictions.eq("name", matchMode.queryStringFrom(queryString)));
|
647
|
} else {
|
648
|
crit.add(Restrictions.ilike("name", matchMode.queryStringFrom(queryString)));
|
649
|
}
|
650
|
|
651
|
if(pageSize != null) {
|
652
|
crit.setMaxResults(pageSize);
|
653
|
if(pageNumber != null) {
|
654
|
crit.setFirstResult(pageNumber * pageSize);
|
655
|
}
|
656
|
}
|
657
|
List<CommonTaxonName> results = crit.list();
|
658
|
return results;
|
659
|
}
|
660
|
|
661
|
@Override
|
662
|
public Integer countDescriptionByCommonName(String queryString, MatchMode matchMode) {
|
663
|
//TODO inprove performance
|
664
|
List<CommonTaxonName> results = searchDescriptionByCommonName(queryString, matchMode, null, null);
|
665
|
return results.size();
|
666
|
}
|
667
|
|
668
|
@Override
|
669
|
public DescriptionBase find(LSID lsid) {
|
670
|
DescriptionBase descriptionBase = super.find(lsid);
|
671
|
if(descriptionBase != null) {
|
672
|
List<String> propertyPaths = new ArrayList<String>();
|
673
|
propertyPaths.add("createdBy");
|
674
|
propertyPaths.add("updatedBy");
|
675
|
propertyPaths.add("taxon");
|
676
|
propertyPaths.add("taxonName");
|
677
|
propertyPaths.add("descriptionElements");
|
678
|
propertyPaths.add("descriptionElements.createdBy");
|
679
|
propertyPaths.add("descriptionElements.updatedBy");
|
680
|
propertyPaths.add("descriptionElements.feature");
|
681
|
propertyPaths.add("descriptionElements.multilanguageText");
|
682
|
propertyPaths.add("descriptionElements.multilanguageText.language");
|
683
|
propertyPaths.add("descriptionElements.area");
|
684
|
propertyPaths.add("descriptionElements.status");
|
685
|
propertyPaths.add("descriptionElements.modifyingText");
|
686
|
propertyPaths.add("descriptionElementsmodifyingText.language");
|
687
|
propertyPaths.add("descriptionElements.modifiers");
|
688
|
|
689
|
defaultBeanInitializer.initialize(descriptionBase, propertyPaths);
|
690
|
}
|
691
|
return descriptionBase;
|
692
|
}
|
693
|
|
694
|
|
695
|
@Override
|
696
|
public List<Integer> getIndividualAssociationSpecimenIDs(UUID taxonUuid,
|
697
|
Set<Feature> features, Integer pageSize,
|
698
|
Integer pageNumber, List<String> propertyPaths){
|
699
|
Query query = prepareGetDescriptionElementForTaxon(taxonUuid, features, IndividualsAssociation.class, pageSize, pageNumber, "de.associatedSpecimenOrObservation.id");
|
700
|
@SuppressWarnings("unchecked")
|
701
|
List<Integer> results = query.list();
|
702
|
return results;
|
703
|
}
|
704
|
|
705
|
@Override
|
706
|
public <T extends DescriptionElementBase> List<T> getDescriptionElementForTaxon(
|
707
|
UUID taxonUuid, Set<Feature> features,
|
708
|
Class<T> type, Integer pageSize,
|
709
|
Integer pageNumber, List<String> propertyPaths) {
|
710
|
|
711
|
// Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
|
712
|
|
713
|
Query query = prepareGetDescriptionElementForTaxon(taxonUuid, features, type, pageSize, pageNumber, "de");
|
714
|
|
715
|
if (logger.isDebugEnabled()){logger.debug(" dao: get list ...");}
|
716
|
@SuppressWarnings("unchecked")
|
717
|
List<T> results = query.list();
|
718
|
if (logger.isDebugEnabled()){logger.debug(" dao: initialize ...");}
|
719
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
720
|
if (logger.isDebugEnabled()){logger.debug(" dao: initialize - DONE");}
|
721
|
|
722
|
// Logger.getLogger("org.hibernate.SQL").setLevel(Level.WARN);
|
723
|
return results;
|
724
|
}
|
725
|
|
726
|
@Override
|
727
|
public <T extends DescriptionElementBase> long countDescriptionElementForTaxon(
|
728
|
UUID taxonUuid, Set<Feature> features, Class<T> type) {
|
729
|
|
730
|
Query query = prepareGetDescriptionElementForTaxon(taxonUuid, features, type, null, null, "count(de)");
|
731
|
|
732
|
return (Long)query.uniqueResult();
|
733
|
}
|
734
|
|
735
|
private <T extends DescriptionElementBase> Query prepareGetDescriptionElementForTaxon(UUID taxonUuid,
|
736
|
Set<Feature> features, Class<T> type, Integer pageSize, Integer pageNumber, String selectString) {
|
737
|
|
738
|
String queryString = "SELECT " + selectString + " FROM DescriptionElementBase AS de" +
|
739
|
" LEFT JOIN de.inDescription AS d" +
|
740
|
" LEFT JOIN d.taxon AS t" +
|
741
|
" WHERE d.class = 'TaxonDescription' AND t.uuid = :taxon_uuid ";
|
742
|
|
743
|
if(type != null){
|
744
|
queryString += " and de.class = :type";
|
745
|
}
|
746
|
if (features != null && features.size() > 0){
|
747
|
queryString += " and de.feature in (:features) ";
|
748
|
}
|
749
|
// System.out.println(queryString);
|
750
|
Query query = getSession().createQuery(queryString);
|
751
|
|
752
|
query.setParameter("taxon_uuid", taxonUuid);
|
753
|
if(type != null){
|
754
|
query.setParameter("type", type.getSimpleName());
|
755
|
}
|
756
|
if(features != null && features.size() > 0){
|
757
|
query.setParameterList("features", features) ;
|
758
|
}
|
759
|
|
760
|
if(pageSize != null) {
|
761
|
query.setMaxResults(pageSize);
|
762
|
if(pageNumber != null) {
|
763
|
query.setFirstResult(pageNumber * pageSize);
|
764
|
}
|
765
|
}
|
766
|
return query;
|
767
|
}
|
768
|
|
769
|
/* (non-Javadoc)
|
770
|
* @see eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao#listTaxonDescriptionMedia(java.util.UUID, java.lang.Boolean, java.util.Set, java.lang.Integer, java.lang.Integer, java.util.List)
|
771
|
*/
|
772
|
@Override
|
773
|
public List<Media> listTaxonDescriptionMedia(UUID taxonUuid,
|
774
|
Boolean limitToGalleries, Set<MarkerType> markerTypes,
|
775
|
Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
776
|
|
777
|
AuditEvent auditEvent = getAuditEventFromContext();
|
778
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
779
|
String queryString = " SELECT media " +
|
780
|
getTaxonDescriptionMediaQueryString(
|
781
|
taxonUuid, limitToGalleries, markerTypes);
|
782
|
queryString +=
|
783
|
" GROUP BY media "
|
784
|
// " ORDER BY index(media) " //not functional
|
785
|
;
|
786
|
|
787
|
Query query = getSession().createQuery(queryString);
|
788
|
|
789
|
setTaxonDescriptionMediaParameters(query, taxonUuid, limitToGalleries, markerTypes);
|
790
|
|
791
|
|
792
|
// addMarkerTypesCriterion(markerTypes, hql);
|
793
|
|
794
|
setPagingParameter(query, pageSize, pageNumber);
|
795
|
|
796
|
List<Media> results = query.list();
|
797
|
|
798
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
799
|
|
800
|
return results;
|
801
|
} else {
|
802
|
throw new OperationNotSupportedInPriorViewException("countTaxonDescriptionMedia(UUID taxonUuid, boolean restrictToGalleries)");
|
803
|
}
|
804
|
}
|
805
|
|
806
|
|
807
|
/* (non-Javadoc)
|
808
|
* @see eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao#countTaxonDescriptionMedia(java.util.UUID, java.lang.Boolean, java.util.Set)
|
809
|
*/
|
810
|
@Override
|
811
|
public int countTaxonDescriptionMedia(UUID taxonUuid,
|
812
|
Boolean limitToGalleries, Set<MarkerType> markerTypes) {
|
813
|
AuditEvent auditEvent = getAuditEventFromContext();
|
814
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
815
|
String queryString = " SELECT count(DISTINCT media) " +
|
816
|
getTaxonDescriptionMediaQueryString(
|
817
|
taxonUuid, limitToGalleries, markerTypes);
|
818
|
|
819
|
Query query = getSession().createQuery(queryString);
|
820
|
setTaxonDescriptionMediaParameters(query, taxonUuid, limitToGalleries, markerTypes);
|
821
|
return ((Long)query.uniqueResult()).intValue();
|
822
|
}else{
|
823
|
throw new OperationNotSupportedInPriorViewException("countTaxonDescriptionMedia(UUID taxonUuid)");
|
824
|
}
|
825
|
|
826
|
}
|
827
|
|
828
|
private void setTaxonDescriptionMediaParameters(Query query, UUID taxonUuid, Boolean limitToGalleries, Set<MarkerType> markerTypes) {
|
829
|
if(taxonUuid != null){
|
830
|
query.setParameter("uuid", taxonUuid);
|
831
|
}
|
832
|
|
833
|
}
|
834
|
|
835
|
/**
|
836
|
* @param taxonUuid
|
837
|
* @param restrictToGalleries
|
838
|
* @param markerTypes
|
839
|
* @return
|
840
|
*/
|
841
|
private String getTaxonDescriptionMediaQueryString(UUID taxonUuid,
|
842
|
Boolean restrictToGalleries, Set<MarkerType> markerTypes) {
|
843
|
String fromQueryString =
|
844
|
" FROM DescriptionElementBase as deb INNER JOIN " +
|
845
|
" deb.inDescription as td "
|
846
|
+ " INNER JOIN td.taxon as t "
|
847
|
+ " JOIN deb.media as media "
|
848
|
+ " LEFT JOIN td.markers marker ";
|
849
|
|
850
|
String whereQueryString = " WHERE (1=1) ";
|
851
|
if (taxonUuid != null){
|
852
|
whereQueryString += " AND t.uuid = :uuid ";
|
853
|
}
|
854
|
if (restrictToGalleries){
|
855
|
whereQueryString += " AND td.imageGallery is true ";
|
856
|
}
|
857
|
if (markerTypes != null && !markerTypes.isEmpty()){
|
858
|
whereQueryString += " AND (1=0";
|
859
|
for (MarkerType markerType : markerTypes){
|
860
|
whereQueryString += " OR ( marker.markerType.id = " + markerType.getId() + " AND marker.flag is true)";
|
861
|
|
862
|
}
|
863
|
whereQueryString += ") ";
|
864
|
}
|
865
|
|
866
|
return fromQueryString + whereQueryString;
|
867
|
|
868
|
}
|
869
|
|
870
|
/* (non-Javadoc)
|
871
|
* @see eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao#listNamedAreasInUse(java.lang.Integer, java.lang.Integer, java.util.List)
|
872
|
*/
|
873
|
@SuppressWarnings("unchecked")
|
874
|
@Override
|
875
|
public List<TermDto> listNamedAreasInUse(boolean includeAllParents, Integer pageSize, Integer pageNumber) {
|
876
|
|
877
|
// Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
|
878
|
|
879
|
StringBuilder queryString = new StringBuilder(
|
880
|
"SELECT DISTINCT a.id, a.partOf.id"
|
881
|
+ " FROM Distribution AS d JOIN d.area AS a");
|
882
|
Query query = getSession().createQuery(queryString.toString());
|
883
|
|
884
|
List<Object[]> areasInUse = query.list();
|
885
|
List<Object[]> parentResults = new ArrayList<Object[]>();
|
886
|
|
887
|
if(!areasInUse.isEmpty()) {
|
888
|
Set<Object> allAreaIds = new HashSet<Object>(areasInUse.size());
|
889
|
|
890
|
if(includeAllParents) {
|
891
|
// find all parent nodes
|
892
|
String allAreasQueryStr = "select a.id, a.partOf.id from NamedArea as a";
|
893
|
query = getSession().createQuery(allAreasQueryStr);
|
894
|
List<Object[]> allAreasResult = query.list();
|
895
|
Map<Object, Object> allAreasMap = ArrayUtils.toMap(allAreasResult.toArray());
|
896
|
|
897
|
Set<Object> parents = new HashSet<Object>();
|
898
|
|
899
|
for(Object[] leaf : areasInUse) {
|
900
|
allAreaIds.add(leaf[0]);
|
901
|
Object parentId = leaf[1];
|
902
|
while (parentId != null) {
|
903
|
if(parents.contains(parentId)) {
|
904
|
// break if the parent already is in the set
|
905
|
break;
|
906
|
}
|
907
|
parents.add(parentId);
|
908
|
parentId = allAreasMap.get(parentId);
|
909
|
}
|
910
|
}
|
911
|
allAreaIds.addAll(parents);
|
912
|
} else {
|
913
|
// only add the ids found so far
|
914
|
for(Object[] leaf : areasInUse) {
|
915
|
allAreaIds.add(leaf[0]);
|
916
|
}
|
917
|
}
|
918
|
|
919
|
|
920
|
// NOTE can't use "select new TermDto(distinct a.uuid, r , a.vocabulary.uuid) since we will get multiple
|
921
|
// rows for a term with multiple representations
|
922
|
String parentAreasQueryStr = "select a.uuid, r, p.uuid, v.uuid, a.orderIndex "
|
923
|
+ "from NamedArea as a LEFT JOIN a.partOf as p LEFT JOIN a.representations AS r LEFT JOIN a.vocabulary as v "
|
924
|
+ "where a.id in (:allAreaIds) order by a.idInVocabulary";
|
925
|
query = getSession().createQuery(parentAreasQueryStr);
|
926
|
query.setParameterList("allAreaIds", allAreaIds);
|
927
|
if(pageSize != null) {
|
928
|
query.setMaxResults(pageSize);
|
929
|
if(pageNumber != null) {
|
930
|
query.setFirstResult(pageNumber * pageSize);
|
931
|
}
|
932
|
}
|
933
|
parentResults = query.list();
|
934
|
}
|
935
|
List<TermDto> dtoList = termDtoListFrom(parentResults);
|
936
|
|
937
|
return dtoList;
|
938
|
}
|
939
|
|
940
|
/**
|
941
|
* @param results
|
942
|
* @return
|
943
|
*/
|
944
|
private List<TermDto> termDtoListFrom(List<Object[]> results) {
|
945
|
Map<UUID, TermDto> dtoMap = new HashMap<UUID, TermDto>(results.size());
|
946
|
for (Object[] elements : results) {
|
947
|
UUID uuid = (UUID)elements[0];
|
948
|
if(dtoMap.containsKey(uuid)){
|
949
|
dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
|
950
|
} else {
|
951
|
Set<Representation> representations;
|
952
|
if(elements[1] instanceof Representation) {
|
953
|
representations = new HashSet<Representation>(1);
|
954
|
representations.add((Representation)elements[1]);
|
955
|
} else {
|
956
|
representations = (Set<Representation>)elements[1];
|
957
|
}
|
958
|
dtoMap.put(uuid, new TermDto(uuid, representations, (UUID)elements[2], (UUID)elements[3], (Integer)elements[4]));
|
959
|
}
|
960
|
}
|
961
|
return new ArrayList<TermDto>(dtoMap.values());
|
962
|
}
|
963
|
|
964
|
|
965
|
|
966
|
}
|