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.name;
|
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.Criteria;
|
19
|
import org.hibernate.Query;
|
20
|
import org.hibernate.criterion.Criterion;
|
21
|
import org.hibernate.criterion.Order;
|
22
|
import org.hibernate.criterion.Projections;
|
23
|
import org.hibernate.criterion.Restrictions;
|
24
|
import org.hibernate.envers.query.AuditEntity;
|
25
|
import org.hibernate.envers.query.AuditQuery;
|
26
|
import org.springframework.beans.factory.annotation.Autowired;
|
27
|
import org.springframework.beans.factory.annotation.Qualifier;
|
28
|
import org.springframework.stereotype.Repository;
|
29
|
|
30
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
31
|
import eu.etaxonomy.cdm.model.common.RelationshipBase;
|
32
|
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
|
33
|
import eu.etaxonomy.cdm.model.name.BacterialName;
|
34
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
35
|
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
|
36
|
import eu.etaxonomy.cdm.model.name.HybridRelationship;
|
37
|
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
|
38
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
39
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
40
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
41
|
import eu.etaxonomy.cdm.model.name.Rank;
|
42
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
43
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
44
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
45
|
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
|
46
|
import eu.etaxonomy.cdm.model.name.ViralName;
|
47
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
48
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
49
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
50
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
|
51
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
52
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
53
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
54
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
55
|
|
56
|
/**
|
57
|
* @author a.mueller
|
58
|
*
|
59
|
*/
|
60
|
@Repository
|
61
|
@Qualifier("taxonNameDaoHibernateImpl")
|
62
|
public class TaxonNameDaoHibernateImpl extends IdentifiableDaoBase<TaxonNameBase> implements ITaxonNameDao {
|
63
|
|
64
|
private static final Logger logger = Logger.getLogger(TaxonNameDaoHibernateImpl.class);
|
65
|
|
66
|
@Autowired
|
67
|
private ITaxonDao taxonDao;
|
68
|
|
69
|
public TaxonNameDaoHibernateImpl() {
|
70
|
super(TaxonNameBase.class);
|
71
|
indexedClasses = new Class[6];
|
72
|
indexedClasses[0] = BacterialName.class;
|
73
|
indexedClasses[1] = BotanicalName.class;
|
74
|
indexedClasses[2] = CultivarPlantName.class;
|
75
|
indexedClasses[3] = NonViralName.class;
|
76
|
indexedClasses[4] = ViralName.class;
|
77
|
indexedClasses[5] = ZoologicalName.class;
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public int countHybridNames(NonViralName name, HybridRelationshipType type) {
|
82
|
AuditEvent auditEvent = getAuditEventFromContext();
|
83
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
84
|
Query query = null;
|
85
|
if(type == null) {
|
86
|
query = getSession().createQuery("select count(relation) from HybridRelationship relation where relation.relatedFrom = :name");
|
87
|
} else {
|
88
|
query = getSession().createQuery("select count(relation) from HybridRelationship relation where relation.relatedFrom = :name and relation.type = :type");
|
89
|
query.setParameter("type", type);
|
90
|
}
|
91
|
query.setParameter("name",name);
|
92
|
return ((Long)query.uniqueResult()).intValue();
|
93
|
} else {
|
94
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(HybridRelationship.class,auditEvent.getRevisionNumber());
|
95
|
query.add(AuditEntity.relatedId("relatedFrom").eq(name.getId()));
|
96
|
query.addProjection(AuditEntity.id().count("id"));
|
97
|
|
98
|
if(type != null) {
|
99
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
100
|
}
|
101
|
|
102
|
return ((Long)query.getSingleResult()).intValue();
|
103
|
}
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public int countNames(String queryString) {
|
108
|
checkNotInPriorView("TaxonNameDaoHibernateImpl.countNames(String queryString)");
|
109
|
Criteria criteria = getSession().createCriteria(TaxonNameBase.class);
|
110
|
|
111
|
if (queryString != null) {
|
112
|
criteria.add(Restrictions.ilike("nameCache", queryString));
|
113
|
}
|
114
|
criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));
|
115
|
|
116
|
return ((Number)criteria.uniqueResult()).intValue();
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public int countNames(String queryString, MatchMode matchMode, List<Criterion> criteria) {
|
121
|
|
122
|
Criteria crit = getSession().createCriteria(type);
|
123
|
if (matchMode == MatchMode.EXACT) {
|
124
|
crit.add(Restrictions.eq("nameCache", matchMode.queryStringFrom(queryString)));
|
125
|
} else {
|
126
|
crit.add(Restrictions.ilike("nameCache", matchMode.queryStringFrom(queryString)));
|
127
|
}
|
128
|
if(criteria != null) {
|
129
|
for (Criterion criterion : criteria) {
|
130
|
crit.add(criterion);
|
131
|
}
|
132
|
}
|
133
|
|
134
|
crit.setProjection(Projections.projectionList().add(Projections.rowCount()));
|
135
|
return ((Number)crit.uniqueResult()).intValue();
|
136
|
}
|
137
|
|
138
|
@Override
|
139
|
public int countNames(String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank) {
|
140
|
AuditEvent auditEvent = getAuditEventFromContext();
|
141
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
142
|
Criteria criteria = getSession().createCriteria(TaxonNameBase.class);
|
143
|
|
144
|
/**
|
145
|
* Given HHH-2951 - "Restrictions.eq when passed null, should create a NullRestriction"
|
146
|
* We need to convert nulls to NullRestrictions for now
|
147
|
*/
|
148
|
if(genusOrUninomial != null) {
|
149
|
criteria.add(Restrictions.eq("genusOrUninomial",genusOrUninomial));
|
150
|
} else {
|
151
|
criteria.add(Restrictions.isNull("genusOrUninomial"));
|
152
|
}
|
153
|
|
154
|
if(infraGenericEpithet != null) {
|
155
|
criteria.add(Restrictions.eq("infraGenericEpithet", infraGenericEpithet));
|
156
|
} else {
|
157
|
criteria.add(Restrictions.isNull("infraGenericEpithet"));
|
158
|
}
|
159
|
|
160
|
if(specificEpithet != null) {
|
161
|
criteria.add(Restrictions.eq("specificEpithet", specificEpithet));
|
162
|
} else {
|
163
|
criteria.add(Restrictions.isNull("specificEpithet"));
|
164
|
}
|
165
|
|
166
|
if(infraSpecificEpithet != null) {
|
167
|
criteria.add(Restrictions.eq("infraSpecificEpithet",infraSpecificEpithet));
|
168
|
} else {
|
169
|
criteria.add(Restrictions.isNull("infraSpecificEpithet"));
|
170
|
}
|
171
|
|
172
|
if(rank != null) {
|
173
|
criteria.add(Restrictions.eq("rank", rank));
|
174
|
}
|
175
|
|
176
|
criteria.setProjection(Projections.rowCount());
|
177
|
return ((Number)criteria.uniqueResult()).intValue();
|
178
|
} else {
|
179
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameBase.class,auditEvent.getRevisionNumber());
|
180
|
|
181
|
if(genusOrUninomial != null) {
|
182
|
query.add(AuditEntity.property("genusOrUninomial").eq(genusOrUninomial));
|
183
|
} else {
|
184
|
query.add(AuditEntity.property("genusOrUninomial").isNull());
|
185
|
}
|
186
|
|
187
|
if(infraGenericEpithet != null) {
|
188
|
query.add(AuditEntity.property("infraGenericEpithet").eq(infraGenericEpithet));
|
189
|
} else {
|
190
|
query.add(AuditEntity.property("infraGenericEpithet").isNull());
|
191
|
}
|
192
|
|
193
|
if(specificEpithet != null) {
|
194
|
query.add(AuditEntity.property("specificEpithet").eq(specificEpithet));
|
195
|
} else {
|
196
|
query.add(AuditEntity.property("specificEpithet").isNull());
|
197
|
}
|
198
|
|
199
|
if(infraSpecificEpithet != null) {
|
200
|
query.add(AuditEntity.property("infraSpecificEpithet").eq(infraSpecificEpithet));
|
201
|
} else {
|
202
|
query.add(AuditEntity.property("infraSpecificEpithet").isNull());
|
203
|
}
|
204
|
|
205
|
if(rank != null) {
|
206
|
query.add(AuditEntity.relatedId("rank").eq(rank.getId()));
|
207
|
}
|
208
|
|
209
|
query.addProjection(AuditEntity.id().count("id"));
|
210
|
return ((Long)query.getSingleResult()).intValue();
|
211
|
}
|
212
|
}
|
213
|
|
214
|
@Override
|
215
|
public int countNameRelationships(TaxonNameBase name, NameRelationship.Direction direction, NameRelationshipType type) {
|
216
|
|
217
|
AuditEvent auditEvent = getAuditEventFromContext();
|
218
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
219
|
Query query = null;
|
220
|
if(type == null) {
|
221
|
query = getSession().createQuery("select count(relation) from NameRelationship relation where relation." + direction +" = :name");
|
222
|
} else {
|
223
|
query = getSession().createQuery("select count(relation) from NameRelationship relation where relation." + direction +" = :name and relation.type = :type");
|
224
|
query.setParameter("type", type);
|
225
|
}
|
226
|
query.setParameter("name",name);
|
227
|
return ((Long)query.uniqueResult()).intValue();
|
228
|
} else {
|
229
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(NameRelationship.class,auditEvent.getRevisionNumber());
|
230
|
query.add(AuditEntity.relatedId(direction.toString()).eq(name.getId()));
|
231
|
query.addProjection(AuditEntity.id().count("id"));
|
232
|
|
233
|
if(type != null) {
|
234
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
235
|
}
|
236
|
|
237
|
return ((Long)query.getSingleResult()).intValue();
|
238
|
}
|
239
|
}
|
240
|
|
241
|
|
242
|
@Override
|
243
|
public int countTypeDesignations(TaxonNameBase name, SpecimenTypeDesignationStatus status) {
|
244
|
checkNotInPriorView("countTypeDesignations(TaxonNameBase name, SpecimenTypeDesignationStatus status)");
|
245
|
Query query = null;
|
246
|
if(status == null) {
|
247
|
query = getSession().createQuery("select count(designation) from TypeDesignationBase designation join designation.typifiedNames name where name = :name");
|
248
|
} else {
|
249
|
query = getSession().createQuery("select count(designation) from TypeDesignationBase designation join designation.typifiedNames name where name = :name and designation.typeStatus = :status");
|
250
|
query.setParameter("status", status);
|
251
|
}
|
252
|
query.setParameter("name",name);
|
253
|
return ((Long)query.uniqueResult()).intValue();
|
254
|
}
|
255
|
|
256
|
@Override
|
257
|
public List<HybridRelationship> getHybridNames(NonViralName name, HybridRelationshipType type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
258
|
AuditEvent auditEvent = getAuditEventFromContext();
|
259
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
260
|
Criteria criteria = getSession().createCriteria(HybridRelationship.class);
|
261
|
criteria.add(Restrictions.eq("relatedFrom", name));
|
262
|
if(type != null) {
|
263
|
criteria.add(Restrictions.eq("type", type));
|
264
|
}
|
265
|
|
266
|
if(pageSize != null) {
|
267
|
criteria.setMaxResults(pageSize);
|
268
|
if(pageNumber != null) {
|
269
|
criteria.setFirstResult(pageNumber * pageSize);
|
270
|
} else {
|
271
|
criteria.setFirstResult(0);
|
272
|
}
|
273
|
}
|
274
|
|
275
|
addOrder(criteria, orderHints);
|
276
|
|
277
|
List<HybridRelationship> results = criteria.list();
|
278
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
279
|
return results;
|
280
|
} else {
|
281
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(HybridRelationship.class,auditEvent.getRevisionNumber());
|
282
|
query.add(AuditEntity.relatedId("relatedFrom").eq(name.getId()));
|
283
|
|
284
|
if(type != null) {
|
285
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
286
|
}
|
287
|
|
288
|
if(pageSize != null) {
|
289
|
query.setMaxResults(pageSize);
|
290
|
if(pageNumber != null) {
|
291
|
query.setFirstResult(pageNumber * pageSize);
|
292
|
} else {
|
293
|
query.setFirstResult(0);
|
294
|
}
|
295
|
}
|
296
|
|
297
|
List<HybridRelationship> results = query.getResultList();
|
298
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
299
|
return results;
|
300
|
}
|
301
|
}
|
302
|
|
303
|
/* (non-Javadoc)
|
304
|
* @see eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao#getNameRelationships(eu.etaxonomy.cdm.model.name.TaxonNameBase, eu.etaxonomy.cdm.model.common.RelationshipBase.Direction, eu.etaxonomy.cdm.model.name.NameRelationshipType, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
|
305
|
*/
|
306
|
@Override
|
307
|
public List<NameRelationship> getNameRelationships(TaxonNameBase name, NameRelationship.Direction direction,
|
308
|
NameRelationshipType type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
|
309
|
List<String> propertyPaths) {
|
310
|
|
311
|
AuditEvent auditEvent = getAuditEventFromContext();
|
312
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
313
|
Criteria criteria = getSession().createCriteria(NameRelationship.class);
|
314
|
if (name != null || direction != null){
|
315
|
criteria.add(Restrictions.eq(direction.toString(), name));
|
316
|
}
|
317
|
if(type != null) {
|
318
|
criteria.add(Restrictions.eq("type", type));
|
319
|
}
|
320
|
|
321
|
if(pageSize != null) {
|
322
|
criteria.setMaxResults(pageSize);
|
323
|
if(pageNumber != null) {
|
324
|
criteria.setFirstResult(pageNumber * pageSize);
|
325
|
} else {
|
326
|
criteria.setFirstResult(0);
|
327
|
}
|
328
|
}
|
329
|
addOrder(criteria, orderHints);
|
330
|
|
331
|
List<NameRelationship> results = criteria.list();
|
332
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
333
|
return results;
|
334
|
} else {
|
335
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(NameRelationship.class,auditEvent.getRevisionNumber());
|
336
|
query.add(AuditEntity.relatedId(direction.toString()).eq(name.getId()));
|
337
|
|
338
|
if(type != null) {
|
339
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
340
|
}
|
341
|
|
342
|
if(pageSize != null) {
|
343
|
query.setMaxResults(pageSize);
|
344
|
if(pageNumber != null) {
|
345
|
query.setFirstResult(pageNumber * pageSize);
|
346
|
} else {
|
347
|
query.setFirstResult(0);
|
348
|
}
|
349
|
}
|
350
|
|
351
|
List<NameRelationship> results = query.getResultList();
|
352
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
353
|
return results;
|
354
|
}
|
355
|
}
|
356
|
|
357
|
@Override
|
358
|
public List<TypeDesignationBase> getTypeDesignations(TaxonNameBase name, TypeDesignationStatusBase status, Integer pageSize, Integer pageNumber, List<String> propertyPaths){
|
359
|
return getTypeDesignations(name, null, status, pageSize, pageNumber, propertyPaths);
|
360
|
}
|
361
|
|
362
|
@Override
|
363
|
public <T extends TypeDesignationBase> List<T> getTypeDesignations(TaxonNameBase name,
|
364
|
Class<T> type,
|
365
|
TypeDesignationStatusBase status, Integer pageSize, Integer pageNumber,
|
366
|
List<String> propertyPaths){
|
367
|
checkNotInPriorView("getTypeDesignations(TaxonNameBase name,TypeDesignationStatusBase status, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
|
368
|
Query query = null;
|
369
|
String queryString = "select designation from TypeDesignationBase designation join designation.typifiedNames name where name = :name";
|
370
|
|
371
|
if(status != null) {
|
372
|
queryString += " and designation.typeStatus = :status";
|
373
|
}
|
374
|
if(type != null){
|
375
|
queryString += " and designation.class = :type";
|
376
|
}
|
377
|
|
378
|
query = getSession().createQuery(queryString);
|
379
|
|
380
|
if(status != null) {
|
381
|
query.setParameter("status", status);
|
382
|
}
|
383
|
if(type != null){
|
384
|
query.setParameter("type", type.getSimpleName());
|
385
|
}
|
386
|
|
387
|
query.setParameter("name",name);
|
388
|
|
389
|
if(pageSize != null) {
|
390
|
query.setMaxResults(pageSize);
|
391
|
if(pageNumber != null) {
|
392
|
query.setFirstResult(pageNumber * pageSize);
|
393
|
} else {
|
394
|
query.setFirstResult(0);
|
395
|
}
|
396
|
}
|
397
|
return defaultBeanInitializer.initializeAll((List<T>)query.list(), propertyPaths);
|
398
|
}
|
399
|
|
400
|
|
401
|
public List<TaxonNameBase<?,?>> searchNames(String queryString, MatchMode matchMode, Integer pageSize, Integer pageNumber) {
|
402
|
checkNotInPriorView("TaxonNameDaoHibernateImpl.searchNames(String queryString, Integer pageSize, Integer pageNumber)");
|
403
|
Criteria criteria = getSession().createCriteria(TaxonNameBase.class);
|
404
|
|
405
|
if (queryString != null) {
|
406
|
criteria.add(Restrictions.ilike("nameCache", queryString));
|
407
|
}
|
408
|
if(pageSize != null) {
|
409
|
criteria.setMaxResults(pageSize);
|
410
|
if(pageNumber != null) {
|
411
|
criteria.setFirstResult(pageNumber * pageSize);
|
412
|
} else {
|
413
|
criteria.setFirstResult(0);
|
414
|
}
|
415
|
}
|
416
|
List<TaxonNameBase<?,?>> results = criteria.list();
|
417
|
return results;
|
418
|
}
|
419
|
|
420
|
|
421
|
@Override
|
422
|
public List<TaxonNameBase<?,?>> searchNames(String queryString, Integer pageSize, Integer pageNumber) {
|
423
|
return searchNames(queryString, MatchMode.BEGINNING, pageSize, pageNumber);
|
424
|
}
|
425
|
|
426
|
|
427
|
@Override
|
428
|
public List<TaxonNameBase> searchNames(String genusOrUninomial,String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank, Integer pageSize,Integer pageNumber, List<OrderHint> orderHints,
|
429
|
List<String> propertyPaths) {
|
430
|
AuditEvent auditEvent = getAuditEventFromContext();
|
431
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
432
|
Criteria criteria = getSession().createCriteria(TaxonNameBase.class);
|
433
|
|
434
|
/**
|
435
|
* Given HHH-2951 - "Restrictions.eq when passed null, should create a NullRestriction"
|
436
|
* We need to convert nulls to NullRestrictions for now
|
437
|
*/
|
438
|
if(genusOrUninomial != null) {
|
439
|
criteria.add(Restrictions.eq("genusOrUninomial",genusOrUninomial));
|
440
|
} else {
|
441
|
criteria.add(Restrictions.isNull("genusOrUninomial"));
|
442
|
}
|
443
|
|
444
|
if(infraGenericEpithet != null) {
|
445
|
criteria.add(Restrictions.eq("infraGenericEpithet", infraGenericEpithet));
|
446
|
} else {
|
447
|
criteria.add(Restrictions.isNull("infraGenericEpithet"));
|
448
|
}
|
449
|
|
450
|
if(specificEpithet != null) {
|
451
|
criteria.add(Restrictions.eq("specificEpithet", specificEpithet));
|
452
|
} else {
|
453
|
criteria.add(Restrictions.isNull("specificEpithet"));
|
454
|
}
|
455
|
|
456
|
if(infraSpecificEpithet != null) {
|
457
|
criteria.add(Restrictions.eq("infraSpecificEpithet",infraSpecificEpithet));
|
458
|
} else {
|
459
|
criteria.add(Restrictions.isNull("infraSpecificEpithet"));
|
460
|
}
|
461
|
|
462
|
if(rank != null) {
|
463
|
criteria.add(Restrictions.eq("rank", rank));
|
464
|
}
|
465
|
|
466
|
if(pageSize != null) {
|
467
|
criteria.setMaxResults(pageSize);
|
468
|
if(pageNumber != null) {
|
469
|
criteria.setFirstResult(pageNumber * pageSize);
|
470
|
} else {
|
471
|
criteria.setFirstResult(0);
|
472
|
}
|
473
|
}
|
474
|
|
475
|
addOrder(criteria, orderHints);
|
476
|
|
477
|
List<TaxonNameBase> results = criteria.list();
|
478
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
479
|
return results;
|
480
|
} else {
|
481
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameBase.class,auditEvent.getRevisionNumber());
|
482
|
|
483
|
if(genusOrUninomial != null) {
|
484
|
query.add(AuditEntity.property("genusOrUninomial").eq(genusOrUninomial));
|
485
|
} else {
|
486
|
query.add(AuditEntity.property("genusOrUninomial").isNull());
|
487
|
}
|
488
|
|
489
|
if(infraGenericEpithet != null) {
|
490
|
query.add(AuditEntity.property("infraGenericEpithet").eq(infraGenericEpithet));
|
491
|
} else {
|
492
|
query.add(AuditEntity.property("infraGenericEpithet").isNull());
|
493
|
}
|
494
|
|
495
|
if(specificEpithet != null) {
|
496
|
query.add(AuditEntity.property("specificEpithet").eq(specificEpithet));
|
497
|
} else {
|
498
|
query.add(AuditEntity.property("specificEpithet").isNull());
|
499
|
}
|
500
|
|
501
|
if(infraSpecificEpithet != null) {
|
502
|
query.add(AuditEntity.property("infraSpecificEpithet").eq(infraSpecificEpithet));
|
503
|
} else {
|
504
|
query.add(AuditEntity.property("infraSpecificEpithet").isNull());
|
505
|
}
|
506
|
|
507
|
if(rank != null) {
|
508
|
query.add(AuditEntity.relatedId("rank").eq(rank.getId()));
|
509
|
}
|
510
|
|
511
|
if(pageSize != null) {
|
512
|
query.setMaxResults(pageSize);
|
513
|
if(pageNumber != null) {
|
514
|
query.setFirstResult(pageNumber * pageSize);
|
515
|
} else {
|
516
|
query.setFirstResult(0);
|
517
|
}
|
518
|
}
|
519
|
|
520
|
List<TaxonNameBase> results = query.getResultList();
|
521
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
522
|
return results;
|
523
|
}
|
524
|
}
|
525
|
|
526
|
@Override
|
527
|
public List<? extends TaxonNameBase<?,?>> findByName(String queryString,
|
528
|
MatchMode matchmode, Integer pageSize, Integer pageNumber, List<Criterion> criteria, List<String> propertyPaths) {
|
529
|
|
530
|
Criteria crit = getSession().createCriteria(type);
|
531
|
if (matchmode == MatchMode.EXACT) {
|
532
|
crit.add(Restrictions.eq("nameCache", matchmode.queryStringFrom(queryString)));
|
533
|
} else {
|
534
|
crit.add(Restrictions.ilike("nameCache", matchmode.queryStringFrom(queryString)));
|
535
|
}
|
536
|
if(criteria != null){
|
537
|
for (Criterion criterion : criteria) {
|
538
|
crit.add(criterion);
|
539
|
}
|
540
|
}
|
541
|
crit.addOrder(Order.asc("nameCache"));
|
542
|
|
543
|
if(pageSize != null) {
|
544
|
crit.setMaxResults(pageSize);
|
545
|
if(pageNumber != null) {
|
546
|
crit.setFirstResult(pageNumber * pageSize);
|
547
|
}
|
548
|
}
|
549
|
|
550
|
List<? extends TaxonNameBase<?,?>> results = crit.list();
|
551
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
552
|
|
553
|
return results;
|
554
|
}
|
555
|
|
556
|
@Override
|
557
|
public List<? extends TaxonNameBase<?,?>> findByTitle(String queryString,
|
558
|
MatchMode matchmode, Integer pageSize, Integer pageNumber, List<Criterion> criteria, List<String> propertyPaths) {
|
559
|
|
560
|
Criteria crit = getSession().createCriteria(type);
|
561
|
if (matchmode == MatchMode.EXACT) {
|
562
|
crit.add(Restrictions.eq("titleCache", matchmode.queryStringFrom(queryString)));
|
563
|
} else {
|
564
|
crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
|
565
|
}
|
566
|
if(criteria != null){
|
567
|
for (Criterion criterion : criteria) {
|
568
|
crit.add(criterion);
|
569
|
}
|
570
|
}
|
571
|
crit.addOrder(Order.asc("titleCache"));
|
572
|
|
573
|
if(pageSize != null) {
|
574
|
crit.setMaxResults(pageSize);
|
575
|
if(pageNumber != null) {
|
576
|
crit.setFirstResult(pageNumber * pageSize);
|
577
|
}
|
578
|
}
|
579
|
|
580
|
List<? extends TaxonNameBase<?,?>> results = crit.list();
|
581
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
582
|
|
583
|
return results;
|
584
|
}
|
585
|
|
586
|
|
587
|
@Override
|
588
|
public TaxonNameBase<?,?> findByUuid(UUID uuid, List<Criterion> criteria, List<String> propertyPaths) {
|
589
|
|
590
|
Criteria crit = getSession().createCriteria(type);
|
591
|
|
592
|
if (uuid != null) {
|
593
|
crit.add(Restrictions.eq("uuid", uuid));
|
594
|
} else {
|
595
|
logger.warn("UUID is NULL");
|
596
|
return null;
|
597
|
}
|
598
|
if(criteria != null){
|
599
|
for (Criterion criterion : criteria) {
|
600
|
crit.add(criterion);
|
601
|
}
|
602
|
}
|
603
|
crit.addOrder(Order.asc("uuid"));
|
604
|
|
605
|
List<? extends TaxonNameBase<?,?>> results = crit.list();
|
606
|
if (results.size() == 1) {
|
607
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
608
|
TaxonNameBase<?, ?> taxonName = results.iterator().next();
|
609
|
return taxonName;
|
610
|
} else if (results.size() > 1) {
|
611
|
logger.error("Multiple results for UUID: " + uuid);
|
612
|
} else if (results.size() == 0) {
|
613
|
logger.info("No results for UUID: " + uuid);
|
614
|
}
|
615
|
|
616
|
return null;
|
617
|
}
|
618
|
|
619
|
@Override
|
620
|
public List<RelationshipBase> getAllRelationships(Integer limit, Integer start) {
|
621
|
AuditEvent auditEvent = getAuditEventFromContext();
|
622
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
623
|
// for some reason the HQL .class discriminator didn't work here so I created this preliminary
|
624
|
// implementation for now. Should be cleaned in future.
|
625
|
|
626
|
List<RelationshipBase> result = new ArrayList<RelationshipBase>();
|
627
|
|
628
|
int nameRelSize = countAllRelationships(NameRelationship.class);
|
629
|
if (nameRelSize > start){
|
630
|
|
631
|
String hql = " FROM %s as rb ORDER BY rb.id ";
|
632
|
hql = String.format(hql, NameRelationship.class.getSimpleName());
|
633
|
Query query = getSession().createQuery(hql);
|
634
|
query.setFirstResult(start);
|
635
|
if (limit != null){
|
636
|
query.setMaxResults(limit);
|
637
|
}
|
638
|
result = query.list();
|
639
|
}
|
640
|
limit = limit - result.size();
|
641
|
if (limit > 0){
|
642
|
String hql = " FROM HybridRelationship as rb ORDER BY rb.id ";
|
643
|
hql = String.format(hql, HybridRelationship.class.getSimpleName());
|
644
|
Query query = getSession().createQuery(hql);
|
645
|
start = (nameRelSize > start) ? 0 : (start - nameRelSize);
|
646
|
query.setFirstResult(start);
|
647
|
if (limit != null){
|
648
|
query.setMaxResults(limit);
|
649
|
}
|
650
|
result.addAll( query.list());
|
651
|
}
|
652
|
return result;
|
653
|
} else {
|
654
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(RelationshipBase.class,auditEvent.getRevisionNumber());
|
655
|
return query.getResultList();
|
656
|
}
|
657
|
}
|
658
|
|
659
|
|
660
|
/**
|
661
|
* TODO not yet in interface
|
662
|
* @param clazz
|
663
|
* @return
|
664
|
*/
|
665
|
public int countAllRelationships(Class<? extends RelationshipBase> clazz) {
|
666
|
if (clazz != null && ! NameRelationship.class.isAssignableFrom(clazz) && ! HybridRelationship.class.isAssignableFrom(clazz) ){
|
667
|
throw new RuntimeException("Class must be assignable by a taxon or snonym relation");
|
668
|
}
|
669
|
int size = 0;
|
670
|
|
671
|
if (clazz == null || NameRelationship.class.isAssignableFrom(clazz)){
|
672
|
String hql = " SELECT count(rel) FROM NameRelationship rel";
|
673
|
size += (Long)getSession().createQuery(hql).list().get(0);
|
674
|
}
|
675
|
if (clazz == null || HybridRelationship.class.isAssignableFrom(clazz)){
|
676
|
String hql = " SELECT count(rel) FROM HybridRelationship rel";
|
677
|
size += (Long)getSession().createQuery(hql).list().get(0);
|
678
|
}
|
679
|
return size;
|
680
|
}
|
681
|
|
682
|
|
683
|
@Override
|
684
|
public Integer countByName(String queryString, MatchMode matchmode, List<Criterion> criteria) {
|
685
|
//TODO improve performance
|
686
|
List<? extends TaxonNameBase<?,?>> results = findByName(queryString, matchmode, null, null, criteria, null);
|
687
|
return results.size();
|
688
|
|
689
|
}
|
690
|
|
691
|
/* (non-Javadoc)
|
692
|
* @see eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao#getUuidAndTitleCacheOfNames()
|
693
|
*/
|
694
|
@Override
|
695
|
public List<UuidAndTitleCache> getUuidAndTitleCacheOfNames() {
|
696
|
String queryString = "SELECT uuid, fullTitleCache FROM TaxonNameBase";
|
697
|
|
698
|
List<Object[]> result = getSession().createSQLQuery(queryString).list();
|
699
|
|
700
|
if(result.size() == 0){
|
701
|
return null;
|
702
|
}else{
|
703
|
List<UuidAndTitleCache> list = new ArrayList<UuidAndTitleCache>(result.size());
|
704
|
|
705
|
for (Object object : result){
|
706
|
|
707
|
Object[] objectArray = (Object[]) object;
|
708
|
|
709
|
UUID uuid = UUID.fromString((String) objectArray[0]);
|
710
|
String titleCache = (String) objectArray[1];
|
711
|
|
712
|
list.add(new UuidAndTitleCache(type, uuid, titleCache));
|
713
|
}
|
714
|
|
715
|
return list;
|
716
|
}
|
717
|
}
|
718
|
|
719
|
@Override
|
720
|
public Integer countByName(Class<? extends TaxonNameBase> clazz,String queryString, MatchMode matchmode, List<Criterion> criteria) {
|
721
|
return super.countByParam(clazz, "nameCache", queryString, matchmode, criteria);
|
722
|
}
|
723
|
|
724
|
@Override
|
725
|
public List<TaxonNameBase> findByName(Class<? extends TaxonNameBase> clazz, String queryString, MatchMode matchmode, List<Criterion> criteria,Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
726
|
return super.findByParam(clazz, "nameCache", queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
727
|
}
|
728
|
|
729
|
@Override
|
730
|
public UUID delete (TaxonNameBase persistentObject){
|
731
|
Set<TaxonBase> taxonBases = persistentObject.getTaxonBases();
|
732
|
super.delete(persistentObject);
|
733
|
|
734
|
for (TaxonBase taxonBase: taxonBases){
|
735
|
taxonDao.delete(taxonBase);
|
736
|
}
|
737
|
return persistentObject.getUuid();
|
738
|
}
|
739
|
|
740
|
|
741
|
@Override
|
742
|
public ZoologicalName findZoologicalNameByUUID(UUID uuid){
|
743
|
Criteria criteria = getSession().createCriteria(type);
|
744
|
if (uuid != null) {
|
745
|
criteria.add(Restrictions.eq("uuid", uuid));
|
746
|
} else {
|
747
|
logger.warn("UUID is NULL");
|
748
|
return null;
|
749
|
}
|
750
|
|
751
|
List<? extends TaxonNameBase<?,?>> results = criteria.list();
|
752
|
if (results.size() == 1) {
|
753
|
defaultBeanInitializer.initializeAll(results, null);
|
754
|
TaxonNameBase<?, ?> taxonName = results.iterator().next();
|
755
|
if (taxonName.isInstanceOf(ZoologicalName.class)) {
|
756
|
ZoologicalName zoologicalName = CdmBase.deproxy(taxonName, ZoologicalName.class);
|
757
|
return zoologicalName;
|
758
|
} else {
|
759
|
logger.warn("This UUID (" + uuid + ") does not belong to a ZoologicalName. It belongs to: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
|
760
|
}
|
761
|
} else if (results.size() > 1) {
|
762
|
logger.error("Multiple results for UUID: " + uuid);
|
763
|
} else if (results.size() == 0) {
|
764
|
logger.info("No results for UUID: " + uuid);
|
765
|
}
|
766
|
return null;
|
767
|
}
|
768
|
|
769
|
}
|