Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / DescriptionHelper.java
index b54b5d8a4717fd65673a33a37caa5708c68ba1b7..b8980595a32a526d1999a9a3c122706a04a67652 100644 (file)
@@ -6,7 +6,6 @@
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
-
 package eu.etaxonomy.taxeditor.model;
 
 import java.lang.reflect.InvocationTargetException;
@@ -15,29 +14,26 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 
-import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.hibernate.LazyInitializationException;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
-import eu.etaxonomy.cdm.api.service.DefaultCategoricalDescriptionBuilder;
-import eu.etaxonomy.cdm.api.service.DefaultQuantitativeDescriptionBuilder;
-import eu.etaxonomy.cdm.api.service.DescriptionBuilder;
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.format.common.ExtendedTimePeriodFormatter;
+import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
+import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.Group;
+import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.LanguageString;
 import eu.etaxonomy.cdm.model.common.LanguageStringBase;
 import eu.etaxonomy.cdm.model.common.Marker;
 import eu.etaxonomy.cdm.model.common.MarkerType;
-import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
 import eu.etaxonomy.cdm.model.common.RelationshipBase;
 import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
-import eu.etaxonomy.cdm.model.common.Representation;
 import eu.etaxonomy.cdm.model.common.TimePeriod;
-import eu.etaxonomy.cdm.model.common.User;
 import eu.etaxonomy.cdm.model.description.CategoricalData;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
@@ -53,6 +49,7 @@ import eu.etaxonomy.cdm.model.description.SpecimenDescription;
 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.media.Media;
@@ -60,28 +57,36 @@ import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
 import eu.etaxonomy.cdm.model.name.HybridRelationship;
 import eu.etaxonomy.cdm.model.name.NameRelationship;
 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
+import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
 import eu.etaxonomy.cdm.model.name.TaxonName;
+import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
 import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.permission.Group;
+import eu.etaxonomy.cdm.model.permission.User;
+import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
 import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
