| 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.model.taxon; |
|---|
| 11 | |
|---|
| 12 | import java.util.Comparator; |
|---|
| 13 | import java.util.StringTokenizer; |
|---|
| 14 | |
|---|
| 15 | import org.apache.log4j.Logger; |
|---|
| 16 | import org.springframework.stereotype.Component; |
|---|
| 17 | |
|---|
| 18 | import eu.etaxonomy.cdm.model.common.AbstractStringComparator; |
|---|
| 19 | import eu.etaxonomy.cdm.model.common.TermVocabulary; |
|---|
| 20 | import eu.etaxonomy.cdm.model.name.NonViralName; |
|---|
| 21 | import eu.etaxonomy.cdm.model.name.Rank; |
|---|
| 22 | import eu.etaxonomy.cdm.model.name.TaxonNameBase; |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Comparator that compares two TaxonNode instances by the titleCache of their referenced names. |
|---|
| 26 | * @author a.kohlbecker |
|---|
| 27 | * @date 24.06.2009 |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | @Component |
|---|
| 31 | public class TaxonNodeByNameComparator extends AbstractStringComparator implements Comparator<TaxonNode>, ITaxonNodeComparator<TaxonNode> { |
|---|
| 32 | |
|---|
| 33 | private static final String HYBRID_SIGN = "\u00D7"; |
|---|
| 34 | |
|---|
| 35 | private static final Logger logger = Logger.getLogger(TaxonNodeByNameComparator.class); |
|---|
| 36 | |
|---|
| 37 | private boolean ignoreHybridSign = true; |
|---|
| 38 | private boolean sortInfraGenericFirst = true; |
|---|
| 39 | |
|---|
| 40 | /* (non-Javadoc) |
|---|
| 41 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
|---|
| 42 | */ |
|---|
| 43 | public int compare(TaxonNode o1, TaxonNode o2) { |
|---|
| 44 | |
|---|
| 45 | String titleCache1 = createSortableTitleCache(o1); |
|---|
| 46 | String titleCache2 = createSortableTitleCache(o2); |
|---|
| 47 | |
|---|
| 48 | if(isIgnoreHybridSign()) { |
|---|
| 49 | logger.trace("ignoring Hybrid Signs: " + HYBRID_SIGN); |
|---|
| 50 | titleCache1 = titleCache1.replace(HYBRID_SIGN, ""); |
|---|
| 51 | titleCache2 = titleCache2.replace(HYBRID_SIGN, ""); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | titleCache1 = applySubstitutionRules(titleCache1); |
|---|
| 55 | titleCache2 = applySubstitutionRules(titleCache2); |
|---|
| 56 | |
|---|
| 57 | // 1 |
|---|
| 58 | StringTokenizer s2 = new StringTokenizer(titleCache1, "\""); |
|---|
| 59 | if (s2.countTokens()>0){ |
|---|
| 60 | titleCache1 = ""; |
|---|
| 61 | } |
|---|
| 62 | while(s2.hasMoreTokens()){ |
|---|
| 63 | titleCache1 += s2.nextToken(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | // 2 |
|---|
| 67 | s2 = new StringTokenizer(titleCache2, "\""); |
|---|
| 68 | if (s2.countTokens()>0){ |
|---|
| 69 | titleCache2 = ""; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | while(s2.hasMoreTokens()){ |
|---|
| 73 | titleCache2 += s2.nextToken(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | return titleCache1.compareTo(titleCache2); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | private String createSortableTitleCache(TaxonNode taxonNode) { |
|---|
| 81 | |
|---|
| 82 | String titleCache = null; |
|---|
| 83 | TaxonNameBase name = taxonNode.getTaxon().getName(); |
|---|
| 84 | if(taxonNode.getTaxon() != null && taxonNode.getTaxon().getName() != null ){ |
|---|
| 85 | if (taxonNode.getTaxon().getName() instanceof NonViralName){ |
|---|
| 86 | logger.trace(name + " isNonViralName"); |
|---|
| 87 | NonViralName nonViralName = (NonViralName)name; |
|---|
| 88 | if (name.isInfraSpecific()){ |
|---|
| 89 | logger.trace(name + " isInfraSpecific"); |
|---|
| 90 | if (nonViralName.getSpecificEpithet().equals(nonViralName.getInfraSpecificEpithet())){ |
|---|
| 91 | titleCache = nonViralName.getNameCache() + " "+nonViralName.getAuthorshipCache(); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | if (name.isInfraGeneric()){ |
|---|
| 95 | logger.trace(name + " isInfraGeneric"); |
|---|
| 96 | titleCache = nonViralName.getGenusOrUninomial() + " " + nonViralName.getInfraGenericEpithet(); |
|---|
| 97 | } |
|---|
| 98 | if (nonViralName.getRank().isSpeciesAggregate()){ |
|---|
| 99 | logger.trace(name + " isSpeciesAggregate"); |
|---|
| 100 | titleCache = nonViralName.getGenusOrUninomial() + " " + nonViralName.getSpecificEpithet(); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | if (titleCache == null){ |
|---|
| 105 | logger.trace("titleCache still null, using name.getTitleCache()"); |
|---|
| 106 | titleCache = name.getTitleCache(); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | logger.trace("SortableTitleCache: " + titleCache); |
|---|
| 110 | return titleCache; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | public boolean isIgnoreHybridSign() { |
|---|
| 115 | return ignoreHybridSign; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | public void setIgnoreHybridSign(boolean ignore) { |
|---|
| 119 | this.ignoreHybridSign = ignore; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | public boolean isSortInfraGenericFirst() { |
|---|
| 123 | return sortInfraGenericFirst; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | public void setSortInfraGenericFirst(boolean infraGenericFirst) { |
|---|
| 127 | this.sortInfraGenericFirst = infraGenericFirst; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | } |
|---|