Remove dot at the end of derivative labels
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / derivateSearch / DerivateLabelProvider.java
index 06116c3f3156d6e04dd715443c4ea606aca67177..91c08921aea19b6bc5f2b6d99f16d2910685c3dd 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2013 EDIT
 * European Distributed Institute of Taxonomy
@@ -27,21 +26,25 @@ import org.hibernate.LazyInitializationException;
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.format.CdmFormatterFactory;
+import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.DefinedTerm;
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
 import eu.etaxonomy.cdm.model.common.Identifier;
 import eu.etaxonomy.cdm.model.molecular.DnaSample;
 import eu.etaxonomy.cdm.model.molecular.Sequence;
 import eu.etaxonomy.cdm.model.molecular.SingleRead;
 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
+import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
 import eu.etaxonomy.taxeditor.model.ImageResources;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
@@ -60,7 +63,7 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
 
     private static Set<SingleRead> multiLinkSingleReads;
 
-    private static Map<DerivedUnit, List<SpecimenTypeDesignation>> typeDesignations;
+    private static Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignations;
 
     private ConversationHolder conversation;
 
@@ -131,7 +134,80 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
     /** {@inheritDoc} */
     @Override
     public String getText(Object element) {
-        return getDerivateText(element);
+        if(element instanceof TreeNode){
+            element = ((TreeNode) element).getValue();
+        }
+        String text = null;
+
+        //check if collection code does not exist -> use collection name then
+        FormatKey collectionKey = FormatKey.COLLECTION_CODE;
+        text = CdmFormatterFactory.format(element,new FormatKey[]{FormatKey.COLLECTION_CODE});
+        if(CdmUtils.isBlank(text)){
+            collectionKey = FormatKey.COLLECTION_NAME;
+        }
+
+        //Use titlecache for FieldUnits
+        if(element instanceof FieldUnit){
+            text = ((FieldUnit) element).getTitleCache();
+        }
+        else if(element instanceof MediaSpecimen){
+            text = CdmFormatterFactory.format(element,
+                    new FormatKey[]{
+                            FormatKey.MEDIA_TITLE_CACHE, FormatKey.SPACE,
+                            collectionKey, FormatKey.SPACE,
+                            FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE,
+                            FormatKey.MEDIA_TITLE, FormatKey.SPACE,
+                            FormatKey.MEDIA_ARTIST, FormatKey.SPACE,
+                            });
+        }
+        else if (element instanceof DnaSample) {
+            text = CdmFormatterFactory.format(element,
+                    new FormatKey[] {
+                            collectionKey, FormatKey.SPACE,
+                            FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE,
+                            FormatKey.SAMPLE_DESIGNATION, FormatKey.SPACE
+                            });
+        }
+        else if (element instanceof DerivedUnit) {
+            text = CdmFormatterFactory.format(element,
+                    new FormatKey[] {
+                            collectionKey, FormatKey.SPACE,
+                            FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE
+                            });
+        }
+        else if (element instanceof Sequence) {
+            text = CdmFormatterFactory.format(element,
+                    new FormatKey[] {
+                            FormatKey.SEQUENCE_DNA_MARKER, FormatKey.SPACE
+                            });
+        }
+        else if (element instanceof SingleRead) {
+            text = CdmFormatterFactory.format(element,
+                    new FormatKey[] {
+                            FormatKey.SINGLE_READ_PHEROGRAM_TITLE_CACHE, FormatKey.SPACE,
+                            FormatKey.AMPLIFICATION_LABEL, FormatKey.SPACE,
+            });
+        }
+        else if(element instanceof IdentifiableEntity){
+
+               IdentifiableEntity identifiableEntity = (IdentifiableEntity) element;
+               if(identifiableEntity.isProtectedTitleCache()){
+                       text = identifiableEntity.getTitleCache();
+               }
+        }
+        if(CdmUtils.isBlank(text) || text.equals(IdentifiableEntityDefaultCacheStrategy.TITLE_CACHE_GENERATION_NOT_IMPLEMENTED)){
+            if(element instanceof CdmBase){
+                text = ((CdmBase) element).getUuid().toString();
+            }
+            else{
+                text = element.toString();
+            }
+        }
+        //remove dot at the end
+        if(text.endsWith(".")){
+            text = text.substring(0, text.length()-1);
+        }
+               return text;
     }
 
     /** {@inheritDoc} */
@@ -230,8 +306,8 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
             DerivedUnit derivedUnit = (DerivedUnit)derivate;
             if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
                 //check for type designation
