Merge branch 'release/5.11.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / CharacterMatrixConfigLabelAccumulator.java
index 084eab571b8822cdc889ab4605ec329105c1b142..01bbd805420648361178c2e414df73c95e5c039e 100644 (file)
@@ -8,12 +8,22 @@
 */
 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.taxon.TaxonNode;
+import eu.etaxonomy.cdm.model.description.QuantitativeData;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 
 /**
  * @author pplitzner
@@ -22,6 +32,8 @@ import eu.etaxonomy.cdm.model.taxon.TaxonNode;
  */
 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";
@@ -39,17 +51,46 @@ public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumu
         boolean isEditable = true;
 
         Object rowObject = matrix.getBodyDataProvider().getRowObject(rowPosition);
-        if(rowObject instanceof TaxonNode){
+        if(rowObject instanceof TaxonNodeDto){
             configLabels.addLabel(CharacterMatrix.LABEL_TAXON_ROW);
         }
         else if(rowObject instanceof TaxonRowWrapperDTO){
-            if(MatrixUtility.isEntityComputed(((TaxonRowWrapperDTO) rowObject).getDescription())){
+            TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO) rowObject;
+            if(MatrixUtility.isAggregatedTaxonDescription(taxonRowWrapper)){
                 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION);
                 isEditable = false;
+                if(columnPosition==0){
+                    configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION_ICON);
+                }
+            }
+            else if(MatrixUtility.isDefaultTaxonDescription(taxonRowWrapper)){
+                configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION);
+                if(columnPosition==0){
+                    configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION_ICON);
+                }
+            }
+            else if(MatrixUtility.isLiteratureTaxonDescription(taxonRowWrapper)){
+                configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION);
+                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);
+                        }
+                    }
+                }
             }
-            else{
-                configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DESCRIPTION);
+            //check for supplemental data
+            if(!taxonRowWrapper.getDescription().getSources().isEmpty() && columnPosition==0){
+                configLabels.addLabel(CharacterMatrix.LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA);
             }
+            configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DESCRIPTION);
         }
 
         if(columnPosition==0){
@@ -67,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){
@@ -81,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;
+    }
 }