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
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.taxon;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.Collections;
|
14
|
import java.util.Comparator;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.Iterator;
|
18
|
import java.util.List;
|
19
|
import java.util.Set;
|
20
|
import java.util.SortedSet;
|
21
|
import java.util.TreeSet;
|
22
|
import java.util.UUID;
|
23
|
|
24
|
import org.apache.log4j.Logger;
|
25
|
import org.apache.lucene.queryParser.ParseException;
|
26
|
import org.hibernate.Criteria;
|
27
|
import org.hibernate.FetchMode;
|
28
|
import org.hibernate.Hibernate;
|
29
|
import org.hibernate.Query;
|
30
|
import org.hibernate.Session;
|
31
|
import org.hibernate.criterion.Criterion;
|
32
|
import org.hibernate.criterion.Projections;
|
33
|
import org.hibernate.criterion.Restrictions;
|
34
|
import org.hibernate.envers.query.AuditEntity;
|
35
|
import org.hibernate.envers.query.AuditQuery;
|
36
|
import org.hibernate.search.FullTextSession;
|
37
|
import org.hibernate.search.Search;
|
38
|
import org.springframework.beans.factory.annotation.Autowired;
|
39
|
import org.springframework.beans.factory.annotation.Qualifier;
|
40
|
import org.springframework.dao.DataAccessException;
|
41
|
import org.springframework.stereotype.Repository;
|
42
|
|
43
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
44
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
45
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
46
|
import eu.etaxonomy.cdm.model.common.LSID;
|
47
|
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
|
48
|
import eu.etaxonomy.cdm.model.common.RelationshipBase;
|
49
|
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
|
50
|
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
|
51
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
52
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
53
|
import eu.etaxonomy.cdm.model.name.Rank;
|
54
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
55
|
import eu.etaxonomy.cdm.model.name.TaxonNameComparator;
|
56
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
57
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
58
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
59
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
60
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
|
61
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
62
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
63
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
64
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
65
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
66
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
67
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
68
|
import eu.etaxonomy.cdm.persistence.dao.QueryParseException;
|
69
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.AlternativeSpellingSuggestionParser;
|
70
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
|
71
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
72
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
73
|
import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
|
74
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
75
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
76
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
77
|
|
78
|
|
79
|
/**
|
80
|
* @author a.mueller
|
81
|
* @created 24.11.2008
|
82
|
* @version 1.0
|
83
|
*/
|
84
|
@Repository
|
85
|
@Qualifier("taxonDaoHibernateImpl")
|
86
|
public class TaxonDaoHibernateImpl extends IdentifiableDaoBase<TaxonBase> implements ITaxonDao {
|
87
|
private AlternativeSpellingSuggestionParser<TaxonBase> alternativeSpellingSuggestionParser;
|
88
|
private static final Logger logger = Logger.getLogger(TaxonDaoHibernateImpl.class);
|
89
|
|
90
|
public TaxonDaoHibernateImpl() {
|
91
|
super(TaxonBase.class);
|
92
|
indexedClasses = new Class[2];
|
93
|
indexedClasses[0] = Taxon.class;
|
94
|
indexedClasses[1] = Synonym.class;
|
95
|
super.defaultField = "name.titleCache_tokenized";
|
96
|
}
|
97
|
|
98
|
@Autowired
|
99
|
private ITaxonNameDao taxonNameDao;
|
100
|
|
101
|
@Autowired(required = false) //TODO switched of because it caused problems when starting CdmApplicationController
|
102
|
public void setAlternativeSpellingSuggestionParser(AlternativeSpellingSuggestionParser<TaxonBase> alternativeSpellingSuggestionParser) {
|
103
|
this.alternativeSpellingSuggestionParser = alternativeSpellingSuggestionParser;
|
104
|
}
|
105
|
|
106
|
|
107
|
/* (non-Javadoc)
|
108
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getRootTaxa(eu.etaxonomy.cdm.model.reference.Reference)
|
109
|
*/
|
110
|
public List<Taxon> getRootTaxa(Reference sec) {
|
111
|
return getRootTaxa(sec, CdmFetch.FETCH_CHILDTAXA(), true, false);
|
112
|
}
|
113
|
|
114
|
/* (non-Javadoc)
|
115
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getRootTaxa(eu.etaxonomy.cdm.model.name.Rank, eu.etaxonomy.cdm.model.reference.Reference, eu.etaxonomy.cdm.persistence.fetch.CdmFetch, java.lang.Boolean, java.lang.Boolean)
|
116
|
*/
|
117
|
public List<Taxon> getRootTaxa(Rank rank, Reference sec, CdmFetch cdmFetch, Boolean onlyWithChildren, Boolean withMisapplications, List<String> propertyPaths) {
|
118
|
checkNotInPriorView("TaxonDaoHibernateImpl.getRootTaxa(Rank rank, Reference sec, CdmFetch cdmFetch, Boolean onlyWithChildren, Boolean withMisapplications)");
|
119
|
if (onlyWithChildren == null){
|
120
|
onlyWithChildren = true;
|
121
|
}
|
122
|
if (withMisapplications == null){
|
123
|
withMisapplications = true;
|
124
|
}
|
125
|
if (cdmFetch == null){
|
126
|
cdmFetch = CdmFetch.NO_FETCH();
|
127
|
}
|
128
|
|
129
|
Criteria crit = getSession().createCriteria(Taxon.class);
|
130
|
|
131
|
crit.setFetchMode("name", FetchMode.JOIN);
|
132
|
crit.createAlias("name", "name");
|
133
|
|
134
|
if (rank != null) {
|
135
|
crit.add(Restrictions.eq("name.rank", rank));
|
136
|
}else{
|
137
|
crit.add(Restrictions.isNull("taxonomicParentCache"));
|
138
|
}
|
139
|
|
140
|
if (sec != null){
|
141
|
crit.add(Restrictions.eq("sec", sec) );
|
142
|
}
|
143
|
|
144
|
if (! cdmFetch.includes(CdmFetch.FETCH_CHILDTAXA())){
|
145
|
logger.info("Not fetching child taxa");
|
146
|
//TODO overwrite LAZY (SELECT) does not work (bug in hibernate?)
|
147
|
crit.setFetchMode("relationsToThisTaxon.fromTaxon", FetchMode.LAZY);
|
148
|
}
|
149
|
|
150
|
List<Taxon> results = new ArrayList<Taxon>();
|
151
|
List<Taxon> taxa = crit.list();
|
152
|
for(Taxon taxon : taxa){
|
153
|
|
154
|
|
155
|
//childTaxa
|
156
|
//TODO create restriction instead
|
157
|
// (a) not using cache fields
|
158
|
/*Hibernate.initialize(taxon.getRelationsFromThisTaxon());
|
159
|
if (onlyWithChildren == false || taxon.getRelationsFromThisTaxon().size() > 0){
|
160
|
if (withMisapplications == true || ! taxon.isMisappliedName()){
|
161
|
defaultBeanInitializer.initialize(taxon, propertyPaths);
|
162
|
results.add(taxon);
|
163
|
}
|
164
|
}*/
|
165
|
// (b) using cache fields
|
166
|
if (onlyWithChildren == false || taxon.hasTaxonomicChildren()){
|
167
|
if (withMisapplications == true || ! taxon.isMisapplication()){
|
168
|
defaultBeanInitializer.initialize(taxon, propertyPaths);
|
169
|
results.add(taxon);
|
170
|
}
|
171
|
}
|
172
|
}
|
173
|
return results;
|
174
|
}
|
175
|
|
176
|
/* (non-Javadoc)
|
177
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getRootTaxa(eu.etaxonomy.cdm.model.reference.Reference, eu.etaxonomy.cdm.persistence.fetch.CdmFetch, java.lang.Boolean, java.lang.Boolean)
|
178
|
*/
|
179
|
public List<Taxon> getRootTaxa(Reference sec, CdmFetch cdmFetch, Boolean onlyWithChildren, Boolean withMisapplications) {
|
180
|
return getRootTaxa(null, sec, cdmFetch, onlyWithChildren, withMisapplications, null);
|
181
|
}
|
182
|
|
183
|
/*
|
184
|
* (non-Javadoc)
|
185
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)
|
186
|
*/
|
187
|
public List<TaxonBase> getTaxaByName(String queryString, Reference sec) {
|
188
|
|
189
|
return getTaxaByName(queryString, true, sec);
|
190
|
}
|
191
|
|
192
|
/*
|
193
|
* (non-Javadoc)
|
194
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getTaxaByName(java.lang.String, java.lang.Boolean, eu.etaxonomy.cdm.model.reference.Reference)
|
195
|
*/
|
196
|
public List<TaxonBase> getTaxaByName(String queryString, Boolean accepted, Reference sec) {
|
197
|
checkNotInPriorView("TaxonDaoHibernateImpl.getTaxaByName(String name, Reference sec)");
|
198
|
|
199
|
Criteria criteria = null;
|
200
|
if (accepted == true) {
|
201
|
criteria = getSession().createCriteria(Taxon.class);
|
202
|
} else {
|
203
|
criteria = getSession().createCriteria(Synonym.class);
|
204
|
}
|
205
|
|
206
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
207
|
criteria.createAlias("name", "name");
|
208
|
|
209
|
if (sec != null && sec.getId() != 0) {
|
210
|
criteria.add(Restrictions.eq("sec", sec ) );
|
211
|
}
|
212
|
|
213
|
if (queryString != null) {
|
214
|
criteria.add(Restrictions.ilike("name.nameCache", queryString));
|
215
|
}
|
216
|
|
217
|
return (List<TaxonBase>)criteria.list();
|
218
|
}
|
219
|
|
220
|
public List<TaxonBase> getTaxaByName(Class<? extends TaxonBase> clazz, String queryString, MatchMode matchMode,
|
221
|
Integer pageSize, Integer pageNumber) {
|
222
|
|
223
|
return getTaxaByName(clazz, queryString, null, matchMode, null, pageSize, pageNumber, null);
|
224
|
}
|
225
|
|
226
|
/*
|
227
|
* (non-Javadoc)
|
228
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.persistence.query.MatchMode, java.lang.Boolean, java.lang.Integer, java.lang.Integer)
|
229
|
*/
|
230
|
public List<TaxonBase> getTaxaByName(String queryString, MatchMode matchMode,
|
231
|
Boolean accepted, Integer pageSize, Integer pageNumber) {
|
232
|
|
233
|
if (accepted == true) {
|
234
|
return getTaxaByName(Taxon.class, queryString, matchMode, pageSize, pageNumber);
|
235
|
} else {
|
236
|
return getTaxaByName(Synonym.class, queryString, matchMode, pageSize, pageNumber);
|
237
|
}
|
238
|
}
|
239
|
|
240
|
/*
|
241
|
* (non-Javadoc)
|
242
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getTaxaByName(java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.taxon.Classification, eu.etaxonomy.cdm.persistence.query.MatchMode, java.util.Set, java.lang.Integer, java.lang.Integer, java.util.List)
|
243
|
*/
|
244
|
public List<TaxonBase> getTaxaByName(Class<? extends TaxonBase> clazz, String queryString, Classification classification,
|
245
|
MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageSize,
|
246
|
Integer pageNumber, List<String> propertyPaths) {
|
247
|
|
248
|
boolean doCount = false;
|
249
|
long zstVorher;
|
250
|
long zstNachher;
|
251
|
|
252
|
zstVorher = System.currentTimeMillis();
|
253
|
Query query = prepareTaxaByName(clazz, "nameCache", queryString, classification, matchMode, namedAreas, pageSize, pageNumber, doCount);
|
254
|
|
255
|
if (query != null){
|
256
|
List<TaxonBase> results = query.list();
|
257
|
|
258
|
//results.addAll (prepareTaxaByCommonName(queryString, classification, matchMode, namedAreas, pageSize, pageNumber, doCount).list());
|
259
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
260
|
//TaxonComparatorSearch comp = new TaxonComparatorSearch();
|
261
|
//Collections.sort(results, comp);
|
262
|
zstNachher = System.currentTimeMillis();
|
263
|
System.out.println("Zeit ben?tigt (getTaxaByName): " + ((zstNachher - zstVorher)) + " msec " + results.size());
|
264
|
return results;
|
265
|
}
|
266
|
|
267
|
return new ArrayList<TaxonBase>();
|
268
|
|
269
|
}
|
270
|
|
271
|
/*
|
272
|
* (non-Javadoc)
|
273
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getTaxaByName(java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.taxon.Classification, eu.etaxonomy.cdm.persistence.query.MatchMode, java.util.Set, java.lang.Integer, java.lang.Integer, java.util.List)
|
274
|
*/
|
275
|
//new search for the editor, for performance issues the return values are only uuid and titleCache, to avoid the initialisation of all objects
|
276
|
public List<UuidAndTitleCache<TaxonBase>> getTaxaByNameForEditor(Class<? extends TaxonBase> clazz, String queryString, Classification classification,
|
277
|
MatchMode matchMode, Set<NamedArea> namedAreas, List<String> propertyPaths) {
|
278
|
long zstVorher;
|
279
|
long zstNachher;
|
280
|
zstVorher = System.currentTimeMillis();
|
281
|
boolean doCount = false;
|
282
|
Query query = prepareTaxaByNameForEditor(clazz, "nameCache", queryString, classification, matchMode, namedAreas, doCount);
|
283
|
if (query != null){
|
284
|
//TODO: only for testing!
|
285
|
|
286
|
Iterator<Object[]> results = query.iterate();
|
287
|
List<UuidAndTitleCache<TaxonBase>> resultObjects = new ArrayList<UuidAndTitleCache<TaxonBase>>();
|
288
|
Object[] result;
|
289
|
while (results.hasNext()){
|
290
|
result = results.next();
|
291
|
resultObjects.add( new UuidAndTitleCache((UUID) result[0], (String)result[1]));
|
292
|
}
|
293
|
//TODO: only for testing!
|
294
|
zstNachher = System.currentTimeMillis();
|
295
|
System.out.println("Zeit ben?tigt (getTaxaByNameForEditor): " + ((zstNachher - zstVorher)) + " msec " + resultObjects.size());
|
296
|
|
297
|
return resultObjects;
|
298
|
|
299
|
}
|
300
|
return new ArrayList<UuidAndTitleCache<TaxonBase>>();
|
301
|
|
302
|
}
|
303
|
|
304
|
/*
|
305
|
* (non-Javadoc)
|
306
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getTaxaByCommonName(java.lang.String, eu.etaxonomy.cdm.model.taxon.Classification, eu.etaxonomy.cdm.persistence.query.MatchMode, java.util.Set, java.lang.Integer, java.lang.Integer, java.util.List)
|
307
|
*/
|
308
|
public List<TaxonBase> getTaxaByCommonName(String queryString, Classification classification,
|
309
|
MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageSize,
|
310
|
Integer pageNumber, List<String> propertyPaths) {
|
311
|
boolean doCount = false;
|
312
|
Query query = prepareTaxaByCommonName(queryString, classification, matchMode, namedAreas, pageSize, pageNumber, doCount);
|
313
|
if (query != null){
|
314
|
List<TaxonBase> results = query.list();
|
315
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
316
|
return results;
|
317
|
}
|
318
|
return new ArrayList<TaxonBase>();
|
319
|
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* @param clazz
|
324
|
* @param searchField the field in TaxonNameBase to be searched through usually either <code>nameCache</code> or <code>titleCache</code>
|
325
|
* @param queryString
|
326
|
* @param classification TODO
|
327
|
* @param matchMode
|
328
|
* @param namedAreas
|
329
|
* @param pageSize
|
330
|
* @param pageNumber
|
331
|
* @param doCount
|
332
|
* @return
|
333
|
*
|
334
|
*
|
335
|
*/
|
336
|
private Query prepareTaxaByNameForEditor(Class<? extends TaxonBase> clazz, String searchField, String queryString, Classification classification,
|
337
|
MatchMode matchMode, Set<NamedArea> namedAreas, boolean doCount) {
|
338
|
return prepareQuery(clazz, searchField, queryString, classification,
|
339
|
matchMode, namedAreas, doCount, true);
|
340
|
}
|
341
|
|
342
|
private Query prepareQuery(Class<? extends TaxonBase> clazz, String searchField, String queryString, Classification classification,
|
343
|
MatchMode matchMode, Set<NamedArea> namedAreas, boolean doCount, boolean doForEditor){
|
344
|
|
345
|
String hqlQueryString = matchMode.queryStringFrom(queryString);
|
346
|
String selectWhat;
|
347
|
if (doForEditor){
|
348
|
selectWhat = "t.uuid, t.titleCache";
|
349
|
}else selectWhat = (doCount ? "count(t)": "t");
|
350
|
|
351
|
String hql = "";
|
352
|
Set<NamedArea> areasExpanded = new HashSet<NamedArea>();
|
353
|
if(namedAreas != null && namedAreas.size() > 0){
|
354
|
// expand areas and restrict by distribution area
|
355
|
List<NamedArea> childAreas;
|
356
|
Query areaQuery = getSession().createQuery("select childArea from NamedArea as childArea left join childArea.partOf as parentArea where parentArea = :area");
|
357
|
expandNamedAreas(namedAreas, areasExpanded, areaQuery);
|
358
|
}
|
359
|
boolean doAreaRestriction = areasExpanded.size() > 0;
|
360
|
|
361
|
Set<UUID> namedAreasUuids = new HashSet<UUID>();
|
362
|
for (NamedArea area:areasExpanded){
|
363
|
namedAreasUuids.add(area.getUuid());
|
364
|
}
|
365
|
|
366
|
String taxonSubselect = null;
|
367
|
String synonymSubselect = null;
|
368
|
|
369
|
if(classification != null){
|
370
|
|
371
|
if(doAreaRestriction){
|
372
|
|
373
|
taxonSubselect = "select t.id from" +
|
374
|
" Distribution e" +
|
375
|
" join e.inDescription d" +
|
376
|
" join d.taxon t" +
|
377
|
" join t.name n " +
|
378
|
" join t.taxonNodes as tn "+
|
379
|
" where" +
|
380
|
" e.area.uuid in (:namedAreasUuids) AND" +
|
381
|
" tn.classification = :classification" +
|
382
|
" AND n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
383
|
|
384
|
|
385
|
synonymSubselect = "select s.id from" +
|
386
|
" Distribution e" +
|
387
|
" join e.inDescription d" +
|
388
|
" join d.taxon t" + // the taxa
|
389
|
" join t.taxonNodes as tn "+
|
390
|
" join t.synonymRelations sr" +
|
391
|
" join sr.relatedFrom s" + // the synonyms
|
392
|
" join s.name sn"+
|
393
|
" where" +
|
394
|
" e.area.uuid in (:namedAreasUuids) AND" +
|
395
|
" tn.classification = :classification" +
|
396
|
" AND sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
397
|
|
398
|
} else {
|
399
|
|
400
|
taxonSubselect = "select t.id from" +
|
401
|
" Taxon t" +
|
402
|
" join t.name n " +
|
403
|
" join t.taxonNodes as tn "+
|
404
|
" where" +
|
405
|
" tn.classification = :classification" +
|
406
|
" AND n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
407
|
|
408
|
synonymSubselect = "select s.id from" +
|
409
|
" Taxon t" + // the taxa
|
410
|
" join t.taxonNodes as tn "+
|
411
|
" join t.synonymRelations sr" +
|
412
|
" join sr.relatedFrom s" + // the synonyms
|
413
|
" join s.name sn"+
|
414
|
" where" +
|
415
|
" tn.classification = :classification" +
|
416
|
" AND sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
417
|
}
|
418
|
} else {
|
419
|
|
420
|
if(doAreaRestriction){
|
421
|
|
422
|
taxonSubselect = "select t.id from " +
|
423
|
" Distribution e" +
|
424
|
" join e.inDescription d" +
|
425
|
" join d.taxon t" +
|
426
|
" join t.name n "+
|
427
|
" where" +
|
428
|
(doAreaRestriction ? " e.area.uuid in (:namedAreasUuids) AND" : "") +
|
429
|
" n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
430
|
|
431
|
synonymSubselect = "select s.id from" +
|
432
|
" Distribution e" +
|
433
|
" join e.inDescription d" +
|
434
|
" join d.taxon t" + // the taxa
|
435
|
" join t.synonymRelations sr" +
|
436
|
" join sr.relatedFrom s" + // the synonyms
|
437
|
" join s.name sn"+
|
438
|
" where" +
|
439
|
(doAreaRestriction ? " e.area.uuid in (:namedAreasUuids) AND" : "") +
|
440
|
" sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
441
|
|
442
|
} else {
|
443
|
|
444
|
taxonSubselect = "select t.id from " +
|
445
|
" Taxon t" +
|
446
|
" join t.name n "+
|
447
|
" where" +
|
448
|
" n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
449
|
|
450
|
synonymSubselect = "select s.id from" +
|
451
|
" Taxon t" + // the taxa
|
452
|
" join t.synonymRelations sr" +
|
453
|
" join sr.relatedFrom s" + // the synonyms
|
454
|
" join s.name sn"+
|
455
|
" where" +
|
456
|
" sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
457
|
}
|
458
|
|
459
|
|
460
|
}
|
461
|
|
462
|
|
463
|
|
464
|
|
465
|
Query subTaxon = null;
|
466
|
Query subSynonym = null;
|
467
|
if(clazz.equals(Taxon.class)){
|
468
|
// find Taxa
|
469
|
subTaxon = getSession().createQuery(taxonSubselect).setParameter("queryString", hqlQueryString);
|
470
|
//subTaxon = getSession().createQuery(taxonSubselect);
|
471
|
|
472
|
if(doAreaRestriction){
|
473
|
subTaxon.setParameterList("namedAreasUuids", namedAreasUuids);
|
474
|
}
|
475
|
if(classification != null){
|
476
|
subTaxon.setParameter("classification", classification);
|
477
|
}
|
478
|
} else if(clazz.equals(Synonym.class)){
|
479
|
// find synonyms
|
480
|
subSynonym = getSession().createQuery(synonymSubselect).setParameter("queryString", hqlQueryString);
|
481
|
|
482
|
if(doAreaRestriction){
|
483
|
subSynonym.setParameterList("namedAreasUuids", namedAreasUuids);
|
484
|
}
|
485
|
if(classification != null){
|
486
|
subSynonym.setParameter("classification", classification);
|
487
|
}
|
488
|
} else {
|
489
|
// find taxa and synonyms
|
490
|
subSynonym = getSession().createQuery(synonymSubselect).setParameter("queryString", hqlQueryString);
|
491
|
subTaxon = getSession().createQuery(taxonSubselect).setParameter("queryString", hqlQueryString);
|
492
|
if(doAreaRestriction){
|
493
|
subTaxon.setParameterList("namedAreasUuids", namedAreasUuids);
|
494
|
subSynonym.setParameterList("namedAreasUuids", namedAreasUuids);
|
495
|
}
|
496
|
if(classification != null){
|
497
|
subTaxon.setParameter("classification", classification);
|
498
|
subSynonym.setParameter("classification", classification);
|
499
|
}
|
500
|
}
|
501
|
|
502
|
List<Integer> taxa = new ArrayList<Integer>();
|
503
|
List<Integer> synonyms = new ArrayList<Integer>();
|
504
|
if(clazz.equals(Taxon.class)){
|
505
|
taxa = subTaxon.list();
|
506
|
|
507
|
}else if (clazz.equals(Synonym.class)){
|
508
|
synonyms = subSynonym.list();
|
509
|
}else {
|
510
|
taxa = subTaxon.list();
|
511
|
synonyms = subSynonym.list();
|
512
|
}
|
513
|
if(clazz.equals(Taxon.class)){
|
514
|
if (taxa.size()>0){
|
515
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa)";
|
516
|
}else{
|
517
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t";
|
518
|
}
|
519
|
} else if(clazz.equals(Synonym.class) ){
|
520
|
if (synonyms.size()>0){
|
521
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:synonyms)";
|
522
|
}else{
|
523
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t";
|
524
|
}
|
525
|
} else {
|
526
|
if(synonyms.size()>0 && taxa.size()>0){
|
527
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa) OR t.id in (:synonyms)";
|
528
|
}else if (synonyms.size()>0 ){
|
529
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:synonyms)";
|
530
|
} else if (taxa.size()>0 ){
|
531
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa) ";
|
532
|
} else{
|
533
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t";
|
534
|
}
|
535
|
}
|
536
|
|
537
|
if (hql == "") return null;
|
538
|
if(!doCount){
|
539
|
//hql += " order by t.titleCache"; //" order by t.name.nameCache";
|
540
|
hql += " order by t.name.genusOrUninomial, case when t.name.specificEpithet like '\"%\"' then 1 else 0 end, t.name.specificEpithet, t.name.rank desc, t.name.nameCache";
|
541
|
|
542
|
|
543
|
}
|
544
|
|
545
|
Query query = getSession().createQuery(hql);
|
546
|
|
547
|
|
548
|
if(clazz.equals(Taxon.class) && taxa.size()>0){
|
549
|
//find taxa
|
550
|
query.setParameterList("taxa", taxa );
|
551
|
} else if(clazz.equals(Synonym.class) && synonyms.size()>0){
|
552
|
// find synonyms
|
553
|
query.setParameterList("synonyms", synonyms);
|
554
|
|
555
|
|
556
|
} else {
|
557
|
// find taxa and synonyms
|
558
|
if (taxa.size()>0){
|
559
|
query.setParameterList("taxa", taxa);
|
560
|
}
|
561
|
if (synonyms.size()>0){
|
562
|
query.setParameterList("synonyms",synonyms);
|
563
|
}
|
564
|
if (taxa.size()== 0 && synonyms.size() == 0){
|
565
|
return null;
|
566
|
}
|
567
|
}
|
568
|
return query;
|
569
|
|
570
|
}
|
571
|
|
572
|
|
573
|
/**
|
574
|
* @param clazz
|
575
|
* @param searchField the field in TaxonNameBase to be searched through usually either <code>nameCache</code> or <code>titleCache</code>
|
576
|
* @param queryString
|
577
|
* @param classification TODO
|
578
|
* @param matchMode
|
579
|
* @param namedAreas
|
580
|
* @param pageSize
|
581
|
* @param pageNumber
|
582
|
* @param doCount
|
583
|
* @return
|
584
|
*
|
585
|
* FIXME implement classification restriction & implement test: see {@link TaxonDaoHibernateImplTest#testCountTaxaByName()}
|
586
|
*/
|
587
|
private Query prepareTaxaByName(Class<? extends TaxonBase> clazz, String searchField, String queryString, Classification classification,
|
588
|
MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageSize, Integer pageNumber, boolean doCount) {
|
589
|
|
590
|
//TODO ? checkNotInPriorView("TaxonDaoHibernateImpl.countTaxaByName(String queryString, Boolean accepted, Reference sec)");
|
591
|
|
592
|
/*String hqlQueryString = matchMode.queryStringFrom(queryString);
|
593
|
|
594
|
String selectWhat = (doCount ? "count(t)": "t");
|
595
|
|
596
|
String hql = "";
|
597
|
Set<NamedArea> areasExpanded = new HashSet<NamedArea>();
|
598
|
if(namedAreas != null && namedAreas.size() > 0){
|
599
|
// expand areas and restrict by distribution area
|
600
|
List<NamedArea> childAreas;
|
601
|
Query areaQuery = getSession().createQuery("select childArea from NamedArea as childArea left join childArea.partOf as parentArea where parentArea = :area");
|
602
|
expandNamedAreas(namedAreas, areasExpanded, areaQuery);
|
603
|
}
|
604
|
boolean doAreaRestriction = areasExpanded.size() > 0;
|
605
|
|
606
|
Set<UUID> namedAreasUuids = new HashSet<UUID>();
|
607
|
for (NamedArea area:areasExpanded){
|
608
|
namedAreasUuids.add(area.getUuid());
|
609
|
}
|
610
|
|
611
|
String taxonSubselect = null;
|
612
|
String synonymSubselect = null;
|
613
|
|
614
|
if(classification != null){
|
615
|
|
616
|
if(doAreaRestriction){
|
617
|
|
618
|
taxonSubselect = "select t.id from" +
|
619
|
" Distribution e" +
|
620
|
" join e.inDescription d" +
|
621
|
" join d.taxon t" +
|
622
|
" join t.name n " +
|
623
|
" join t.taxonNodes as tn "+
|
624
|
" where" +
|
625
|
" e.area.uuid in (:namedAreasUuids) AND" +
|
626
|
" tn.classification = :classification" +
|
627
|
" AND n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
628
|
|
629
|
|
630
|
synonymSubselect = "select s.id from" +
|
631
|
" Distribution e" +
|
632
|
" join e.inDescription d" +
|
633
|
" join d.taxon t" + // the taxa
|
634
|
" join t.taxonNodes as tn "+
|
635
|
" join t.synonymRelations sr" +
|
636
|
" join sr.relatedFrom s" + // the synonyms
|
637
|
" join s.name sn"+
|
638
|
" where" +
|
639
|
" e.area.uuid in (:namedAreasUuids) AND" +
|
640
|
" tn.classification = :classification" +
|
641
|
" AND sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
642
|
|
643
|
} else {
|
644
|
|
645
|
taxonSubselect = "select t.id from" +
|
646
|
" Taxon t" +
|
647
|
" join t.name n " +
|
648
|
" join t.taxonNodes as tn "+
|
649
|
" where" +
|
650
|
" tn.classification = :classification" +
|
651
|
" AND n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
652
|
|
653
|
synonymSubselect = "select s.id from" +
|
654
|
" Taxon t" + // the taxa
|
655
|
" join t.taxonNodes as tn "+
|
656
|
" join t.synonymRelations sr" +
|
657
|
" join sr.relatedFrom s" + // the synonyms
|
658
|
" join s.name sn"+
|
659
|
" where" +
|
660
|
" tn.classification = :classification" +
|
661
|
" AND sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
662
|
}
|
663
|
} else {
|
664
|
|
665
|
if(doAreaRestriction){
|
666
|
|
667
|
taxonSubselect = "select t.id from " +
|
668
|
" Distribution e" +
|
669
|
" join e.inDescription d" +
|
670
|
" join d.taxon t" +
|
671
|
" join t.name n "+
|
672
|
" where" +
|
673
|
(doAreaRestriction ? " e.area.uuid in (:namedAreasUuids) AND" : "") +
|
674
|
" n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
675
|
|
676
|
synonymSubselect = "select s.id from" +
|
677
|
" Distribution e" +
|
678
|
" join e.inDescription d" +
|
679
|
" join d.taxon t" + // the taxa
|
680
|
" join t.synonymRelations sr" +
|
681
|
" join sr.relatedFrom s" + // the synonyms
|
682
|
" join s.name sn"+
|
683
|
" where" +
|
684
|
(doAreaRestriction ? " e.area.uuid in (:namedAreasUuids) AND" : "") +
|
685
|
" sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
686
|
|
687
|
} else {
|
688
|
|
689
|
taxonSubselect = "select t.id from " +
|
690
|
" Taxon t" +
|
691
|
" join t.name n "+
|
692
|
" where" +
|
693
|
" n." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
694
|
|
695
|
synonymSubselect = "select s.id from" +
|
696
|
" Taxon t" + // the taxa
|
697
|
" join t.synonymRelations sr" +
|
698
|
" join sr.relatedFrom s" + // the synonyms
|
699
|
" join s.name sn"+
|
700
|
" where" +
|
701
|
" sn." + searchField + " " + matchMode.getMatchOperator() + " :queryString";
|
702
|
}
|
703
|
|
704
|
|
705
|
}
|
706
|
|
707
|
|
708
|
|
709
|
|
710
|
Query subTaxon = null;
|
711
|
Query subSynonym = null;
|
712
|
if(clazz.equals(Taxon.class)){
|
713
|
// find Taxa
|
714
|
subTaxon = getSession().createQuery(taxonSubselect).setParameter("queryString", hqlQueryString);
|
715
|
//subTaxon = getSession().createQuery(taxonSubselect);
|
716
|
|
717
|
if(doAreaRestriction){
|
718
|
subTaxon.setParameterList("namedAreasUuids", namedAreasUuids);
|
719
|
}
|
720
|
if(classification != null){
|
721
|
subTaxon.setParameter("classification", classification);
|
722
|
}
|
723
|
} else if(clazz.equals(Synonym.class)){
|
724
|
// find synonyms
|
725
|
subSynonym = getSession().createQuery(synonymSubselect).setParameter("queryString", hqlQueryString);
|
726
|
|
727
|
if(doAreaRestriction){
|
728
|
subSynonym.setParameterList("namedAreasUuids", namedAreasUuids);
|
729
|
}
|
730
|
if(classification != null){
|
731
|
subSynonym.setParameter("classification", classification);
|
732
|
}
|
733
|
} else {
|
734
|
// find taxa and synonyms
|
735
|
subSynonym = getSession().createQuery(synonymSubselect).setParameter("queryString", hqlQueryString);
|
736
|
subTaxon = getSession().createQuery(taxonSubselect).setParameter("queryString", hqlQueryString);
|
737
|
if(doAreaRestriction){
|
738
|
subTaxon.setParameterList("namedAreasUuids", namedAreasUuids);
|
739
|
subSynonym.setParameterList("namedAreasUuids", namedAreasUuids);
|
740
|
}
|
741
|
if(classification != null){
|
742
|
subTaxon.setParameter("classification", classification);
|
743
|
subSynonym.setParameter("classification", classification);
|
744
|
}
|
745
|
}
|
746
|
|
747
|
List<Integer> taxa = new ArrayList<Integer>();
|
748
|
List<Integer> synonyms = new ArrayList<Integer>();
|
749
|
if(clazz.equals(Taxon.class)){
|
750
|
taxa = subTaxon.list();
|
751
|
|
752
|
}else if (clazz.equals(Synonym.class)){
|
753
|
synonyms = subSynonym.list();
|
754
|
}else {
|
755
|
taxa = subTaxon.list();
|
756
|
synonyms = subSynonym.list();
|
757
|
}
|
758
|
if(clazz.equals(Taxon.class)){
|
759
|
if (taxa.size()>0){
|
760
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa)";
|
761
|
}else{
|
762
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t";
|
763
|
}
|
764
|
} else if(clazz.equals(Synonym.class) ){
|
765
|
if (synonyms.size()>0){
|
766
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:synonyms)";
|
767
|
}else{
|
768
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t";
|
769
|
}
|
770
|
} else {
|
771
|
if(synonyms.size()>0 && taxa.size()>0){
|
772
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa) OR t.id in (:synonyms)";
|
773
|
}else if (synonyms.size()>0 ){
|
774
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:synonyms)";
|
775
|
} else if (taxa.size()>0 ){
|
776
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa) ";
|
777
|
} else{
|
778
|
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t";
|
779
|
}
|
780
|
}
|
781
|
|
782
|
if (hql == "") return null;
|
783
|
if(!doCount){
|
784
|
//hql += " order by t.titleCache"; //" order by t.name.nameCache";
|
785
|
hql += " order by t.name.genusOrUninomial, case when t.name.specificEpithet like '\"%\"' then 1 else 0 end, t.name.specificEpithet, t.name.rank desc, t.name.nameCache";
|
786
|
|
787
|
|
788
|
}
|
789
|
|
790
|
Query query = getSession().createQuery(hql);
|
791
|
|
792
|
|
793
|
if(clazz.equals(Taxon.class) && taxa.size()>0){
|
794
|
//find taxa
|
795
|
query.setParameterList("taxa", taxa );
|
796
|
} else if(clazz.equals(Synonym.class) && synonyms.size()>0){
|
797
|
// find synonyms
|
798
|
query.setParameterList("synonyms", synonyms);
|
799
|
|
800
|
|
801
|
} else {
|
802
|
// find taxa and synonyms
|
803
|
if (taxa.size()>0){
|
804
|
query.setParameterList("taxa", taxa);
|
805
|
}
|
806
|
if (synonyms.size()>0){
|
807
|
query.setParameterList("synonyms",synonyms);
|
808
|
}
|
809
|
if (taxa.size()== 0 && synonyms.size() == 0){
|
810
|
return null;
|
811
|
}
|
812
|
}*/
|
813
|
Query query = prepareQuery(clazz, searchField, queryString, classification,
|
814
|
matchMode, namedAreas, doCount, false);
|
815
|
|
816
|
if(pageSize != null && !doCount) {
|
817
|
query.setMaxResults(pageSize);
|
818
|
if(pageNumber != null) {
|
819
|
query.setFirstResult(pageNumber * pageSize);
|
820
|
}
|
821
|
}
|
822
|
|
823
|
return query;
|
824
|
}
|
825
|
|
826
|
private Query prepareTaxaByCommonName(String queryString, Classification classification,
|
827
|
MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageSize, Integer pageNumber, boolean doCount){
|
828
|
|
829
|
String hql= "from Taxon t " +
|
830
|
"join t.descriptions d "+
|
831
|
"join d.descriptionElements e " +
|
832
|
"join e.feature f " +
|
833
|
"where f.supportsCommonTaxonName = true and e.name "+matchMode.getMatchOperator()+" :queryString";//and ls.text like 'common%'";
|
834
|
|
835
|
Query query = getSession().createQuery(hql);
|
836
|
|
837
|
query.setParameter("queryString", queryString);
|
838
|
|
839
|
if(pageSize != null && !doCount) {
|
840
|
query.setMaxResults(pageSize);
|
841
|
if(pageNumber != null) {
|
842
|
query.setFirstResult(pageNumber * pageSize);
|
843
|
}
|
844
|
}
|
845
|
return query;
|
846
|
}
|
847
|
|
848
|
/* (non-Javadoc)
|
849
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countTaxaByName(java.lang.String, eu.etaxonomy.cdm.persistence.query.MatchMode, eu.etaxonomy.cdm.persistence.query.SelectMode, eu.etaxonomy.cdm.model.reference.Reference, java.util.Set)
|
850
|
*/
|
851
|
public long countTaxaByName(Class<? extends TaxonBase> clazz, String queryString, Classification classification,
|
852
|
MatchMode matchMode, Set<NamedArea> namedAreas) {
|
853
|
|
854
|
boolean doCount = true;
|
855
|
Query query = prepareTaxaByName(clazz, "nameCache", queryString, classification, matchMode, namedAreas, null, null, doCount);
|
856
|
if (query != null) {
|
857
|
return (Long)query.uniqueResult();
|
858
|
}
|
859
|
return 0;
|
860
|
|
861
|
}
|
862
|
|
863
|
/**
|
864
|
* @param namedAreas
|
865
|
* @param areasExpanded
|
866
|
* @param areaQuery
|
867
|
*/
|
868
|
private void expandNamedAreas(Collection<NamedArea> namedAreas, Set<NamedArea> areasExpanded, Query areaQuery) {
|
869
|
List<NamedArea> childAreas;
|
870
|
for(NamedArea a : namedAreas){
|
871
|
areasExpanded.add(a);
|
872
|
areaQuery.setParameter("area", a);
|
873
|
childAreas = areaQuery.list();
|
874
|
if(childAreas.size() > 0){
|
875
|
areasExpanded.addAll(childAreas);
|
876
|
expandNamedAreas(childAreas, areasExpanded, areaQuery);
|
877
|
}
|
878
|
}
|
879
|
}
|
880
|
|
881
|
// /* (non-Javadoc)
|
882
|
// * @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countTaxaByName(java.lang.String, eu.etaxonomy.cdm.persistence.query.MatchMode, eu.etaxonomy.cdm.persistence.query.SelectMode)
|
883
|
// */
|
884
|
// public Integer countTaxaByName(String queryString, MatchMode matchMode, SelectMode selectMode) {
|
885
|
// return countTaxaByName(queryString, matchMode, selectMode, null);
|
886
|
// }
|
887
|
|
888
|
// /* (non-Javadoc)
|
889
|
// * @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countTaxaByName(java.lang.String, eu.etaxonomy.cdm.persistence.query.MatchMode, eu.etaxonomy.cdm.persistence.query.SelectMode, eu.etaxonomy.cdm.model.reference.Reference)
|
890
|
// */
|
891
|
// public Integer countTaxaByName(String queryString,
|
892
|
// MatchMode matchMode, SelectMode selectMode, Reference sec) {
|
893
|
//
|
894
|
// Long count = countTaxaByName(queryString, matchMode, selectMode, sec, null);
|
895
|
// return count.intValue();
|
896
|
//
|
897
|
// }
|
898
|
|
899
|
// public Integer countTaxaByName(String queryString, MatchMode matchMode, Boolean accepted) {
|
900
|
//
|
901
|
// SelectMode selectMode = (accepted ? SelectMode.TAXA : SelectMode.SYNONYMS);
|
902
|
// Long count = countTaxaByName(queryString, matchMode, selectMode, null, null);
|
903
|
// return count.intValue();
|
904
|
// }
|
905
|
|
906
|
public List<TaxonBase> getAllTaxonBases(Integer pagesize, Integer page) {
|
907
|
return super.list(pagesize, page);
|
908
|
}
|
909
|
|
910
|
/*
|
911
|
* (non-Javadoc)
|
912
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getAllSynonyms(java.lang.Integer, java.lang.Integer)
|
913
|
*/
|
914
|
public List<Synonym> getAllSynonyms(Integer limit, Integer start) {
|
915
|
Criteria criteria = getSession().createCriteria(Synonym.class);
|
916
|
|
917
|
if(limit != null) {
|
918
|
criteria.setFirstResult(start);
|
919
|
criteria.setMaxResults(limit);
|
920
|
}
|
921
|
|
922
|
return criteria.list();
|
923
|
}
|
924
|
|
925
|
/*
|
926
|
* (non-Javadoc)
|
927
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getAllTaxa(java.lang.Integer, java.lang.Integer)
|
928
|
*/
|
929
|
public List<Taxon> getAllTaxa(Integer limit, Integer start) {
|
930
|
Criteria criteria = getSession().createCriteria(Taxon.class);
|
931
|
|
932
|
if(limit != null) {
|
933
|
criteria.setFirstResult(start);
|
934
|
criteria.setMaxResults(limit);
|
935
|
}
|
936
|
|
937
|
return criteria.list();
|
938
|
}
|
939
|
|
940
|
/*
|
941
|
* (non-Javadoc)
|
942
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getAllRelationships(java.lang.Integer, java.lang.Integer)
|
943
|
*/
|
944
|
public List<RelationshipBase> getAllRelationships(Integer limit, Integer start) {
|
945
|
AuditEvent auditEvent = getAuditEventFromContext();
|
946
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
947
|
Criteria criteria = getSession().createCriteria(RelationshipBase.class);
|
948
|
criteria.setFirstResult(start);
|
949
|
criteria.setMaxResults(limit);
|
950
|
return (List<RelationshipBase>)criteria.list();
|
951
|
} else {
|
952
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(RelationshipBase.class,auditEvent.getRevisionNumber());
|
953
|
return (List<RelationshipBase>)query.getResultList();
|
954
|
}
|
955
|
}
|
956
|
|
957
|
/** Sets the taxonomic parent to null. Does not handle taxonomic relationships. */
|
958
|
// private boolean nullifyTaxonomicParent(Taxon taxon) {
|
959
|
//
|
960
|
// try {
|
961
|
// Method nullifyTaxonomicParent = taxon.getClass().getMethod("nullifyTaxonomicParent");
|
962
|
// nullifyTaxonomicParent.invoke(taxon);
|
963
|
// } catch (NoSuchMethodException ex) {
|
964
|
// logger.error("NoSuchMethod: " + ex.getMessage());
|
965
|
// return false;
|
966
|
// } catch (IllegalArgumentException ex) {
|
967
|
// logger.error("IllegalArgumentException: " + ex.getMessage());
|
968
|
// return false;
|
969
|
// } catch (IllegalAccessException ex) {
|
970
|
// logger.error("IllegalAccessException: " + ex.getMessage());
|
971
|
// return false;
|
972
|
// } catch (InvocationTargetException ex) {
|
973
|
// logger.error("IllegalAccessException: " + ex.getMessage());
|
974
|
// return false;
|
975
|
// }
|
976
|
// return true;
|
977
|
// }
|
978
|
|
979
|
@Override
|
980
|
public UUID delete(TaxonBase taxonBase) throws DataAccessException{
|
981
|
if (taxonBase == null){
|
982
|
logger.warn("TaxonBase was 'null'");
|
983
|
return null;
|
984
|
}
|
985
|
|
986
|
// Merge the object in if it is detached
|
987
|
//
|
988
|
// I think this is preferable to catching lazy initialization errors
|
989
|
// as that solution only swallows and hides the exception, but doesn't
|
990
|
// actually solve it.
|
991
|
getSession().merge(taxonBase);
|
992
|
|
993
|
if (taxonBase instanceof Taxon){ // is Taxon
|
994
|
for (Iterator<TaxonRelationship> iterator = ((Taxon)taxonBase).getRelationsFromThisTaxon().iterator(); iterator.hasNext();){
|
995
|
TaxonRelationship relationFromThisTaxon = iterator.next();
|
996
|
|
997
|
// decrease children count of taxonomic parent by one
|
998
|
if (relationFromThisTaxon.getType().equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())) {
|
999
|
Taxon toTaxon = relationFromThisTaxon.getToTaxon(); // parent
|
1000
|
if (toTaxon != null) {
|
1001
|
toTaxon.setTaxonomicChildrenCount(toTaxon.getTaxonomicChildrenCount() - 1);
|
1002
|
}
|
1003
|
}
|
1004
|
}
|
1005
|
}
|
1006
|
|
1007
|
return super.delete(taxonBase);
|
1008
|
}
|
1009
|
|
1010
|
|
1011
|
/* (non-Javadoc)
|
1012
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#findByName(java.lang.String, eu.etaxonomy.cdm.persistence.query.MatchMode, int, int, boolean)
|
1013
|
*/
|
1014
|
public List<TaxonBase> findByNameTitleCache(Class<? extends TaxonBase>clazz, String queryString, Classification classification, MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageNumber, Integer pageSize, List<String> propertyPaths) {
|
1015
|
|
1016
|
boolean doCount = false;
|
1017
|
Query query = prepareTaxaByName(clazz, "titleCache", queryString, classification, matchMode, namedAreas, pageSize, pageNumber, doCount);
|
1018
|
if (query != null){
|
1019
|
List<TaxonBase> results = query.list();
|
1020
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
1021
|
return results;
|
1022
|
}
|
1023
|
return new ArrayList<TaxonBase>();
|
1024
|
|
1025
|
}
|
1026
|
|
1027
|
/*
|
1028
|
* (non-Javadoc)
|
1029
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countMatchesByName(java.lang.String, eu.etaxonomy.cdm.persistence.query.MatchMode, boolean)
|
1030
|
*/
|
1031
|
public int countMatchesByName(String queryString, MatchMode matchMode, boolean onlyAcccepted) {
|
1032
|
checkNotInPriorView("TaxonDaoHibernateImpl.countMatchesByName(String queryString, ITitledDao.MATCH_MODE matchMode, boolean onlyAcccepted)");
|
1033
|
Criteria crit = getSession().createCriteria(type);
|
1034
|
crit.add(Restrictions.ilike("titleCache", matchMode.queryStringFrom(queryString)));
|
1035
|
crit.setProjection(Projections.rowCount());
|
1036
|
int result = ((Integer)crit.list().get(0)).intValue();
|
1037
|
return result;
|
1038
|
}
|
1039
|
|
1040
|
/*
|
1041
|
* (non-Javadoc)
|
1042
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countMatchesByName(java.lang.String, eu.etaxonomy.cdm.persistence.query.MatchMode, boolean, java.util.List)
|
1043
|
*/
|
1044
|
public int countMatchesByName(String queryString, MatchMode matchMode, boolean onlyAcccepted, List<Criterion> criteria) {
|
1045
|
checkNotInPriorView("TaxonDaoHibernateImpl.countMatchesByName(String queryString, ITitledDao.MATCH_MODE matchMode, boolean onlyAcccepted, List<Criterion> criteria)");
|
1046
|
Criteria crit = getSession().createCriteria(type);
|
1047
|
crit.add(Restrictions.ilike("titleCache", matchMode.queryStringFrom(queryString)));
|
1048
|
if(criteria != null){
|
1049
|
for (Criterion criterion : criteria) {
|
1050
|
crit.add(criterion);
|
1051
|
}
|
1052
|
}
|
1053
|
crit.setProjection(Projections.rowCount());
|
1054
|
int result = ((Integer)crit.list().get(0)).intValue();
|
1055
|
return result;
|
1056
|
}
|
1057
|
|
1058
|
/*
|
1059
|
* (non-Javadoc)
|
1060
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countTaxonRelationships(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType, eu.etaxonomy.cdm.model.common.RelationshipBase.Direction)
|
1061
|
*/
|
1062
|
public int countTaxonRelationships(Taxon taxon, TaxonRelationshipType type, Direction direction) {
|
1063
|
AuditEvent auditEvent = getAuditEventFromContext();
|
1064
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
1065
|
Query query = null;
|
1066
|
|
1067
|
if(type == null) {
|
1068
|
query = getSession().createQuery("select count(taxonRelationship) from TaxonRelationship taxonRelationship where taxonRelationship."+direction+" = :relatedTaxon");
|
1069
|
} else {
|
1070
|
query = getSession().createQuery("select count(taxonRelationship) from TaxonRelationship taxonRelationship where taxonRelationship."+direction+" = :relatedTaxon and taxonRelationship.type = :type");
|
1071
|
query.setParameter("type",type);
|
1072
|
}
|
1073
|
query.setParameter("relatedTaxon", taxon);
|
1074
|
|
1075
|
return ((Long)query.uniqueResult()).intValue();
|
1076
|
} else {
|
1077
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonRelationship.class,auditEvent.getRevisionNumber());
|
1078
|
query.add(AuditEntity.relatedId(direction.toString()).eq(taxon.getId()));
|
1079
|
query.addProjection(AuditEntity.id().count("id"));
|
1080
|
|
1081
|
if(type != null) {
|
1082
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
1083
|
}
|
1084
|
|
1085
|
return ((Long)query.getSingleResult()).intValue();
|
1086
|
}
|
1087
|
}
|
1088
|
|
1089
|
/*
|
1090
|
* (non-Javadoc)
|
1091
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countSynonyms(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType)
|
1092
|
*/
|
1093
|
public int countSynonyms(Taxon taxon, SynonymRelationshipType type) {
|
1094
|
AuditEvent auditEvent = getAuditEventFromContext();
|
1095
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
1096
|
Criteria criteria = getSession().createCriteria(SynonymRelationship.class);
|
1097
|
|
1098
|
criteria.add(Restrictions.eq("relatedTo", taxon));
|
1099
|
if(type != null) {
|
1100
|
criteria.add(Restrictions.eq("type", type));
|
1101
|
}
|
1102
|
criteria.setProjection(Projections.rowCount());
|
1103
|
return (Integer)criteria.uniqueResult();
|
1104
|
} else {
|
1105
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(SynonymRelationship.class,auditEvent.getRevisionNumber());
|
1106
|
query.add(AuditEntity.relatedId("relatedTo").eq(taxon.getId()));
|
1107
|
query.addProjection(AuditEntity.id().count("id"));
|
1108
|
|
1109
|
if(type != null) {
|
1110
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
1111
|
}
|
1112
|
|
1113
|
return ((Long)query.getSingleResult()).intValue();
|
1114
|
}
|
1115
|
}
|
1116
|
|
1117
|
/*
|
1118
|
* (non-Javadoc)
|
1119
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countSynonyms(eu.etaxonomy.cdm.model.taxon.Synonym, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType)
|
1120
|
*/
|
1121
|
public int countSynonyms(Synonym synonym, SynonymRelationshipType type) {
|
1122
|
AuditEvent auditEvent = getAuditEventFromContext();
|
1123
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
1124
|
Criteria criteria = getSession().createCriteria(SynonymRelationship.class);
|
1125
|
|
1126
|
criteria.add(Restrictions.eq("relatedFrom", synonym));
|
1127
|
if(type != null) {
|
1128
|
criteria.add(Restrictions.eq("type", type));
|
1129
|
}
|
1130
|
|
1131
|
criteria.setProjection(Projections.rowCount());
|
1132
|
return (Integer)criteria.uniqueResult();
|
1133
|
} else {
|
1134
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(SynonymRelationship.class,auditEvent.getRevisionNumber());
|
1135
|
query.add(AuditEntity.relatedId("relatedFrom").eq(synonym.getId()));
|
1136
|
query.addProjection(AuditEntity.id().count("id"));
|
1137
|
|
1138
|
if(type != null) {
|
1139
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
1140
|
}
|
1141
|
|
1142
|
return ((Long)query.getSingleResult()).intValue();
|
1143
|
}
|
1144
|
}
|
1145
|
|
1146
|
/*
|
1147
|
* (non-Javadoc)
|
1148
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countTaxaByName(java.lang.Class, java.lang.String, java.lang.String, java.lang.String, java.lang.String, eu.etaxonomy.cdm.model.name.Rank)
|
1149
|
*/
|
1150
|
public int countTaxaByName(Class<? extends TaxonBase> clazz, String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank) {
|
1151
|
checkNotInPriorView("TaxonDaoHibernateImpl.countTaxaByName(Boolean accepted, String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank)");
|
1152
|
Criteria criteria = null;
|
1153
|
|
1154
|
if(clazz == null) {
|
1155
|
criteria = getSession().createCriteria(TaxonBase.class);
|
1156
|
} else {
|
1157
|
criteria = getSession().createCriteria(clazz);
|
1158
|
}
|
1159
|
|
1160
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
1161
|
criteria.createAlias("name", "name");
|
1162
|
|
1163
|
if(genusOrUninomial == null) {
|
1164
|
criteria.add(Restrictions.isNull("name.genusOrUninomial"));
|
1165
|
} else if(!genusOrUninomial.equals("*")) {
|
1166
|
criteria.add(Restrictions.eq("name.genusOrUninomial", genusOrUninomial));
|
1167
|
}
|
1168
|
|
1169
|
if(infraGenericEpithet == null) {
|
1170
|
criteria.add(Restrictions.isNull("name.infraGenericEpithet"));
|
1171
|
} else if(!infraGenericEpithet.equals("*")) {
|
1172
|
criteria.add(Restrictions.eq("name.infraGenericEpithet", infraGenericEpithet));
|
1173
|
}
|
1174
|
|
1175
|
if(specificEpithet == null) {
|
1176
|
criteria.add(Restrictions.isNull("name.specificEpithet"));
|
1177
|
} else if(!specificEpithet.equals("*")) {
|
1178
|
criteria.add(Restrictions.eq("name.specificEpithet", specificEpithet));
|
1179
|
|
1180
|
}
|
1181
|
|
1182
|
if(infraSpecificEpithet == null) {
|
1183
|
criteria.add(Restrictions.isNull("name.infraSpecificEpithet"));
|
1184
|
} else if(!infraSpecificEpithet.equals("*")) {
|
1185
|
criteria.add(Restrictions.eq("name.infraSpecificEpithet", infraSpecificEpithet));
|
1186
|
}
|
1187
|
|
1188
|
if(rank != null) {
|
1189
|
criteria.add(Restrictions.eq("name.rank", rank));
|
1190
|
}
|
1191
|
|
1192
|
criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));
|
1193
|
|
1194
|
return (Integer)criteria.uniqueResult();
|
1195
|
}
|
1196
|
|
1197
|
/*
|
1198
|
* (non-Javadoc)
|
1199
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#findTaxaByName(java.lang.Class, java.lang.String, java.lang.String, java.lang.String, java.lang.String, eu.etaxonomy.cdm.model.name.Rank, java.lang.Integer, java.lang.Integer)
|
1200
|
*/
|
1201
|
public List<TaxonBase> findTaxaByName(Class<? extends TaxonBase> clazz, String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank, Integer pageSize, Integer pageNumber) {
|
1202
|
checkNotInPriorView("TaxonDaoHibernateImpl.findTaxaByName(Boolean accepted, String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank, Integer pageSize, Integer pageNumber)");
|
1203
|
Criteria criteria = null;
|
1204
|
|
1205
|
if(clazz == null) {
|
1206
|
criteria = getSession().createCriteria(TaxonBase.class);
|
1207
|
} else {
|
1208
|
criteria = getSession().createCriteria(clazz);
|
1209
|
}
|
1210
|
|
1211
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
1212
|
criteria.createAlias("name", "name");
|
1213
|
|
1214
|
if(genusOrUninomial == null) {
|
1215
|
criteria.add(Restrictions.isNull("name.genusOrUninomial"));
|
1216
|
} else if(!genusOrUninomial.equals("*")) {
|
1217
|
criteria.add(Restrictions.eq("name.genusOrUninomial", genusOrUninomial));
|
1218
|
}
|
1219
|
|
1220
|
if(infraGenericEpithet == null) {
|
1221
|
criteria.add(Restrictions.isNull("name.infraGenericEpithet"));
|
1222
|
} else if(!infraGenericEpithet.equals("*")) {
|
1223
|
criteria.add(Restrictions.eq("name.infraGenericEpithet", infraGenericEpithet));
|
1224
|
}
|
1225
|
|
1226
|
if(specificEpithet == null) {
|
1227
|
criteria.add(Restrictions.isNull("name.specificEpithet"));
|
1228
|
} else if(!specificEpithet.equals("*")) {
|
1229
|
criteria.add(Restrictions.eq("name.specificEpithet", specificEpithet));
|
1230
|
|
1231
|
}
|
1232
|
|
1233
|
if(infraSpecificEpithet == null) {
|
1234
|
criteria.add(Restrictions.isNull("name.infraSpecificEpithet"));
|
1235
|
} else if(!infraSpecificEpithet.equals("*")) {
|
1236
|
criteria.add(Restrictions.eq("name.infraSpecificEpithet", infraSpecificEpithet));
|
1237
|
}
|
1238
|
|
1239
|
if(rank != null) {
|
1240
|
criteria.add(Restrictions.eq("name.rank", rank));
|
1241
|
}
|
1242
|
|
1243
|
if(pageSize != null) {
|
1244
|
criteria.setMaxResults(pageSize);
|
1245
|
if(pageNumber != null) {
|
1246
|
criteria.setFirstResult(pageNumber * pageSize);
|
1247
|
} else {
|
1248
|
criteria.setFirstResult(0);
|
1249
|
}
|
1250
|
}
|
1251
|
|
1252
|
return (List<TaxonBase>)criteria.list();
|
1253
|
}
|
1254
|
|
1255
|
/*
|
1256
|
* (non-Javadoc)
|
1257
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getTaxonRelationships(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List, eu.etaxonomy.cdm.model.common.RelationshipBase.Direction)
|
1258
|
*/
|
1259
|
public List<TaxonRelationship> getTaxonRelationships(Taxon taxon, TaxonRelationshipType type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths, Direction direction) {
|
1260
|
AuditEvent auditEvent = getAuditEventFromContext();
|
1261
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
1262
|
Criteria criteria = getSession().createCriteria(TaxonRelationship.class);
|
1263
|
|
1264
|
criteria.add(Restrictions.eq("relatedTo", taxon));
|
1265
|
if(type != null) {
|
1266
|
criteria.add(Restrictions.eq("type", type));
|
1267
|
}
|
1268
|
|
1269
|
addOrder(criteria,orderHints);
|
1270
|
|
1271
|
if(pageSize != null) {
|
1272
|
criteria.setMaxResults(pageSize);
|
1273
|
if(pageNumber != null) {
|
1274
|
criteria.setFirstResult(pageNumber * pageSize);
|
1275
|
} else {
|
1276
|
criteria.setFirstResult(0);
|
1277
|
}
|
1278
|
}
|
1279
|
|
1280
|
List<TaxonRelationship> result = (List<TaxonRelationship>)criteria.list();
|
1281
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
1282
|
|
1283
|
return result;
|
1284
|
} else {
|
1285
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonRelationship.class,auditEvent.getRevisionNumber());
|
1286
|
query.add(AuditEntity.relatedId("relatedTo").eq(taxon.getId()));
|
1287
|
|
1288
|
if(type != null) {
|
1289
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
1290
|
}
|
1291
|
|
1292
|
if(pageSize != null) {
|
1293
|
query.setMaxResults(pageSize);
|
1294
|
if(pageNumber != null) {
|
1295
|
query.setFirstResult(pageNumber * pageSize);
|
1296
|
} else {
|
1297
|
query.setFirstResult(0);
|
1298
|
}
|
1299
|
}
|
1300
|
|
1301
|
List<TaxonRelationship> result = (List<TaxonRelationship>)query.getResultList();
|
1302
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
1303
|
|
1304
|
// Ugly, but for now, there is no way to sort on a related entity property in Envers,
|
1305
|
// and we can't live without this functionality in CATE as it screws up the whole
|
1306
|
// taxon tree thing
|
1307
|
if(orderHints != null && !orderHints.isEmpty()) {
|
1308
|
SortedSet<TaxonRelationship> sortedList = new TreeSet<TaxonRelationship>(new TaxonRelationshipFromTaxonComparator());
|
1309
|
sortedList.addAll(result);
|
1310
|
return new ArrayList<TaxonRelationship>(sortedList);
|
1311
|
}
|
1312
|
|
1313
|
return result;
|
1314
|
}
|
1315
|
}
|
1316
|
|
1317
|
class TaxonRelationshipFromTaxonComparator implements Comparator<TaxonRelationship> {
|
1318
|
|
1319
|
public int compare(TaxonRelationship o1, TaxonRelationship o2) {
|
1320
|
return o1.getFromTaxon().getTitleCache().compareTo(o2.getFromTaxon().getTitleCache());
|
1321
|
}
|
1322
|
|
1323
|
}
|
1324
|
|
1325
|
class SynonymRelationshipFromTaxonComparator implements Comparator<SynonymRelationship> {
|
1326
|
|
1327
|
public int compare(SynonymRelationship o1, SynonymRelationship o2) {
|
1328
|
return o1.getSynonym().getTitleCache().compareTo(o2.getSynonym().getTitleCache());
|
1329
|
}
|
1330
|
|
1331
|
}
|
1332
|
|
1333
|
/*
|
1334
|
* (non-Javadoc)
|
1335
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getSynonyms(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
|
1336
|
*/
|
1337
|
public List<SynonymRelationship> getSynonyms(Taxon taxon, SynonymRelationshipType type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1338
|
AuditEvent auditEvent = getAuditEventFromContext();
|
1339
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
1340
|
Criteria criteria = getSession().createCriteria(SynonymRelationship.class);
|
1341
|
|
1342
|
criteria.add(Restrictions.eq("relatedTo", taxon));
|
1343
|
if(type != null) {
|
1344
|
criteria.add(Restrictions.eq("type", type));
|
1345
|
}
|
1346
|
|
1347
|
addOrder(criteria,orderHints);
|
1348
|
|
1349
|
if(pageSize != null) {
|
1350
|
criteria.setMaxResults(pageSize);
|
1351
|
if(pageNumber != null) {
|
1352
|
criteria.setFirstResult(pageNumber * pageSize);
|
1353
|
} else {
|
1354
|
criteria.setFirstResult(0);
|
1355
|
}
|
1356
|
}
|
1357
|
|
1358
|
List<SynonymRelationship> result = (List<SynonymRelationship>)criteria.list();
|
1359
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
1360
|
|
1361
|
return result;
|
1362
|
} else {
|
1363
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(SynonymRelationship.class,auditEvent.getRevisionNumber());
|
1364
|
query.add(AuditEntity.relatedId("relatedTo").eq(taxon.getId()));
|
1365
|
|
1366
|
if(type != null) {
|
1367
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
1368
|
}
|
1369
|
|
1370
|
if(pageSize != null) {
|
1371
|
query.setMaxResults(pageSize);
|
1372
|
if(pageNumber != null) {
|
1373
|
query.setFirstResult(pageNumber * pageSize);
|
1374
|
} else {
|
1375
|
query.setFirstResult(0);
|
1376
|
}
|
1377
|
}
|
1378
|
|
1379
|
List<SynonymRelationship> result = (List<SynonymRelationship>)query.getResultList();
|
1380
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
1381
|
|
1382
|
return result;
|
1383
|
}
|
1384
|
}
|
1385
|
|
1386
|
/*
|
1387
|
* (non-Javadoc)
|
1388
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getSynonyms(eu.etaxonomy.cdm.model.taxon.Synonym, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
|
1389
|
*/
|
1390
|
public List<SynonymRelationship> getSynonyms(Synonym synonym, SynonymRelationshipType type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1391
|
AuditEvent auditEvent = getAuditEventFromContext();
|
1392
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
1393
|
Criteria criteria = getSession().createCriteria(SynonymRelationship.class);
|
1394
|
|
1395
|
criteria.add(Restrictions.eq("relatedFrom", synonym));
|
1396
|
if(type != null) {
|
1397
|
criteria.add(Restrictions.eq("type", type));
|
1398
|
}
|
1399
|
|
1400
|
addOrder(criteria,orderHints);
|
1401
|
|
1402
|
if(pageSize != null) {
|
1403
|
criteria.setMaxResults(pageSize);
|
1404
|
if(pageNumber != null) {
|
1405
|
criteria.setFirstResult(pageNumber * pageSize);
|
1406
|
} else {
|
1407
|
criteria.setFirstResult(0);
|
1408
|
}
|
1409
|
}
|
1410
|
|
1411
|
List<SynonymRelationship> result = (List<SynonymRelationship>)criteria.list();
|
1412
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
1413
|
|
1414
|
return result;
|
1415
|
} else {
|
1416
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(SynonymRelationship.class,auditEvent.getRevisionNumber());
|
1417
|
query.add(AuditEntity.relatedId("relatedFrom").eq(synonym.getId()));
|
1418
|
|
1419
|
if(type != null) {
|
1420
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
1421
|
}
|
1422
|
|
1423
|
if(pageSize != null) {
|
1424
|
query.setMaxResults(pageSize);
|
1425
|
if(pageNumber != null) {
|
1426
|
query.setFirstResult(pageNumber * pageSize);
|
1427
|
} else {
|
1428
|
query.setFirstResult(0);
|
1429
|
}
|
1430
|
}
|
1431
|
|
1432
|
List<SynonymRelationship> result = (List<SynonymRelationship>)query.getResultList();
|
1433
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
1434
|
|
1435
|
return result;
|
1436
|
}
|
1437
|
}
|
1438
|
|
1439
|
@Override
|
1440
|
public void rebuildIndex() {
|
1441
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
1442
|
|
1443
|
for(TaxonBase taxonBase : list(null,null)) { // re-index all taxon base
|
1444
|
Hibernate.initialize(taxonBase.getName());
|
1445
|
fullTextSession.index(taxonBase);
|
1446
|
}
|
1447
|
fullTextSession.flushToIndexes();
|
1448
|
}
|
1449
|
|
1450
|
@Override
|
1451
|
public String suggestQuery(String queryString) {
|
1452
|
checkNotInPriorView("TaxonDaoHibernateImpl.suggestQuery(String queryString)");
|
1453
|
String alternativeQueryString = null;
|
1454
|
if (alternativeSpellingSuggestionParser != null) {
|
1455
|
try {
|
1456
|
|
1457
|
alternativeSpellingSuggestionParser.parse(queryString);
|
1458
|
org.apache.lucene.search.Query alternativeQuery = alternativeSpellingSuggestionParser.suggest(queryString);
|
1459
|
if (alternativeQuery != null) {
|
1460
|
alternativeQueryString = alternativeQuery
|
1461
|
.toString("name.titleCache");
|
1462
|
}
|
1463
|
|
1464
|
} catch (ParseException e) {
|
1465
|
throw new QueryParseException(e, queryString);
|
1466
|
}
|
1467
|
}
|
1468
|
return alternativeQueryString;
|
1469
|
}
|
1470
|
|
1471
|
/*
|
1472
|
* (non-Javadoc)
|
1473
|
* @see eu.etaxonomy.cdm.api.service.ITaxonService#getUuidAndTitleCacheOfAcceptedTaxa(eu.etaxonomy.cdm.model.taxon.Classification)
|
1474
|
*/
|
1475
|
public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification) {
|
1476
|
|
1477
|
int classificationId = classification.getId();
|
1478
|
|
1479
|
String queryString = "SELECT nodes.uuid, taxa.titleCache FROM TaxonNode AS nodes LEFT JOIN TaxonBase AS taxa ON nodes.taxon_id = taxa.id WHERE taxa.DTYPE = 'Taxon' AND nodes.classification_id = " + classificationId;
|
1480
|
|
1481
|
List<Object[]> result = getSession().createSQLQuery(queryString).list();
|
1482
|
|
1483
|
if(result.size() == 0){
|
1484
|
return null;
|
1485
|
}else{
|
1486
|
List<UuidAndTitleCache<TaxonNode>> list = new ArrayList<UuidAndTitleCache<TaxonNode>>(result.size());
|
1487
|
|
1488
|
for (Object object : result){
|
1489
|
|
1490
|
Object[] objectArray = (Object[]) object;
|
1491
|
|
1492
|
UUID uuid = UUID.fromString((String) objectArray[0]);
|
1493
|
String titleCache = (String) objectArray[1];
|
1494
|
|
1495
|
list.add(new UuidAndTitleCache(TaxonNode.class, uuid, titleCache));
|
1496
|
}
|
1497
|
|
1498
|
return list;
|
1499
|
}
|
1500
|
}
|
1501
|
|
1502
|
|
1503
|
public class UuidAndTitleCacheOfAcceptedTaxon{
|
1504
|
UUID uuid;
|
1505
|
|
1506
|
String titleCache;
|
1507
|
|
1508
|
public UuidAndTitleCacheOfAcceptedTaxon(UUID uuid, String titleCache){
|
1509
|
this.uuid = uuid;
|
1510
|
this.titleCache = titleCache;
|
1511
|
}
|
1512
|
|
1513
|
public UUID getUuid() {
|
1514
|
return uuid;
|
1515
|
}
|
1516
|
|
1517
|
public void setUuid(UUID uuid) {
|
1518
|
this.uuid = uuid;
|
1519
|
}
|
1520
|
|
1521
|
public String getTitleCache() {
|
1522
|
return titleCache;
|
1523
|
}
|
1524
|
|
1525
|
public void setTitleCache(String titleCache) {
|
1526
|
this.titleCache = titleCache;
|
1527
|
}
|
1528
|
}
|
1529
|
|
1530
|
@Override
|
1531
|
public TaxonBase find(LSID lsid) {
|
1532
|
TaxonBase taxonBase = super.find(lsid);
|
1533
|
if(taxonBase != null) {
|
1534
|
List<String> propertyPaths = new ArrayList<String>();
|
1535
|
propertyPaths.add("createdBy");
|
1536
|
propertyPaths.add("updatedBy");
|
1537
|
propertyPaths.add("name");
|
1538
|
propertyPaths.add("sec");
|
1539
|
propertyPaths.add("relationsToThisTaxon");
|
1540
|
propertyPaths.add("relationsToThisTaxon.fromTaxon");
|
1541
|
propertyPaths.add("relationsToThisTaxon.toTaxon");
|
1542
|
propertyPaths.add("relationsFromThisTaxon");
|
1543
|
propertyPaths.add("relationsFromThisTaxon.toTaxon");
|
1544
|
propertyPaths.add("relationsToThisTaxon.type");
|
1545
|
propertyPaths.add("synonymRelations");
|
1546
|
propertyPaths.add("synonymRelations.synonym");
|
1547
|
propertyPaths.add("synonymRelations.type");
|
1548
|
propertyPaths.add("descriptions");
|
1549
|
|
1550
|
defaultBeanInitializer.initialize(taxonBase, propertyPaths);
|
1551
|
}
|
1552
|
return taxonBase;
|
1553
|
}
|
1554
|
|
1555
|
public List<TaxonBase> getTaxaByCommonName(String queryString,
|
1556
|
Classification classification, MatchMode matchMode,
|
1557
|
Set<NamedArea> namedAreas, Integer pageSize, Integer pageNumber) {
|
1558
|
// TODO Auto-generated method stub
|
1559
|
return null;
|
1560
|
}
|
1561
|
|
1562
|
|
1563
|
public List<Synonym> createAllInferredSynonyms(Taxon taxon, Classification tree){
|
1564
|
List <Synonym> inferredSynonyms = new ArrayList<Synonym>();
|
1565
|
|
1566
|
inferredSynonyms.addAll(createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_EPITHET_OF()));
|
1567
|
inferredSynonyms.addAll(createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_GENUS_OF()));
|
1568
|
inferredSynonyms.addAll(createInferredSynonyms(taxon, tree, SynonymRelationshipType.POTENTIAL_COMBINATION_OF()));
|
1569
|
|
1570
|
return inferredSynonyms;
|
1571
|
}
|
1572
|
|
1573
|
|
1574
|
/**
|
1575
|
* Returns an existing ZoologicalName or extends an internal hashmap if it does not exist.
|
1576
|
* Very likely only useful for createInferredSynonyms().
|
1577
|
* @param uuid
|
1578
|
* @param zooHashMap
|
1579
|
* @return
|
1580
|
*/
|
1581
|
private ZoologicalName getZoologicalName(UUID uuid, HashMap <UUID, ZoologicalName> zooHashMap) {
|
1582
|
ZoologicalName taxonName = this.taxonNameDao.findZoologicalNameByUUID(uuid);
|
1583
|
if (taxonName == null) {
|
1584
|
taxonName = zooHashMap.get(uuid);
|
1585
|
}
|
1586
|
return taxonName;
|
1587
|
}
|
1588
|
|
1589
|
public List<Synonym> createInferredSynonyms(Taxon taxon, Classification tree, SynonymRelationshipType type){
|
1590
|
List <Synonym> inferredSynonyms = new ArrayList<Synonym>();
|
1591
|
List<Synonym> inferredSynonymsToBeRemoved = new ArrayList<Synonym>();
|
1592
|
|
1593
|
HashMap <UUID, ZoologicalName> zooHashMap = new HashMap<UUID, ZoologicalName>();
|
1594
|
UUID uuid;
|
1595
|
|
1596
|
uuid= taxon.getName().getUuid();
|
1597
|
ZoologicalName taxonName = getZoologicalName(uuid, zooHashMap);
|
1598
|
String epithetOfTaxon = taxonName.getSpecificEpithet();
|
1599
|
String genusOfTaxon = taxonName.getGenusOrUninomial();
|
1600
|
Set<TaxonNode> nodes = taxon.getTaxonNodes();
|
1601
|
List<String> taxonNames = new ArrayList<String>();
|
1602
|
|
1603
|
for (TaxonNode node: nodes){
|
1604
|
HashMap<String, String> synonymsGenus = new HashMap<String, String>(); // Changed this to be able to store the idInSource to a genusName
|
1605
|
List<String> synonymsEpithet = new ArrayList<String>();
|
1606
|
|
1607
|
if (node.getClassification().equals(tree)){
|
1608
|
if (!node.isTopmostNode()){
|
1609
|
TaxonNode parent = (TaxonNode)node.getParent();
|
1610
|
parent = (TaxonNode)HibernateProxyHelper.deproxy(parent);
|
1611
|
TaxonNameBase parentName = parent.getTaxon().getName();
|
1612
|
parentName = (TaxonNameBase)HibernateProxyHelper.deproxy(parentName);
|
1613
|
|
1614
|
//create inferred synonyms for species, subspecies or subgenus
|
1615
|
if (parentName.isGenus() || parentName.isSpecies() || parentName.getRank().equals(Rank.SUBGENUS())){
|
1616
|
|
1617
|
Synonym inferredEpithet;
|
1618
|
Synonym inferredGenus = null;
|
1619
|
Synonym potentialCombination;
|
1620
|
|
1621
|
List<String> propertyPaths = new ArrayList<String>();
|
1622
|
propertyPaths.add("synonym");
|
1623
|
propertyPaths.add("synonym.name");
|
1624
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
1625
|
orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
|
1626
|
|
1627
|
List<SynonymRelationship> synonymRelationshipsOfGenus = getSynonyms(parent.getTaxon(), SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null,orderHints,propertyPaths);
|
1628
|
List<SynonymRelationship> synonymRelationshipsOfTaxon= getSynonyms(taxon, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null,orderHints,propertyPaths);
|
1629
|
|
1630
|
if (type.equals(SynonymRelationshipType.INFERRED_EPITHET_OF())){
|
1631
|
|
1632
|
for (SynonymRelationship synonymRelationOfGenus:synonymRelationshipsOfGenus){
|
1633
|
TaxonNameBase synName;
|
1634
|
ZoologicalName inferredSynName;
|
1635
|
Synonym syn = synonymRelationOfGenus.getSynonym();
|
1636
|
HibernateProxyHelper.deproxy(syn);
|
1637
|
|
1638
|
// Determine the idInSource
|
1639
|
String idInSource = getIdInSource(syn);
|
1640
|
|
1641
|
// Determine the sourceReference
|
1642
|
Reference sourceReference = syn.getSec();
|
1643
|
|
1644
|
synName = syn.getName();
|
1645
|
ZoologicalName zooName = getZoologicalName(synName.getUuid(), zooHashMap);
|
1646
|
String synGenusName = zooName.getGenusOrUninomial();
|
1647
|
if (synGenusName != null && !synonymsGenus.containsKey(synGenusName)){
|
1648
|
synonymsGenus.put(synGenusName, idInSource);
|
1649
|
}
|
1650
|
inferredSynName = ZoologicalName.NewInstance(Rank.SPECIES());
|
1651
|
|
1652
|
// DEBUG
|
1653
|
if (epithetOfTaxon == null) {
|
1654
|
logger.error("This specificEpithet is NULL");
|
1655
|
}
|
1656
|
|
1657
|
inferredSynName.setSpecificEpithet(epithetOfTaxon);
|
1658
|
inferredSynName.setGenusOrUninomial(synGenusName);
|
1659
|
inferredEpithet = Synonym.NewInstance(inferredSynName, null);
|
1660
|
|
1661
|
// Set the sourceReference
|
1662
|
inferredEpithet.setSec(sourceReference);
|
1663
|
|
1664
|
// Add the original source
|
1665
|
if (idInSource != null) {
|
1666
|
IdentifiableSource originalSource = IdentifiableSource.NewInstance(idInSource, "InferredEpithetOf", syn.getSec(), null);
|
1667
|
|
1668
|
// Add the citation
|
1669
|
Reference citation = getCitation(syn);
|
1670
|
if (citation != null) {
|
1671
|
originalSource.setCitation(citation);
|
1672
|
inferredEpithet.addSource(originalSource);
|
1673
|
}
|
1674
|
}
|
1675
|
|
1676
|
taxon.addSynonym(inferredEpithet, SynonymRelationshipType.INFERRED_GENUS_OF());
|
1677
|
inferredSynonyms.add(inferredEpithet);
|
1678
|
inferredSynName.generateTitle();
|
1679
|
zooHashMap.put(inferredSynName.getUuid(), inferredSynName);
|
1680
|
taxonNames.add(inferredSynName.getNameCache());
|
1681
|
}
|
1682
|
|
1683
|
if (!taxonNames.isEmpty()){
|
1684
|
List<String> synNotInCDM = this.taxaByNameNotInDB(taxonNames);
|
1685
|
ZoologicalName name;
|
1686
|
if (!synNotInCDM.isEmpty()){
|
1687
|
inferredSynonymsToBeRemoved.clear();
|
1688
|
|
1689
|
for (Synonym syn :inferredSynonyms){
|
1690
|
name = getZoologicalName(syn.getName().getUuid(), zooHashMap);
|
1691
|
if (!synNotInCDM.contains(name.getNameCache())){
|
1692
|
inferredSynonymsToBeRemoved.add(syn);
|
1693
|
}
|
1694
|
}
|
1695
|
|
1696
|
// Remove identified Synonyms from inferredSynonyms
|
1697
|
for (Synonym synonym : inferredSynonymsToBeRemoved) {
|
1698
|
inferredSynonyms.remove(synonym);
|
1699
|
}
|
1700
|
}
|
1701
|
}
|
1702
|
|
1703
|
}else if (type.equals(SynonymRelationshipType.INFERRED_GENUS_OF())){
|
1704
|
|
1705
|
|
1706
|
for (SynonymRelationship synonymRelationOfTaxon:synonymRelationshipsOfTaxon){
|
1707
|
TaxonNameBase synName;
|
1708
|
ZoologicalName inferredSynName;
|
1709
|
|
1710
|
Synonym syn = synonymRelationOfTaxon.getSynonym();
|
1711
|
synName =syn.getName();
|
1712
|
HibernateProxyHelper.deproxy(syn);
|
1713
|
|
1714
|
// Determine the idInSource
|
1715
|
String idInSource = getIdInSource(syn);
|
1716
|
|
1717
|
// Determine the sourceReference
|
1718
|
Reference sourceReference = syn.getSec();
|
1719
|
|
1720
|
synName = syn.getName();
|
1721
|
ZoologicalName zooName = getZoologicalName(synName.getUuid(), zooHashMap);
|
1722
|
String speciesEpithetName = zooName.getSpecificEpithet();
|
1723
|
if (synonymsEpithet != null && !synonymsEpithet.contains(speciesEpithetName)){
|
1724
|
synonymsEpithet.add(speciesEpithetName);
|
1725
|
}
|
1726
|
inferredSynName = ZoologicalName.NewInstance(Rank.SPECIES());
|
1727
|
inferredSynName.setSpecificEpithet(speciesEpithetName);
|
1728
|
inferredSynName.setGenusOrUninomial(genusOfTaxon);
|
1729
|
inferredGenus = Synonym.NewInstance(inferredSynName, null);
|
1730
|
|
1731
|
// Set the sourceReference
|
1732
|
inferredGenus.setSec(sourceReference);
|
1733
|
|
1734
|
// Add the original source
|
1735
|
if (idInSource != null) {
|
1736
|
IdentifiableSource originalSource = IdentifiableSource.NewInstance(idInSource, "InferredGenusOf", syn.getSec(), null);
|
1737
|
|
1738
|
// Add the citation
|
1739
|
Reference citation = getCitation(syn);
|
1740
|
if (citation != null) {
|
1741
|
originalSource.setCitation(citation);
|
1742
|
inferredGenus.addSource(originalSource);
|
1743
|
}
|
1744
|
}
|
1745
|
|
1746
|
taxon.addSynonym(inferredGenus, SynonymRelationshipType.INFERRED_EPITHET_OF());
|
1747
|
inferredSynonyms.add(inferredGenus);
|
1748
|
inferredSynName.generateTitle();
|
1749
|
zooHashMap.put(inferredSynName.getUuid(), inferredSynName);
|
1750
|
taxonNames.add(inferredSynName.getNameCache());
|
1751
|
}
|
1752
|
|
1753
|
if (!taxonNames.isEmpty()){
|
1754
|
List<String> synNotInCDM = this.taxaByNameNotInDB(taxonNames);
|
1755
|
ZoologicalName name;
|
1756
|
if (!synNotInCDM.isEmpty()){
|
1757
|
inferredSynonymsToBeRemoved.clear();
|
1758
|
|
1759
|
for (Synonym syn :inferredSynonyms){
|
1760
|
name = getZoologicalName(syn.getName().getUuid(), zooHashMap);
|
1761
|
if (!synNotInCDM.contains(name.getNameCache())){
|
1762
|
inferredSynonymsToBeRemoved.add(syn);
|
1763
|
}
|
1764
|
}
|
1765
|
|
1766
|
// Remove identified Synonyms from inferredSynonyms
|
1767
|
for (Synonym synonym : inferredSynonymsToBeRemoved) {
|
1768
|
inferredSynonyms.remove(synonym);
|
1769
|
}
|
1770
|
}
|
1771
|
}
|
1772
|
|
1773
|
}else if (type.equals(SynonymRelationshipType.POTENTIAL_COMBINATION_OF())){
|
1774
|
|
1775
|
Reference sourceReference = null; // TODO: Determination of sourceReference is redundant
|
1776
|
|
1777
|
for (SynonymRelationship synonymRelationOfGenus:synonymRelationshipsOfGenus){
|
1778
|
TaxonNameBase synName;
|
1779
|
Synonym syn = synonymRelationOfGenus.getSynonym();
|
1780
|
synName =syn.getName();
|
1781
|
|
1782
|
HibernateProxyHelper.deproxy(syn);
|
1783
|
|
1784
|
// Set the sourceReference
|
1785
|
sourceReference = syn.getSec();
|
1786
|
|
1787
|
// Determine the idInSource
|
1788
|
String idInSource = getIdInSource(syn);
|
1789
|
|
1790
|
ZoologicalName zooName = getZoologicalName(synName.getUuid(), zooHashMap);
|
1791
|
String synGenusName = zooName.getGenusOrUninomial();
|
1792
|
if (synGenusName != null && !synonymsGenus.containsKey(synGenusName)){
|
1793
|
synonymsGenus.put(synGenusName, idInSource);
|
1794
|
}
|
1795
|
}
|
1796
|
|
1797
|
ZoologicalName inferredSynName;
|
1798
|
for (SynonymRelationship synonymRelationOfTaxon:synonymRelationshipsOfTaxon){
|
1799
|
|
1800
|
Synonym syn = synonymRelationOfTaxon.getSynonym();
|
1801
|
HibernateProxyHelper.deproxy(syn);
|
1802
|
|
1803
|
// Set sourceReference
|
1804
|
sourceReference = syn.getSec();
|
1805
|
|
1806
|
ZoologicalName zooName = getZoologicalName(syn.getName().getUuid(), zooHashMap);
|
1807
|
String epithetName = zooName.getSpecificEpithet();
|
1808
|
if (epithetName != null && !synonymsEpithet.contains(epithetName)){
|
1809
|
synonymsEpithet.add(epithetName);
|
1810
|
}
|
1811
|
}
|
1812
|
for (String epithetName:synonymsEpithet){
|
1813
|
for (String genusName: synonymsGenus.keySet()){
|
1814
|
inferredSynName = ZoologicalName.NewInstance(Rank.SPECIES());
|
1815
|
inferredSynName.setSpecificEpithet(epithetName);
|
1816
|
inferredSynName.setGenusOrUninomial(genusName);
|
1817
|
potentialCombination = Synonym.NewInstance(inferredSynName, null);
|
1818
|
|
1819
|
// Set the sourceReference
|
1820
|
potentialCombination.setSec(sourceReference);
|
1821
|
|
1822
|
// Add the original source
|
1823
|
String idInSource = synonymsGenus.get(genusName);
|
1824
|
if (idInSource != null) {
|
1825
|
IdentifiableSource originalSource = IdentifiableSource.NewInstance(idInSource, "PotentialCombinationOf", sourceReference, null);
|
1826
|
|
1827
|
// Add the citation
|
1828
|
if (sourceReference != null) {
|
1829
|
originalSource.setCitation(sourceReference);
|
1830
|
potentialCombination.addSource(originalSource);
|
1831
|
}
|
1832
|
}
|
1833
|
|
1834
|
inferredSynonyms.add(potentialCombination);
|
1835
|
inferredSynName.generateTitle();
|
1836
|
zooHashMap.put(inferredSynName.getUuid(), inferredSynName);
|
1837
|
taxonNames.add(inferredSynName.getNameCache());
|
1838
|
}
|
1839
|
|
1840
|
if (!taxonNames.isEmpty()){
|
1841
|
List<String> synNotInCDM = this.taxaByNameNotInDB(taxonNames);
|
1842
|
ZoologicalName name;
|
1843
|
if (!synNotInCDM.isEmpty()){
|
1844
|
inferredSynonymsToBeRemoved.clear();
|
1845
|
|
1846
|
for (Synonym syn :inferredSynonyms){
|
1847
|
try{
|
1848
|
name = (ZoologicalName) syn.getName();
|
1849
|
}catch (ClassCastException e){
|
1850
|
name = getZoologicalName(syn.getName().getUuid(), zooHashMap);
|
1851
|
}
|
1852
|
if (!synNotInCDM.contains(name.getNameCache())){
|
1853
|
inferredSynonymsToBeRemoved.add(syn);
|
1854
|
}
|
1855
|
}
|
1856
|
|
1857
|
// Remove identified Synonyms from inferredSynonyms
|
1858
|
for (Synonym synonym : inferredSynonymsToBeRemoved) {
|
1859
|
inferredSynonyms.remove(synonym);
|
1860
|
}
|
1861
|
}
|
1862
|
}
|
1863
|
}
|
1864
|
}else {
|
1865
|
logger.info("The synonymrelationship type is not defined.");
|
1866
|
return null;
|
1867
|
}
|
1868
|
}
|
1869
|
}
|
1870
|
}
|
1871
|
}
|
1872
|
|
1873
|
|
1874
|
return inferredSynonyms;
|
1875
|
}
|
1876
|
|
1877
|
|
1878
|
/**
|
1879
|
* Returns the idInSource for a given Synonym.
|
1880
|
* @param syn
|
1881
|
*/
|
1882
|
private String getIdInSource(Synonym syn) {
|
1883
|
String idInSource = null;
|
1884
|
Set<IdentifiableSource> sources = syn.getSources();
|
1885
|
if (sources.size() == 1) {
|
1886
|
IdentifiableSource source = sources.iterator().next();
|
1887
|
if (source != null) {
|
1888
|
idInSource = source.getIdInSource();
|
1889
|
}
|
1890
|
} else if (sources.size() > 1) {
|
1891
|
int count = 1;
|
1892
|
idInSource = "";
|
1893
|
for (IdentifiableSource source : sources) {
|
1894
|
idInSource += source.getIdInSource();
|
1895
|
if (count < sources.size()) {
|
1896
|
idInSource += "; ";
|
1897
|
}
|
1898
|
count++;
|
1899
|
}
|
1900
|
}
|
1901
|
|
1902
|
return idInSource;
|
1903
|
}
|
1904
|
|
1905
|
/**
|
1906
|
* Returns the citation for a given Synonym.
|
1907
|
* @param syn
|
1908
|
*/
|
1909
|
private Reference getCitation(Synonym syn) {
|
1910
|
Reference citation = null;
|
1911
|
Set<IdentifiableSource> sources = syn.getSources();
|
1912
|
if (sources.size() == 1) {
|
1913
|
IdentifiableSource source = sources.iterator().next();
|
1914
|
if (source != null) {
|
1915
|
citation = source.getCitation();
|
1916
|
}
|
1917
|
} else if (sources.size() > 1) {
|
1918
|
logger.warn("This Synonym has more than one source: " + syn.getUuid() + " (" + syn.getTitleCache() +")");
|
1919
|
}
|
1920
|
|
1921
|
return citation;
|
1922
|
}
|
1923
|
|
1924
|
/* private void xxx(List<SynonymRelationship> synonymRelationships, HashMap <UUID, ZoologicalName> zooHashMap, SynonymRelationshipType type, String addString){
|
1925
|
|
1926
|
for (SynonymRelationship synonymRelation:synonymRelationships){
|
1927
|
TaxonNameBase synName;
|
1928
|
NonViralName inferredSynName;
|
1929
|
Synonym syn = synonymRelation.getSynonym();
|
1930
|
HibernateProxyHelper.deproxy(syn);
|
1931
|
|
1932
|
synName = syn.getName();
|
1933
|
ZoologicalName zooName = zooHashMap.get(synName.getUuid());
|
1934
|
String synGenusName = zooName.getGenusOrUninomial();
|
1935
|
|
1936
|
switch(type.getId()){
|
1937
|
case SynonymRelationshipType.INFERRED_EPITHET_OF().getId():
|
1938
|
inferredSynName.setSpecificEpithet(addString);
|
1939
|
break;
|
1940
|
case SynonymRelationshipType.INFERRED_GENUS_OF().getId():
|
1941
|
break;
|
1942
|
case SynonymRelationshipType.POTENTIAL_COMBINATION_OF().getId():
|
1943
|
break;
|
1944
|
default:
|
1945
|
}
|
1946
|
if (!synonymsGenus.contains(synGenusName)){
|
1947
|
synonymsGenus.add(synGenusName);
|
1948
|
}
|
1949
|
inferredSynName = NonViralName.NewInstance(Rank.SPECIES());
|
1950
|
inferredSynName.setSpecificEpithet(epithetOfTaxon);
|
1951
|
inferredSynName.setGenusOrUninomial(synGenusName);
|
1952
|
inferredEpithet = Synonym.NewInstance(inferredSynName, null);
|
1953
|
taxon.addSynonym(inferredEpithet, SynonymRelationshipType.INFERRED_GENUS_OF());
|
1954
|
inferredSynonyms.add(inferredEpithet);
|
1955
|
inferredSynName.generateTitle();
|
1956
|
taxonNames.add(inferredSynName.getNameCache());
|
1957
|
}
|
1958
|
|
1959
|
|
1960
|
if (!taxonNames.isEmpty()){
|
1961
|
List<String> synNotInCDM = this.taxaByNameNotInDB(taxonNames);
|
1962
|
ZoologicalName name;
|
1963
|
if (!synNotInCDM.isEmpty()){
|
1964
|
for (Synonym syn :inferredSynonyms){
|
1965
|
name =zooHashMap.get(syn.getName().getUuid());
|
1966
|
if (!synNotInCDM.contains(name.getNameCache())){
|
1967
|
inferredSynonyms.remove(syn);
|
1968
|
}
|
1969
|
}
|
1970
|
}
|
1971
|
}
|
1972
|
}*/
|
1973
|
|
1974
|
/*
|
1975
|
* (non-Javadoc)
|
1976
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#countAllRelationships()
|
1977
|
*/
|
1978
|
public int countAllRelationships() {
|
1979
|
List<RelationshipBase> relationships = this.getAllRelationships(null, 0);
|
1980
|
return relationships.size();
|
1981
|
}
|
1982
|
|
1983
|
|
1984
|
public List<String> taxaByNameNotInDB(List<String> taxonNames){
|
1985
|
List<TaxonBase> notInDB = new ArrayList<TaxonBase>();
|
1986
|
//get all taxa, already in db
|
1987
|
Query query = getSession().createQuery("from TaxonNameBase t where t.nameCache IN (:taxonList)");
|
1988
|
query.setParameterList("taxonList", taxonNames);
|
1989
|
List<TaxonNameBase> taxaInDB = query.list();
|
1990
|
//compare the original list with the result of the query
|
1991
|
for (TaxonNameBase taxonName: taxaInDB){
|
1992
|
if (taxonName.isInstanceOf(NonViralName.class)) {
|
1993
|
NonViralName nonViralName = CdmBase.deproxy(taxonName, NonViralName.class);
|
1994
|
String nameCache = nonViralName.getNameCache();
|
1995
|
if (taxonNames.contains(nameCache)){
|
1996
|
taxonNames.remove(nameCache);
|
1997
|
}
|
1998
|
}
|
1999
|
}
|
2000
|
|
2001
|
return taxonNames;
|
2002
|
}
|
2003
|
|
2004
|
//TODO: mal nur mit UUID probieren (ohne fetch all properties), vielleicht geht das schneller?
|
2005
|
public List<UUID> findIdenticalTaxonNameIds(List<String> propertyPaths){
|
2006
|
Query query=getSession().createQuery("select tmb2 from ZoologicalName tmb, ZoologicalName tmb2 fetch all properties where tmb.id != tmb2.id and tmb.nameCache = tmb2.nameCache");
|
2007
|
System.err.println("query: " + query.getQueryString());
|
2008
|
List<UUID> zooNames = query.list();
|
2009
|
System.err.println("number of identical names"+zooNames.size());
|
2010
|
|
2011
|
|
2012
|
return zooNames;
|
2013
|
|
2014
|
}
|
2015
|
|
2016
|
public List<TaxonNameBase> findIdenticalTaxonNames(List<String> propertyPaths) {
|
2017
|
|
2018
|
//hole alle TaxonNames, die es mindestens zweimal gibt.
|
2019
|
|
2020
|
|
2021
|
|
2022
|
Query query=getSession().createQuery("select tmb2 from ZoologicalName tmb, ZoologicalName tmb2 fetch all properties where tmb.id != tmb2.id and tmb.nameCache = tmb2.nameCache");
|
2023
|
System.err.println("query: " + query.getQueryString());
|
2024
|
List<TaxonNameBase> zooNames = query.list();
|
2025
|
System.err.println("number of identical names"+zooNames.size());
|
2026
|
|
2027
|
TaxonNameComparator taxComp = new TaxonNameComparator();
|
2028
|
Collections.sort(zooNames, taxComp);
|
2029
|
System.err.println("list is sorted");
|
2030
|
for (TaxonNameBase taxonNameBase: zooNames){
|
2031
|
defaultBeanInitializer.initialize(taxonNameBase, propertyPaths);
|
2032
|
}
|
2033
|
|
2034
|
return zooNames;
|
2035
|
}
|
2036
|
|
2037
|
public List<TaxonNameBase> findIdenticalNamesNew(List<String> propertyPaths){
|
2038
|
|
2039
|
//Hole die beiden Source_ids von "Fauna Europaea" und "Erms" und in sources der names darf jeweils nur das entgegengesetzte auftreten (i member of tmb.taxonBases)
|
2040
|
Query query = getSession().createQuery("Select id from Reference where titleCache like 'Fauna Europaea database'");
|
2041
|
List<String> secRefFauna = query.list();
|
2042
|
query = getSession().createQuery("Select id from Reference where titleCache like 'ERMS'");
|
2043
|
List<String> secRefErms = query.list();
|
2044
|
//Query query = getSession().createQuery("select tmb2.nameCache from ZoologicalName tmb, TaxonBase tb1, ZoologicalName tmb2, TaxonBase tb2 where tmb.id != tmb2.id and tb1.name = tmb and tb2.name = tmb2 and tmb.nameCache = tmb2.nameCache and tb1.sec != tb2.sec");
|
2045
|
//Get all names of fauna europaea
|
2046
|
query = getSession().createQuery("select zn.nameCache from ZoologicalName zn, TaxonBase tb where tb.name = zn and tb.sec.id = :secRefFauna");
|
2047
|
query.setParameter("secRefFauna", secRefFauna.get(0));
|
2048
|
List<String> namesFauna= query.list();
|
2049
|
|
2050
|
//Get all names of erms
|
2051
|
|
2052
|
query = getSession().createQuery("select zn.nameCache from ZoologicalName zn, TaxonBase tb where tb.name = zn and tb.sec.id = :secRefErms");
|
2053
|
query.setParameter("secRefErms", secRefErms.get(0));
|
2054
|
|
2055
|
List<String> namesErms = query.list();
|
2056
|
/*TaxonNameComparator comp = new TaxonNameComparator();
|
2057
|
Collections.sort(namesFauna);
|
2058
|
Collections.sort(namesErms);
|
2059
|
*/
|
2060
|
List <String> identicalNames = new ArrayList<String>();
|
2061
|
String predecessor = "";
|
2062
|
|
2063
|
for (String nameFauna: namesFauna){
|
2064
|
if (namesErms.contains(nameFauna)){
|
2065
|
identicalNames.add(nameFauna);
|
2066
|
}
|
2067
|
}
|
2068
|
System.err.println("number of identical names: " + identicalNames.size());
|
2069
|
|
2070
|
query = getSession().createQuery("from ZoologicalName zn where zn.nameCache IN (:identicalNames)");
|
2071
|
query.setParameterList("identicalNames", identicalNames);
|
2072
|
List<TaxonNameBase> result = query.list();
|
2073
|
TaxonNameBase temp = result.get(0);
|
2074
|
|
2075
|
Iterator<OriginalSourceBase> sources = temp.getSources().iterator();
|
2076
|
System.err.println(temp.getSources().size());
|
2077
|
if (sources.hasNext()){
|
2078
|
System.err.println("funktioniert..."+ sources.next().getIdInSource());
|
2079
|
//sources.next().getIdInSource();
|
2080
|
}
|
2081
|
TaxonNameComparator taxComp = new TaxonNameComparator();
|
2082
|
Collections.sort(result, taxComp);
|
2083
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
2084
|
return result;
|
2085
|
|
2086
|
}
|
2087
|
|
2088
|
|
2089
|
|
2090
|
public String getPhylumName(TaxonNameBase name){
|
2091
|
List results = new ArrayList();
|
2092
|
try{
|
2093
|
Query query = getSession().createSQLQuery("select getPhylum("+ name.getId()+");");
|
2094
|
results = query.list();
|
2095
|
}catch(Exception e){
|
2096
|
System.err.println(name.getUuid());
|
2097
|
return null;
|
2098
|
}
|
2099
|
System.err.println("phylum of "+ name.getTitleCache() );
|
2100
|
return (String)results.get(0);
|
2101
|
}
|
2102
|
|
2103
|
|
2104
|
public long countTaxaByCommonName(String searchString,
|
2105
|
Classification classification, MatchMode matchMode,
|
2106
|
Set<NamedArea> namedAreas) {
|
2107
|
boolean doCount = true;
|
2108
|
Query query = prepareTaxaByCommonName(searchString, classification, matchMode, namedAreas, null, null, doCount);
|
2109
|
if (query != null && !query.list().isEmpty()) {
|
2110
|
Object o = query.uniqueResult();
|
2111
|
if(o != null) {
|
2112
|
return (Long)o;
|
2113
|
}
|
2114
|
}
|
2115
|
return 0;
|
2116
|
|
2117
|
}
|
2118
|
|
2119
|
|
2120
|
public long deleteSynonymRelationships(Synonym syn) {
|
2121
|
|
2122
|
/*
|
2123
|
* DELETE RT
|
2124
|
FROM RelTaxon AS RT INNER JOIN
|
2125
|
Taxon AS FaEuSyn ON RT.TaxonFk1 = FaEuSyn.TaxonId INNER JOIN
|
2126
|
Taxon AS ERMSAcc ON FaEuSyn.RankFk = ERMSAcc.RankFk AND FaEuSyn.FullName = ERMSAcc.FullName AND ISNULL(FaEuSyn.TaxonStatusFk, 0)
|
2127
|
<> ERMSAcc.TaxonStatusFk
|
2128
|
WHERE (FaEuSyn.OriginalDB = N'FaEu') AND (ERMSAcc.OriginalDB = N'ERMS') AND (ERMSAcc.TaxonStatusFk = 1) AND (ERMSAcc.KingdomFk = 2) AND
|
2129
|
(RT.RelTaxonQualifierFk > 100)
|
2130
|
*/
|
2131
|
Session session = this.getSession();
|
2132
|
Query q = session.createQuery("delete SynonymRelationship sr where sr.relatedFrom = :syn");
|
2133
|
q.setParameter("syn", syn);
|
2134
|
return q.executeUpdate();
|
2135
|
}
|
2136
|
|
2137
|
|
2138
|
@Override
|
2139
|
public Integer countSynonymRelationships(TaxonBase taxonBase,
|
2140
|
SynonymRelationshipType type, Direction relatedfrom) {
|
2141
|
AuditEvent auditEvent = getAuditEventFromContext();
|
2142
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
2143
|
Query query = null;
|
2144
|
|
2145
|
if(type == null) {
|
2146
|
query = getSession().createQuery("select count(synonymRelationship) from SynonymRelationship synonymRelationship where synonymRelationship."+relatedfrom+" = :relatedSynonym");
|
2147
|
} else {
|
2148
|
query = getSession().createQuery("select count(synonymRelationship) from SynonymRelationship synonymRelationship where synonymRelationship."+relatedfrom+" = :relatedSynonym and synonymRelationship.type = :type");
|
2149
|
query.setParameter("type",type);
|
2150
|
}
|
2151
|
query.setParameter("relatedTaxon", taxonBase);
|
2152
|
|
2153
|
return ((Long)query.uniqueResult()).intValue();
|
2154
|
} else {
|
2155
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonRelationship.class,auditEvent.getRevisionNumber());
|
2156
|
query.add(AuditEntity.relatedId(relatedfrom.toString()).eq(taxonBase.getId()));
|
2157
|
query.addProjection(AuditEntity.id().count("id"));
|
2158
|
|
2159
|
if(type != null) {
|
2160
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
2161
|
}
|
2162
|
|
2163
|
return ((Long)query.getSingleResult()).intValue();
|
2164
|
}
|
2165
|
}
|
2166
|
|
2167
|
|
2168
|
@Override
|
2169
|
public List<SynonymRelationship> getSynonymRelationships(TaxonBase taxonBase,
|
2170
|
SynonymRelationshipType type, Integer pageSize, Integer pageNumber,
|
2171
|
List<OrderHint> orderHints, List<String> propertyPaths,
|
2172
|
Direction direction) {
|
2173
|
|
2174
|
AuditEvent auditEvent = getAuditEventFromContext();
|
2175
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
2176
|
Criteria criteria = getSession().createCriteria(SynonymRelationship.class);
|
2177
|
|
2178
|
if (direction.equals(Direction.relatedTo)){
|
2179
|
criteria.add(Restrictions.eq("relatedTo", taxonBase));
|
2180
|
}else{
|
2181
|
criteria.add(Restrictions.eq("relatedFrom", taxonBase));
|
2182
|
}
|
2183
|
if(type != null) {
|
2184
|
criteria.add(Restrictions.eq("type", type));
|
2185
|
}
|
2186
|
|
2187
|
addOrder(criteria,orderHints);
|
2188
|
|
2189
|
if(pageSize != null) {
|
2190
|
criteria.setMaxResults(pageSize);
|
2191
|
if(pageNumber != null) {
|
2192
|
criteria.setFirstResult(pageNumber * pageSize);
|
2193
|
} else {
|
2194
|
criteria.setFirstResult(0);
|
2195
|
}
|
2196
|
}
|
2197
|
|
2198
|
List<SynonymRelationship> result = (List<SynonymRelationship>)criteria.list();
|
2199
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
2200
|
|
2201
|
return result;
|
2202
|
} else {
|
2203
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonRelationship.class,auditEvent.getRevisionNumber());
|
2204
|
|
2205
|
if (direction.equals(Direction.relatedTo)){
|
2206
|
query.add(AuditEntity.relatedId("relatedTo").eq(taxonBase.getId()));
|
2207
|
}else{
|
2208
|
query.add(AuditEntity.relatedId("relatedFrom").eq(taxonBase.getId()));
|
2209
|
}
|
2210
|
|
2211
|
if(type != null) {
|
2212
|
query.add(AuditEntity.relatedId("type").eq(type.getId()));
|
2213
|
}
|
2214
|
|
2215
|
if(pageSize != null) {
|
2216
|
query.setMaxResults(pageSize);
|
2217
|
if(pageNumber != null) {
|
2218
|
query.setFirstResult(pageNumber * pageSize);
|
2219
|
} else {
|
2220
|
query.setFirstResult(0);
|
2221
|
}
|
2222
|
}
|
2223
|
|
2224
|
List<SynonymRelationship> result = (List<SynonymRelationship>)query.getResultList();
|
2225
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
2226
|
|
2227
|
// Ugly, but for now, there is no way to sort on a related entity property in Envers,
|
2228
|
// and we can't live without this functionality in CATE as it screws up the whole
|
2229
|
// taxon tree thing
|
2230
|
if(orderHints != null && !orderHints.isEmpty()) {
|
2231
|
SortedSet<SynonymRelationship> sortedList = new TreeSet<SynonymRelationship>(new SynonymRelationshipFromTaxonComparator());
|
2232
|
sortedList.addAll(result);
|
2233
|
return new ArrayList<SynonymRelationship>(sortedList);
|
2234
|
}
|
2235
|
|
2236
|
return result;
|
2237
|
}
|
2238
|
}
|
2239
|
|
2240
|
|
2241
|
/* (non-Javadoc)
|
2242
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getUuidAndTitleCacheTaxon()
|
2243
|
*/
|
2244
|
@Override
|
2245
|
public List<UuidAndTitleCache<TaxonBase>> getUuidAndTitleCacheTaxon() {
|
2246
|
String queryString = String.format("select uuid, titleCache from %s where DTYPE = '%s'", type.getSimpleName(), Taxon.class.getSimpleName());
|
2247
|
Query query = getSession().createQuery(queryString);
|
2248
|
|
2249
|
List<UuidAndTitleCache<TaxonBase>> result = getUuidAndTitleCache(query);
|
2250
|
|
2251
|
return result;
|
2252
|
}
|
2253
|
|
2254
|
|
2255
|
/* (non-Javadoc)
|
2256
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getUuidAndTitleCacheSynonym()
|
2257
|
*/
|
2258
|
@Override
|
2259
|
public List<UuidAndTitleCache<TaxonBase>> getUuidAndTitleCacheSynonym() {
|
2260
|
String queryString = String.format("select uuid, titleCache from %s where DTYPE = '%s'", type.getSimpleName(), Synonym.class.getSimpleName());
|
2261
|
Query query = getSession().createQuery(queryString);
|
2262
|
|
2263
|
List<UuidAndTitleCache<TaxonBase>> result = getUuidAndTitleCache(query);
|
2264
|
|
2265
|
return result;
|
2266
|
}
|
2267
|
|
2268
|
|
2269
|
|
2270
|
|
2271
|
|
2272
|
|
2273
|
|
2274
|
|
2275
|
|
2276
|
|
2277
|
}
|