adapt comparators to new model.compare structure (cont.)
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / CharacterMatrixConfigLabelAccumulator.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
10
11 import java.util.Optional;
12 import java.util.Set;
13
14 import org.eclipse.nebula.widgets.nattable.layer.LabelStack;
15 import org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator;
16
17 import eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto;
18 import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
19 import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
20 import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
21 import eu.etaxonomy.cdm.model.description.CategoricalData;
22 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23 import eu.etaxonomy.cdm.model.description.DescriptionType;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.cdm.model.description.QuantitativeData;
26 import eu.etaxonomy.cdm.model.description.TaxonDescription;
27 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
28
29 /**
30 * @author pplitzner
31 * @since Jul 6, 2018
32 *
33 */
34 public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumulator {
35
36 public static final String HAS_DEFAULT = "HAS_DEFAULT";
37 public static final String DEFAULT_OVERRIDDEN = "DEFAULT_OVERRIDDEN";
38 public static final String QUANTITATIVE = "QUANTITATIVE";
39 public static final String CATEGORICAL = "CATEGORICAL";
40 public static final String CATEGORICAL_EDITABLE = CATEGORICAL+"_EDITABLE";
41 public static final String QUANTITATIVE_EDITABLE = QUANTITATIVE+"_EDITABLE";
42
43 private CharacterMatrix matrix;
44
45 public CharacterMatrixConfigLabelAccumulator(CharacterMatrix matrix) {
46 super();
47 this.matrix = matrix;
48 }
49
50 @Override
51 public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
52 boolean isEditable = true;
53
54 Object rowObject = matrix.getBodyDataProvider().getRowObject(rowPosition);
55 if(rowObject instanceof TaxonNodeDto){
56 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_ROW);
57 }
58 else if(rowObject instanceof TaxonRowWrapperDTO){
59 TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO) rowObject;
60 if(MatrixUtility.isAggregatedTaxonDescription(taxonRowWrapper)){
61 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION);
62 isEditable = false;
63 if(columnPosition==0){
64 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION_ICON);
65 }
66 }
67 else if(MatrixUtility.isDefaultTaxonDescription(taxonRowWrapper)){
68 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION);
69 if(columnPosition==0){
70 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION_ICON);
71 }
72 }
73 else if(MatrixUtility.isLiteratureTaxonDescription(taxonRowWrapper)){
74 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION);
75 if(columnPosition==0){
76 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION_ICON);
77 }
78 // check for existing default values
79 if(columnPosition>=CharacterMatrix.LEADING_COLUMN_COUNT){
80 Feature feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
81 if(hasDefaultValue(feature, taxonRowWrapper)){
82 configLabels.addLabel(HAS_DEFAULT);
83 if(hasDefaultOverriddenValue(feature, taxonRowWrapper)){
84 configLabels.removeLabel(HAS_DEFAULT);
85 configLabels.addLabel(DEFAULT_OVERRIDDEN);
86 }
87 }
88 }
89 }
90 //check for supplemental data
91 if(!taxonRowWrapper.getDescription().getDescription().getSources().isEmpty() && columnPosition==0){
92 configLabels.addLabel(CharacterMatrix.LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA);
93 }
94 configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DESCRIPTION);
95 }
96
97 if(columnPosition==0){
98 configLabels.addLabel(CharacterMatrix.TAXON_COLUMN);
99 }
100 else if(columnPosition==1){
101 configLabels.addLabel(CharacterMatrix.COLLECTOR_COLUMN);
102 }
103 else if(columnPosition==2){
104 configLabels.addLabel(CharacterMatrix.IDENTIFIER_COLUMN);
105 }
106 else if(columnPosition==3){
107 configLabels.addLabel(CharacterMatrix.COUNTRY_COLUMN);
108 }
109 else{
110 Feature feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
111 configLabels.addLabel(MatrixUtility.getProperty(feature));
112 // check for default values
113 if(rowObject instanceof SpecimenRowWrapperDTO){
114 SpecimenRowWrapperDTO specimenRowWrapper = (SpecimenRowWrapperDTO)rowObject;
115 if(hasDefaultValue(feature, specimenRowWrapper)){
116 configLabels.addLabel(HAS_DEFAULT);
117 if(hasDefaultOverriddenValue(feature, specimenRowWrapper)){
118 configLabels.removeLabel(HAS_DEFAULT);
119 configLabels.addLabel(DEFAULT_OVERRIDDEN);
120 }
121 }
122 }
123
124 if(feature.isSupportsCategoricalData()){
125 configLabels.addLabel(CATEGORICAL);
126 if(isEditable){
127 configLabels.addLabel(CATEGORICAL_EDITABLE);
128 }
129 }
130 else if(feature.isSupportsQuantitativeData()){
131 configLabels.addLabel(QUANTITATIVE);
132 if(isEditable){
133 configLabels.addLabel(QUANTITATIVE_EDITABLE);
134 }
135 }
136 }
137 }
138
139 private boolean hasDefaultOverriddenValue(Feature feature, RowWrapperDTO<?> rowWrapper) {
140 DescriptionElementBase dataValueForFeature = rowWrapper.getDataValueForFeature(feature);
141 if(dataValueForFeature!=null){
142 if(dataValueForFeature instanceof CategoricalData){
143 return !((CategoricalData) dataValueForFeature).getStatesOnly().isEmpty();
144 }
145 else if(dataValueForFeature instanceof QuantitativeData){
146 return !((QuantitativeData) dataValueForFeature).getStatisticalValues().isEmpty();
147 }
148 }
149 return false;
150 }
151
152 private boolean hasDefaultValue(Feature feature, RowWrapperDTO<?> rowWrapperDTO) {
153 if(rowWrapperDTO instanceof SpecimenRowWrapperDTO
154 && ((SpecimenRowWrapperDTO) rowWrapperDTO).getDefaultDescription()!=null){
155 TaxonDescription taxDescription = (TaxonDescription)((SpecimenRowWrapperDTO)rowWrapperDTO).getDefaultDescription().getDescription().getDescription();
156 return hasDefaultValue(feature, taxDescription);
157 }
158 else if(rowWrapperDTO instanceof TaxonRowWrapperDTO){
159 TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO)rowWrapperDTO;
160 Set<DescriptionBaseDto> descriptions = taxonRowWrapper.getTaxonDescriptions();
161 for (DescriptionBaseDto taxonDescription : descriptions) {
162 if(matrix.getDescriptiveDataSet().getDescriptions().contains(taxonDescription)
163 && taxonDescription.getDescription().getTypes().contains(DescriptionType.DEFAULT_VALUES_FOR_AGGREGATION)){
164 return hasDefaultValue(feature, (TaxonDescription)taxonDescription.getDescription());
165 }
166 }
167 }
168 return false;
169 }
170
171 private boolean hasDefaultValue(Feature feature, TaxonDescription defaultDescription) {
172 if(defaultDescription!=null){
173 Optional<DescriptionElementBase> descriptionElement = defaultDescription.getElements().stream()
174 .filter(element->element.getFeature().equals(feature))
175 .findAny();
176 if(descriptionElement.isPresent() && descriptionElement.get() instanceof CategoricalData){
177 return !((CategoricalData) descriptionElement.get()).getStatesOnly().isEmpty();
178 }
179 else if(descriptionElement.isPresent() && descriptionElement.get() instanceof QuantitativeData){
180 return !((QuantitativeData) descriptionElement.get()).getStatisticalValues().isEmpty();
181 }
182
183 }
184 return false;
185 }
186 }