ref #9816: tooltip and disabled field for taxon descriptions also
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / CharacterMatrixConfigLabelAccumulator.java
index 065e92c93aef6ad42fac1b3d9b74456166bc3717..557ff5db811ff36e514f9c69b8177efb15eb09a9 100644 (file)
@@ -8,12 +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.CategoricalDataDto;
+import eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto;
+import eu.etaxonomy.cdm.api.service.dto.DescriptionElementDto;
+import eu.etaxonomy.cdm.api.service.dto.QuantitativeDataDto;
+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.Feature;
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.persistence.dto.FeatureDto;
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 
 /**
  * @author pplitzner
@@ -22,10 +31,13 @@ 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";
     public static final String QUANTITATIVE_EDITABLE = QUANTITATIVE+"_EDITABLE";
+    public static final String NOT_EDITABLE = "NOT_EDITABLE";
 
     private CharacterMatrix matrix;
 
@@ -39,7 +51,7 @@ 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){
@@ -62,11 +74,22 @@ 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){
+                    FeatureDto 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){
-                configLabels.addLabel(CharacterMatrix.LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA);
-            }
+            //check for supplemental data TODO: add supplemental data to DTO
+//            if(!taxonRowWrapper.getDescription().getSources().isEmpty() && columnPosition==0){
+//                configLabels.addLabel(CharacterMatrix.LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA);
+//            }
             configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DESCRIPTION);
         }
 
@@ -83,20 +106,102 @@ public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumu
             configLabels.addLabel(CharacterMatrix.COUNTRY_COLUMN);
         }
         else{
-            Feature feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
+            FeatureDto 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 (rowObject instanceof RowWrapperDTO){
+                   if (hasMoreThanOneValue(feature, (RowWrapperDTO)rowObject)){
+                       isEditable = false;
+                   }
+            }
+
             if(feature.isSupportsCategoricalData()){
                 configLabels.addLabel(CATEGORICAL);
                 if(isEditable){
                     configLabels.addLabel(CATEGORICAL_EDITABLE);
+                }else{
+                       configLabels.addLabel(NOT_EDITABLE);
                 }
             }
             else if(feature.isSupportsQuantitativeData()){
                 configLabels.addLabel(QUANTITATIVE);
                 if(isEditable){
                     configLabels.addLabel(QUANTITATIVE_EDITABLE);
+                }else{
+                       configLabels.addLabel(NOT_EDITABLE);
                 }
             }
         }
     }
+
+    private boolean hasDefaultOverriddenValue(FeatureDto feature, RowWrapperDTO<?> rowWrapper) {
+        Set<DescriptionElementDto> dataValueForFeature = rowWrapper.getDataValueForFeature(feature.getUuid());
+        if(dataValueForFeature!=null){
+               for (DescriptionElementDto dto:dataValueForFeature){
+                   if(dto instanceof CategoricalDataDto && !((CategoricalDataDto) dto).getStates().isEmpty()){
+                       return true;
+                   }
+                   else if(dto instanceof QuantitativeDataDto && !((QuantitativeDataDto) dto).getValues().isEmpty()){
+                       return true;
+                   }
+               }
+        }
+        return false;
+    }
+
+    private boolean hasDefaultValue(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
+        if(rowWrapperDTO instanceof SpecimenRowWrapperDTO
+                && ((SpecimenRowWrapperDTO) rowWrapperDTO).getDefaultDescription()!=null){
+            DescriptionBaseDto taxDescription = ((SpecimenRowWrapperDTO)rowWrapperDTO).getDefaultDescription().getDescription();
+            return hasDefaultValue(feature, taxDescription);
+        }
+        else if(rowWrapperDTO instanceof TaxonRowWrapperDTO){
+            TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO)rowWrapperDTO;
+            Set<DescriptionBaseDto> descriptions = taxonRowWrapper.getTaxonDescriptions();
+            for (DescriptionBaseDto taxonDescription : descriptions) {
+//                if(matrix.getDescriptiveDataSet().getDescriptions().contains(taxonDescription)
+//                        && taxonDescription.getTypes().contains(DescriptionType.DEFAULT_VALUES_FOR_AGGREGATION)){
+//                    return hasDefaultValue(feature, taxonDescription);
+//                }
+                //TODO: correct implementation
+                return false;
+            }
+        }
+        return false;
+    }
+    private boolean hasMoreThanOneValue(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
+        
+            Set<DescriptionElementDto> elements = rowWrapperDTO.getDataValueForFeature(feature.getUuid());
+            return elements != null && elements.size()>1;
+        
+       
+    }
+
+
+    private boolean hasDefaultValue(FeatureDto feature, DescriptionBaseDto defaultDescription) {
+        if(defaultDescription!=null){
+            Optional<DescriptionElementDto> descriptionElement = defaultDescription.getElements().stream()
+                    .filter(element->element.getFeatureUuid().equals(feature.getUuid()))
+                    .findAny();
+            if(descriptionElement.isPresent() && descriptionElement.get() instanceof CategoricalDataDto){
+                return !((CategoricalDataDto) descriptionElement.get()).getStates().isEmpty();
+            }
+            else if(descriptionElement.isPresent() && descriptionElement.get() instanceof QuantitativeDataDto){
+                return !((QuantitativeDataDto) descriptionElement.get()).getValues().isEmpty();
+            }
+
+        }
+        return false;
+    }
 }