Project

General

Profile

Download (10.2 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.ArrayList;
12
import java.util.Collection;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.UUID;
18
import java.util.stream.Collectors;
19

    
20
import org.eclipse.jface.layout.GridDataFactory;
21
import org.eclipse.jface.window.Window;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.SelectionAdapter;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.layout.RowLayout;
26
import org.eclipse.swt.widgets.Button;
27
import org.eclipse.swt.widgets.Composite;
28

    
29
import eu.etaxonomy.cdm.api.service.IDescriptionService;
30
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
31
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
32
import eu.etaxonomy.cdm.api.service.UpdateResult;
33
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
34
import eu.etaxonomy.cdm.common.CdmUtils;
35
import eu.etaxonomy.cdm.model.description.CategoricalData;
36
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37
import eu.etaxonomy.cdm.model.description.Feature;
38
import eu.etaxonomy.cdm.model.description.QuantitativeData;
39
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
43
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
44
import eu.etaxonomy.taxeditor.model.ImageResources;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46
import eu.etaxonomy.taxeditor.store.CdmStore;
47
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonSelectionDialog;
48

    
49
/**
50
 * @author pplitzner
51
 * @since Jul 9, 2018
52
 *
53
 */
54
public class CharacterMatrixBottomToolbar extends Composite{
55

    
56
    private CharacterMatrix matrix;
57

    
58
    public CharacterMatrixBottomToolbar(CharacterMatrix matrix, int style) {
59
        super(matrix, style);
60
        this.matrix = matrix;
61

    
62
        init();
63
    }
64

    
65
    private void init() {
66

    
67
        setLayout(new RowLayout());
68
        GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
69

    
70
        /**
71
         * Add description button
72
         */
73
        Button btnAddDescription = new Button(this, SWT.PUSH);
74
        btnAddDescription.setImage(ImageResources.getImage(ImageResources.ADD_ICON_GREEN));
75
        btnAddDescription.addSelectionListener(new SelectionAdapter() {
76
            @Override
77
            public void widgetSelected(SelectionEvent e) {
78
                String error = "";
79
                SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(matrix.getShell(), matrix);
80
                if(dialog.open()==Window.OK){
81
                    Collection<SpecimenNodeWrapper> wrappers = dialog.getSpecimen();
82
                    for (SpecimenNodeWrapper wrapper : wrappers) {
83
                        SpecimenDescription description = CdmStore.getService(IDescriptiveDataSetService.class)
84
                                .findDescriptionForDescriptiveDataSet(matrix.getDescriptiveDataSet().getUuid(),
85
                                        wrapper.getUuidAndTitleCache().getUuid());
86
                        // description elements
87
                        Map<Feature, DescriptionElementBase> featureToElementMap = new HashMap<>();
88
                        Set<DescriptionElementBase> elements = description.getElements();
89
                        for (DescriptionElementBase descriptionElementBase : elements) {
90
                            Feature feature = descriptionElementBase.getFeature();
91
                            featureToElementMap.put(feature, descriptionElementBase);
92
                        }
93
                        RowWrapperDTO rowWrapper = CdmStore.getService(IDescriptiveDataSetService.class).createRowWrapper(wrapper.getTaxonNode(), description, matrix.getDescriptiveDataSet());
94
                        if(rowWrapper==null){
95
                            error += description;
96
                            continue;
97
                        }
98
                        matrix.getDescriptions().add(rowWrapper);
99
                        matrix.getDescriptiveDataSet().addDescription(description);
100
                        matrix.setDirty();
101
                        matrix.getSpecimenCache().remove(wrapper);
102
                    }
103
                    if(CdmUtils.isNotBlank(error)){
104
                        MessagingUtils.warningDialog("Errors during row creation", this,
105
                                String.format("Could not create rows for the following description:\n\n%s", error));
106
                    }
107
                }
108
            }
109
        });
110
        /**
111
         * Remove description button
112
         */
113
        Button btnRemoveDescription = new Button(this, SWT.PUSH);
114
        btnRemoveDescription.setImage(ImageResources.getImage(ImageResources.ACTIVE_DELETE_ICON));
115
        btnRemoveDescription.addSelectionListener(new SelectionAdapter() {
116
            @Override
117
            public void widgetSelected(SelectionEvent e) {
118
                int[] fullySelectedRowPositions = matrix.getBodyLayer().getSelectionLayer().getFullySelectedRowPositions();
119
                List<RowWrapperDTO> toRemove = new ArrayList<>();
120
                for (int i : fullySelectedRowPositions) {
121
                    Object rowObject = matrix.getBodyDataProvider().getRowObject(i);
122
                    if(rowObject instanceof RowWrapperDTO){
123
                        toRemove.add((RowWrapperDTO) rowObject);
124
                    }
125
                }
126
                toRemove.forEach(rowToRemove->{
127
                    matrix.getDescriptions().remove(rowToRemove);
128
                    matrix.getDescriptiveDataSet().removeDescription(rowToRemove.getSpecimenDescription());
129
                    matrix.setDirty();
130
                });
131
            }
132
        });
133
        /**
134
         * Aggregate button
135
         */
136
        Button btnAggregate = new Button(this, SWT.PUSH);
137
        btnAggregate.setText("Aggregate");
138
        btnAggregate.addSelectionListener(new SelectionAdapter() {
139
            @Override
140
            public void widgetSelected(SelectionEvent e) {
141
                Set<TaxonNode> taxonSubtreeFilter = matrix.getDescriptiveDataSet().getTaxonSubtreeFilter();
142
                List<TaxonNodeDto> nodeDtos = taxonSubtreeFilter.stream()
143
                        .map(node -> new TaxonNodeDto(node)).collect(Collectors.toList());
144
                TaxonNodeDto parentDto = CdmStore.getService(ITaxonNodeService.class).findCommonParentDto(nodeDtos);
145
                UUID taxonUuid = parentDto.getTaxonUuid();
146
                int response = MessagingUtils.confirmDialog(
147
                        "Choose location for the aggregated description",
148
                        String.format("The aggregated description will be stored at "
149
                                + "the common parent taxon of this data set:\n%s\n\n"
150
                                + "Do you want to use this taxon?"
151
                                , parentDto.getTaxonTitleCache()), "Yes", "Choose taxon", "Cancel");
152
                if(response==2){
153
                    return;
154
                }
155
                else if(response==1){
156
                    Taxon taxon = TaxonSelectionDialog.selectTaxon(getShell(), null);
157
                    if(taxon==null){
158
                        return;
159
                    }
160
                    taxonUuid = taxon.getUuid();
161
                }
162
                List<UUID> descriptionUuids = new ArrayList<>();
163
                matrix.getDescriptiveDataSet().getDescriptions().forEach(desc->descriptionUuids.add(desc.getUuid()));
164
                UpdateResult result = CdmStore.getService(IDescriptionService.class).aggregateDescription(taxonUuid, descriptionUuids, matrix.getDescriptiveDataSet().getLabel());
165
                matrix.addUpdateResult(result);
166
                matrix.setDirty();
167

    
168
                aggregateCategorcialHistogram(matrix.getFeatureToHistogramMap());
169
                aggregateQuantitativeSummary(matrix.getFeatureToQuantDataStatisticsMap());
170
            }
171

    
172
        });
173
    }
174

    
175
    @SuppressWarnings("unchecked")
176
    private void aggregateCategorcialHistogram(Map<Feature, CategoricalDataHistogram> featureToHistogramMap) {
177
        featureToHistogramMap.clear();
178
        matrix.getDescriptions()
179
                .forEach(o -> ((RowWrapperDTO) o).getSpecimenDescription().getElements().stream()
180
                        .filter(descriptionElement -> descriptionElement instanceof CategoricalData)
181
                        .forEach(categoricalData -> {
182
                            Feature feature = ((CategoricalData) categoricalData).getFeature();
183
                            CategoricalDataHistogram dataHistogram = featureToHistogramMap.get(feature);
184
                            if(dataHistogram==null){
185
                                dataHistogram = new CategoricalDataHistogram(feature);
186
                            }
187
                            featureToHistogramMap.put(feature, dataHistogram);
188
                            ((CategoricalData) categoricalData).getStateData()
189
                                    .forEach(stateData -> featureToHistogramMap.get(feature).addState(stateData.getState()));
190
                        }));
191
    }
192

    
193
    @SuppressWarnings("unchecked")
194
    private void aggregateQuantitativeSummary(Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap) {
195
        featureToQuantDataStatisticsMap.clear();
196
        matrix.getDescriptions()
197
        .forEach(o -> ((RowWrapperDTO) o).getSpecimenDescription().getElements().stream()
198
                .filter(descriptionElement -> descriptionElement instanceof QuantitativeData)
199
                .forEach(quantData -> {
200
                    Feature feature = ((QuantitativeData) quantData).getFeature();
201
                    QuantitativeDataStatistics dataStatistics = featureToQuantDataStatisticsMap.get(feature);
202
                    if(dataStatistics==null){
203
                        dataStatistics = new QuantitativeDataStatistics();
204
                    }
205
                    featureToQuantDataStatisticsMap.put(feature, dataStatistics);
206
                    dataStatistics.addQuantitativeData((QuantitativeData) quantData);
207
                }));
208
    }
209

    
210
}
(7-7/20)