Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:
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
    public static final String NOT_APPLICABLE = "NOT APPLICABLE";
47

    
48
    private CharacterMatrix matrix;
49

    
50
    public CharacterMatrixConfigLabelAccumulator(CharacterMatrix matrix) {
51
        super();
52
        this.matrix = matrix;
53
    }
54

    
55
    @Override
56
    public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
57
        boolean isEditable = true;
58
        boolean isApplicable = true;
59

    
60
        Object rowObject = matrix.getBodyDataProvider().getRowObject(rowPosition);
61
        if(rowObject instanceof TaxonNodeDto){
62
            configLabels.addLabel(CharacterMatrix.LABEL_TAXON_ROW);
63
        }
64
        else if(rowObject instanceof TaxonRowWrapperDTO){
65
            TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO) rowObject;
66
            if(MatrixUtility.isAggregatedTaxonDescription(taxonRowWrapper)){
67
                configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION);
68
                isEditable = false;
69
                if(columnPosition==0){
70
                    configLabels.addLabel(CharacterMatrix.LABEL_TAXON_AGGREGATED_DESCRIPTION_ICON);
71
                }
72
            }
73
            else if(MatrixUtility.isDefaultTaxonDescription(taxonRowWrapper)){
74
                configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION);
75
                if(columnPosition==0){
76
                    configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DEFAULT_DESCRIPTION_ICON);
77
                }
78
            }
79
            else if(MatrixUtility.isLiteratureTaxonDescription(taxonRowWrapper)){
80
                configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION);
81
                if(columnPosition==0){
82
                    configLabels.addLabel(CharacterMatrix.LABEL_TAXON_LITERATURE_DESCRIPTION_ICON);
83
                }
84
                // check for existing default values
85
                if(columnPosition>=CharacterMatrix.LEADING_COLUMN_COUNT){
86
                    FeatureDto feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
87
                    if(hasDefaultValue(feature, taxonRowWrapper)){
88
                        configLabels.addLabel(HAS_DEFAULT);
89
                        if(hasDefaultOverriddenValue(feature, taxonRowWrapper)){
90
                            configLabels.removeLabel(HAS_DEFAULT);
91
                            configLabels.addLabel(DEFAULT_OVERRIDDEN);
92
                        }
93
                    }
94
                }
95
            }
96
            //check for supplemental data TODO: add supplemental data to DTO
97
//            if(!taxonRowWrapper.getDescription().getSources().isEmpty() && columnPosition==0){
98
//                configLabels.addLabel(CharacterMatrix.LABEL_DESCRIPTION_HAS_SUPPLEMENTAL_DATA);
99
//            }
100
            configLabels.addLabel(CharacterMatrix.LABEL_TAXON_DESCRIPTION);
101
        }
102

    
103
        if(columnPosition==0){
104
            configLabels.addLabel(CharacterMatrix.TAXON_COLUMN);
105
        }
106
        else if(columnPosition==1){
107
            configLabels.addLabel(CharacterMatrix.COLLECTOR_COLUMN);
108
        }
109
        else if(columnPosition==2){
110
            configLabels.addLabel(CharacterMatrix.IDENTIFIER_COLUMN);
111
        }
112
        else if(columnPosition==3){
113
            configLabels.addLabel(CharacterMatrix.COUNTRY_COLUMN);
114
        }
115
        else{
116
            FeatureDto feature = matrix.getFeatures().get(columnPosition-CharacterMatrix.LEADING_COLUMN_COUNT);
117
            configLabels.addLabel(MatrixUtility.getProperty(feature));
118
            // check for default values
119
            if(rowObject instanceof SpecimenRowWrapperDTO){
120
                SpecimenRowWrapperDTO specimenRowWrapper = (SpecimenRowWrapperDTO)rowObject;
121
                if(hasDefaultValue(feature, specimenRowWrapper)){
122
                    configLabels.addLabel(HAS_DEFAULT);
123
                    if(hasDefaultOverriddenValue(feature, specimenRowWrapper)){
124
                        configLabels.removeLabel(HAS_DEFAULT);
125
                        configLabels.addLabel(DEFAULT_OVERRIDDEN);
126
                    }
127
                }
128
                
129
            }
130
            if (rowObject instanceof RowWrapperDTO){
131
	            if (hasMoreThanOneValue(feature, (RowWrapperDTO)rowObject)){
132
	            	isEditable = false;
133
	            }
134
	            if (!isApplicableCheck(feature, (RowWrapperDTO)rowObject)){
135
	            	isApplicable = false;
136
	            }
137
            }
138

    
139
            if(feature.isSupportsCategoricalData()){
140
            	
141
                configLabels.addLabel(CATEGORICAL);
142
                if(isEditable && isApplicable){
143
                    configLabels.addLabel(CATEGORICAL_EDITABLE);
144
                }else if (!isEditable && isApplicable){
145
                	configLabels.addLabel(NOT_EDITABLE);
146
                }
147
                
148
            }
149
            else if(feature.isSupportsQuantitativeData()){
150
                configLabels.addLabel(QUANTITATIVE);
151
                if(isEditable && isApplicable){
152
                    configLabels.addLabel(QUANTITATIVE_EDITABLE);
153
                }else if (!isEditable && isApplicable){
154
                	configLabels.addLabel(NOT_EDITABLE);
155
                }
156
            }
157
            if (!isApplicable){
158
            	configLabels.addLabel(NOT_APPLICABLE);
159
            	
160
            }
161
        }
