bugfix for DescriptionDaoImpl.getDescriptionElementForTaxon (missing implementation...
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / TaxonNodeByNameComparator.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.api.service;
11
12 import java.util.Comparator;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.StringTokenizer;
16 import java.util.regex.Pattern;
17
18 import org.springframework.stereotype.Component;
19
20 import eu.etaxonomy.cdm.model.name.NonViralName;
21 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
22
23 /**
24 * Comparator that compares two TaxonNode instances by the titleCache of their referenced names.
25 * @author a.kohlbecker
26 * @date 24.06.2009
27 *
28 * TODO DISCUSS: move into model ?
29 */
30 @Component
31 public class TaxonNodeByNameComparator extends AbstractStringComparator implements Comparator<TaxonNode>, ITaxonNodeComparator<TaxonNode> {
32
33 /* (non-Javadoc)
34 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
35 */
36 @SuppressWarnings("unchecked")
37 public int compare(TaxonNode o1, TaxonNode o2) {
38
39 String titleCache1 = null;
40 String titleCache2 = null;
41
42 if(o1.getTaxon() != null && o1.getTaxon().getName() != null ){
43
44 if (o1.getTaxon().getName() instanceof NonViralName){
45 NonViralName nonViralName = (NonViralName)o1.getTaxon().getName();
46 if (nonViralName.isInfraSpecific()){
47 if (nonViralName.getSpecificEpithet().equals(nonViralName.getInfraSpecificEpithet())){
48 titleCache1 = nonViralName.getNameCache() + " "+nonViralName.getAuthorshipCache();
49 }
50 }
51
52 }
53 if (titleCache1 == null){
54 titleCache1 = o1.getTaxon().getName().getTitleCache();
55 }
56 }
57 if(o2.getTaxon() != null && o2.getTaxon().getName() != null){
58 if (o2.getTaxon().getName() instanceof NonViralName){
59 NonViralName nonViralName = (NonViralName)o2.getTaxon().getName();
60 if (nonViralName.isInfraSpecific()){
61 if (nonViralName.getSpecificEpithet().equals(nonViralName.getInfraSpecificEpithet())){
62 titleCache2 = nonViralName.getNameCache() + " "+nonViralName.getAuthorshipCache();
63 }
64 }
65
66 }
67 if (titleCache2 == null){
68 titleCache2 = o2.getTaxon().getName().getTitleCache();
69 }
70 }
71
72 titleCache1 = applySubstitutionRules(titleCache1);
73 titleCache2 = applySubstitutionRules(titleCache2);
74
75 StringTokenizer s2 = new StringTokenizer(titleCache1, "\"");
76 if (s2.countTokens()>0){
77 titleCache1 = null;
78 }
79 while(s2.hasMoreTokens()){
80 titleCache1 += s2.nextToken();
81 }
82 s2 = new StringTokenizer(titleCache2, "\"");
83 if (s2.countTokens()>0){
84 titleCache2 = null;
85 }
86
87 while(s2.hasMoreTokens()){
88 titleCache2 += s2.nextToken();
89 }
90
91 return titleCache1.compareTo(titleCache2);
92 }
93
94
95 }