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.Iterator;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.apache.lucene.analysis.SimpleAnalyzer;
|
19
|
import org.apache.lucene.queryParser.ParseException;
|
20
|
import org.apache.lucene.queryParser.QueryParser;
|
21
|
import org.apache.lucene.search.Sort;
|
22
|
import org.apache.lucene.search.SortField;
|
23
|
import org.hibernate.Criteria;
|
24
|
import org.hibernate.FetchMode;
|
25
|
import org.hibernate.Hibernate;
|
26
|
import org.hibernate.LazyInitializationException;
|
27
|
import org.hibernate.Query;
|
28
|
import org.hibernate.criterion.Criterion;
|
29
|
import org.hibernate.criterion.Projections;
|
30
|
import org.hibernate.criterion.Restrictions;
|
31
|
import org.hibernate.search.FullTextSession;
|
32
|
import org.hibernate.search.Search;
|
33
|
import org.hibernate.search.SearchFactory;
|
34
|
import org.springframework.beans.factory.annotation.Autowired;
|
35
|
import org.springframework.beans.factory.annotation.Qualifier;
|
36
|
import org.springframework.dao.DataAccessException;
|
37
|
import org.springframework.stereotype.Repository;
|
38
|
|
39
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
40
|
import eu.etaxonomy.cdm.model.common.Extension;
|
41
|
import eu.etaxonomy.cdm.model.common.Marker;
|
42
|
import eu.etaxonomy.cdm.model.common.OriginalSource;
|
43
|
import eu.etaxonomy.cdm.model.common.RelationshipBase;
|
44
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
45
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
46
|
import eu.etaxonomy.cdm.model.media.Rights;
|
47
|
import eu.etaxonomy.cdm.model.name.Rank;
|
48
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
49
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
50
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
51
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
|
52
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
53
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
54
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
55
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
56
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
57
|
import eu.etaxonomy.cdm.persistence.dao.QueryParseException;
|
58
|
import eu.etaxonomy.cdm.persistence.dao.common.ITitledDao;
|
59
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.AlternativeSpellingSuggestionParser;
|
60
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
|
61
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
62
|
import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
|
63
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
64
|
|
65
|
/**
|
66
|
* @author a.mueller
|
67
|
* @created 24.11.2008
|
68
|
* @version 1.0
|
69
|
*/
|
70
|
@Repository
|
71
|
@Qualifier("taxonDaoHibernateImpl")
|
72
|
public class TaxonDaoHibernateImpl extends IdentifiableDaoBase<TaxonBase> implements ITaxonDao {
|
73
|
private AlternativeSpellingSuggestionParser<TaxonBase> alternativeSpellingSuggestionParser;
|
74
|
|
75
|
private static final Logger logger = Logger.getLogger(TaxonDaoHibernateImpl.class);
|
76
|
|
77
|
public TaxonDaoHibernateImpl() {
|
78
|
super(TaxonBase.class);
|
79
|
}
|
80
|
|
81
|
@Autowired(required = false) //TODO switched of because it caused problems when starting CdmApplicationController
|
82
|
public void setAlternativeSpellingSuggestionParser(AlternativeSpellingSuggestionParser<TaxonBase> alternativeSpellingSuggestionParser) {
|
83
|
this.alternativeSpellingSuggestionParser = alternativeSpellingSuggestionParser;
|
84
|
}
|
85
|
|
86
|
/* (non-Javadoc)
|
87
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase)
|
88
|
*/
|
89
|
public List<Taxon> getRootTaxa(ReferenceBase sec) {
|
90
|
return getRootTaxa(sec, CdmFetch.FETCH_CHILDTAXA(), true, false);
|
91
|
}
|
92
|
|
93
|
|
94
|
|
95
|
/* (non-Javadoc)
|
96
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getRootTaxa(eu.etaxonomy.cdm.model.name.Rank, eu.etaxonomy.cdm.model.reference.ReferenceBase, eu.etaxonomy.cdm.persistence.fetch.CdmFetch, java.lang.Boolean, java.lang.Boolean)
|
97
|
*/
|
98
|
public List<Taxon>
|
99
|
getRootTaxa(Rank rank, ReferenceBase sec, CdmFetch cdmFetch, Boolean onlyWithChildren, Boolean withMisapplications) {
|
100
|
if (onlyWithChildren == null){
|
101
|
onlyWithChildren = true;
|
102
|
}
|
103
|
if (withMisapplications == null){
|
104
|
withMisapplications = true;
|
105
|
}
|
106
|
if (cdmFetch == null){
|
107
|
cdmFetch = CdmFetch.NO_FETCH();
|
108
|
}
|
109
|
|
110
|
Criteria crit = getSession().createCriteria(Taxon.class);
|
111
|
//crit.add(Restrictions.isNull("taxonomicParentCache"));
|
112
|
|
113
|
crit.setFetchMode("name", FetchMode.JOIN);
|
114
|
crit.createAlias("name", "name");
|
115
|
|
116
|
if (rank != null) {
|
117
|
crit.add(Restrictions.eq("name.rank", rank));
|
118
|
}
|
119
|
|
120
|
if (sec != null){
|
121
|
crit.add(Restrictions.eq("sec", sec) );
|
122
|
}
|
123
|
|
124
|
if (! cdmFetch.includes(CdmFetch.FETCH_CHILDTAXA())){
|
125
|
logger.warn("no child taxa fetch");
|
126
|
//TODO overwrite LAZY (SELECT) does not work (bug in hibernate?)
|
127
|
crit.setFetchMode("relationsToThisTaxon.fromTaxon", FetchMode.LAZY);
|
128
|
}
|
129
|
|
130
|
List<Taxon> results = new ArrayList<Taxon>();
|
131
|
List<Taxon> taxa = crit.list();
|
132
|
for(Taxon taxon : taxa){
|
133
|
//childTaxa
|
134
|
//TODO create restriction instead
|
135
|
if (onlyWithChildren == false || taxon.hasTaxonomicChildren()){
|
136
|
if (withMisapplications == true || ! taxon.isMisappliedName()){
|
137
|
results.add(taxon);
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
return results;
|
142
|
}
|
143
|
|
144
|
/* (non-Javadoc)
|
145
|
* @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase, eu.etaxonomy.cdm.persistence.fetch.CdmFetch, java.lang.Boolean, java.lang.Boolean)
|
146
|
*/
|
147
|
public List<Taxon> getRootTaxa(ReferenceBase sec, CdmFetch cdmFetch, Boolean onlyWithChildren, Boolean withMisapplications) {
|
148
|
if (onlyWithChildren == null){
|
149
|
onlyWithChildren = true;
|
150
|
}
|
151
|
if (withMisapplications == null){
|
152
|
withMisapplications = true;
|
153
|
}
|
154
|
if (cdmFetch == null){
|
155
|
cdmFetch = CdmFetch.NO_FETCH();
|
156
|
}
|
157
|
|
158
|
|
159
|
// String query = "from Taxon root ";
|
160
|
// query += " where root.taxonomicParentCache is NULL ";
|
161
|
// if (sec != null){
|
162
|
// query += " AND root.sec.id = :sec ";
|
163
|
// }
|
164
|
// Query q = getSession().createQuery(query);
|
165
|
// if (sec != null){
|
166
|
// q.setInteger("sec", sec.getId());
|
167
|
// }
|
168
|
|
169
|
|
170
|
Criteria crit = getSession().createCriteria(Taxon.class);
|
171
|
crit.add(Restrictions.isNull("taxonomicParentCache"));
|
172
|
if (sec != null){
|
173
|
crit.add(Restrictions.eq("sec", sec) );
|
174
|
}
|
175
|
|
176
|
|
177
|
if (! cdmFetch.includes(CdmFetch.FETCH_CHILDTAXA())){
|
178
|
logger.warn("no child taxa fetch");
|
179
|
//TODO overwrite LAZY (SELECT) does not work (bug in hibernate?)
|
180
|
crit.setFetchMode("relationsToThisTaxon.fromTaxon", FetchMode.LAZY);
|
181
|
}
|
182
|
|
183
|
List<Taxon> results = new ArrayList<Taxon>();
|
184
|
List<Taxon> taxa = crit.list();
|
185
|
for(Taxon taxon : taxa){
|
186
|
//childTaxa
|
187
|
//TODO create restriction instead
|
188
|
if (onlyWithChildren == false || taxon.hasTaxonomicChildren()){
|
189
|
if (withMisapplications == true || ! taxon.isMisappliedName()){
|
190
|
results.add(taxon);
|
191
|
}
|
192
|
}
|
193
|
}
|
194
|
return results;
|
195
|
}
|
196
|
|
197
|
|
198
|
public List<TaxonBase> getTaxaByName(String queryString, ReferenceBase sec) {
|
199
|
|
200
|
return getTaxaByName(queryString, true, sec);
|
201
|
}
|
202
|
|
203
|
public List<TaxonBase> getTaxaByName(String queryString, Boolean accepted, ReferenceBase sec) {
|
204
|
|
205
|
Criteria criteria = null;
|
206
|
if (accepted == true) {
|
207
|
criteria = getSession().createCriteria(Taxon.class);
|
208
|
} else {
|
209
|
criteria = getSession().createCriteria(Synonym.class);
|
210
|
}
|
211
|
|
212
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
213
|
criteria.createAlias("name", "name");
|
214
|
|
215
|
// FIXME: sec restriction caused problems in cich image import: results was empty
|
216
|
// if (sec != null){
|
217
|
// if(sec.getId() == 0){
|
218
|
// getSession().save(sec);
|
219
|
// }
|
220
|
// criteria.add(Restrictions.eq("sec", sec ) );
|
221
|
// }
|
222
|
if (queryString != null) {
|
223
|
criteria.add(Restrictions.ilike("name.nameCache", queryString));
|
224
|
}
|
225
|
List<TaxonBase> results = criteria.list();
|
226
|
return results;
|
227
|
}
|
228
|
|
229
|
public List<TaxonBase> getTaxaByName(String queryString, MatchMode matchMode,
|
230
|
Boolean accepted, Integer pageSize, Integer pageNumber) {
|
231
|
|
232
|
Criteria criteria = null;
|
233
|
if (accepted == true) {
|
234
|
criteria = getSession().createCriteria(Taxon.class);
|
235
|
} else {
|
236
|
criteria = getSession().createCriteria(Synonym.class);
|
237
|
}
|
238
|
|
239
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
240
|
criteria.createAlias("name", "name");
|
241
|
|
242
|
if (matchMode == MatchMode.EXACT) {
|
243
|
criteria.add(Restrictions.eq("name.nameCache", matchMode.queryStringFrom(queryString)));
|
244
|
} else {
|
245
|
criteria.add(Restrictions.ilike("name.nameCache", matchMode.queryStringFrom(queryString)));
|
246
|
}
|
247
|
|
248
|
|
249
|
// if (queryString != null) {
|
250
|
// criteria.add(Restrictions.ilike("name.nameCache", queryString));
|
251
|
// }
|
252
|
//
|
253
|
if(pageSize != null) {
|
254
|
criteria.setMaxResults(pageSize);
|
255
|
if(pageNumber != null) {
|
256
|
criteria.setFirstResult(pageNumber * pageSize);
|
257
|
}
|
258
|
}
|
259
|
|
260
|
List<TaxonBase> results = criteria.list();
|
261
|
return results;
|
262
|
}
|
263
|
|
264
|
public List<TaxonBase> getAllTaxonBases(Integer pagesize, Integer page) {
|
265
|
Criteria crit = getSession().createCriteria(TaxonBase.class);
|
266
|
List<TaxonBase> results = crit.list();
|
267
|
// TODO add page & pagesize criteria
|
268
|
return results;
|
269
|
}
|
270
|
|
271
|
public List<Synonym> getAllSynonyms(Integer limit, Integer start) {
|
272
|
Criteria crit = getSession().createCriteria(Synonym.class);
|
273
|
List<Synonym> results = crit.list();
|
274
|
return results;
|
275
|
}
|
276
|
|
277
|
public List<Taxon> getAllTaxa(Integer limit, Integer start) {
|
278
|
Criteria crit = getSession().createCriteria(Taxon.class);
|
279
|
List<Taxon> results = crit.list();
|
280
|
return results;
|
281
|
}
|
282
|
|
283
|
public List<RelationshipBase> getAllRelationships(Integer limit, Integer start) {
|
284
|
Criteria crit = getSession().createCriteria(RelationshipBase.class);
|
285
|
List<RelationshipBase> results = crit.list();
|
286
|
return results;
|
287
|
}
|
288
|
|
289
|
@Override
|
290
|
public UUID delete(TaxonBase taxonBase) throws DataAccessException{
|
291
|
if (taxonBase == null){
|
292
|
logger.warn("TaxonBase was 'null'");
|
293
|
return null;
|
294
|
}
|
295
|
|
296
|
// Merge the object in if it is detached
|
297
|
//
|
298
|
// I think this is preferable to catching lazy initialization errors
|
299
|
// as that solution only swallows and hides the exception, but doesn't
|
300
|
// actually solve it.
|
301
|
getSession().merge(taxonBase);
|
302
|
|
303
|
for(Iterator<Annotation> iterator = taxonBase.getAnnotations().iterator(); iterator.hasNext();) {
|
304
|
Annotation annotation = iterator.next();
|
305
|
annotation.setAnnotatedObj(null);
|
306
|
iterator.remove();
|
307
|
getSession().delete(annotation);
|
308
|
}
|
309
|
|
310
|
for(Iterator<Marker> iterator = taxonBase.getMarkers().iterator(); iterator.hasNext();) {
|
311
|
Marker marker = iterator.next();
|
312
|
marker.setMarkedObj(null);
|
313
|
iterator.remove();
|
314
|
getSession().delete(marker);
|
315
|
}
|
316
|
|
317
|
for(Iterator<Extension> iterator = taxonBase.getExtensions().iterator(); iterator.hasNext();) {
|
318
|
Extension extension = iterator.next();
|
319
|
extension.setExtendedObj(null);
|
320
|
iterator.remove();
|
321
|
getSession().delete(extension);
|
322
|
}
|
323
|
|
324
|
for(Iterator<OriginalSource> iterator = taxonBase.getSources().iterator(); iterator.hasNext();) {
|
325
|
OriginalSource source = iterator.next();
|
326
|
source.setSourcedObj(null);
|
327
|
iterator.remove();
|
328
|
getSession().delete(source);
|
329
|
}
|
330
|
|
331
|
for(Iterator<Rights> iterator = taxonBase.getRights().iterator(); iterator.hasNext();) {
|
332
|
Rights rights = iterator.next();
|
333
|
iterator.remove();
|
334
|
getSession().delete(rights);
|
335
|
}
|
336
|
|
337
|
if (taxonBase instanceof Taxon){ // is Taxon
|
338
|
//taxonRelationships
|
339
|
Taxon taxon = (Taxon)taxonBase;
|
340
|
|
341
|
for (Iterator<TaxonRelationship> iterator = taxon.getRelationsFromThisTaxon().iterator(); iterator.hasNext();){
|
342
|
TaxonRelationship relationToThisTaxon = iterator.next();
|
343
|
iterator.remove();
|
344
|
relationToThisTaxon.setFromTaxon(null);
|
345
|
relationToThisTaxon.setToTaxon(null);
|
346
|
getSession().delete(relationToThisTaxon);
|
347
|
}
|
348
|
|
349
|
for (Iterator<TaxonRelationship> iterator = taxon.getRelationsToThisTaxon().iterator(); iterator.hasNext();){
|
350
|
TaxonRelationship relationFromThisTaxon = iterator.next();
|
351
|
iterator.remove();
|
352
|
relationFromThisTaxon.setToTaxon(null);
|
353
|
relationFromThisTaxon.setFromTaxon(null);
|
354
|
getSession().delete(relationFromThisTaxon);
|
355
|
}
|
356
|
|
357
|
//SynonymRelationships
|
358
|
for (Iterator<SynonymRelationship> iterator = taxon.getSynonymRelations().iterator(); iterator.hasNext();){
|
359
|
SynonymRelationship synonymRelation = iterator.next();
|
360
|
iterator.remove();
|
361
|
synonymRelation.setAcceptedTaxon(null);
|
362
|
synonymRelation.setSynonym(null);
|
363
|
getSession().delete(synonymRelation);
|
364
|
}
|
365
|
|
366
|
// Descriptions
|
367
|
for (Iterator<TaxonDescription> iterDesc = taxon.getDescriptions().iterator(); iterDesc.hasNext();) {
|
368
|
TaxonDescription taxonDescription = iterDesc.next();
|
369
|
iterDesc.remove();
|
370
|
taxonDescription.setTaxon(null);
|
371
|
for (Iterator<DescriptionElementBase> iterDescElem =
|
372
|
taxonDescription.getElements().iterator(); iterDescElem.hasNext();) {
|
373
|
DescriptionElementBase descriptionElement = iterDescElem.next();
|
374
|
iterDescElem.remove();
|
375
|
getSession().delete(descriptionElement);
|
376
|
}
|
377
|
getSession().delete(taxonDescription);
|
378
|
}
|
379
|
|
380
|
} else { //is Synonym
|
381
|
Synonym synonym = (Synonym)taxonBase;
|
382
|
for (Iterator<SynonymRelationship> iterator = synonym.getSynonymRelations().iterator(); iterator.hasNext();){
|
383
|
SynonymRelationship synonymRelation = iterator.next();
|
384
|
iterator.remove();
|
385
|
synonymRelation.setAcceptedTaxon(null);
|
386
|
synonymRelation.setSynonym(null);
|
387
|
} ;
|
388
|
}
|
389
|
return super.delete(taxonBase);
|
390
|
}
|
391
|
|
392
|
|
393
|
// TODO add generic return type !!
|
394
|
public List findByName(String queryString, MatchMode matchMode, int page, int pagesize, boolean onlyAcccepted) {
|
395
|
ArrayList<Criterion> criteria = new ArrayList<Criterion>();
|
396
|
//TODO ... Restrictions.eq(propertyName, value)
|
397
|
return super.findByTitle(queryString, matchMode, page, pagesize, criteria);
|
398
|
|
399
|
}
|
400
|
|
401
|
public int countMatchesByName(String queryString, MatchMode matchMode, boolean onlyAcccepted) {
|
402
|
|
403
|
Criteria crit = getSession().createCriteria(type);
|
404
|
crit.add(Restrictions.ilike("persistentTitleCache", matchMode.queryStringFrom(queryString)));
|
405
|
crit.setProjection(Projections.rowCount());
|
406
|
int result = ((Integer)crit.list().get(0)).intValue();
|
407
|
return result;
|
408
|
}
|
409
|
|
410
|
|
411
|
public int countMatchesByName(String queryString, MatchMode matchMode, boolean onlyAcccepted, List<Criterion> criteria) {
|
412
|
|
413
|
Criteria crit = getSession().createCriteria(type);
|
414
|
crit.add(Restrictions.ilike("persistentTitleCache", matchMode.queryStringFrom(queryString)));
|
415
|
if(criteria != null){
|
416
|
for (Criterion criterion : criteria) {
|
417
|
crit.add(criterion);
|
418
|
}
|
419
|
}
|
420
|
crit.setProjection(Projections.rowCount());
|
421
|
int result = ((Integer)crit.list().get(0)).intValue();
|
422
|
return result;
|
423
|
}
|
424
|
|
425
|
public int countRelatedTaxa(Taxon taxon, TaxonRelationshipType type) {
|
426
|
Query query = null;
|
427
|
|
428
|
if(type == null) {
|
429
|
query = getSession().createQuery("select count(taxonRelationship) from TaxonRelationship taxonRelationship where taxonRelationship.relatedTo = :relatedTo");
|
430
|
} else {
|
431
|
query = getSession().createQuery("select count(taxonRelationship) from TaxonRelationship taxonRelationship where taxonRelationship.relatedTo = :relatedTo and taxonRelationship.type = :type");
|
432
|
query.setParameter("type",type);
|
433
|
}
|
434
|
|
435
|
query.setParameter("relatedTo", taxon);
|
436
|
|
437
|
return ((Long)query.uniqueResult()).intValue();
|
438
|
}
|
439
|
|
440
|
public int countSynonyms(Taxon taxon, SynonymRelationshipType type) {
|
441
|
Query query = null;
|
442
|
|
443
|
if(type == null) {
|
444
|
query = getSession().createQuery("select count(synonymRelationship) from SynonymRelationship synonymRelationship where synonymRelationship.relatedTo = :relatedTo");
|
445
|
} else {
|
446
|
query = getSession().createQuery("select count(synonymRelationship) from SynonymRelationship synonymRelationship where synonymRelationship.relatedTo = :relatedTo and synonymRelationship.type = :type");
|
447
|
query.setParameter("type",type);
|
448
|
}
|
449
|
|
450
|
query.setParameter("relatedTo", taxon);
|
451
|
|
452
|
return ((Long)query.uniqueResult()).intValue();
|
453
|
}
|
454
|
|
455
|
public int countTaxa(String queryString, Boolean accepted) {
|
456
|
QueryParser queryParser = new QueryParser("name.persistentTitleCache", new SimpleAnalyzer());
|
457
|
|
458
|
try {
|
459
|
org.apache.lucene.search.Query query = queryParser.parse(queryString);
|
460
|
|
461
|
FullTextSession fullTextSession = Search.createFullTextSession(this.getSession());
|
462
|
org.hibernate.search.FullTextQuery fullTextQuery = null;
|
463
|
|
464
|
if(accepted == null) {
|
465
|
fullTextQuery = fullTextSession.createFullTextQuery(query, TaxonBase.class);
|
466
|
} else {
|
467
|
if(accepted) {
|
468
|
fullTextQuery = fullTextSession.createFullTextQuery(query, Taxon.class);
|
469
|
} else {
|
470
|
fullTextQuery = fullTextSession.createFullTextQuery(query, Synonym.class);
|
471
|
}
|
472
|
}
|
473
|
|
474
|
Integer result = fullTextQuery.getResultSize();
|
475
|
return result;
|
476
|
|
477
|
} catch (ParseException e) {
|
478
|
throw new QueryParseException(e, queryString);
|
479
|
}
|
480
|
}
|
481
|
|
482
|
public int countTaxaByName(String queryString, Boolean accepted, ReferenceBase sec) {
|
483
|
|
484
|
Criteria criteria = null;
|
485
|
|
486
|
if (accepted == true) {
|
487
|
criteria = getSession().createCriteria(Taxon.class);
|
488
|
} else {
|
489
|
criteria = getSession().createCriteria(Synonym.class);
|
490
|
}
|
491
|
|
492
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
493
|
criteria.createAlias("name", "name");
|
494
|
|
495
|
if (sec != null){
|
496
|
if(sec.getId() == 0){
|
497
|
getSession().save(sec);
|
498
|
}
|
499
|
criteria.add(Restrictions.eq("sec", sec ) );
|
500
|
}
|
501
|
if (queryString != null) {
|
502
|
criteria.add(Restrictions.ilike("name.nameCache", queryString));
|
503
|
}
|
504
|
criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));
|
505
|
|
506
|
return (Integer)criteria.uniqueResult();
|
507
|
}
|
508
|
|
509
|
public int countTaxaByName(Boolean accepted, String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank) {
|
510
|
Criteria criteria = null;
|
511
|
|
512
|
if(accepted == null) {
|
513
|
criteria = getSession().createCriteria(TaxonBase.class);
|
514
|
} else {
|
515
|
if(accepted) {
|
516
|
criteria = getSession().createCriteria(Taxon.class);
|
517
|
} else {
|
518
|
criteria = getSession().createCriteria(Synonym.class);
|
519
|
}
|
520
|
}
|
521
|
|
522
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
523
|
criteria.createAlias("name", "name");
|
524
|
|
525
|
if(genusOrUninomial != null) {
|
526
|
criteria.add(Restrictions.eq("name.genusOrUninomial", genusOrUninomial));
|
527
|
}
|
528
|
|
529
|
if(infraGenericEpithet != null) {
|
530
|
criteria.add(Restrictions.eq("name.infraGenericEpithet", infraGenericEpithet));
|
531
|
}
|
532
|
|
533
|
if(specificEpithet != null) {
|
534
|
criteria.add(Restrictions.eq("name.specificEpithet", specificEpithet));
|
535
|
}
|
536
|
|
537
|
if(infraSpecificEpithet != null) {
|
538
|
criteria.add(Restrictions.eq("name.infraSpecificEpithet", infraSpecificEpithet));
|
539
|
}
|
540
|
|
541
|
if(rank != null) {
|
542
|
criteria.add(Restrictions.eq("name.rank", rank));
|
543
|
}
|
544
|
|
545
|
criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));
|
546
|
|
547
|
return (Integer)criteria.uniqueResult();
|
548
|
}
|
549
|
|
550
|
public List<TaxonBase> findTaxaByName(Boolean accepted, String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank, Integer pageSize, Integer pageNumber) {
|
551
|
Criteria criteria = null;
|
552
|
|
553
|
if(accepted == null) {
|
554
|
criteria = getSession().createCriteria(TaxonBase.class);
|
555
|
} else {
|
556
|
if(accepted) {
|
557
|
criteria = getSession().createCriteria(Taxon.class);
|
558
|
} else {
|
559
|
criteria = getSession().createCriteria(Synonym.class);
|
560
|
}
|
561
|
}
|
562
|
|
563
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
564
|
criteria.createAlias("name", "name");
|
565
|
|
566
|
if(genusOrUninomial != null) {
|
567
|
criteria.add(Restrictions.eq("name.genusOrUninomial", genusOrUninomial));
|
568
|
}
|
569
|
|
570
|
if(infraGenericEpithet != null) {
|
571
|
criteria.add(Restrictions.eq("name.infraGenericEpithet", infraGenericEpithet));
|
572
|
} else {
|
573
|
criteria.add(Restrictions.isNull("name.infraGenericEpithet"));
|
574
|
}
|
575
|
|
576
|
if(specificEpithet != null) {
|
577
|
criteria.add(Restrictions.eq("name.specificEpithet", specificEpithet));
|
578
|
}
|
579
|
|
580
|
if(infraSpecificEpithet != null) {
|
581
|
criteria.add(Restrictions.eq("name.infraSpecificEpithet", infraSpecificEpithet));
|
582
|
}
|
583
|
|
584
|
if(rank != null) {
|
585
|
criteria.add(Restrictions.eq("name.rank", rank));
|
586
|
}
|
587
|
|
588
|
if(pageSize != null) {
|
589
|
criteria.setMaxResults(pageSize);
|
590
|
if(pageNumber != null) {
|
591
|
criteria.setFirstResult(pageNumber * pageSize);
|
592
|
} else {
|
593
|
criteria.setFirstResult(0);
|
594
|
}
|
595
|
}
|
596
|
|
597
|
return (List<TaxonBase>)criteria.list();
|
598
|
}
|
599
|
|
600
|
public List<TaxonRelationship> getRelatedTaxa(Taxon taxon, TaxonRelationshipType type, Integer pageSize, Integer pageNumber) {
|
601
|
Query query = null;
|
602
|
|
603
|
if(type == null) {
|
604
|
query = getSession().createQuery("select taxonRelationship from TaxonRelationship taxonRelationship join fetch taxonRelationship.relatedFrom where taxonRelationship.relatedTo = :relatedTo");
|
605
|
} else {
|
606
|
query = getSession().createQuery("select taxonRelationship from TaxonRelationship taxonRelationship join fetch taxonRelationship.relatedFrom where taxonRelationship.relatedTo = :relatedTo and taxonRelationship.type = :type");
|
607
|
query.setParameter("type",type);
|
608
|
}
|
609
|
|
610
|
query.setParameter("relatedTo", taxon);
|
611
|
|
612
|
if(pageSize != null) {
|
613
|
query.setMaxResults(pageSize);
|
614
|
if(pageNumber != null) {
|
615
|
query.setFirstResult(pageNumber * pageSize);
|
616
|
} else {
|
617
|
query.setFirstResult(0);
|
618
|
}
|
619
|
}
|
620
|
|
621
|
return (List<TaxonRelationship>)query.list();
|
622
|
}
|
623
|
|
624
|
public List<SynonymRelationship> getSynonyms(Taxon taxon, SynonymRelationshipType type, Integer pageSize, Integer pageNumber) {
|
625
|
Query query = null;
|
626
|
|
627
|
if(type == null) {
|
628
|
query = getSession().createQuery("select synonymRelationship from SynonymRelationship synonymRelationship join fetch synonymRelationship.relatedFrom where synonymRelationship.relatedTo = :relatedTo");
|
629
|
} else {
|
630
|
query = getSession().createQuery("select synonymRelationship from SynonymRelationship synonymRelationship join fetch synonymRelationship.relatedFrom where synonymRelationship.relatedTo = :relatedTo and synonymRelationship.type = :type");
|
631
|
query.setParameter("type",type);
|
632
|
}
|
633
|
|
634
|
query.setParameter("relatedTo", taxon);
|
635
|
|
636
|
if(pageSize != null) {
|
637
|
query.setMaxResults(pageSize);
|
638
|
if(pageNumber != null) {
|
639
|
query.setFirstResult(pageNumber * pageSize);
|
640
|
} else {
|
641
|
query.setFirstResult(0);
|
642
|
}
|
643
|
}
|
644
|
|
645
|
return (List<SynonymRelationship>)query.list();
|
646
|
}
|
647
|
|
648
|
public List<TaxonBase> searchTaxa(String queryString, Boolean accepted, Integer pageSize, Integer pageNumber) {
|
649
|
QueryParser queryParser = new QueryParser("name.persistentTitleCache", new SimpleAnalyzer());
|
650
|
List<TaxonBase> results = new ArrayList<TaxonBase>();
|
651
|
|
652
|
try {
|
653
|
org.apache.lucene.search.Query query = queryParser.parse(queryString);
|
654
|
|
655
|
FullTextSession fullTextSession = Search.createFullTextSession(getSession());
|
656
|
org.hibernate.search.FullTextQuery fullTextQuery = null;
|
657
|
Criteria criteria = null;
|
658
|
|
659
|
if(accepted == null) {
|
660
|
fullTextQuery = fullTextSession.createFullTextQuery(query, TaxonBase.class);
|
661
|
criteria = getSession().createCriteria( TaxonBase.class );
|
662
|
} else {
|
663
|
if(accepted) {
|
664
|
fullTextQuery = fullTextSession.createFullTextQuery(query, Taxon.class);
|
665
|
criteria = getSession().createCriteria( Taxon.class );
|
666
|
} else {
|
667
|
fullTextQuery = fullTextSession.createFullTextQuery(query, Synonym.class);
|
668
|
criteria = getSession().createCriteria( Synonym.class );
|
669
|
}
|
670
|
}
|
671
|
|
672
|
org.apache.lucene.search.Sort sort = new Sort(new SortField("name.titleCache_forSort"));
|
673
|
fullTextQuery.setSort(sort);
|
674
|
|
675
|
criteria.setFetchMode( "name", FetchMode.JOIN );
|
676
|
fullTextQuery.setCriteriaQuery(criteria);
|
677
|
|
678
|
if(pageSize != null) {
|
679
|
fullTextQuery.setMaxResults(pageSize);
|
680
|
if(pageNumber != null) {
|
681
|
fullTextQuery.setFirstResult(pageNumber * pageSize);
|
682
|
} else {
|
683
|
fullTextQuery.setFirstResult(0);
|
684
|
}
|
685
|
}
|
686
|
|
687
|
return (List<TaxonBase>)fullTextQuery.list();
|
688
|
|
689
|
} catch (ParseException e) {
|
690
|
throw new QueryParseException(e, queryString);
|
691
|
}
|
692
|
}
|
693
|
|
694
|
public void purgeIndex() {
|
695
|
FullTextSession fullTextSession = Search.createFullTextSession(getSession());
|
696
|
|
697
|
fullTextSession.purgeAll(type); // remove all taxon base from indexes
|
698
|
// fullTextSession.flushToIndexes() not implemented in 3.0.0.GA
|
699
|
}
|
700
|
|
701
|
public void rebuildIndex() {
|
702
|
FullTextSession fullTextSession = Search.createFullTextSession(getSession());
|
703
|
|
704
|
for(TaxonBase taxonBase : list(null,null)) { // re-index all taxon base
|
705
|
Hibernate.initialize(taxonBase.getName());
|
706
|
fullTextSession.index(taxonBase);
|
707
|
}
|
708
|
// fullTextSession.flushToIndexes() not implemented in 3.0.0.GA
|
709
|
}
|
710
|
|
711
|
public void optimizeIndex() {
|
712
|
FullTextSession fullTextSession = Search.createFullTextSession(getSession());
|
713
|
SearchFactory searchFactory = fullTextSession.getSearchFactory();
|
714
|
searchFactory.optimize(type); // optimize the indices ()
|
715
|
// fullTextSession.flushToIndexes() not implemented in 3.0.0.GA
|
716
|
}
|
717
|
|
718
|
public String suggestQuery(String queryString) {
|
719
|
try {
|
720
|
String alternativeQueryString = null;
|
721
|
alternativeSpellingSuggestionParser.parse(queryString);
|
722
|
org.apache.lucene.search.Query alternativeQuery = alternativeSpellingSuggestionParser.suggest(queryString);
|
723
|
if(alternativeQuery != null) {
|
724
|
alternativeQueryString = alternativeQuery.toString("name.persistentTitleCache");
|
725
|
}
|
726
|
return alternativeQueryString;
|
727
|
} catch (ParseException e) {
|
728
|
throw new QueryParseException(e, queryString);
|
729
|
}
|
730
|
}
|
731
|
}
|