-                if(typeDesignations.isEmpty()){
-                    for (SpecimenTypeDesignation specimenTypeDesignation : CdmStore.getService(IOccurrenceService.class).listTypeDesignations(derivedUnit, null, null, null, null)) {
+                if(typeDesignations.get(derivedUnit)==null){
+                    for (SpecimenTypeDesignation specimenTypeDesignation : derivedUnit.getSpecimenTypeDesignations()) {
                         addTypeDesignation(derivedUnit, specimenTypeDesignation);
                     }
                 }
@@ -256,20 +332,6 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
                 }
                 String mostSignificantIdentifier = derivedUnit.getMostSignificantIdentifier();
                 label += mostSignificantIdentifier!=null?mostSignificantIdentifier+separator:emptyString;
-                //type designation extension
-                List<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
-                if(list!=null){
-                    for (SpecimenTypeDesignation specimenTypeDesignation : list) {
-                        label += "("+specimenTypeDesignation.getTypeStatus()+" of ";
-                        for (TaxonNameBase taxonNameBase : specimenTypeDesignation.getTypifiedNames()) {
-                            label += taxonNameBase+separator;
-                        }
-                        if(label.endsWith(separator)){
-                            label = label.substring(0, label.length()-separator.length());
-                        }
-                        label += ")";
-                    }
-                }
             }
             else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
                 //TissueSample should only be created by using it's own class
@@ -345,6 +407,7 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
             }
             else if(cdmBase.isInstanceOf(DerivedUnit.class)){
                 DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(element, DerivedUnit.class);
+
                 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
                     return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
                 }
@@ -355,9 +418,13 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
                     return hasCharacterData?ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
                 }
                 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
+                    if(typeDesignations.get(derivedUnit)!=null && !typeDesignations.get(derivedUnit).isEmpty()){
+                        return ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_TYPE);
+                    }
                     return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
                 }
-                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
+                else if(derivedUnit.getRecordBasis().isMedia()
+                        || derivedUnit.getRecordBasis().isKindOf(SpecimenOrObservationType.Media)){
                     if(derivedUnit.getKindOfUnit()!=null){
                         if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
                             return hasCharacterData?ImageResources.getImage(ImageResources.ARTWORK_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
@@ -387,7 +454,7 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
                 }
             }
         }
-        return super.getImage(element);
+        return ImageResources.getImage(ImageResources.DEFAULT_DERIVATIVE);
     }
 
     public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
@@ -409,22 +476,8 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
         return null;
     }
 
-    /**
-     * Refreshes cached label extensions
-     * @param multiLinkSingleReads
-     */
-    public void refresh() {
-        this.multiLinkSingleReads = new HashSet<SingleRead>();
-        for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
-            if(entry.getValue().size()>1){
-                multiLinkSingleReads.add(entry.getKey());
-            }
-        }
-        this.typeDesignations = new HashMap<DerivedUnit, List<SpecimenTypeDesignation>>();
-    }
-
     private static void addTypeDesignation(DerivedUnit derivedUnit, SpecimenTypeDesignation typeDesignation){
-        List<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
+        Collection<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
         if(list==null){
             list = new ArrayList<SpecimenTypeDesignation>();
         }
@@ -436,4 +489,34 @@ public class DerivateLabelProvider extends ColumnLabelProvider {
         return multiLinkSingleReads;
     }
 
+    public void updateLabelCache(Collection<SpecimenOrObservationBase<?>> rootElements) {
+        multiLinkSingleReads = new HashSet<SingleRead>();
+        typeDesignations = new HashMap<DerivedUnit, Collection<SpecimenTypeDesignation>>();
+        for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
+            if(entry.getValue().size()>1){
+                multiLinkSingleReads.add(entry.getKey());
+            }
+        }
+        if(rootElements!=null){
+            Collection<DerivedUnit> derivedUnits = new ArrayList<DerivedUnit>();
+            for (SpecimenOrObservationBase specimenOrObservationBase : rootElements) {
+                List<DerivedUnit> childUnits = CdmStore.getService(IOccurrenceService.class).getAllChildDerivatives(specimenOrObservationBase.getUuid());
+                if (childUnits != null){
+                    derivedUnits.addAll(childUnits);
+                }
+                if(specimenOrObservationBase.isInstanceOf(DerivedUnit.class)){
+                    specimenOrObservationBase = CdmStore.getService(IOccurrenceService.class).load(specimenOrObservationBase.getUuid());
+                    if (specimenOrObservationBase != null){
+                        derivedUnits.add(HibernateProxyHelper.deproxy(specimenOrObservationBase, DerivedUnit.class));
+                    }
+                }
+            }
+            for (DerivedUnit derivedUnit : derivedUnits) {
+                if(!derivedUnit.getSpecimenTypeDesignations().isEmpty()){
+                    typeDesignations.put(derivedUnit, derivedUnit.getSpecimenTypeDesignations());
+                }
+            }
+        }
+    }
+
 }