rename abstract classes and some other cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / format / ReferencingObjectFormatter.java
index f322960ab41f340ef2179a54067f5f0db95dabc9..14fe9f2d817616b5ea0ba3438831e7c101b2a16b 100644 (file)
@@ -15,6 +15,10 @@ import java.util.Set;
 import org.apache.commons.lang3.StringUtils;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
+import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
+import eu.etaxonomy.cdm.format.description.DescriptionElementFormatter;
+import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
 import eu.etaxonomy.cdm.format.occurrences.DistanceStringFormatter;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.common.CdmBase;
@@ -28,18 +32,24 @@ import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.common.RelationshipBase;
 import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
 import eu.etaxonomy.cdm.model.common.TimePeriod;
+import eu.etaxonomy.cdm.model.description.CategoricalData;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
 import eu.etaxonomy.cdm.model.description.Distribution;
 import eu.etaxonomy.cdm.model.description.IDescribable;
+import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
 import eu.etaxonomy.cdm.model.description.KeyStatement;
 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
+import eu.etaxonomy.cdm.model.description.QuantitativeData;
 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
+import eu.etaxonomy.cdm.model.description.StateData;
+import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
+import eu.etaxonomy.cdm.model.description.TemporalData;
 import eu.etaxonomy.cdm.model.description.TextData;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
