| 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 | |
|---|
| 10 | package eu.etaxonomy.cdm.model.taxon; |
|---|
| 11 | |
|---|
| 12 | import java.io.Serializable; |
|---|
| 13 | import java.util.Comparator; |
|---|
| 14 | import java.util.Iterator; |
|---|
| 15 | import java.util.Set; |
|---|
| 16 | |
|---|
| 17 | import org.apache.log4j.Logger; |
|---|
| 18 | |
|---|
| 19 | import org.joda.time.DateTime; |
|---|
| 20 | |
|---|
| 21 | import eu.etaxonomy.cdm.model.name.NomenclaturalStatus; |
|---|
| 22 | import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType; |
|---|
| 23 | import eu.etaxonomy.cdm.model.name.TaxonNameBase; |
|---|
| 24 | import eu.etaxonomy.cdm.model.name.ZoologicalName; |
|---|
| 25 | import eu.etaxonomy.cdm.model.reference.INomenclaturalReference; |
|---|
| 26 | import eu.etaxonomy.cdm.model.reference.Reference; |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * This class makes available a method to compare two {@link TaxonBase taxa} by |
|---|
| 30 | * comparing the publication dates of the corresponding |
|---|
| 31 | * {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names}. |
|---|
| 32 | * |
|---|
| 33 | * @author a.mueller |
|---|
| 34 | * @created 11.06.2008 |
|---|
| 35 | * @version 1.0 |
|---|
| 36 | */ |
|---|
| 37 | public class TaxonComparator implements Comparator<TaxonBase>, Serializable { |
|---|
| 38 | private static final Logger logger = Logger.getLogger(TaxonComparator.class); |
|---|
| 39 | |
|---|
| 40 | /* (non-Javadoc) |
|---|
| 41 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
|---|
| 42 | */ |
|---|
| 43 | /** |
|---|
| 44 | * Returns an integer generated by comparing the |
|---|
| 45 | * {@link eu.etaxonomy.cdm.model.name.INomenclaturalReference#getYear() publication years} |
|---|
| 46 | * of both {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names} |
|---|
| 47 | * used in the given {@link TaxonBase taxa}. |
|---|
| 48 | * Returns a negative value if the publication year corresponding to the |
|---|
| 49 | * first given taxon precedes the publication year corresponding to the |
|---|
| 50 | * second given taxon. Returns a positive value if the contrary is true and |
|---|
| 51 | * 0 if both publication years and the date, when they are created, are identical. |
|---|
| 52 | * In case one of the publication |
|---|
| 53 | * years is "null" and the other is not, the "empty" publication year will |
|---|
| 54 | * be considered to be always preceded by the "not null" publication year. |
|---|
| 55 | * If both publication years are "null" the creation date is used for the comparison |
|---|
| 56 | * |
|---|
| 57 | * |
|---|
| 58 | * @see java.lang.String#compareTo(String) |
|---|
| 59 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
|---|
| 60 | */ |
|---|
| 61 | public int compare(TaxonBase taxonBase1, TaxonBase taxonBase2) { |
|---|
| 62 | int result; |
|---|
| 63 | //boolean invalOrNudForTaxon1 = false; |
|---|
| 64 | //boolean invalOrNudForTaxon2 = false; |
|---|
| 65 | |
|---|
| 66 | boolean invalTaxon1 = false; |
|---|
| 67 | boolean invalTaxon2 = false; |
|---|
| 68 | boolean nudumTaxon1 = false; |
|---|
| 69 | boolean nudumTaxon2 = false; |
|---|
| 70 | |
|---|
| 71 | //if a taxon has nomenclatural status "nom. inval." or "nom. nud." |
|---|
| 72 | //TODO: Überprüfen!!! |
|---|
| 73 | Set status = taxonBase1.getName().getStatus(); |
|---|
| 74 | Iterator iterator = status.iterator(); |
|---|
| 75 | if (iterator.hasNext()){ |
|---|
| 76 | NomenclaturalStatus nomStatus1 = (NomenclaturalStatus) iterator.next(); |
|---|
| 77 | Set status2 = taxonBase2.getName().getStatus(); |
|---|
| 78 | iterator = status2.iterator(); // is that right? or better iterator = status2.iterator(); ??? |
|---|
| 79 | if (iterator.hasNext()){ |
|---|
| 80 | NomenclaturalStatus nomStatus2 = (NomenclaturalStatus)iterator.next(); |
|---|
| 81 | /* |
|---|
| 82 | if (nomStatus1.getType().equals(NomenclaturalStatusType.NUDUM()) || |
|---|
| 83 | nomStatus1.getType().equals(NomenclaturalStatusType.INVALID())){ |
|---|
| 84 | invalOrNudForTaxon1 = true; |
|---|
| 85 | } |
|---|
| 86 | if (nomStatus2.getType().equals(NomenclaturalStatusType.NUDUM()) || nomStatus2.getType().equals(NomenclaturalStatusType.INVALID())){ |
|---|
| 87 | invalOrNudForTaxon2 = true; |
|---|
| 88 | } |
|---|
| 89 | if (invalOrNudForTaxon1 && !invalOrNudForTaxon2){ |
|---|
| 90 | return 1; |
|---|
| 91 | }else if (!invalOrNudForTaxon1 && invalOrNudForTaxon2){ |
|---|
| 92 | return -1; |
|---|
| 93 | } |
|---|
| 94 | else{ // both taxon are invalid or nudum |
|---|
| 95 | //result = 0; |
|---|
| 96 | } |
|---|
| 97 | */ |
|---|
| 98 | // ##### |
|---|
| 99 | if (nomStatus1.getType().equals(NomenclaturalStatusType.INVALID())){ |
|---|
| 100 | invalTaxon1 = true; |
|---|
| 101 | } |
|---|
| 102 | if (nomStatus1.getType().equals(NomenclaturalStatusType.NUDUM())){ |
|---|
| 103 | nudumTaxon1 = true; |
|---|
| 104 | } |
|---|
| 105 | if (nomStatus2.getType().equals(NomenclaturalStatusType.INVALID())){ |
|---|
| 106 | invalTaxon2 = true; |
|---|
| 107 | } |
|---|
| 108 | if (nomStatus2.getType().equals(NomenclaturalStatusType.NUDUM())){ |
|---|
| 109 | nudumTaxon2 = true; |
|---|
| 110 | } |
|---|
| 111 | if (nudumTaxon1 && !nudumTaxon2){ |
|---|
| 112 | return 1; |
|---|
| 113 | }else if (nudumTaxon1 && nudumTaxon2){ |
|---|
| 114 | //continue |
|---|
| 115 | }else if (invalTaxon1 && !nudumTaxon2){ |
|---|
| 116 | if (invalTaxon2){ |
|---|
| 117 | //continue |
|---|
| 118 | }else{ |
|---|
| 119 | return 1; |
|---|
| 120 | } |
|---|
| 121 | }else if (nudumTaxon2 && !nudumTaxon1){ |
|---|
| 122 | return -1; |
|---|
| 123 | }else if(invalTaxon2){ |
|---|
| 124 | return -1; |
|---|
| 125 | } |
|---|
| 126 | // ##### |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | }else{//if taxonbase2.getName().getStatus = NULL and taxonbase2 not |
|---|
| 130 | return 1; |
|---|
| 131 | } |
|---|
| 132 | }else{//if taxonbase1.getName().getStatus = NULL |
|---|
| 133 | iterator = taxonBase2.getName().getStatus().iterator(); |
|---|
| 134 | if (!iterator.hasNext()){ |
|---|
| 135 | //if (taxonBase2.getName().getStatus() == null){ // both are null, continue checking |
|---|
| 136 | }else{ |
|---|
| 137 | return -1; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | Integer intDate1 = getIntegerDate(taxonBase1); |
|---|
| 142 | Integer intDate2 = getIntegerDate(taxonBase2); |
|---|
| 143 | |
|---|
| 144 | if (intDate1 == null && intDate2 == null){ |
|---|
| 145 | result = 0; |
|---|
| 146 | }else if (intDate1 == null){ |
|---|
| 147 | return 1; |
|---|
| 148 | }else if (intDate2 == null){ |
|---|
| 149 | return -1; |
|---|
| 150 | }else{ |
|---|
| 151 | result = intDate1.compareTo(intDate2); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | if (result == 0){ |
|---|
| 155 | TaxonNameBase taxName1 = taxonBase1.getName(); |
|---|
| 156 | TaxonNameBase taxName2 = taxonBase2.getName(); |
|---|
| 157 | |
|---|
| 158 | return taxName1.compareTo(taxName2); |
|---|
| 159 | |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | if (result == 0){ |
|---|
| 163 | DateTime date11 = taxonBase1.getCreated(); |
|---|
| 164 | DateTime date12 = taxonBase2.getCreated(); |
|---|
| 165 | if (date11 == null && date12 == null) { |
|---|
| 166 | return 0; |
|---|
| 167 | } |
|---|
| 168 | if (date11 == null) { |
|---|
| 169 | return 1; |
|---|
| 170 | } |
|---|
| 171 | if (date12 == null) { |
|---|
| 172 | return -1; |
|---|
| 173 | } |
|---|
| 174 | result = date11.compareTo(date12); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | //for ticket #393 if the publication year is the same, the order is alphabetically |
|---|
| 178 | |
|---|
| 179 | if (result == 0){ |
|---|
| 180 | TaxonNameBase taxName1 = taxonBase1.getName(); |
|---|
| 181 | TaxonNameBase taxName2 = taxonBase2.getName(); |
|---|
| 182 | |
|---|
| 183 | return taxName1.compareTo(taxName2); |
|---|
| 184 | |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | return result; |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | private Integer getIntegerDate(TaxonBase taxonBase){ |
|---|
| 193 | Integer result; |
|---|
| 194 | |
|---|
| 195 | if (taxonBase == null){ |
|---|
| 196 | result = null; |
|---|
| 197 | }else{ |
|---|
| 198 | TaxonNameBase name = taxonBase.getName(); |
|---|
| 199 | if (name == null){ |
|---|
| 200 | result = null; |
|---|
| 201 | }else{ |
|---|
| 202 | if (name instanceof ZoologicalName){ |
|---|
| 203 | |
|---|
| 204 | result = (((ZoologicalName)name).getPublicationYear()); |
|---|
| 205 | }else{ |
|---|
| 206 | Reference ref = (Reference) name.getNomenclaturalReference(); |
|---|
| 207 | if (ref == null){ |
|---|
| 208 | result = null; |
|---|
| 209 | }else{ |
|---|
| 210 | if (ref.getDatePublished() == null){ |
|---|
| 211 | Reference inRef = ref.getInReference(); |
|---|
| 212 | if (inRef == null){ |
|---|
| 213 | result = null; |
|---|
| 214 | }else{ |
|---|
| 215 | if (inRef.getDatePublished() == null){ |
|---|
| 216 | result = null; |
|---|
| 217 | }else{ |
|---|
| 218 | result = ref.getInReference().getDatePublished().getStartYear(); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | }else{ |
|---|
| 222 | result = ref.getDatePublished().getStartYear(); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | return result; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | @SuppressWarnings("unchecked") |
|---|
| 233 | @Deprecated |
|---|
| 234 | private String getDate(TaxonBase taxonBase){ |
|---|
| 235 | String result = null; |
|---|
| 236 | if (taxonBase == null){ |
|---|
| 237 | result = null; |
|---|
| 238 | }else{ |
|---|
| 239 | TaxonNameBase name = taxonBase.getName(); |
|---|
| 240 | if (name == null){ |
|---|
| 241 | result = null; |
|---|
| 242 | }else{ |
|---|
| 243 | if (name instanceof ZoologicalName){ |
|---|
| 244 | |
|---|
| 245 | result = String.valueOf(((ZoologicalName)name).getPublicationYear()); |
|---|
| 246 | }else{ |
|---|
| 247 | INomenclaturalReference ref = name.getNomenclaturalReference(); |
|---|
| 248 | if (ref == null){ |
|---|
| 249 | result = null; |
|---|
| 250 | }else{ |
|---|
| 251 | result = ref.getYear(); |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | if (result != null){ |
|---|
| 257 | result = result.trim(); |
|---|
| 258 | } |
|---|
| 259 | if ("".equals(result)){ |
|---|
| 260 | result = null; |
|---|
| 261 | } |
|---|
| 262 | return result; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | } |
|---|