cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / strategy / cache / name / TaxonNameDefaultCacheStrategy.java
index 098708c04749b3ff2de5b4dd5f2a497e942480bc..d8f6cb9a1cb4801c3864812624c65f54efbd480f 100644 (file)
@@ -14,7 +14,8 @@ import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.common.UTF8;
@@ -29,7 +30,6 @@ import eu.etaxonomy.cdm.strategy.cache.TaggedTextBuilder;
 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImplRegExBase;
 
-
 /**
  * This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName>
  * interface.<BR>
@@ -43,7 +43,7 @@ public class TaxonNameDefaultCacheStrategy
         extends NameCacheStrategyBase
         implements INonViralNameCacheStrategy {
 
-    private static final Logger logger = Logger.getLogger(TaxonNameDefaultCacheStrategy.class);
+    private static final Logger logger = LogManager.getLogger(TaxonNameDefaultCacheStrategy.class);
        private static final long serialVersionUID = -6577757501563212669L;
 
     final static UUID uuid = UUID.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
@@ -71,7 +71,6 @@ public class TaxonNameDefaultCacheStrategy
         return new TaxonNameDefaultCacheStrategy();
     }
 
-
 // ************ CONSTRUCTOR *******************/
 
     protected TaxonNameDefaultCacheStrategy(){
@@ -91,7 +90,6 @@ public class TaxonNameDefaultCacheStrategy
         this.nameAuthorSeperator = nameAuthorSeperator;
     }
 
-
     /**
      * String the basionym author part starts with e.g. '('.
      * This should correspond with the {@link TaxonNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
@@ -116,10 +114,8 @@ public class TaxonNameDefaultCacheStrategy
         this.basionymEnd = basionymEnd;
     }
 
-
     /**
      * String to separate ex author from author.
-     * @return
      */
     public String getExAuthorSeperator() {
         return exAuthorSeperator;
@@ -128,16 +124,12 @@ public class TaxonNameDefaultCacheStrategy
         this.exAuthorSeperator = exAuthorSeperator;
     }
 
-
     /**
      * String that separates the basionym/original_combination author part from the combination author part
-     * @return
      */
     public CharSequence getBasionymAuthorCombinationAuthorSeperator() {
         return basionymAuthorCombinationAuthorSeperator;
     }
-
-
     public void setBasionymAuthorCombinationAuthorSeperator( CharSequence basionymAuthorCombinationAuthorSeperator) {
         this.basionymAuthorCombinationAuthorSeperator = basionymAuthorCombinationAuthorSeperator;
     }
@@ -307,7 +299,7 @@ public class TaxonNameDefaultCacheStrategy
             return tags;
         }else if (taxonName.isAutonym()){
             //Autonym
-            tags.addAll(handleTaggedAutonym(taxonName));
+            tags.addAll(handleTaggedAutonym(taxonName, true));
         }else{ //not Autonym
             List<TaggedText> nameTags = getTaggedName(taxonName);
             tags.addAll(nameTags);
@@ -352,21 +344,21 @@ public class TaxonNameDefaultCacheStrategy
             return tags;
 
         }else if (rank == null){
-            tags = getRanklessTaggedNameCache(taxonName);
+            tags = getRanklessTaggedNameCache(taxonName, true);
                }else if (rank.isCultivar()){
                        tags = getCultivarTaggedNameCache(taxonName);
         }else if (rank.isInfraSpecific()){
             tags = getInfraSpeciesTaggedNameCache(taxonName);
         }else if (rank.isSpecies() || isAggregateWithAuthorship(taxonName, rank) ){ //exception see #4288
-            tags = getSpeciesTaggedNameCache(taxonName);
+            tags = getSpeciesTaggedNameCache(taxonName, true);
         }else if (rank.isInfraGeneric()){
-            tags = getInfraGenusTaggedNameCache(taxonName);
+            tags = getInfraGenusTaggedNameCache(taxonName, true);
         }else if (rank.isGenus()){
-            tags = getGenusOrUninomialTaggedNameCache(taxonName);
+            tags = getGenusOrUninomialTaggedNameCache(taxonName, true);
         }else if (rank.isSupraGeneric()){
-            tags = getGenusOrUninomialTaggedNameCache(taxonName);
+            tags = getGenusOrUninomialTaggedNameCache(taxonName, true);
         }else{
-            tags = getRanklessTaggedNameCache(taxonName);
+            tags = getRanklessTaggedNameCache(taxonName, true);
             logger.warn("Rank does not belong to a rank class: " + rank.getTitleCache() + ". Used rankless nameCache for name " + taxonName.getUuid());
         }
         //TODO handle appended phrase here instead of different places, check first if this is true for all
