Project

General

Profile

Download (5.18 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.HashSet;
12
import java.util.Set;
13

    
14
import org.apache.commons.lang.StringUtils;
15
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
16
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
17
import org.eclipse.nebula.widgets.nattable.summaryrow.DefaultSummaryRowConfiguration;
18
import org.eclipse.nebula.widgets.nattable.summaryrow.ISummaryProvider;
19

    
20
import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
21
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22
import eu.etaxonomy.cdm.model.description.CategoricalData;
23
import eu.etaxonomy.cdm.model.description.QuantitativeData;
24

    
25
/**
26
 * Configuration for the aggregation algorithm for the columns
27
 * of the character matrix
28
 * @author pplitzner
29
 * @since Jan 4, 2018
30
 *
31
 */
32
public class AggregationConfiguration extends DefaultSummaryRowConfiguration {
33

    
34
    private CharacterMatrix matrix;
35

    
36
    public AggregationConfiguration(CharacterMatrix matrix) {
37
        this.matrix = matrix;
38
    }
39

    
40
    @Override
41
    public void addSummaryProviderConfig(IConfigRegistry configRegistry) {
42
//        for(int i=0;i<matrix.getFeatures().size();i++){
43
//            //no summary for the supplemental columns
44
//            if(i<CharacterMatrix.LEADING_COLUMN_COUNT){
45
//                int index = i;
46
//                configRegistry.registerConfigAttribute(
47
//                        SummaryRowConfigAttributes.SUMMARY_PROVIDER,
48
//                        columnIndex -> "",
49
//                        DisplayMode.NORMAL,
50
//                        SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+index);
51
//            }
52
//            //register aggregation configuration for each feature
53
//            Feature feature = matrix.getFeatures().get(i);
54
//            if(feature.isSupportsQuantitativeData()){
55
//                configRegistry.registerConfigAttribute(
56
//                        SummaryRowConfigAttributes.SUMMARY_PROVIDER,
57
//                        new QuantitativeSummaryProvider(),
58
//                        DisplayMode.NORMAL,
59
//                        SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+MatrixUtility.getProperty(feature));
60
//            }
61
//            if(feature.isSupportsCategoricalData()){
62
//                configRegistry.registerConfigAttribute(
63
//                        SummaryRowConfigAttributes.SUMMARY_PROVIDER,
64
//                        new CategoricalSummaryProvider(),
65
//                        DisplayMode.NORMAL,
66
//                        SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+MatrixUtility.getProperty(feature));
67
//            }
68
//        }
69
    }
70

    
71
    private class QuantitativeSummaryProvider implements ISummaryProvider {
72
        @Override
73
        public Object summarize(int columnIndex) {
74
            Float minTotal = null;
75
            Float maxTotal = null;
76
            ListDataProvider<Object> bodyDataProvider = AggregationConfiguration.this.matrix.getBodyDataProvider();
77
            int rowCount = bodyDataProvider.getRowCount();
78

    
79
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
80
                Object rowObject = bodyDataProvider.getRowObject(rowIndex);
81
                if(rowObject instanceof TaxonRowWrapperDTO){
82
                    // do not aggregate taxon descriptions
83
                    continue;
84
                }
85
                Object dataValue = bodyDataProvider.getDataValue(columnIndex,
86
                        rowIndex);
87
                if(HibernateProxyHelper.isInstanceOf(dataValue, QuantitativeData.class)){
88
                    QuantitativeData quantitativeData = HibernateProxyHelper.deproxy(dataValue, QuantitativeData.class);
89
                    Float min = quantitativeData.getMin();
90
                    Float max = quantitativeData.getMax();
91
                    if(min!=null && (minTotal==null || min<minTotal)){
92
                        minTotal = min;
93
                    }
94
                    if(max!=null && (maxTotal==null || max>maxTotal)){
95
                        maxTotal = max;
96
                    }
97
                }
98
            }
99
            String summaryString = MatrixUtility.getQuantitativeLabel(minTotal, null, maxTotal);
100
            return summaryString;
101
        }
102

    
103
    }
104

    
105
    private class CategoricalSummaryProvider implements ISummaryProvider {
106
        @Override
107
        public Object summarize(int columnIndex) {
108
            int rowCount = AggregationConfiguration.this.matrix.getBodyDataProvider().getRowCount();
109
            Set states = new HashSet<>();
110
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
111
                Object dataValue = AggregationConfiguration.this.matrix.getBodyDataProvider().getDataValue(columnIndex,
112
                        rowIndex);
113
                if(dataValue instanceof CategoricalData){
114
                    states.addAll(((CategoricalData) dataValue).getStatesOnly());
115
                }
116
            }
117
            return StringUtils.join(states, ", "); //$NON-NLS-1$
118
        }
119
    }
120
}
(1-1/21)