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