@@ -381,13 +373,13 @@ public class TaxonNameDefaultCacheStrategy
         List<TaggedText> scientificNameTags;
         TaggedTextBuilder builder = TaggedTextBuilder.NewInstance();
         if (isNotBlank(taxonName.getInfraSpecificEpithet())){
-            scientificNameTags = getInfraSpeciesTaggedNameCache(taxonName);
+            scientificNameTags = getInfraSpeciesTaggedNameCache(taxonName, false, false);
         } else if (isNotBlank(taxonName.getSpecificEpithet())){
-            scientificNameTags = getSpeciesTaggedNameCache(taxonName);
+            scientificNameTags = getSpeciesTaggedNameCache(taxonName, false);
         } else if (isNotBlank(taxonName.getInfraGenericEpithet())){
-            scientificNameTags = getInfraGenusTaggedNameCache(taxonName);
+            scientificNameTags = getInfraGenusTaggedNameCache(taxonName, false);
         } else /*if (isNotBlank(taxonName.getGenusOrUninomial())) */{
-            scientificNameTags = getGenusOrUninomialTaggedNameCache(taxonName);
+            scientificNameTags = getGenusOrUninomialTaggedNameCache(taxonName, false);
         }
 
         UUID rankUuid = taxonName.getRank().getUuid();
@@ -409,18 +401,22 @@ public class TaxonNameDefaultCacheStrategy
         }
         if (rankIsHandled){
             builder.addAll(scientificNameTags);
-            if (isNotBlank(cultivarStr)){
-                builder.add(TagEnum.cultivar, cultivarStr);
-            }
         }else if (rankUuid.equals(Rank.uuidGraftChimaera)){
             //TODO not yet fully implemented
             cultivarStr = "+ " + CdmUtils.concat(" ", taxonName.getGenusOrUninomial(), surroundedCultivarEpithet(taxonName.getCultivarEpithet()));
-            builder.add(TagEnum.cultivar, cultivarStr);
+        }else if (rankUuid.equals(Rank.uuidDenominationClass)){
+            //TODO dummy implementation
+            cultivarStr = CdmUtils.concat(" ", taxonName.getGenusOrUninomial(), surroundedCultivarEpithet(taxonName.getCultivarEpithet()));
         } else { //(!rankIsHandled)
             throw new IllegalStateException("Unsupported rank " + taxonName.getRank().getTitleCache() + " for cultivar.");
         }
+        if (isNotBlank(cultivarStr)){
+            builder.add(TagEnum.cultivar, cultivarStr);
+        }
 
