ref #9984: add areaLabel to common name label
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / DescriptionHelper.java
index 8b913c614db9f42905532f0758fc2d40c0a459de..c49f3ed0c5218cf1a799a75c3946b92de247a171 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
 * European Distributed Institute of Taxonomy
@@ -7,27 +6,20 @@
 * 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.util.Arrays;
 import java.util.List;
 
-import org.hibernate.LazyInitializationException;
+import org.apache.commons.lang3.StringUtils;
 
-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.model.common.IIdentifiableEntity;
-import eu.etaxonomy.cdm.model.common.ISourceable;
-import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
+import eu.etaxonomy.cdm.format.DescriptionElementFormatter;
+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.common.ExtendedTimePeriod;
 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.description.CategoricalData;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
@@ -39,6 +31,7 @@ import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
 import eu.etaxonomy.cdm.model.description.QuantitativeData;
 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
+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;
@@ -47,15 +40,15 @@ import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
- * <p>DescriptionHelper class.</p>
+ * DescriptionHelper class.
  *
  * @author p.ciardelli
  * @author n.hoffmann
- * @created 02.04.2009
- * @version 1.0
  */
 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.
@@ -64,40 +57,8 @@ public class DescriptionHelper {
         * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
         * @return a {@link java.lang.String} object.
         */
-       public static String getCache(DescriptionElementBase element,
-                       Language language) {
-               String cache = null;
-               if (element instanceof TextData) {
-                       cache = ((TextData) element).getText(language);
-               }
-               if (element instanceof CommonTaxonName) {
-                       cache = ((CommonTaxonName) element).getName();
-               }
-               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) {
-                       Distribution distribution = (Distribution) element;
-
-                       NamedArea area = distribution.getArea();
-                       if(area != null){
-                               cache =  area.getLabel();
-
-                               PresenceAbsenceTerm status = distribution.getStatus();
-                               if (status == null){
-                                       cache += ", no status";
-                               }else {
-                                       cache += ", " + status.getLabel();
-                               }
-                       }
-               }
-               return cache == null ? "" : cache;
+       public static String getCache(DescriptionElementBase element, Language language) {
+           return DescriptionElementFormatter.format(element, language);
        }
 
        /**
@@ -124,21 +85,27 @@ public class DescriptionHelper {
                if (element instanceof TextData) {
                        ((TextData) element).putText(language, value);
                        return;
-               }
-               if (element instanceof CommonTaxonName) {
+               }else if (element instanceof CommonTaxonName) {
                        ((CommonTaxonName) element).setName(value);
                        return;
-               }
-               if (element instanceof TaxonInteraction) {
+               }else if (element instanceof TaxonInteraction) {
 
-               }
-               if(element instanceof Distribution){
+               }else if(element instanceof Distribution){
                        MessagingUtils.warn(DescriptionHelper.class, "trying to set cache on distribution, don't know what to do at the moment.");
                        return;
+               }else{
+                       MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
                }
-               MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
        }
 
+//     public static String getObjectDescription(Object o){
+//         if (o instanceof CdmBase){
+//             return ReferencingObjectFormatter.format((CdmBase)o, CdmStore.getDefaultLanguage());
+//         }else{
+//             return o.toString();
+//         }
+//     }
+
        /**
         * Set whatever the element's title cache equivalent is,
         * depending on its class, using the default language.
@@ -151,127 +118,44 @@ public class DescriptionHelper {
                setCache(element, value, CdmStore.getDefaultLanguage());
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.IReferencingObjectsService#getObjectDescription(java.lang.Object)
-        */
-       /**
-        * <p>getObjectDescription</p>
-        *
-        * @param element a {@link java.lang.Object} object.
-        * @return a {@link java.lang.String} object.
-        */
-       public static String getObjectDescription(Object element) {
-               if (element instanceof IdentifiableEntity) {
-                       try{
-                               return ((IdentifiableEntity) element).getTitleCache();
-                       }catch(LazyInitializationException e){
-                               String result = "No Session to initialize title cache for IdentifiableEntity";
-                               MessagingUtils.error(DescriptionHelper.class, result, e);
-                               return "TODO: " + result;
-                       }
-               }
-               if (element instanceof OriginalSourceBase) {
-                       try{
-                               OriginalSourceBase originalSource = (OriginalSourceBase) element;
-                               ISourceable sourcedObject = originalSource.getSourcedObj();
-                               String sourceObjectTitle = "";
-                               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");
-                               }
-
-                               return CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
-                       }catch(LazyInitializationException e){
-                               String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
-                               MessagingUtils.error(DescriptionHelper.class, result, e);
-                               return "TODO: " + result;
-                       }
-               }
-               if (element instanceof LanguageStringBase) {
-                       return ((LanguageStringBase) element).getText();
-               }
-               if (element instanceof DescriptionElementBase) {
-                       return getCache((DescriptionElementBase) element);
-               }
-               if (element instanceof Marker) {
-                       Marker marker = (Marker) element;
-                       MarkerType type = marker.getMarkerType();
-                       return (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
-               }
-               // TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
-               return element.toString();
-       }
-
-       /**
-        * <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();
-       }
+//     public static String getObjectClassname(Object element) {
+//             if (element instanceof CdmBase){
+//                 return ((CdmBase) element).getUserFriendlyTypeName();
+//             }else{
+//                 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().getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();
+                       
                } 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";
@@ -286,7 +170,7 @@ public class DescriptionHelper {
                        PresenceAbsenceTerm status = distribution.getStatus();
 
                        if (status != null) {
-                               text += ", " + status.getLabel();
+                               text += ", " + status.getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();
                        }else{
                                text += ", NO STATUS";
                        }
@@ -294,12 +178,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) {
@@ -308,14 +186,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())){
@@ -325,10 +195,10 @@ public class DescriptionHelper {
                                        text += source.getCitation().getTitleCache();
                                }
                                if(source.getNameUsedInSource() != null){
-                                       text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
+                                       text += " [" + source.getNameUsedInSource().getTitleCache() + "]";
                                }
                        }
-                       if(CdmUtils.isEmpty(text)){
+                       if(isBlank(text)){
                                text = "No sources provided";
                        }
                }else{
@@ -336,15 +206,9 @@ public class DescriptionHelper {
                        LanguageString languageString = element.getPreferredLanguageString(languages);
                        text = languageString != null ? languageString.getText() : null;
                }
-               return CdmUtils.isEmpty(text) ? "No text provided" : text;
+               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();
@@ -357,70 +221,68 @@ 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) {
                        name = "No name provided";
                }
                Language language = commonName.getLanguage();
-               if (language == null) {
-                       name += " (No language provided)";
-               } else {
-                       name += " (" + language.getDescription() + ")";
+               NamedArea area = commonName.getArea();
+               String areaLabel = null;
+               String languageLabel = null;
+               if (area != null){
+                       areaLabel = area.getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();                  
+               } else {                        
+                       languageLabel = language.getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();
+               }
+               if (areaLabel != null || languageLabel != null){
+                       name += " (" + languageLabel;
+                       if (languageLabel != null){
+                               name += languageLabel;
+                       }
+                       if (areaLabel != null){
+                               if (language != null){
+                                       name += ", ";
+                               }
+                               name += areaLabel;
+                       }
+                       name +=  ")";
                }
+               
+               
                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) {
                        text = "No label provided";
                }
 
-               return "Description: " + text;
+               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 text provided";
+               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){
                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){
-                       return getCategoricalDataText((CategoricalData) element);
+                       String categoricalDataText = getCategoricalDataText((CategoricalData) element);
+                       if(categoricalDataText==null || categoricalDataText.isEmpty()){
+                           categoricalDataText = NO_LABEL_STRING;
+                       }
+            return categoricalDataText;
                }
                else if (element instanceof CommonTaxonName) {
                        return getCommonNameText((CommonTaxonName) element);
@@ -432,7 +294,11 @@ public class DescriptionHelper {
                        return getIndividualsAssociationText((IndividualsAssociation) element);
                }
                else if (element instanceof QuantitativeData) {
-                       return getQuantitativeDataText((QuantitativeData) element);
+                       String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
+                       if(isBlank(quantitativeDataText)){
+                quantitativeDataText = NO_LABEL_STRING;
+                       }
+            return quantitativeDataText;
                }
                else if (element instanceof TaxonInteraction) {
                        return getTaxonInteractionText((TaxonInteraction) element);
@@ -440,8 +306,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 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