Merge branch 'develop' into termSearch
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / DescriptionHelper.java
index e3f3ad5aef0bbd112b51d5fec70db62b2030b95e..b54b5d8a4717fd65673a33a37caa5708c68ba1b7 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
 * European Distributed Institute of Taxonomy
@@ -19,10 +18,12 @@ import java.util.Set;
 import org.apache.commons.lang.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.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.Group;
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
@@ -35,6 +36,7 @@ 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;
@@ -44,6 +46,7 @@ import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
 import eu.etaxonomy.cdm.model.description.Distribution;
 import eu.etaxonomy.cdm.model.description.Feature;
 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;
@@ -58,13 +61,13 @@ 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.SpecimenTypeDesignation;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.name.TaxonName;
 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.taxon.Classification;
-import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
@@ -76,7 +79,6 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  *
  * @author p.ciardelli
  * @author n.hoffmann
- * @created 02.04.2009
  */
 public class DescriptionHelper {
 
@@ -90,9 +92,11 @@ public class DescriptionHelper {
         */
        public static String getCache(DescriptionElementBase element,
                        Language language) {
-               
+
                String mainElementLabel= null;
                DescriptionBase<?> descr = element.getInDescription();
+               descr = CdmBase.deproxy(descr, DescriptionBase.class);
+
                if (descr != null){
                        if (descr.isInstanceOf(TaxonDescription.class)){
                                Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
@@ -105,16 +109,18 @@ public class DescriptionHelper {
                                        mainElementLabel = specimen.getTitleCache();
                                }
                        }else if (descr.isInstanceOf(TaxonNameDescription.class)){
-                               TaxonNameBase<?, ?> name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
+                               TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
                                if (name != null){
                                        mainElementLabel = name.getTitleCache();
                                }
                        }
                }
-               
+
+
                String cache = null;
                if (element instanceof TextData) {
-                       cache = ((TextData) element).getText(language);
+                       //cache = ((TextData) element).getText(language);
+                   cache = "Text Data";
                }
                if (element instanceof CommonTaxonName) {
                        cache = ((CommonTaxonName) element).getName();
@@ -144,7 +150,7 @@ public class DescriptionHelper {
                        }
                }
                String result = cache == null ? "" : cache;
-               if (StringUtils.isNotBlank(mainElementLabel)){
+               if (isNotBlank(mainElementLabel)){
                        result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
                }
                return result;
@@ -207,19 +213,20 @@ public class DescriptionHelper {
         * @return a {@link java.lang.String} object.
         */
        public static String getObjectDescription(Object element) {
+           String resultString = null;
                if (element instanceof IdentifiableEntity) {
                        try{
-                               return ((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);
-                               return "TODO: " + result;
+                               resultString = "TODO: " + result;
                        }
                }else if (element instanceof OriginalSourceBase) {
                        try{
                                OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
 //                             ISourceable sourcedObject = originalSource.getSourcedObj();
-                               //due to #5743 the bidirectionality for sourced object had to be removed 
+                               //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();
@@ -229,71 +236,133 @@ public class DescriptionHelper {
 //                                     throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
 //                             }
 
-                               return CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
+                               resultString = 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;
+                               resultString = "TODO: " + result;
                        }
                }else if (element instanceof LanguageStringBase) {
-                       return ((LanguageStringBase) element).getText();
+                   resultString = ((LanguageStringBase) element).getText();
                }else if (element instanceof DescriptionElementBase) {
-                       return getCache((DescriptionElementBase) element);
+                   resultString = getCache((DescriptionElementBase) element);
                }else if (element instanceof RelationshipBase<?, ?, ?>) {
-                       return getCache((RelationshipBase<?, ?, ?>) element);
+                   resultString = getCache((RelationshipBase<?, ?, ?>) element);
                }else if (element instanceof TypeDesignationBase<?>) {
-                       return getCache((TypeDesignationBase<?>) element);
+                   resultString = getCache((TypeDesignationBase<?>) element);
                }else if (element instanceof HomotypicalGroup) {
-                       return getCache((HomotypicalGroup) element);
+                   resultString = getCache((HomotypicalGroup) element);
                }else if (element instanceof TaxonNode) {
-                       return getCache((TaxonNode) element);
+                   resultString = getCache((TaxonNode) element);
                }else if (element instanceof DeterminationEvent) {
-                       return getCache((DeterminationEvent) element);
+                   resultString = getCache((DeterminationEvent) element);
+               }else if (element instanceof GatheringEvent){
+                       resultString = getCache((GatheringEvent) element);
                }else if (element instanceof Marker) {
                        Marker marker = (Marker) element;
                        MarkerType type = marker.getMarkerType();
-                       return (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
+                       resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
                }else if (element instanceof User) {
                        User user = (User) element;
-                       return user.getUsername();
+                       resultString = user.getUsername();
                }else if (element instanceof Group) {
                        Group group = (Group) element;
-                       return group.getName();
-               }else{
+                       resultString = group.getName();
+               }else if (element instanceof KeyStatement) {
+            KeyStatement keyStatement = (KeyStatement) element;
+            resultString = getCache(keyStatement);
+        }else{
                        // TODO write return texts for HomotypicalGroup, etc.
-                       return element.toString();
+            resultString = element.toString();
+               }
+
+               if (resultString == null){
+                   resultString = element.toString();
                }
+               return resultString;
+       }
+       
+       private static String getCache(GatheringEvent gatheringEvent){
+               String ALTITUDE_PREFIX = "alt. ";
+               final String METER = "m";
+
+               String result = "";
+
+               //collector
+               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();
+               Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
+               strCountry = repCountry == null ? null: repCountry.getLabel();
+               result = CdmUtils.concat(", ", result, strCountry);
+
+               //locality
+               LanguageString locality = gatheringEvent.getLocality();
+               if (locality != null) {
+                       result = CdmUtils.concat(", ", result, locality.getText());
+               }
+
+               //elevation
+               String elevationStr;
+               if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
+                       elevationStr = gatheringEvent.getAbsoluteElevationText();
+               }else{
+                       String text = gatheringEvent.getAbsoluteElevationText();
+                       Integer min = gatheringEvent.getAbsoluteElevation();
+                       Integer max = gatheringEvent.getAbsoluteElevationMax();
+                       elevationStr = DerivedUnitFacade.distanceString(min, max, text, METER);
+               }
+               if (isNotBlank(elevationStr)){
+                       result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
+                       result += elevationStr;
+               }
+
+               //exact locality
+               if (gatheringEvent.getExactLocation() != null){
+                       String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
+                       result = CdmUtils.concat(", ", result, exactLocation);
+               }
+
+               return result;
        }
 
 
        private static String getCache(DeterminationEvent detEvent) {
                //taxon
                String taxonStr = null;
-               TaxonNameBase<?,?> taxonName = detEvent.getTaxonName();
+               TaxonName taxonName = detEvent.getTaxonName();
                TaxonBase<?> taxon = detEvent.getTaxon();
                if (taxonName != null){
                        taxonStr = taxonName.getTitleCache();
                }
-               if (StringUtils.isBlank(taxonStr) && taxon != null){
+               if (isBlank(taxonStr) && taxon != null){
                        taxonStr = taxon.getTitleCache();
                }
-               if (StringUtils.isBlank(taxonStr)){
+               if (isBlank(taxonStr)){
                        taxonStr = "no or unlabled taxon";
                }
-                       
-                       
+
+
                //unit
                SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
                String unitStr;
                if (unit != null){
                        unitStr = unit.getTitleCache();
-                       if (StringUtils.isBlank(unitStr)){
+                       if (isBlank(unitStr)){
                                unitStr = "Unlabled unit";
                        }
                }else{
                        unitStr = "no unit";
                }
-               
+
                String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
 
                return result;
@@ -305,7 +374,7 @@ public class DescriptionHelper {
                if (classification != null){
                        String classificationStr = classification.getName() == null ? "" : classification.getName().getText();
                        result = CdmUtils.concat("" , result, classificationStr);
-                       if (StringUtils.isBlank(result)){
+                       if (isBlank(result)){
                                result = classification.toString();
                        }
                }
@@ -318,7 +387,7 @@ public class DescriptionHelper {
                        if (parentTaxon == null){
                                parentStr = "no parent taxon";
                        }else{
-                               TaxonNameBase<?,?> parentName = parentTaxon.getName();
+                               TaxonName parentName = parentTaxon.getName();
                                if (parentName == null){
                                        parentStr = "child of " + parentTaxon.getTitleCache();
                                }else{
@@ -327,20 +396,20 @@ public class DescriptionHelper {
                        }
                }
                result = CdmUtils.concat(": ", result, parentStr);
-               
+
                return result;
        }
 
        private static String getCache(TypeDesignationBase<?> designation) {
                designation = CdmBase.deproxy(designation);
                TypeDesignationStatusBase<?> status = designation.getTypeStatus();
-               Set<TaxonNameBase> from;
+               Set<TaxonName> from;
                IdentifiableEntity<?> to;
                from = designation.getTypifiedNames();
                if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
                        to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
                }else if (designation.isInstanceOf(NameTypeDesignation.class)){
-                       to = ((NameTypeDesignation)designation).getTypeName();  
+                       to = ((NameTypeDesignation)designation).getTypeName();
                }else{
                        throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
                }
@@ -350,24 +419,24 @@ public class DescriptionHelper {
                        if (typeRepr != null){
                                typeLabel = typeRepr.getAbbreviatedLabel();
                        }
-                       if (StringUtils.isBlank(typeLabel) && typeRepr != null){
+                       if (isBlank(typeLabel) && typeRepr != null){
                                typeLabel = typeRepr.getLabel();
                        }
-                       if (StringUtils.isBlank(typeLabel) ){
+                       if (isBlank(typeLabel) ){
                                typeLabel = status.getSymbol();
                        }
-                       if (StringUtils.isBlank(typeLabel)){
+                       if (isBlank(typeLabel)){
                                typeLabel = status.getTitleCache();
                        }
                }
-               if (StringUtils.isBlank(typeLabel)){
+               if (isBlank(typeLabel)){
                        typeLabel = "->";
                }
                String fromString = "";
-               for (TaxonNameBase<?,?> name : from){
+               for (TaxonName name : from){
                        CdmUtils.concat(",", fromString, name.getTitleCache());
                }
-               String result = CdmUtils.concat(" ", new String[]{from == null ? null : fromString, 
+               String result = CdmUtils.concat(" ", new String[]{from == null ? null : fromString,
                                typeLabel, to == null? null : to.getTitleCache()});
                return result;
        }
@@ -377,10 +446,7 @@ public class DescriptionHelper {
                RelationshipTermBase<?> type = rel.getType();
                IdentifiableEntity<?> from;
                IdentifiableEntity<?> to;
-               if (rel.isInstanceOf(SynonymRelationship.class)){
-                       from = ((SynonymRelationship)rel).getSynonym();
-                       to = ((SynonymRelationship)rel).getAcceptedTaxon();
-               }else if (rel.isInstanceOf(NameRelationship.class)){
+               if (rel.isInstanceOf(NameRelationship.class)){
                        from = ((NameRelationship)rel).getFromName();
                        to = ((NameRelationship)rel).getToName();
                }else if (rel.isInstanceOf(HybridRelationship.class)){
@@ -408,36 +474,44 @@ public class DescriptionHelper {
                        if (typeRepr != null){
                                typeLabel = typeRepr.getAbbreviatedLabel();
                        }
-                       if (StringUtils.isBlank(typeLabel) && typeRepr != null){
+                       if (isBlank(typeLabel) && typeRepr != null){
                                typeLabel = typeRepr.getLabel();
                        }
-                       if (StringUtils.isBlank(typeLabel) ){
+                       if (isBlank(typeLabel) ){
                                typeLabel = type.getSymbol();
                        }
-                       if (StringUtils.isBlank(typeLabel)){
+                       if (isBlank(typeLabel)){
                                typeLabel = type.getTitleCache();
                        }
                }
-               if (StringUtils.isBlank(typeLabel)){
+               if (isBlank(typeLabel)){
                        typeLabel = "->";
                }
-               String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(), 
+               String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
                                typeLabel, to == null? null : to.getTitleCache()});
                return result;
        }
-       
+
 
        private static String getCache(HomotypicalGroup hg) {
                String result = "";
-               for (TaxonNameBase<?,?> tnb : hg.getTypifiedNames()){
+               for (TaxonName tnb : hg.getTypifiedNames()){
                        result = CdmUtils.concat(", ", result, tnb.getTitleCache());
                }
-               if (StringUtils.isBlank(result)){
+               if (isBlank(result)){
                        result = "No typified names";
                }
                return result;
        }
 
+       private static String getCache(KeyStatement ks) {
+        String result = "";
+
+        result = "KeyStatement";
+
+        return result;
+    }
+
        /**
         * <p>getObjectClassname</p>
         *
@@ -561,7 +635,7 @@ public class DescriptionHelper {
                                        text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
                                }
                        }
-                       if(CdmUtils.isEmpty(text)){
+                       if(isBlank(text)){
                                text = "No sources provided";
                        }
                }else{
@@ -569,7 +643,7 @@ 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;
        }
 
        /**
@@ -686,4 +760,12 @@ public class DescriptionHelper {
                        return element.toString();
                }
        }
+       
+       private static boolean isNotBlank(String str){
+               return StringUtils.isNotBlank(str);
+       }
+       
+       private static boolean isBlank(String str){
+               return StringUtils.isBlank(str);
+       }
 }