Project

General

Profile

Download (10.9 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.List;
14
import java.util.Map;
15
import java.util.UUID;
16
import java.util.stream.Collectors;
17

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

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

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

    
55
    private CharacterMatrix matrix;
56

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

    
61
        init();
62
    }
63

    
64
    private void init() {
65

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

    
69
        /**
70
         * Add description button
71
         */
72
        Button btnAddDescription = new Button(this, SWT.PUSH);
73
        btnAddDescription.setImage(ImageResources.getImage(ImageResources.ADD_ICON_GREEN));
74
        btnAddDescription.addSelectionListener(new SelectionAdapter() {
75
            @Override
76
            public void widgetSelected(SelectionEvent e) {
77
                String error = "";
78
                SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(matrix.getShell(), matrix);
79
                if(dialog.open()==Window.OK){
80
                    Collection<SpecimenNodeWrapper> wrappers = dialog.getSpecimen();
81
                    for (SpecimenNodeWrapper wrapper : wrappers) {
82
                        SpecimenDescription specimenDescription = CdmStore.getService(IDescriptiveDataSetService.class)
83
                                .findSpecimenDescription(matrix.getDescriptiveDataSet().getUuid(),
84
                                        wrapper.getUuidAndTitleCache().getUuid(), true);
85
                        SpecimenRowWrapperDTO rowWrapper = CdmStore.getService(IDescriptiveDataSetService.class)
86
                                .createSpecimenRowWrapper(specimenDescription, matrix.getDescriptiveDataSet());
87
                        if(rowWrapper==null){
88
                            error += specimenDescription;
89
                            continue;
90
                        }
91
                        //add specimen description
92
                        matrix.getDescriptions().add(rowWrapper);
93
                        matrix.getDescriptiveDataSet().addDescription(specimenDescription);
94
                        matrix.getCdmEntitiySession().load(specimenDescription, true);
95

    
96
                        //add taxon description
97
                        TaxonRowWrapperDTO taxonDescription = rowWrapper.getDefaultTaxonDescription();
98
                        if(taxonDescription!=null){
99
                            matrix.getDescriptiveDataSet().addDescription(taxonDescription.getDescription());
100
                            matrix.getCdmEntitiySession().load(taxonDescription.getDescription(), true);
101
                        }
102

    
103
                        matrix.setDirty();
104
                        matrix.getSpecimenCache().remove(wrapper);
105
                    }
106
                    if(CdmUtils.isNotBlank(error)){
107
                        MessagingUtils.warningDialog("Errors during row creation", this,
108
                                String.format("Could not create rows for the following description:\n\n%s", error));
109
                    }
110
                }
111
            }
112
        });
113
        /**
114
         * Remove description button
115
         */
116
        Button btnRemoveDescription = new Button(this, SWT.PUSH);
117
        btnRemoveDescription.setImage(ImageResources.getImage(ImageResources.ACTIVE_DELETE_ICON));
118
        btnRemoveDescription.addSelectionListener(new SelectionAdapter() {
119
            @Override
120
            public void widgetSelected(SelectionEvent e) {
121
                int[] fullySelectedRowPositions = matrix.getBodyLayer().getSelectionLayer().getFullySelectedRowPositions();
122
                List<RowWrapperDTO> toRemove = new ArrayList<>();
123
                for (int i : fullySelectedRowPositions) {
124
                    Object rowObject = matrix.getBodyDataProvider().getRowObject(i);
125
                    if(rowObject instanceof RowWrapperDTO){
126
                        toRemove.add((RowWrapperDTO) rowObject);
127
                    }
128
                }
129
                toRemove.forEach(rowToRemove->{
130
                    matrix.getDescriptions().remove(rowToRemove);
131
                    matrix.getDescriptiveDataSet().removeDescription(rowToRemove.getDescription());
132
                    matrix.setDirty();
133
                });
134
            }
135
        });
136
        /**
137
         * Aggregate button
138
         */
139
        Button btnAggregate = new Button(this, SWT.PUSH);
140
        btnAggregate.setText("Aggregate");
141
        btnAggregate.addSelectionListener(new SelectionAdapter() {
142
            @Override
143
            public void widgetSelected(SelectionEvent e) {
144
                List<TaxonNode> taxonSubtreeFilter = CdmStore.getService(IDescriptiveDataSetService.class).loadFilteredTaxonNodes(matrix.getDescriptiveDataSet(), null);
145
                List<TaxonNodeDto> nodeDtos = taxonSubtreeFilter.stream()
146
                        .map(node -> new TaxonNodeDto(node)).collect(Collectors.toList());
147
                TaxonNodeDto parentDto = CdmStore.getService(ITaxonNodeService.class).findCommonParentDto(nodeDtos);
148
                UUID taxonUuid = parentDto.getTaxonUuid();
149
                int response = MessagingUtils.confirmDialog(
150
                        "Choose location for the aggregated description",
151
                        String.format("The aggregated description will be stored at "
152
                                + "the common parent taxon of this data set:\n%s\n\n"
153
                                + "Do you want to use this taxon?"
154
                                , parentDto.getTaxonTitleCache()), "Yes", "Choose taxon", "Cancel");
155
                if(response==2){
156
                    return;
157
                }
158
                else if(response==1){
159
                    Taxon taxon = TaxonSelectionDialog.selectTaxon(getShell(), null);
160
                    if(taxon==null){
161
                        return;
162
                    }
163
                    taxonUuid = taxon.getUuid();
164
                }
165
                List<UUID> descriptionUuids = new ArrayList<>();
166
                matrix.getDescriptiveDataSet().getDescriptions().forEach(desc->descriptionUuids.add(desc.getUuid()));
167
                UpdateResult result = CdmStore.getService(IDescriptiveDataSetService.class).aggregateDescription(taxonUuid, descriptionUuids, matrix.getDescriptiveDataSet().getLabel(), matrix.getDescriptiveDataSet().getUuid());
168
                TaxonDescription taxonDescription = (TaxonDescription) result.getCdmEntity();
169
                TaxonRowWrapperDTO taxonRowWrapper = CdmStore.getService(IDescriptiveDataSetService.class).createTaxonRowWrapper(taxonDescription.getUuid(), matrix.getDescriptiveDataSet().getUuid());
170
                matrix.getDescriptions().add(taxonRowWrapper);
171
                matrix.addUpdateResult(result);
172

    
173
                aggregateCategorcialHistogram(matrix.getFeatureToHistogramMap());
174
                aggregateQuantitativeSummary(matrix.getFeatureToQuantDataStatisticsMap());
175
            }
176

    
177
        });
178
    }
