Improve DescriptionHelper / Referencing objects label provider #5835
authorAndreas Müller <a.mueller@bgbm.org>
Thu, 26 May 2016 23:21:24 +0000 (01:21 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Thu, 26 May 2016 23:21:24 +0000 (01:21 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/DescriptionHelper.java

index 735a2df5d0e552c4cdb01628902b1132a61c5857..a241954d1d71eb5f15afc1331fe5237b25ffd908 100644 (file)
 
 package eu.etaxonomy.taxeditor.model;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Set;
 
+import org.apache.commons.lang.StringUtils;
 import org.hibernate.LazyInitializationException;
 
 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.CdmBase;
+import eu.etaxonomy.cdm.model.common.Group;
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.LanguageString;
@@ -28,6 +32,10 @@ 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.User;
 import eu.etaxonomy.cdm.model.description.CategoricalData;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
@@ -38,12 +46,25 @@ import eu.etaxonomy.cdm.model.description.Feature;
 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.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.TextData;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.media.Media;
+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.SpecimenTypeDesignation;
+import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
+import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
@@ -52,7 +73,6 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  * @author p.ciardelli
  * @author n.hoffmann
  * @created 02.04.2009
- * @version 1.0
  */
 public class DescriptionHelper {
 
@@ -66,6 +86,28 @@ public class DescriptionHelper {
         */
        public static String getCache(DescriptionElementBase element,
                        Language language) {
+               
+               String mainElementLabel= null;
+               DescriptionBase<?> descr = element.getInDescription();
+               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)){
+                               TaxonNameBase<?, ?> 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);
@@ -97,7 +139,11 @@ public class DescriptionHelper {
                                }
                        }
                }
-               return cache == null ? "" : cache;
+               String result = cache == null ? "" : cache;
+               if (StringUtils.isNotBlank(mainElementLabel)){
+                       result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
+               }
+               return result;
        }
 
        /**
@@ -124,19 +170,17 @@ 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.");
        }
 
        /**
@@ -151,9 +195,7 @@ public class DescriptionHelper {
                setCache(element, value, CdmStore.getDefaultLanguage());
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.IReferencingObjectsService#getObjectDescription(java.lang.Object)
-        */
+
        /**
         * <p>getObjectDescription</p>
         *
@@ -169,10 +211,9 @@ public class DescriptionHelper {
                                MessagingUtils.error(DescriptionHelper.class, result, e);
                                return "TODO: " + result;
                        }
-               }
-               if (element instanceof OriginalSourceBase) {
+               }else if (element instanceof OriginalSourceBase) {
                        try{
-                               OriginalSourceBase originalSource = (OriginalSourceBase) element;
+                               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)";
@@ -190,20 +231,148 @@ public class DescriptionHelper {
                                MessagingUtils.error(DescriptionHelper.class, result, e);
                                return "TODO: " + result;
                        }
-               }
-               if (element instanceof LanguageStringBase) {
+               }else if (element instanceof LanguageStringBase) {
                        return ((LanguageStringBase) element).getText();
-               }
-               if (element instanceof DescriptionElementBase) {
+               }else if (element instanceof DescriptionElementBase) {
                        return getCache((DescriptionElementBase) element);
-               }
-               if (element instanceof Marker) {
+               }else if (element instanceof RelationshipBase<?, ?, ?>) {
+                       return getCache((RelationshipBase<?, ?, ?>) element);
+               }else if (element instanceof TypeDesignationBase<?>) {
+                       return getCache((TypeDesignationBase<?>) element);
+               }else if (element instanceof HomotypicalGroup) {
+                       return getCache((HomotypicalGroup) 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() + ")";
+               }else if (element instanceof User) {
+                       User user = (User) element;
+                       return user.getUsername();
+               }else if (element instanceof Group) {
+                       Group group = (Group) element;
+                       return group.getName();
+               }else{
+                       // TODO write return texts for HomotypicalGroup, etc.
+                       return element.toString();
+               }
+       }
+
+
+       private static String getCache(TypeDesignationBase<?> designation) {
+               designation = CdmBase.deproxy(designation);
+               TypeDesignationStatusBase<?> status = designation.getTypeStatus();
+               Set<TaxonNameBase> 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();  
+               }else{
+                       throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
                }
-               // TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
-               return element.toString();
+               String typeLabel = null;
+               if (status != null){
+                       Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
+                       if (typeRepr != null){
+                               typeLabel = typeRepr.getAbbreviatedLabel();
+                       }
+                       if (StringUtils.isBlank(typeLabel) && typeRepr != null){
+                               typeLabel = typeRepr.getLabel();
+                       }
+                       if (StringUtils.isBlank(typeLabel) ){
+                               typeLabel = status.getSymbol();
+                       }
+                       if (StringUtils.isBlank(typeLabel)){
+                               typeLabel = status.getTitleCache();
+                       }
+               }
+               if (StringUtils.isBlank(typeLabel)){
+                       typeLabel = status.getTitleCache();
+                       typeLabel = "->";
+               }
+               String fromString = "";
+               for (TaxonNameBase<?,?> name : from){
+                       CdmUtils.concat(",", fromString, name.getTitleCache());
+               }
+               String result = CdmUtils.concat(" ", new String[]{from == null ? null : fromString, 
+                               typeLabel, to == null? null : to.getTitleCache()});
+               return result;
+       }
+
+       private static String getCache(RelationshipBase<?, ?, ?> rel) {
+               rel = CdmBase.deproxy(rel);
+               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)){
+                       from = ((NameRelationship)rel).getFromName();
+                       to = ((NameRelationship)rel).getToName();
+               }else if (rel.isInstanceOf(HybridRelationship.class)){
+                       from = ((HybridRelationship)rel).getParentName();
+                       to = ((HybridRelationship)rel).getHybridName();
+               }else if (rel.isInstanceOf(TaxonRelationship.class)){
+                       from = ((TaxonRelationship)rel).getFromTaxon();
+                       to = ((TaxonRelationship)rel).getToTaxon();
+               }else{
+                       try {
+                               Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
+                               Method toMethod = rel.getClass().getMethod("getRelatedFrom");
+                               fromMethod.setAccessible(true);
+                               toMethod.setAccessible(true);
+                               from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
+                               to = (IdentifiableEntity<?>)toMethod.invoke(rel);
+                       } catch (NoSuchMethodException e) {
+                               throw new RuntimeException(e);
+                       } catch (SecurityException e) {
+                               throw new RuntimeException(e);
+                       } catch (IllegalAccessException e) {
+                               throw new RuntimeException(e);
+                       } catch (IllegalArgumentException e) {
+                               throw new RuntimeException(e);
+                       } catch (InvocationTargetException e) {
+                               throw new RuntimeException(e);
+                       }
+                       
+               }
+               String typeLabel = null;
+               if (type != null){
+                       Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
+                       if (typeRepr != null){
+                               typeLabel = typeRepr.getAbbreviatedLabel();
+                       }
+                       if (StringUtils.isBlank(typeLabel) && typeRepr != null){
+                               typeLabel = typeRepr.getLabel();
+                       }
+                       if (StringUtils.isBlank(typeLabel) ){
+                               typeLabel = type.getSymbol();
+                       }
+                       if (StringUtils.isBlank(typeLabel)){
+                               typeLabel = type.getTitleCache();
+                       }
+               }
+               if (StringUtils.isBlank(typeLabel)){
+                       typeLabel = type.getTitleCache();
+                       typeLabel = "->";
+               }
+               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()){
+                       result = CdmUtils.concat(", ", result, tnb.getTitleCache());
+               }
+               if (StringUtils.isBlank(result)){
+                       result = "No typified names";
+               }
+               return result;
        }
 
        /**