Merge branch 'release/5.11.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / CharacterMatrixConfigLabelAccumulator.java
index 48fc5b7f22a3ba373f06d43e7ccb49b6dcc176aa..01bbd805420648361178c2e414df73c95e5c039e 100644 (file)
@@ -8,11 +8,21 @@
 */
 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
 
+import java.util.Optional;
+import java.util.Set;
+
 import org.eclipse.nebula.widgets.nattable.layer.LabelStack;
 import org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator;
 
+import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
+import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
 import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
+import eu.etaxonomy.cdm.model.description.CategoricalData;
+import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
+import eu.etaxonomy.cdm.model.description.DescriptionType;
 import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.QuantitativeData;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 
 /**
@@ -22,6 +32,8 @@ import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
  */
 public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumulator {
 
+    public static final String HAS_DEFAULT = "HAS_DEFAULT";
+    public static final String DEFAULT_OVERRIDDEN = "DEFAULT_OVERRIDDEN";
     public static final String QUANTITATIVE = "QUANTITATIVE";
     public static final String CATEGORICAL = "CATEGORICAL";
     public static final String CATEGORICAL_EDITABLE = CATEGORICAL+"_EDITABLE";
@@ -62,6 +74,17 @@ public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumu
                 if(columnPosition==0){
                     configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION_ICON);
                 }
+                // check for existing default values
+                if(columnPosition>=CharacterMatrix.LEADING_COLUMN_COUNT){
+                    Feature feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
+                    if(hasDefaultValue(feature, taxonRowWrapper)){
+                        configLabels.addLabel(HAS_DEFAULT);
+                        if(hasDefaultOverriddenValue(feature, taxonRowWrapper)){
+                            configLabels.removeLabel(HAS_DEFAULT);
+                            configLabels.addLabel(DEFAULT_OVERRIDDEN);
+                        }
+                    }
+                }
             }
             //check for supplemental data
             if(!taxonRowWrapper.getDescription().getSources().isEmpty() && columnPosition==0){
@@ -85,6 +108,18 @@ public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumu
         else{
             Feature feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
             configLabels.addLabel(MatrixUtility.getProperty(feature));
+            // check for default values
+            if(rowObject instanceof SpecimenRowWrapperDTO){
+                SpecimenRowWrapperDTO specimenRowWrapper = (SpecimenRowWrapperDTO)rowObject;
+                if(hasDefaultValue(feature, specimenRowWrapper)){
+                    configLabels.addLabel(HAS_DEFAULT);
+                    if(hasDefaultOverriddenValue(feature, specimenRowWrapper)){
+                        configLabels.removeLabel(HAS_DEFAULT);
+                        configLabels.addLabel(DEFAULT_OVERRIDDEN);
+                    }
+                }
+            }
+
             if(feature.isSupportsCategoricalData()){
                 configLabels.addLabel(CATEGORICAL);
                 if(isEditable){
@@ -99,4 +134,51 @@ public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumu
             }
         }
     }
+
+    private boolean hasDefaultOverriddenValue(Feature feature, RowWrapperDTO<?> rowWrapper) {
+        DescriptionElementBase dataValueForFeature = rowWrapper.getDataValueForFeature(feature);
+        if(dataValueForFeature!=null){
+            if(dataValueForFeature instanceof CategoricalData){
+                return !((CategoricalData) dataValueForFeature).getStatesOnly().isEmpty();
+            }
+            else if(dataValueForFeature instanceof QuantitativeData){
+                return !((QuantitativeData) dataValueForFeature).getStatisticalValues().isEmpty();
+            }
+        }
+        return false;
+    }
+
+    private boolean hasDefaultValue(Feature feature, RowWrapperDTO<?> rowWrapperDTO) {
+        if(rowWrapperDTO instanceof SpecimenRowWrapperDTO
+                && ((SpecimenRowWrapperDTO) rowWrapperDTO).getDefaultDescription()!=null){
+            return hasDefaultValue(feature, ((SpecimenRowWrapperDTO)rowWrapperDTO).getDefaultDescription().getDescription());
+        }
+        else if(rowWrapperDTO instanceof TaxonRowWrapperDTO){
+            TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO)rowWrapperDTO;
+            Set<TaxonDescription> descriptions = taxonRowWrapper.getDescription().getTaxon().getDescriptions();
+            for (TaxonDescription taxonDescription : descriptions) {
+                if(matrix.getDescriptiveDataSet().getDescriptions().contains(taxonDescription)
+                        && taxonDescription.getTypes().contains(DescriptionType.DEFAULT_VALUES_FOR_AGGREGATION)){
+                    return hasDefaultValue(feature, taxonDescription);
+                }
+            }
+        }
+        return false;
+    }
+
+    private boolean hasDefaultValue(Feature feature, TaxonDescription defaultDescription) {
+        if(defaultDescription!=null){
+            Optional<DescriptionElementBase> descriptionElement = defaultDescription.getElements().stream()
+                    .filter(element->element.getFeature().equals(feature))
+                    .findAny();
+            if(descriptionElement.isPresent() && descriptionElement.get() instanceof CategoricalData){
+                return !((CategoricalData) descriptionElement.get()).getStatesOnly().isEmpty();
+            }
+            else if(descriptionElement.isPresent() && descriptionElement.get() instanceof QuantitativeData){
+                return !((QuantitativeData) descriptionElement.get()).getStatisticalValues().isEmpty();
+            }
+
+        }
+        return false;
+    }
 }