179

    
180
    @SuppressWarnings("unchecked")
181
    private void aggregateCategorcialHistogram(Map<Feature, CategoricalDataHistogram> featureToHistogramMap) {
182
        featureToHistogramMap.clear();
183
        matrix.getDescriptions().stream()
184
        .filter(desc->desc instanceof SpecimenRowWrapperDTO)
185
                .forEach(o -> ((SpecimenRowWrapperDTO) o).getDescription().getElements().stream()
186
                        .filter(descriptionElement -> descriptionElement instanceof CategoricalData)
187
                        .forEach(categoricalData -> {
188
                            Feature feature = ((CategoricalData) categoricalData).getFeature();
189
                            CategoricalDataHistogram dataHistogram = featureToHistogramMap.get(feature);
190
                            if(dataHistogram==null){
191
                                dataHistogram = new CategoricalDataHistogram(feature);
192
                            }
193
                            featureToHistogramMap.put(feature, dataHistogram);
194
                            ((CategoricalData) categoricalData).getStateData()
195
                                    .forEach(stateData -> featureToHistogramMap.get(feature).addState(stateData.getState()));
196
                        }));
197
    }
198

    
199
    @SuppressWarnings("unchecked")
200
    private void aggregateQuantitativeSummary(Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap) {
201
        featureToQuantDataStatisticsMap.clear();
202
        matrix.getDescriptions().stream()
203
        .filter(desc->desc instanceof SpecimenRowWrapperDTO)
204
        .forEach(o -> ((SpecimenRowWrapperDTO) o).getDescription().getElements().stream()
205
                .filter(descriptionElement -> descriptionElement instanceof QuantitativeData)
206
                .forEach(quantData -> {
207
                    Feature feature = ((QuantitativeData) quantData).getFeature();
208
                    QuantitativeDataStatistics dataStatistics = featureToQuantDataStatisticsMap.get(feature);
209
                    if(dataStatistics==null){
210
                        dataStatistics = new QuantitativeDataStatistics();
211
                    }
212
                    featureToQuantDataStatisticsMap.put(feature, dataStatistics);
213
                    dataStatistics.addQuantitativeData((QuantitativeData) quantData);
214
                }));
215
    }
216

    
217
}
(7-7/19)