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