rename getRankByAbbrev methods to getRankByIdInVoc #3479
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / name / Rank.java
index edd948fd6c1bd05cf44ec26fc3f660b797a69102..2684c513ebd83c1301fcf28cd77eb6c564a5c258 100644 (file)
@@ -15,7 +15,6 @@ import java.util.Map;
 import java.util.UUID;
 
 import javax.persistence.Entity;
-import javax.persistence.Enumerated;
 import javax.persistence.Transient;
 import javax.validation.constraints.NotNull;
 import javax.xml.bind.annotation.XmlAccessType;
@@ -23,7 +22,9 @@ import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlType;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
+import org.hibernate.annotations.Type;
 import org.hibernate.envers.Audited;
 import org.hibernate.search.annotations.Indexed;
 
@@ -137,7 +138,7 @@ public class Rank extends OrderedTermBase<Rank> {
     private static final UUID uuidCultivar = UUID.fromString("5e98415b-dc6e-440b-95d6-ea33dbb39ad0");
     private static final UUID uuidUnknownRank = UUID.fromString("5c4d6755-2cf6-44ca-9220-cccf8881700b");
 
-    private static Map<String, UUID> abbrevMap = null;
+    private static Map<String, UUID> idInVocMap = null;
     private static Map<String, UUID> labelMap = null;
 
     protected static Map<UUID, Rank> termMap = null;
@@ -174,14 +175,19 @@ public class Rank extends OrderedTermBase<Rank> {
         */
        @XmlAttribute(name ="RankClass")
        @NotNull
-       @Enumerated  //TODO use UserType 
+       @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
+               parameters = {@org.hibernate.annotations.Parameter(name="enumClass", value="eu.etaxonomy.cdm.model.name.RankClass")}
+       )
        private RankClass rankClass;
 
 
-       // ********************* CONSTRUCTORS ************************************+/
-    
-       //for hibernate use only
-       protected Rank() {}
+//********************************** Constructor *********************************/    
+
+       //for hibernate use only
+       @Deprecated
+       protected Rank() {
+               super(TermType.Rank);
+       }
 
     /**
      * Class constructor: creates an additional rank instance with a description
@@ -546,34 +552,34 @@ public class Rank extends OrderedTermBase<Rank> {
 
 
     /**
-     * Returns the rank identified through a name (abbreviated or not).
+     * Returns the rank identified through a label or the identifier within the vocabulary
      * Preliminary implementation for BotanicalNameParser.
      *
      * @param  strRank the string identifying the rank
      * @return                 the rank
      */
-    public static Rank getRankByNameOrAbbreviation(String strRank) throws UnknownCdmTypeException{
-        return getRankByNameOrAbbreviation(strRank, false);
+    public static Rank getRankByNameOrIdInVoc(String strRank) throws UnknownCdmTypeException{
+        return getRankByNameOrIdInVoc(strRank, false);
     }
 
     /**
-     * Returns the rank identified through a name (abbreviated or not) for a given nomenclatural code.
+     * Returns the rank identified through a label or the identifier within the vocabulary
+     * for a given nomenclatural code.
      * Preliminary implementation for BotanicalNameParser.
      *
      * @param  strRank the string identifying the rank
      * @param   nc      the nomenclatural code
      * @return                 the rank
      */
-    public static Rank getRankByNameOrAbbreviation(String strRank, NomenclaturalCode nc)
-                throws UnknownCdmTypeException{
-        return getRankByNameOrAbbreviation(strRank, nc, false);
+    public static Rank getRankByNameOrIdInVoc(String strRank, NomenclaturalCode nc) throws UnknownCdmTypeException{
+        return getRankByNameOrIdInVoc(strRank, nc, false);
     }
 
     // TODO
     // Preliminary implementation for BotanicalNameParser.
     // not yet complete
     /**
-     * Returns the rank identified through a name (abbreviated or not).
+     * Returns the rank identified through a label or the identifier within the vocabulary.
      * Preliminary implementation for BotanicalNameParser.
      *
      * @param  strRank the string identifying the rank
@@ -581,10 +587,9 @@ public class Rank extends OrderedTermBase<Rank> {
      *                         unknown or not yet implemented
      * @return                 the rank
      */
-    public static Rank getRankByNameOrAbbreviation(String strRank, boolean useUnknown)
-            throws UnknownCdmTypeException{
+    public static Rank getRankByNameOrIdInVoc(String strRank, boolean useUnknown) throws UnknownCdmTypeException{
         try {
-            return getRankByAbbreviation(strRank);
+            return getRankByIdInVoc(strRank);
         } catch (UnknownCdmTypeException e) {
             return getRankByName(strRank, useUnknown);
         }
@@ -594,7 +599,7 @@ public class Rank extends OrderedTermBase<Rank> {
     // Preliminary implementation for BotanicalNameParser.
     // not yet complete
     /**
-     * Returns the rank identified through a name (abbreviated or not).
+     * Returns the rank identified through a label or the identifier within the vocabulary.
      * Preliminary implementation for BotanicalNameParser.
      *
      * @param  strRank the string identifying the rank
@@ -603,17 +608,17 @@ public class Rank extends OrderedTermBase<Rank> {
      *                         unknown or not yet implemented
      * @return                 the rank
      */
-    public static Rank getRankByNameOrAbbreviation(String strRank, NomenclaturalCode nc, boolean useUnknown)
+    public static Rank getRankByNameOrIdInVoc(String strRank, NomenclaturalCode nc, boolean useUnknown)
             throws UnknownCdmTypeException{
         try {
-            return getRankByAbbreviation(strRank, nc);
+            return getRankByIdInVoc(strRank, nc);
         } catch (UnknownCdmTypeException e) {
             return getRankByName(strRank, nc, useUnknown);
         }
     }
 
     /**
-     * Returns the rank identified through an abbreviated name.
+     * Returns the rank identified through the vocabulary identifier.
      * Preliminary implementation for BotanicalNameParser.<BR>
      * Note: For abbrev = "[unranked]" the result is undefined.
      * It maybe the infrageneric unranked or the infraspecific unranked.
@@ -622,21 +627,20 @@ public class Rank extends OrderedTermBase<Rank> {
      * @param  abbrev  the string for the name abbreviation
      * @return                 the rank
      */
-    public static Rank getRankByAbbreviation(String abbrev)
-                        throws UnknownCdmTypeException{
-        return getRankByAbbreviation(abbrev, false);
+    public static Rank getRankByIdInVoc(String abbrev) throws UnknownCdmTypeException{
+        return getRankByIdInVoc(abbrev, false);
     }
 
     /**
      * Returns the rank identified through an abbreviated name for a given nomenclatural code.
-     * See also {@link #getRankByAbbreviation(String, boolean)}
+     * See also {@link #getRankByIdInVoc(String, boolean)}
      *
      * @param  abbrev  the string for the name abbreviation
      * @param  nc          the nomenclatural code
      * @return                 the rank
      */
-    public static Rank getRankByAbbreviation(String abbrev, NomenclaturalCode nc) throws UnknownCdmTypeException{
-        return getRankByAbbreviation(abbrev, nc, false);
+    public static Rank getRankByIdInVoc(String abbrev, NomenclaturalCode nc) throws UnknownCdmTypeException{
+        return getRankByIdInVoc(abbrev, nc, false);
     }
 
     // TODO
@@ -650,48 +654,58 @@ public class Rank extends OrderedTermBase<Rank> {
      * If no according abbreviation is available it throws either an UnknownCdmTypeException
      * or an #Rank.UNKNOWN() object depending on the useUnknown flag.
      *
-     * @param  abbrev          the string for the name abbreviation
+     * @param  idInVoc         the string for the name abbreviation
      * @param  useUnknown      if true the rank UNKNOWN_RANK is returned if the abbrev is
      *                         unknown or not yet existent
      * @return  the rank
      */
-    public static Rank getRankByAbbreviation(String abbrev, boolean useUnknown) throws UnknownCdmTypeException{
+    public static Rank getRankByIdInVoc(String idInVoc, boolean useUnknown) throws UnknownCdmTypeException{
         Rank result = null;
-        if (abbrev == null){
-            throw new NullPointerException("Abbrev is NULL in getRankByAbbreviation");
+        if (idInVoc == null){
+            throw new NullPointerException("idInVoc is NULL in getRankByIdInVoc");
         }
-        if (abbrev.trim().equals("")){
-            //handle empty abbrev as unknown
-            abbrev = "oija�m��";
+        if (StringUtils.isBlank(idInVoc)){
+            //handle empty idInVoc as unknown
+            idInVoc = "oijas34\u0155";
         }
-        if (abbrevMap == null){
+        if (idInVocMap == null){
             return null;
         }
-        UUID uuid = abbrevMap.get(abbrev);
+        idInVoc = normalizeSectionAndSubsection(idInVoc);
+        UUID uuid = idInVocMap.get(idInVoc);
         if (uuid != null ){
             result = getTermByUuid(uuid);
         }
         if (result != null){
             return result;
         }else {
-            if (abbrev == null){
-                abbrev = "(null)";
+            if (idInVoc == null){
+                idInVoc = "(null)";
             }
             if (useUnknown){
-                logger.info("Unknown rank name: " + abbrev + ". Rank 'UNKNOWN_RANK' created instead");
+                logger.info("Unknown rank name: " + idInVoc + ". Rank 'UNKNOWN_RANK' created instead");
                 return Rank.UNKNOWN_RANK();
             }else{
-                throw new UnknownCdmTypeException("Unknown rank abbreviation: " + abbrev);
+                throw new UnknownCdmTypeException("Unknown rank abbreviation: " + idInVoc);
             }
         }
     }
 
-    // TODO
+    private static String normalizeSectionAndSubsection(String idInVoc) {
+               if (idInVoc.equals("sect.")){
+                       return "sect.(bot.)";
+               }else if (idInVoc.equals("subsect.")){
+                       return "subsect.(bot.)";
+               }
+       return idInVoc;
+       }
+
+       // TODO
     // Preliminary implementation to cover Botany and Zoology.
     /**
      * Returns the rank identified through an abbreviated name for a given nomenclatural code.
      * Preliminary implementation for ICBN and ICZN.
-     * See also {@link #getRankByAbbreviation(String, boolean)}
+     * See also {@link #getRankByIdInVoc(String, boolean)}
 
      *
      * @param  abbrev          the string for the name abbreviation
@@ -700,7 +714,7 @@ public class Rank extends OrderedTermBase<Rank> {
      *                         unknown or not yet implemented
      * @return  the rank
      */
-    public static Rank getRankByAbbreviation(String abbrev, NomenclaturalCode nc, boolean useUnknown)
+    public static Rank getRankByIdInVoc(String abbrev, NomenclaturalCode nc, boolean useUnknown)
             throws UnknownCdmTypeException{
 
         if (nc != null && nc.equals(NomenclaturalCode.ICZN)) {
@@ -712,7 +726,7 @@ public class Rank extends OrderedTermBase<Rank> {
                 }
             }
         }
-        return getRankByAbbreviation(abbrev, useUnknown);
+        return getRankByIdInVoc(abbrev, useUnknown);
     }
 
     // TODO
@@ -791,11 +805,12 @@ public class Rank extends OrderedTermBase<Rank> {
         }else if (rankName.equalsIgnoreCase("tax.infragen.")){ return Rank.INFRAGENERICTAXON();
         }else if (rankName.equalsIgnoreCase("tax.infrasp.")){ return Rank.INFRASPECIFICTAXON();
         // old ranks
-        }else if (rankName.equalsIgnoreCase("proles")){ return Rank.INFRASPECIFICTAXON(); //to create the name put prol. and the infraspeciesepi to the field unnamed namephrase
-        }else if (rankName.equalsIgnoreCase("race")){ return Rank.INFRASPECIFICTAXON(); //to create the name put prol. and the infraspeciesepi to the field unnamed namephrase
-        }else if (rankName.equalsIgnoreCase("taxon")){ return Rank.INFRASPECIFICTAXON(); //to create the name put prol. and the infraspeciesepi to the field unnamed namephrase
-        }else if (rankName.equalsIgnoreCase("sublusus")){ return Rank.INFRASPECIFICTAXON(); //to create the name put prol. and the infraspeciesepi to the field unnamed namephrase
+        }else if (rankName.equalsIgnoreCase("proles")){ return Rank.PROLES(); 
+        }else if (rankName.equalsIgnoreCase("race")){ return Rank.RACE(); 
+        }else if (rankName.equalsIgnoreCase("sublusus")){ return Rank.SUBLUSUS(); 
 
+        }else if (rankName.equalsIgnoreCase("taxon")){ return Rank.INFRASPECIFICTAXON(); //to create the name put 'taxon' and the infraspeciesepi to the field unnamed namephrase
+  
         }else{
             if (rankName == null){
                 rankName = "(null)";  //see NPE above
@@ -912,9 +927,9 @@ public class Rank extends OrderedTermBase<Rank> {
 
 
     @Override
-    public Rank readCsvLine(Class<Rank> termClass, List<String> csvLine, Map<UUID, DefinedTermBase> terms) {
-        Rank rank = super.readCsvLine(termClass, csvLine, terms);
-        RankClass rankClass = RankClass.byKey(csvLine.get(5));
+    public Rank readCsvLine(Class<Rank> termClass, List<String> csvLine, Map<UUID, DefinedTermBase> terms, boolean abbrevAsId) {
+        Rank rank = super.readCsvLine(termClass, csvLine, terms, abbrevAsId);
+        RankClass rankClass = RankClass.getByKey(csvLine.get(5));
         assert rankClass != null: "XXXXXXXXXXXXXXXXXXXXX  Rank class must not be null: " + csvLine ;
         rank.setRankClass(rankClass);
         return rank;
@@ -945,20 +960,21 @@ public class Rank extends OrderedTermBase<Rank> {
         Representation representation = rank.getRepresentation(lang);
         String abbrevLabel = representation.getAbbreviatedLabel();
         String label = representation.getLabel();
-        if (abbrevLabel == null){
-            logger.warn("Abbreviated label for rank is NULL.Can't add rank: " + CdmUtils.Nz(rank.getLabel()));
-            return;
-        }
+       
         //initialize maps
-        if (abbrevMap == null){
-            abbrevMap = new HashMap<String, UUID>();
+        if (idInVocMap == null){
+            idInVocMap = new HashMap<String, UUID>();
         }
         if (labelMap == null){
             labelMap = new HashMap<String, UUID>();
         }
-        //add to map
-        abbrevMap.put(abbrevLabel, rank.getUuid());
         labelMap.put(label.toLowerCase(), rank.getUuid());
+        //add to map
+        if (StringUtils.isBlank(abbrevLabel)){
+            logger.info("Abbreviated label for rank is NULL or empty.Can't add rank to abbrevLabel map: " + CdmUtils.Nz(rank.getLabel()));
+        }else{
+               idInVocMap.put(abbrevLabel, rank.getUuid());
+        }
     }