162
    }
163

    
164
    private boolean hasDefaultOverriddenValue(FeatureDto feature, RowWrapperDTO<?> rowWrapper) {
165
        Set<DescriptionElementDto> dataValueForFeature = rowWrapper.getDataValueForFeature(feature.getUuid());
166
        if(dataValueForFeature!=null){
167
        	for (DescriptionElementDto dto:dataValueForFeature){
168
	            if(dto instanceof CategoricalDataDto && !((CategoricalDataDto) dto).getStates().isEmpty()){
169
	                return true;
170
	            }
171
	            else if(dto instanceof QuantitativeDataDto && !((QuantitativeDataDto) dto).getValues().isEmpty()){
172
	                return true;
173
	            }
174
        	}
175
        }
176
        return false;
177
    }
178

    
179
    private boolean hasDefaultValue(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
180
        if(rowWrapperDTO instanceof SpecimenRowWrapperDTO
181
                && ((SpecimenRowWrapperDTO) rowWrapperDTO).getDefaultDescription()!=null){
182
            DescriptionBaseDto taxDescription = ((SpecimenRowWrapperDTO)rowWrapperDTO).getDefaultDescription().getDescription();
183
            return hasDefaultValue(feature, taxDescription);
184
        }
185
        else if(rowWrapperDTO instanceof TaxonRowWrapperDTO){
186
            TaxonRowWrapperDTO taxonRowWrapper = (TaxonRowWrapperDTO)rowWrapperDTO;
187
            Set<DescriptionBaseDto> descriptions = taxonRowWrapper.getTaxonDescriptions();
188
            for (DescriptionBaseDto taxonDescription : descriptions) {
189
//                if(matrix.getDescriptiveDataSet().getDescriptions().contains(taxonDescription)
190
//                        && taxonDescription.getTypes().contains(DescriptionType.DEFAULT_VALUES_FOR_AGGREGATION)){
191
//                    return hasDefaultValue(feature, taxonDescription);
192
//                }
193
                //TODO: correct implementation
194
                return false;
195
            }
196
        }
197
        return false;
198
    }
199
    private boolean hasMoreThanOneValue(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
200
        
201
            Set<DescriptionElementDto> elements = rowWrapperDTO.getDataValueForFeature(feature.getUuid());
202
            return elements != null && elements.size()>1;
203
        
204
       
205
    }
206
    
207
    private boolean isApplicableCheck(FeatureDto feature, RowWrapperDTO<?> rowWrapperDTO) {
208
        TermTreeDto tree = matrix.getDescriptiveDataSet().getDescriptiveSystem();
209
        boolean isApplicableCheck = true;
210
        if (tree.getOnlyApplicable().containsKey(feature.getUuid())){
211
        	
212
        	for (FeatureStateDto featureStateDto:tree.getOnlyApplicable().get(feature.getUuid())){
213
        		isApplicableCheck = false;
214
	    		FeatureDto dto = featureStateDto.getFeature();
215
	    		TermDto stateDto = featureStateDto.getState();
216
	    		Set<DescriptionElementDto> descEls = rowWrapperDTO.getDataValueForFeature(dto.getUuid());
217
	    		if (descEls != null){
218
	        		for (DescriptionElementDto el:descEls){
219
	        			if (el instanceof CategoricalDataDto){
220
	        				for (StateDataDto stateData:((CategoricalDataDto) el).getStates()){
221
	        					isApplicableCheck |= stateData.getState().getUuid().equals(stateDto.getUuid());
222
	        				}
223
	        			}
224
	        		}
225
	    		}
226
	    	}        	
227
        }
228
        if (tree.getInapplicableMap().containsKey(feature.getUuid())){
229
        	for (FeatureStateDto featureStateDto:tree.getInapplicableMap().get(feature.getUuid())){
230
        		FeatureDto dto = featureStateDto.getFeature();
231
        		TermDto stateDto = featureStateDto.getState();
232
        		Set<DescriptionElementDto> descEls = rowWrapperDTO.getDataValueForFeature(dto.getUuid());
233
        		if (descEls != null){
234
	        		for (DescriptionElementDto el:descEls){
235
	        			if (el instanceof CategoricalDataDto){
236
	        				for (StateDataDto stateData:((CategoricalDataDto) el).getStates()){
237
	        					isApplicableCheck &= !stateData.getState().getUuid().equals(stateDto.getUuid());
238
	        				}
239
	        			}
240
	        		}
241
        		}
242
        	}        	
243
        }
244
        return isApplicableCheck;   
245
    }
246

    
247

    
248
    private boolean hasDefaultValue(FeatureDto feature, DescriptionBaseDto defaultDescription) {
249
        if(defaultDescription!=null){
250
            Optional<DescriptionElementDto> descriptionElement = defaultDescription.getElements().stream()
251
                    .filter(element->element.getFeatureUuid().equals(feature.getUuid()))
252
                    .findAny();
253
            if(descriptionElement.isPresent() && descriptionElement.get() instanceof CategoricalDataDto){
254
                return !((CategoricalDataDto) descriptionElement.get()).getStates().isEmpty();
255
            }
256
            else if(descriptionElement.isPresent() && descriptionElement.get() instanceof QuantitativeDataDto){
257
                return !((QuantitativeDataDto) descriptionElement.get()).getValues().isEmpty();
258
            }
259

    
260
        }
261
        return false;
262
    }
263
}
(5-5/18)