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.Map.Entry;
|
12
|
import java.util.Optional;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.eclipse.nebula.widgets.nattable.layer.LabelStack;
|
16
|
import org.eclipse.nebula.widgets.nattable.layer.cell.IConfigLabelAccumulator;
|
17
|
|
18
|
import eu.etaxonomy.cdm.api.service.dto.CategoricalDataDto;
|
19
|
import eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto;
|
20
|
import eu.etaxonomy.cdm.api.service.dto.DescriptionElementDto;
|
21
|
import eu.etaxonomy.cdm.api.service.dto.QuantitativeDataDto;
|
22
|
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
|
23
|
import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
|
24
|
import eu.etaxonomy.cdm.api.service.dto.StateDataDto;
|
25
|
import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
|
26
|
import eu.etaxonomy.cdm.persistence.dto.FeatureDto;
|
27
|
import eu.etaxonomy.cdm.persistence.dto.FeatureStateDto;
|
28
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
29
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
30
|
import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
|
31
|
|
32
|
/**
|
33
|
* @author pplitzner
|
34
|
* @since Jul 6, 2018
|
35
|
*
|
36
|
*/
|
37
|
public class CharacterMatrixConfigLabelAccumulator implements IConfigLabelAccumulator {
|
38
|
|
39
|
public static final String HAS_DEFAULT = "HAS_DEFAULT";
|
40
|
public static final String DEFAULT_OVERRIDDEN = "DEFAULT_OVERRIDDEN";
|
41
|
public static final String QUANTITATIVE = "QUANTITATIVE";
|
42
|
public static final String CATEGORICAL = "CATEGORICAL";
|
43
|
public static final String CATEGORICAL_EDITABLE = CATEGORICAL+"_EDITABLE";
|
44
|
public static final String QUANTITATIVE_EDITABLE = QUANTITATIVE+"_EDITABLE";
|
45
|
public static final String NOT_EDITABLE = "NOT_EDITABLE";
|
46
|
|
47
|
private CharacterMatrix matrix;
|
48
|
|
49
|
public CharacterMatrixConfigLabelAccumulator(CharacterMatrix matrix) {
|
50
|
super();
|
51
|
this.matrix = matrix;
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
|
56
|
boolean isEditable = true;
|
57
|
|
58
|
Object rowObject = matrix.getBodyDataProvider().getRowObject(rowPosition);
|
59
|
if(rowObject instanceof TaxonNodeDto){
|
60
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_ROW);
|
61
|
}
|
62
|
else if(rowObject instanceof TaxonRowWrapperDTO){
|
63
|
TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO) rowObject;
|
64
|
if(MatrixUtility.isAggregatedTaxonDescription(taxonRowWrapper)){
|
65
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION);
|
66
|
isEditable = false;
|
67
|
if(columnPosition==0){
|
68
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION_ICON);
|
69
|
}
|
70
|
}
|
71
|
else if(MatrixUtility.isDefaultTaxonDescription(taxonRowWrapper)){
|
72
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION);
|
73
|
if(columnPosition==0){
|
74
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION_ICON);
|
75
|
}
|
76
|
}
|
77
|
else if(MatrixUtility.isLiteratureTaxonDescription(taxonRowWrapper)){
|
78
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION);
|
79
|
if(columnPosition==0){
|
80
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION_ICON);
|
81
|
}
|
82
|
// check for existing default values
|
83
|
if(columnPosition>=CharacterMatrix.LEADING_COLUMN_COUNT){
|
84
|
FeatureDto feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
|
85
|
if(hasDefaultValue(feature, taxonRowWrapper)){
|
86
|
configLabels.addLabel(HAS_DEFAULT);
|
87
|
if(hasDefaultOverriddenValue(feature, taxonRowWrapper)){
|
88
|
configLabels.removeLabel(HAS_DEFAULT);
|
89
|
configLabels.addLabel(DEFAULT_OVERRIDDEN);
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
//check for supplemental data TODO: add supplemental data to DTO
|
95
|
// if(!taxonRowWrapper.getDescription().getSources().isEmpty() && columnPosition==0){
|
96
|
// configLabels.addLabel(CharacterMatrix.LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA);
|
97
|
// }
|
98
|
configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DESCRIPTION);
|
99
|
}
|
100
|
|
101
|
if(columnPosition==0){
|
102
|
configLabels.addLabel(CharacterMatrix.TAXON_COLUMN);
|
103
|
}
|
104
|
else if(columnPosition==1){
|
105
|
configLabels.addLabel(CharacterMatrix.COLLECTOR_COLUMN);
|
106
|
}
|
107
|
else if(columnPosition==2){
|
108
|
configLabels.addLabel(CharacterMatrix.IDENTIFIER_COLUMN);
|
109
|
}
|
110
|
else if(columnPosition==3){
|
111
|
configLabels.addLabel(CharacterMatrix.COUNTRY_COLUMN);
|
112
|
}
|
113
|
else{
|
114
|
FeatureDto feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
|
115
|
configLabels.addLabel(MatrixUtility.getProperty(feature));
|
116
|
// check for default values
|
117
|
if(rowObject instanceof SpecimenRowWrapperDTO){
|
118
|
SpecimenRowWrapperDTO specimenRowWrapper = (SpecimenRowWrapperDTO)rowObject;
|
119
|
if(hasDefaultValue(feature, specimenRowWrapper)){
|
120
|
configLabels.addLabel(HAS_DEFAULT);
|
121
|
if(hasDefaultOverriddenValue(feature, specimenRowWrapper)){
|
122
|
configLabels.removeLabel(HAS_DEFAULT);
|
123
|
configLabels.addLabel(DEFAULT_OVERRIDDEN);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
}
|
128
|
if (rowObject instanceof RowWrapperDTO){
|
129
|
if (hasMoreThanOneValue(feature, (RowWrapperDTO)rowObject)){
|
130
|
isEditable = false;
|
131
|
}
|
132
|
if (!isApplicableCheck(feature, (RowWrapperDTO)rowObject)){
|
133
|
isEditable = false;
|
134
|
}
|
135
|
}
|
136
|
|
137
|
if(feature.isSupportsCategoricalData()){
|
138
|
|
139
|
configLabels.addLabel(CATEGORICAL);
|
140
|
if(isEditable){
|
141
|
configLabels.addLabel(CATEGORICAL_EDITABLE);
|
142
|
}else{
|
143
|
configLabels.addLabel(NOT_EDITABLE);
|
144
|
}
|
145
|
}
|
146
|
else if(feature.isSupportsQuantitativeData()){
|
147
|
configLabels.addLabel(QUANTITATIVE);
|
148
|
if(isEditable){
|
149
|
configLabels.addLabel(QUANTITATIVE_EDITABLE);
|
150
|
}else{
|
151
|
configLabels.addLabel(NOT_EDITABLE);
|
152
|
}
|
153
|
}
|
154
|
}
|
155
|
}
|
156
|
|
157
|
private boolean hasDefaultOverriddenValue(FeatureDto feature, RowWrapperDTO<?> rowWrapper) {
|
158
|
Set<DescriptionElementDto> dataValueForFeature = rowWrapper.getDataValueForFeature(feature.getUuid());
|
159
|
if(dataValueForFeature!=null){
|
160
|
for (DescriptionElementDto dto:dataValueForFeature){
|
161
|
if(dto instanceof CategoricalDataDto && !((CategoricalDataDto) dto).getStates().isEmpty()){
|
162
|
return true;
|
163
|
}
|
164
|
else if(dto instanceof QuantitativeDataDto && !((QuantitativeDataDto) dto).getValues().isEmpty()){
|
165
|
return true;
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
return false;
|
170
|
}
|
171
|
|
172
|
private boolean hasDefaultValue(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
|
173
|
if(rowWrapperDTO instanceof SpecimenRowWrapperDTO
|
174
|
&& ((SpecimenRowWrapperDTO) rowWrapperDTO).getDefaultDescription()!=null){
|
175
|
DescriptionBaseDto taxDescription = ((SpecimenRowWrapperDTO)rowWrapperDTO).getDefaultDescription().getDescription();
|
176
|
return hasDefaultValue(feature, taxDescription);
|
177
|
}
|
178
|
else if(rowWrapperDTO instanceof TaxonRowWrapperDTO){
|
179
|
TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO)rowWrapperDTO;
|
180
|
Set<DescriptionBaseDto> descriptions = taxonRowWrapper.getTaxonDescriptions();
|
181
|
for (DescriptionBaseDto taxonDescription : descriptions) {
|
182
|
// if(matrix.getDescriptiveDataSet().getDescriptions().contains(taxonDescription)
|
183
|
// && taxonDescription.getTypes().contains(DescriptionType.DEFAULT_VALUES_FOR_AGGREGATION)){
|
184
|
// return hasDefaultValue(feature, taxonDescription);
|
185
|
// }
|
186
|
//TODO: correct implementation
|
187
|
return false;
|
188
|
}
|
189
|
}
|
190
|
return false;
|
191
|
}
|
192
|
private boolean hasMoreThanOneValue(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
|
193
|
|
194
|
Set<DescriptionElementDto> elements = rowWrapperDTO.getDataValueForFeature(feature.getUuid());
|
195
|
return elements != null && elements.size()>1;
|
196
|
|
197
|
|
198
|
}
|
199
|
|
200
|
private boolean isApplicableCheck(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
|
201
|
TermTreeDto tree = matrix.getDescriptiveDataSet().getDescriptiveSystem();
|
202
|
boolean isApplicableCheck = true;
|
203
|
if (tree.getOnlyApplicable().containsKey(feature.getUuid())){
|
204
|
|
205
|
for (FeatureStateDto featureStateDto:tree.getOnlyApplicable().get(feature.getUuid())){
|
206
|
isApplicableCheck = false;
|
207
|
FeatureDto dto = featureStateDto.getFeature();
|
208
|
TermDto stateDto = featureStateDto.getState();
|
209
|
Set<DescriptionElementDto> descEls = rowWrapperDTO.getDataValueForFeature(dto.getUuid());
|
210
|
if (descEls != null){
|
211
|
for (DescriptionElementDto el:descEls){
|
212
|
if (el instanceof CategoricalDataDto){
|
213
|
for (StateDataDto stateData:((CategoricalDataDto) el).getStates()){
|
214
|
isApplicableCheck |= stateData.getState().getUuid().equals(stateDto.getUuid());
|
215
|
}
|
216
|
}
|
217
|
}
|
218
|
}
|
219
|
}
|
220
|
}
|
221
|
if (tree.getInapplicableMap().containsKey(feature.getUuid())){
|
222
|
for (FeatureStateDto featureStateDto:tree.getInapplicableMap().get(feature.getUuid())){
|
223
|
FeatureDto dto = featureStateDto.getFeature();
|
224
|
TermDto stateDto = featureStateDto.getState();
|
225
|
Set<DescriptionElementDto> descEls = rowWrapperDTO.getDataValueForFeature(dto.getUuid());
|
226
|
if (descEls != null){
|
227
|
for (DescriptionElementDto el:descEls){
|
228
|
if (el instanceof CategoricalDataDto){
|
229
|
for (StateDataDto stateData:((CategoricalDataDto) el).getStates()){
|
230
|
isApplicableCheck &= !stateData.getState().getUuid().equals(stateDto.getUuid());
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
}
|
235
|
}
|
236
|
}
|
237
|
return isApplicableCheck;
|
238
|
}
|
239
|
|
240
|
|
241
|
private boolean hasDefaultValue(FeatureDto feature, DescriptionBaseDto defaultDescription) {
|
242
|
if(defaultDescription!=null){
|
243
|
Optional<DescriptionElementDto> descriptionElement = defaultDescription.getElements().stream()
|
244
|
.filter(element->element.getFeatureUuid().equals(feature.getUuid()))
|
245
|
.findAny();
|
246
|
if(descriptionElement.isPresent() && descriptionElement.get() instanceof CategoricalDataDto){
|
247
|
return !((CategoricalDataDto) descriptionElement.get()).getStates().isEmpty();
|
248
|
}
|
249
|
else if(descriptionElement.isPresent() && descriptionElement.get() instanceof QuantitativeDataDto){
|
250
|
return !((QuantitativeDataDto) descriptionElement.get()).getValues().isEmpty();
|
251
|
}
|
252
|
|
253
|
}
|
254
|
return false;
|
255
|
}
|
256
|
}
|