ref #7196 Check existing data when adding specimens to matrix
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 17 Jan 2018 10:37:18 +0000 (11:37 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 18 Jan 2018 11:04:24 +0000 (12:04 +0100)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/CharacterMatrix.java

index 58dc28662fa1d89a0e87e514360f93ce7c8a5479..78089ebf50c4b1812aee8ff60bf62385e4b1fd5b 100644 (file)
@@ -123,6 +123,7 @@ import eu.etaxonomy.taxeditor.editor.workingSet.matrix.categorical.CategoricalDa
 import eu.etaxonomy.taxeditor.editor.workingSet.matrix.quantitative.QuantitativeDataCellEditor;
 import eu.etaxonomy.taxeditor.editor.workingSet.matrix.quantitative.QuantitativeDataDisplayConverter;
 import eu.etaxonomy.taxeditor.editor.workingSet.matrix.supplementalInfo.SupplementalInfoDisplayConverter;
+import eu.etaxonomy.taxeditor.model.DescriptionHelper;
 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
 import eu.etaxonomy.taxeditor.model.ImageResources;
@@ -591,23 +592,49 @@ public class CharacterMatrix implements IE4SavablePart, IPartContentHasDetails,
     }
 
     private SpecimenDescription getDescriptionForWorkingSet(SpecimenOrObservationBase specimen){
-        Set<SpecimenDescription> descriptions = specimen.getDescriptions();
-        if(descriptions!=null){
-            Set<Feature> features = workingSet.getDescriptiveSystem().getDistinctFeatures();
-            for (SpecimenDescription specimenDescription : descriptions) {
-                Set<Feature> specimenDescriptionFeatures = new HashSet<>();
-                for (DescriptionElementBase descriptionElementBase : specimenDescription.getElements()) {
-                    specimenDescriptionFeatures.add(descriptionElementBase.getFeature());
-                }
-                if(specimenDescriptionFeatures.containsAll(features)){
-                    return specimenDescription;
+        Set<Feature> wsFeatures = workingSet.getDescriptiveSystem().getDistinctFeatures();
+        List<DescriptionElementBase> matchingDescriptionElements = new ArrayList<>();
+
+        for (SpecimenDescription specimenDescription : (Set<SpecimenDescription>) specimen.getDescriptions()) {
+            Set<Feature> specimenDescriptionFeatures = new HashSet<>();
+            //gather specimen description features and check for match with WS features
+            for (DescriptionElementBase specimenDescriptionElement : specimenDescription.getElements()) {
+                Feature feature = specimenDescriptionElement.getFeature();
+                specimenDescriptionFeatures.add(feature);
+                if(wsFeatures.contains(feature)){
+                    matchingDescriptionElements.add(specimenDescriptionElement);
                 }
             }
+            //if description with the exact same features is found return the description
+            if(specimenDescriptionFeatures.equals(wsFeatures)){
+                return specimenDescription;
+            }
         }
         //Create new specimen description if no match was found
         setDirty();
         SpecimenDescription newDesription = SpecimenDescription.NewInstance(specimen);
-        newDesription.setTitleCache("WorkingSet "+workingSet.getLabel()+" "+newDesription.generateTitle(), true);
+        newDesription.setTitleCache("WorkingSet "+workingSet.getLabel()+": "+newDesription.generateTitle(), true);
+
+        //check for equals description element (same feature and TODO: same values)
+        matchingDescriptionElements.forEach(element ->
+        {
+            if(matchingDescriptionElements.contains(element)){
+                MessagingUtils.informationDialog("Multiple data found",
+                        String.format("Multiple description elements"
+                                //                             + " with different values "
+                                + "found for feature %s. "
+                                + "Data will not be copied to new specimen description", element.getFeature().getLabel()));
+            }
+            else{
+                DescriptionElementBase clone;
+                try {
+                    clone = element.clone(newDesription);
+                    clone.getSources().forEach(source -> source.setOriginalNameString(DescriptionHelper.getLabel(element)));
+                } catch (CloneNotSupportedException e) {
+                    MessagingUtils.error(CharacterMatrix.class, e);
+                }
+            }
+        });
         return newDesription;
 
     }