+import eu.etaxonomy.cdm.model.term.Representation;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
- * <p>DescriptionHelper class.</p>
+ * DescriptionHelper class.
  *
  * @author p.ciardelli
  * @author n.hoffmann
  */
 public class DescriptionHelper {
 
+    public static final String NO_LABEL_STRING = "[no label]";
+
        /**
         * Returns whatever the element's title cache equivalent is,
         * depending on its class.
@@ -116,7 +121,6 @@ public class DescriptionHelper {
                        }
                }
 
-
                String cache = null;
                if (element instanceof TextData) {
                        //cache = ((TextData) element).getText(language);
@@ -132,7 +136,6 @@ public class DescriptionHelper {
                        }else{
                                cache = "No taxon chosen";
                        }
-
                }
                if (element instanceof Distribution) {
                        Distribution distribution = (Distribution) element;
@@ -205,18 +208,11 @@ public class DescriptionHelper {
                setCache(element, value, CdmStore.getDefaultLanguage());
        }
 
-
-       /**
-        * <p>getObjectDescription</p>
-        *
-        * @param element a {@link java.lang.Object} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static String getObjectDescription(Object element) {
            String resultString = null;
                if (element instanceof IdentifiableEntity) {
                        try{
-                           resultString = ((IdentifiableEntity) element).getTitleCache();
+                           resultString = ((IdentifiableEntity<?>) element).getTitleCache();
                        }catch(LazyInitializationException e){
                                String result = "No Session to initialize title cache for IdentifiableEntity";
                                MessagingUtils.error(DescriptionHelper.class, result, e);
@@ -227,14 +223,16 @@ public class DescriptionHelper {
                                OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
 //                             ISourceable sourcedObject = originalSource.getSourcedObj();
                                //due to #5743 the bidirectionality for sourced object had to be removed
+
                                String sourceObjectTitle = "sourced object data not available (#5743)";
-//                             if(sourcedObject instanceof IIdentifiableEntity){
-//                                     sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
-//                             }else if(sourcedObject instanceof DescriptionElementBase){
-//                                     sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
-//                             }else{
-//                                     throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
-//                             }
+
+                               //it is now possible for NomenclaturalSource as they link to the sourced name
+                               if (originalSource instanceof NomenclaturalSource){
+                                   TaxonName sourcedName = ((NomenclaturalSource)originalSource).getSourcedName();
+                                   sourceObjectTitle = sourcedName == null ? "Source not attached to a name" :
+                                       "Nom. source for " + sourcedName.getTitleCache();
+                               }
+
 
                                resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
                        }catch(LazyInitializationException e){
@@ -281,7 +279,7 @@ public class DescriptionHelper {
                }
                return resultString;
        }
-       
+
        private static String getCache(GatheringEvent gatheringEvent){
                String ALTITUDE_PREFIX = "alt. ";
                final String METER = "m";
@@ -292,11 +290,11 @@ public class DescriptionHelper {
                AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
                String collectorStr = collector == null? null : collector.getTitleCache();
                result = CdmUtils.concat(", ", result, collectorStr);
-               
+
                //gathering period
                TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
                result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
-               
+
                //country
                String strCountry = null;
                NamedArea country = gatheringEvent.getCountry();
@@ -334,7 +332,6 @@ public class DescriptionHelper {
                return result;
        }
 
-
        private static String getCache(DeterminationEvent detEvent) {
                //taxon
                String taxonStr = null;
@@ -350,7 +347,6 @@ public class DescriptionHelper {
                        taxonStr = "no or unlabled taxon";
                }
 
-
                //unit
                SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
                String unitStr;
@@ -402,18 +398,30 @@ public class DescriptionHelper {
 
        private static String getCache(TypeDesignationBase<?> designation) {
                designation = CdmBase.deproxy(designation);
-               TypeDesignationStatusBase<?> status = designation.getTypeStatus();
-               Set<TaxonName> from;
-               IdentifiableEntity<?> to;
-               from = designation.getTypifiedNames();
+               //from
+               String fromString = null;
+               Set<TaxonName> from = designation.getTypifiedNames();
+               if(from != null){
+                   for (TaxonName name : from){
+                       fromString = CdmUtils.concat(",", fromString, name.getTitleCache());
+                   }
+               }
+               //to
+               IdentifiableEntity<?> to = null;
+               String toStr = "";
                if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
                        to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
                }else if (designation.isInstanceOf(NameTypeDesignation.class)){
                        to = ((NameTypeDesignation)designation).getTypeName();
+               }else if (designation.isInstanceOf(TextualTypeDesignation.class)){
+                   toStr = ((TextualTypeDesignation)designation).getPreferredText(CdmStore.getDefaultLanguage());
                }else{
                        throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
                }
+               toStr = to == null? toStr : to.getTitleCache();
+               //status
                String typeLabel = null;
+               TypeDesignationStatusBase<?> status = designation.getTypeStatus();
                if (status != null){
                        Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
                        if (typeRepr != null){
@@ -432,12 +440,8 @@ public class DescriptionHelper {
                if (isBlank(typeLabel)){
                        typeLabel = "->";
                }
-               String fromString = "";
-               for (TaxonName name : from){
-                       CdmUtils.concat(",", fromString, name.getTitleCache());
-               }
-               String result = CdmUtils.concat(" ", new String[]{from == null ? null : fromString,
-                               typeLabel, to == null? null : to.getTitleCache()});
+               //concat
+               String result = CdmUtils.concat(" ", new String[]{fromString, typeLabel, toStr});
                return result;
        }
 
@@ -512,73 +516,38 @@ public class DescriptionHelper {
         return result;
     }
 
-       /**
-        * <p>getObjectClassname</p>
-        *
-        * @param element a {@link java.lang.Object} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static String getObjectClassname(Object element) {
                return element.getClass().getSimpleName();
        }
 
-       /**
-        * <p>getFeatureNodeContainerText</p>
-        *
-        * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
                String result = null;
-               if(element.getFeatureNode() != null && element.getFeatureNode().getFeature() != null){
-                       result = element.getFeatureNode().getFeature().getLabel(CdmStore.getDefaultLanguage());
+               if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
+                       result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
                } else{
                        return "No label set";
                }
                if (result == null){
-                       result = element.getFeatureNode().getFeature().getLabel();
+                       result = element.getFeatureNode().getTerm().getLabel();
                }
                return result;
        }
 
-       /**
-        * <p>getQuantitativeDataText</p>
-        *
-        * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static String getQuantitativeDataText(QuantitativeData element) {
-               TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
-
-               return textData.getText(CdmStore.getDefaultLanguage());
+           QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
+           String label = formatter.format(element, getLanguageList());
+               return label;
        }
 
-       /**
-        * <p>getCategoricalDataText</p>
-        *
-        * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static String getCategoricalDataText(CategoricalData element) {
-               TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
-
-               return textData.getText(CdmStore.getDefaultLanguage());
+           CategoricalDataFormatter formatter = new CategoricalDataFormatter(null, null);
+               return formatter.format(element, getLanguageList());
        }
 
        private static List<Language> getLanguageList(){
                return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
        }
 
-       private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
-       private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
-
-
-       /**
-        * <p>getDistributionText</p>
-        *
-        * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static String getDistributionText(Distribution element) {
 
                String text = "EMPTY";
@@ -601,12 +570,6 @@ public class DescriptionHelper {
                return text;
        }
 
-       /**
-        * <p>getImageText</p>
-        *
-        * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static  String getImageText(Media media) {
                 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
                if (title != null) {
@@ -615,14 +578,6 @@ public class DescriptionHelper {
                return "No title.";
        }
 
-
-
-       /**
-        * <p>getElementText</p>
-        *
-        * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static  String getElementText(TextData element) {
                String text = null;
                if(element.getFeature().equals(Feature.CITATION())){
@@ -632,7 +587,7 @@ public class DescriptionHelper {
                                        text += source.getCitation().getTitleCache();
                                }
                                if(source.getNameUsedInSource() != null){
-                                       text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
+                                       text += " [" + source.getNameUsedInSource().getTitleCache() + "]";
                                }
                        }
                        if(isBlank(text)){
@@ -646,12 +601,6 @@ public class DescriptionHelper {
                return isBlank(text) ? "No text provided" : text;
        }
 
-       /**
-        * <p>getTaxonInteractionText</p>
-        *
-        * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static  String getTaxonInteractionText(TaxonInteraction element) {
                String text = "";
                Taxon taxon2 = element.getTaxon2();
@@ -664,12 +613,6 @@ public class DescriptionHelper {
                return text;
        }
 
-       /**
-        * <p>getCommonNameText</p>
-        *
-        * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static  String getCommonNameText(CommonTaxonName commonName) {
                String name = commonName.getName();
                if (name == null || name.length() == 0) {
@@ -679,17 +622,11 @@ public class DescriptionHelper {
                if (language == null) {
                        name += " (No language provided)";
                } else {
-                       name += " (" + language.getDescription() + ")";
+                       name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
                }
                return name;
        }
 
-       /**
-        * <p>getDescriptionText</p>
-        *
-        * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static  String getDescriptionText(DescriptionBase element) {
                String text = element.getTitleCache();
                if (text == null || text.length() == 0) {
@@ -699,38 +636,25 @@ public class DescriptionHelper {
                return text;
        }
 
-       /**
-        * <p>getIndividualsAssociationText</p>
-        *
-        * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static  String getIndividualsAssociationText(IndividualsAssociation element) {
-               SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
+               SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
                if(derivedUnit != null){
                        return derivedUnit.getTitleCache();
                }
                return "No unit chosen";
        }
 
-       /**
-        * <p>getLabel</p>
-        *
-        * @param element a {@link java.lang.Object} object.
-        * @return a {@link java.lang.String} object.
-        */
        public static String getLabel(Object element){
-           String noLabelString = "[no label]";
                if (element instanceof FeatureNodeContainer){
                        return getFeatureNodeContainerText((FeatureNodeContainer) element);
                }
                else if (element instanceof DescriptionBase) {
-                       return getDescriptionText((DescriptionBase) element);
+                       return getDescriptionText((DescriptionBase<?>) element);
                }
                else if(element instanceof CategoricalData){
                        String categoricalDataText = getCategoricalDataText((CategoricalData) element);
                        if(categoricalDataText==null || categoricalDataText.isEmpty()){
-                           categoricalDataText = noLabelString;
+                           categoricalDataText = NO_LABEL_STRING;
                        }
             return categoricalDataText;
                }
@@ -745,8 +669,8 @@ public class DescriptionHelper {
                }
                else if (element instanceof QuantitativeData) {
                        String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
-                       if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
-                quantitativeDataText = noLabelString;
+                       if(isBlank(quantitativeDataText)){
+                quantitativeDataText = NO_LABEL_STRING;
                        }
             return quantitativeDataText;
                }
@@ -756,16 +680,29 @@ public class DescriptionHelper {
                else if (element instanceof TextData) {
                        return getElementText((TextData) element);
                }
+               else if (element instanceof TemporalData) {
+            String lable = getTemporalDataText((TemporalData) element);
+            if(isBlank(lable)){
+                lable = NO_LABEL_STRING;
+            }
+            return lable;
+        }
                else{
                        return element.toString();
                }
        }
-       
-       private static boolean isNotBlank(String str){
+
+    private static String getTemporalDataText(TemporalData element) {
+        ExtendedTimePeriod period = element.getPeriod();
+        ExtendedTimePeriodFormatter formatter = ExtendedTimePeriodFormatter.NewDefaultInstance();
+        return period == null ? NO_LABEL_STRING : formatter.format(period);
+    }
+
+    private static boolean isNotBlank(String str){
                return StringUtils.isNotBlank(str);
        }
-       
+
        private static boolean isBlank(String str){
                return StringUtils.isBlank(str);
        }
-}
+}
\ No newline at end of file