rename getRankByAbbrev methods to getRankByIdInVoc #3479
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / name / Rank.java
index ef77d44ea74da5bc61d7442e0f5529484687c236..2684c513ebd83c1301fcf28cd77eb6c564a5c258 100644 (file)
@@ -22,6 +22,7 @@ import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlType;
 
 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.apache.log4j.Logger;
 import org.hibernate.annotations.Type;
 import org.hibernate.envers.Audited;
@@ -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 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;
     private static Map<String, UUID> labelMap = null;
 
     protected static Map<UUID, Rank> termMap = null;
@@ -551,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
      */
      * 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
      */
      * 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
     /**
     }
 
     // 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
      * Preliminary implementation for BotanicalNameParser.
      *
      * @param  strRank the string identifying the rank
@@ -586,10 +587,9 @@ public class Rank extends OrderedTermBase<Rank> {
      *                         unknown or not yet implemented
      * @return                 the 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 {
         try {
-            return getRankByAbbreviation(strRank);
+            return getRankByIdInVoc(strRank);
         } catch (UnknownCdmTypeException e) {
             return getRankByName(strRank, useUnknown);
         }
         } catch (UnknownCdmTypeException e) {
             return getRankByName(strRank, useUnknown);
         }
@@ -599,7 +599,7 @@ public class Rank extends OrderedTermBase<Rank> {
     // Preliminary implementation for BotanicalNameParser.
     // not yet complete
     /**
     // 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
      * Preliminary implementation for BotanicalNameParser.
      *
      * @param  strRank the string identifying the rank
@@ -608,17 +608,17 @@ public class Rank extends OrderedTermBase<Rank> {
      *                         unknown or not yet implemented
      * @return                 the 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 {
             throws UnknownCdmTypeException{
         try {
-            return getRankByAbbreviation(strRank, nc);
+            return getRankByIdInVoc(strRank, nc);
         } catch (UnknownCdmTypeException e) {
             return getRankByName(strRank, nc, useUnknown);
         }
     }
 
     /**
         } 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.
      * Preliminary implementation for BotanicalNameParser.<BR>
      * Note: For abbrev = "[unranked]" the result is undefined.
      * It maybe the infrageneric unranked or the infraspecific unranked.
@@ -627,21 +627,20 @@ public class Rank extends OrderedTermBase<Rank> {
      * @param  abbrev  the string for the name abbreviation
      * @return                 the 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.
     }
 
     /**
      * 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
      */
      *
      * @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
     }
 
     // TODO
@@ -655,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.
      *
      * 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
      */
      * @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;
         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;
         }
             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 (uuid != null ){
             result = getTermByUuid(uuid);
         }
         if (result != null){
             return result;
         }else {
-            if (abbrev == null){
-                abbrev = "(null)";
+            if (idInVoc == null){
+                idInVoc = "(null)";
             }
             if (useUnknown){
             }
             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{
                 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.
     // 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
 
      *
      * @param  abbrev          the string for the name abbreviation
@@ -705,7 +714,7 @@ public class Rank extends OrderedTermBase<Rank> {
      *                         unknown or not yet implemented
      * @return  the 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)) {
             throws UnknownCdmTypeException{
 
         if (nc != null && nc.equals(NomenclaturalCode.ICZN)) {
@@ -717,7 +726,7 @@ public class Rank extends OrderedTermBase<Rank> {
                 }
             }
         }
                 }
             }
         }
-        return getRankByAbbreviation(abbrev, useUnknown);
+        return getRankByIdInVoc(abbrev, useUnknown);
     }
 
     // TODO
     }
 
     // TODO
@@ -951,20 +960,21 @@ public class Rank extends OrderedTermBase<Rank> {
         Representation representation = rank.getRepresentation(lang);
         String abbrevLabel = representation.getAbbreviatedLabel();
         String label = representation.getLabel();
         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
         //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>();
         }
         }
         if (labelMap == null){
             labelMap = new HashMap<String, UUID>();
         }
-        //add to map
-        abbrevMap.put(abbrevLabel, rank.getUuid());
         labelMap.put(label.toLowerCase(), 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());
+        }
     }
 
 
     }