-        return builder.getTaggedText();
+        List<TaggedText> tags = builder.getTaggedText();
+        addAppendedTaggedPhrase(tags, taxonName, true);
+        return tags;
     }
 
     private String surroundGroupWithBracket(String groupStr) {
@@ -479,11 +475,11 @@ public class TaxonNameDefaultCacheStrategy
      * @param nonViralName
      * @return
      */
-    private List<TaggedText> handleTaggedAutonym(TaxonName nonViralName) {
+    private List<TaggedText> handleTaggedAutonym(TaxonName nonViralName, boolean addAppended) {
        List<TaggedText> tags = null;
        if (nonViralName.isInfraSpecific()){
                //species part
-               tags = getSpeciesTaggedNameCache(nonViralName);
+               tags = getSpeciesTaggedNameCache(nonViralName, addAppended);
 
                //author
                String authorCache = getAuthorshipCache(nonViralName);
@@ -513,7 +509,7 @@ public class TaxonNameDefaultCacheStrategy
 
         } else if (nonViralName.isInfraGeneric()){
                //genus part
-              tags =getGenusOrUninomialTaggedNameCache(nonViralName);
+              tags =getGenusOrUninomialTaggedNameCache(nonViralName, addAppended);
 
               //author
            String authorCache = getAuthorshipCache(nonViralName);
@@ -553,7 +549,7 @@ public class TaxonNameDefaultCacheStrategy
      * @param nonViralName
      * @return
      */
-    protected List<TaggedText> getRanklessTaggedNameCache(INonViralName nonViralName){
+    protected List<TaggedText> getRanklessTaggedNameCache(INonViralName nonViralName, boolean addAppended){
         List<TaggedText> tags = getUninomialTaggedPart(nonViralName);
         String speciesEpi = CdmUtils.Nz(nonViralName.getSpecificEpithet()).trim();
         if (isNotBlank(speciesEpi)){
@@ -566,7 +562,7 @@ public class TaxonNameDefaultCacheStrategy
         }
 
         //result += " (rankless)";
-        addAppendedTaggedPhrase(tags, nonViralName);
+        addAppendedTaggedPhrase(tags, nonViralName, addAppended);
         return tags;
     }
 
@@ -596,9 +592,9 @@ public class TaxonNameDefaultCacheStrategy
      * @param nonViralName
      * @return
      */
-    protected List<TaggedText> getGenusOrUninomialTaggedNameCache(INonViralName nonViralName){
+    protected List<TaggedText> getGenusOrUninomialTaggedNameCache(INonViralName nonViralName, boolean addAppended){
         List<TaggedText> tags = getUninomialTaggedPart(nonViralName);
-        addAppendedTaggedPhrase(tags, nonViralName);
+        addAppendedTaggedPhrase(tags, nonViralName, addAppended);
         return tags;
     }
 
@@ -609,10 +605,10 @@ public class TaxonNameDefaultCacheStrategy
      * @param nonViralName
      * @return
      */
-    protected List<TaggedText> getInfraGenusTaggedNameCache(INonViralName nonViralName){
+    protected List<TaggedText> getInfraGenusTaggedNameCache(INonViralName nonViralName, boolean addAppended){
         Rank rank = nonViralName.getRank();
         if (rank != null && rank.isSpeciesAggregate() && isBlank(nonViralName.getAuthorshipCache())){
-            return getSpeciesAggregateTaggedCache(nonViralName);
+            return getSpeciesAggregateTaggedCache(nonViralName, addAppended);
         }
 
         //genus
@@ -639,7 +635,7 @@ public class TaxonNameDefaultCacheStrategy
 
         addInfraGenericPart(nonViralName, tags, infraGenericMarker, infraGenEpi);
 
-        addAppendedTaggedPhrase(tags, nonViralName);
+        addAppendedTaggedPhrase(tags, nonViralName, addAppended);
         return tags;
     }
 
@@ -673,24 +669,21 @@ public class TaxonNameDefaultCacheStrategy
      * @param nonViralName
      * @return
      */
-    protected List<TaggedText> getSpeciesAggregateTaggedCache(INonViralName nonViralName){
+    protected List<TaggedText> getSpeciesAggregateTaggedCache(INonViralName nonViralName, boolean addAppended){
         if (! isBlank(nonViralName.getAuthorshipCache())){
-               List<TaggedText> result = getSpeciesTaggedNameCache(nonViralName);
+               List<TaggedText> result = getSpeciesTaggedNameCache(nonViralName, addAppended);
                return result;
         }
 
-
        List<TaggedText> tags = getGenusAndSpeciesTaggedPart(nonViralName);
 
         addSpeciesAggregateTaggedEpithet(tags, nonViralName);
-        addAppendedTaggedPhrase(tags, nonViralName);
+        addAppendedTaggedPhrase(tags, nonViralName, addAppended);
         return tags;
     }
 
     /**
      * Adds the aggregate tag to the tag list.
-     * @param tags
-     * @param nonViralName
      */
     private void addSpeciesAggregateTaggedEpithet(List<TaggedText> tags, INonViralName nonViralName) {
         String marker;
@@ -704,26 +697,21 @@ public class TaxonNameDefaultCacheStrategy
         }
     }
 
-
     /**
      * Returns the tag list for a species taxon.
-     * @param nonViralName
-     * @return
      */
-    protected List<TaggedText> getSpeciesTaggedNameCache(INonViralName nonViralName){
+    protected List<TaggedText> getSpeciesTaggedNameCache(INonViralName nonViralName, boolean addAppended){
         List<TaggedText> tags = getGenusAndSpeciesTaggedPart(nonViralName);
-        addAppendedTaggedPhrase(tags, nonViralName);
+        addAppendedTaggedPhrase(tags, nonViralName, addAppended);
         return tags;
     }
 
     protected List<TaggedText> getInfraSpeciesTaggedNameCache(TaxonName name){
         if (name.getNameType().isZoological()){
             boolean includeMarker = includeInfraSpecificMarkerForZooNames(name);
-            return getInfraSpeciesTaggedNameCache(name, includeMarker);
-        }else if (name.isCultivar() || name.getRank() != null && name.getRank().isCultivar()){
-            return getInfraSpeciesTaggedNameCache(name, false);
+            return getInfraSpeciesTaggedNameCache(name, includeMarker, true);
         }else{
-            return getInfraSpeciesTaggedNameCache(name, true);
+            return getInfraSpeciesTaggedNameCache(name, true, true);
         }
     }
 
@@ -738,7 +726,8 @@ public class TaxonNameDefaultCacheStrategy
      * @param includeMarker
      * @return
      */
-    protected List<TaggedText> getInfraSpeciesTaggedNameCache(INonViralName nonViralName, boolean includeMarker){
+    protected List<TaggedText> getInfraSpeciesTaggedNameCache(INonViralName nonViralName,
+            boolean includeMarker, boolean addAppended){
         List<TaggedText> tags = getGenusAndSpeciesTaggedPart(nonViralName);
         if (includeMarker || nonViralName.isTrinomHybrid()){
             String marker = (nonViralName.getRank().getAbbreviation()).trim().replace("null", "");
@@ -758,7 +747,7 @@ public class TaxonNameDefaultCacheStrategy
             tags.add(new TaggedText(TagEnum.name, infrSpecEpi));
         }
 
-        addAppendedTaggedPhrase(tags, nonViralName);
+        addAppendedTaggedPhrase(tags, nonViralName, addAppended);
         return tags;
     }
 
@@ -809,8 +798,12 @@ public class TaxonNameDefaultCacheStrategy
      * Adds the tag for the appended phrase if an appended phrase exists
      * @param tags
      * @param nonViralName
+     * @param addAppended
      */
-    protected void addAppendedTaggedPhrase(List<TaggedText> tags, INonViralName nonViralName){
+    protected void addAppendedTaggedPhrase(List<TaggedText> tags, INonViralName nonViralName, boolean addAppended){
+        if (!addAppended){
+            return;
+        }
         String appendedPhrase = nonViralName ==null ? null : nonViralName.getAppendedPhrase();
         if (isNotBlank(appendedPhrase)){
             tags.add(new TaggedText(TagEnum.name, appendedPhrase));