@@ -113,6 +123,10 @@ public class ReferencingObjectFormatter {
             resultString = ((LanguageStringBase) element).getText();
         }else if (element instanceof DescriptionElementBase) {
             resultString = getCache((DescriptionElementBase) element, defaultLanguage);
+        }else if (element instanceof StateData) {
+            resultString = getCache((StateData) element, defaultLanguage);
+        }else if (element instanceof StatisticalMeasurementValue) {
+            resultString = getCache((StatisticalMeasurementValue) element, defaultLanguage);
         }else if (element instanceof RelationshipBase<?, ?, ?>) {
             resultString = getCache((RelationshipBase<?, ?, ?>) element, defaultLanguage);
         }else if (element instanceof TypeDesignationBase<?>) {
@@ -308,21 +322,26 @@ public class ReferencingObjectFormatter {
 
     private static String getCache(TaxonNode taxonNode) {
         String result = "";
+        Taxon taxon = taxonNode.getTaxon();
+        if (taxon != null){
+            result = taxon.getName() != null ? taxon.getName().getTitleCache() : taxon.getTitleCache();
+        }
 
+        final String invisible = "Invisible root of ";
         String parentStr;
         TaxonNode parentNode = taxonNode.getParent();
         if (parentNode == null){
-            parentStr = "Invisible root of ";
+            parentStr = invisible;
         }else{
             Taxon parentTaxon = parentNode.getTaxon();
             if (parentTaxon == null){
-                parentStr = "Root of ";
+                parentStr = " (root of ";
             }else{
                 TaxonName parentName = parentTaxon.getName();
                 if (parentName == null){
-                    parentStr = "Child of " + parentTaxon.getTitleCache();
+                    parentStr = " (child of " + parentTaxon.getTitleCache();
                 }else{
-                    parentStr = "Child of " + parentName.getTitleCache();
+                    parentStr = " (child of " + parentName.getTitleCache();
                 }
                 parentStr += " in ";
             }
@@ -340,7 +359,7 @@ public class ReferencingObjectFormatter {
             classificationStr = "-no classification-"; //should not happen
         }
 
-        result = CdmUtils.concat("", parentStr, classificationStr);
+        result = CdmUtils.concat("", parentStr, classificationStr, parentStr == invisible? "" : ")");
 
         return result;
     }
@@ -440,46 +459,43 @@ public class ReferencingObjectFormatter {
     private static String getCache(DescriptionElementBase element,
             Language defaultLanguage) {
 
-        String mainElementLabel= null;
         DescriptionBase<?> descr = element.getInDescription();
         descr = CdmBase.deproxy(descr);
 
-        if (descr != null){
-            if (descr.isInstanceOf(TaxonDescription.class)){
-                Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
-                if (taxon != null){
-                    mainElementLabel = taxon.getTitleCache();
-                }
-            }else if (descr.isInstanceOf(SpecimenDescription.class)){
-                SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
-                if (specimen != null){
-                    mainElementLabel = specimen.getTitleCache();
-                }
-            }else if (descr.isInstanceOf(TaxonNameDescription.class)){
-                TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
-                if (name != null){
-                    mainElementLabel = name.getTitleCache();
-                }
-            }
-        }
+        String mainElementLabel = mainElementLabel(descr);
 
         String cache = null;
+        //TextData
         if (element instanceof TextData) {
-            //cache = ((TextData) element).getText(language);
-            cache = "Text Data";
-        }
-        if (element instanceof CommonTaxonName) {
+            LanguageString text = ((TextData) element).getPreferredLanguageString(defaultLanguage);
+            if (text != null) {
+                cache = text.getText();
+            }
+            cache = cache == null || isBlank(cache)? "empty" : StringUtils.truncate(cache, 20);
+        //CommonTaxonName
+        }else  if (element instanceof CommonTaxonName) {
             cache = ((CommonTaxonName) element).getName();
-        }
-        if (element instanceof TaxonInteraction) {
+        //TaxonInteraction
+        }else if (element instanceof TaxonInteraction) {
             Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
             if(taxon2 != null && taxon2.getName() != null){
                 cache = taxon2.getName().getTitleCache();
             }else{
                 cache = "No taxon chosen";
             }
-        }
-        if (element instanceof Distribution) {
+         //IndividualsAssociation
+        }else if (element instanceof IndividualsAssociation) {
+            SpecimenOrObservationBase<?> unit = ((IndividualsAssociation) element).getAssociatedSpecimenOrObservation();
+            if(unit != null){
+                cache = unit.getIdentityCache();
+                if (isBlank(cache)) {
+                    cache = unit.getTitleCache();
+                }
+            }else{
+                cache = "No unit chosen";
+            }
+        //Distribution
+        }else if (element instanceof Distribution) {
             Distribution distribution = (Distribution) element;
 
             NamedArea area = distribution.getArea();
@@ -493,12 +509,90 @@ public class ReferencingObjectFormatter {
                     cache += ", " + status.getLabel();
                 }
             }
+        //CategoricalData
+        }else if (element instanceof CategoricalData) {
+            CategoricalData categoricalData = (CategoricalData) element;
+
+            cache = CategoricalDataFormatter.NewInstance(new FormatKey[] {}).format(categoricalData);
+        //QuantitativeData
+        }else if (element instanceof QuantitativeData) {
+            QuantitativeData quantitativeData = (QuantitativeData) element;
+
+            cache = QuantitativeDataFormatter.NewInstance(new FormatKey[] {}).format(quantitativeData);
+        //CategoricalData
+        }else if (element instanceof TemporalData) {
+            TemporalData temporalData = (TemporalData) element;
+            cache = temporalData.toString();
         }
+
         String result = cache == null ? "" : cache;
+        result = concatWithMainElement(mainElementLabel, result);
+        return result;
+    }
+
+    private static String getCache(StateData stateData,
+            Language defaultLanguage) {
+        String cache = null;
+        if (stateData.getState() != null) {
+            Representation rep = stateData.getState().getPreferredRepresentation(defaultLanguage);
+            if (rep != null) {
+                cache = rep.getLabel();
+            }
+        }
+        cache = isBlank(cache)? stateData.getUuid().toString() : cache;
+        DescriptionBase<?> desc = stateData.getCategoricalData() == null? null : stateData.getCategoricalData().getInDescription();
+        String mainElementLabel = mainElementLabel(desc);
+        return concatWithMainElement(mainElementLabel, cache);
+    }
+
+    private static String getCache(StatisticalMeasurementValue smv, Language defaultLanguage) {
+        String cache = null;
+        if (smv.getType() != null) {
+            Representation rep = smv.getType().getPreferredRepresentation(defaultLanguage);
+            if (rep != null) {
+                cache = rep.getLabel();
+            }
+        }
+        if (smv.getValue() != null) {
+            cache = CdmUtils.concat("=", cache, smv.getValue().toString());
+        }
+        cache = isBlank(cache)? smv.getUuid().toString() : cache;
+        DescriptionBase<?> desc = smv.getQuantitativeData() == null? null : smv.getQuantitativeData().getInDescription();
+        String mainElementLabel = mainElementLabel(desc);
+        return concatWithMainElement(mainElementLabel, cache);
+    }
+
+    /**
+     * Returns the label of the main element (taxon, specimen or name) for
+     * the given description.
+     */
+    private static String mainElementLabel(DescriptionBase<?> descr) {
+        String mainElementLabel = null;
+        if (descr != null){
+            if (descr.isInstanceOf(TaxonDescription.class)){
+                Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
+                if (taxon != null){
+                    mainElementLabel = taxon.getTitleCache();
+                }
+            }else if (descr.isInstanceOf(SpecimenDescription.class)){
+                SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
+                if (specimen != null){
+                    mainElementLabel = specimen.getTitleCache();
+                }
+            }else if (descr.isInstanceOf(TaxonNameDescription.class)){
+                TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
+                if (name != null){
+                    mainElementLabel = name.getTitleCache();
+                }
+            }
+        }
+        return mainElementLabel;
+    }
+
+    private static String concatWithMainElement(String mainElementLabel, String result) {
         if (isNotBlank(mainElementLabel)){
             result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
         }
         return result;
     }
-
 }