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.store.StoreUtil;
|
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
|
if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
|
79
|
return;
|
80
|
}
|
81
|
String error = "";
|
82
|
SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(matrix.getShell(), matrix);
|
83
|
if(dialog.open()==Window.OK){
|
84
|
Collection<SpecimenNodeWrapper> wrappers = dialog.getSpecimen();
|
85
|
for (SpecimenNodeWrapper wrapper : wrappers) {
|
86
|
SpecimenDescription specimenDescription = CdmStore.getService(IDescriptiveDataSetService.class)
|
87
|
.findSpecimenDescription(matrix.getDescriptiveDataSet().getUuid(),
|
88
|
wrapper.getUuidAndTitleCache().getUuid(), true);
|
89
|
SpecimenRowWrapperDTO rowWrapper = CdmStore.getService(IDescriptiveDataSetService.class)
|
90
|
.createSpecimenRowWrapper(specimenDescription, matrix.getDescriptiveDataSet());
|
91
|
if(rowWrapper==null){
|
92
|
error += specimenDescription;
|
93
|
continue;
|
94
|
}
|
95
|
//add specimen description
|
96
|
matrix.getDescriptions().add(rowWrapper);
|
97
|
matrix.getDescriptiveDataSet().addDescription(specimenDescription);
|
98
|
matrix.getCdmEntitiySession().load(specimenDescription, true);
|
99
|
|
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
|
if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
|
119
|
return;
|
120
|
}
|
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
|
CdmStore.getService(IDescriptiveDataSetService.class).removeDescription(
|
132
|
rowToRemove.getDescription().getUuid(), matrix.getDescriptiveDataSet().getUuid());
|
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
|
if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
|
145
|
return;
|
146
|
}
|
147
|
List<TaxonNode> taxonSubtreeFilter = CdmStore.getService(IDescriptiveDataSetService.class).loadFilteredTaxonNodes(matrix.getDescriptiveDataSet(), null);
|
148
|
List<TaxonNodeDto> nodeDtos = taxonSubtreeFilter.stream()
|
149
|
.map(node -> new TaxonNodeDto(node)).collect(Collectors.toList());
|
150
|
TaxonNodeDto parentDto = CdmStore.getService(ITaxonNodeService.class).findCommonParentDto(nodeDtos);
|
151
|
UUID taxonUuid = parentDto.getTaxonUuid();
|
152
|
int response = MessagingUtils.confirmDialog(
|
153
|
"Choose location for the aggregated description",
|
154
|
String.format("The aggregated description will be stored at "
|
155
|
+ "the common parent taxon of this data set:\n%s\n\n"
|
156
|
+ "Do you want to use this taxon?"
|
157
|
, parentDto.getTaxonTitleCache()), "Yes", "Choose taxon", "Cancel");
|
158
|
if(response==2){
|
159
|
return;
|
160
|
}
|
161
|
else if(response==1){
|
162
|
Taxon taxon = TaxonSelectionDialog.selectTaxon(getShell(), null);
|
163
|
if(taxon==null){
|
164
|
return;
|
165
|
}
|
166
|
taxonUuid = taxon.getUuid();
|
167
|
}
|
168
|
List<UUID> descriptionUuids = new ArrayList<>();
|
169
|
matrix.getDescriptiveDataSet().getDescriptions().forEach(desc->descriptionUuids.add(desc.getUuid()));
|
170
|
UpdateResult result = CdmStore.getService(IDescriptiveDataSetService.class).aggregateDescription(taxonUuid, descriptionUuids, matrix.getDescriptiveDataSet().getLabel(), matrix.getDescriptiveDataSet().getUuid());
|
171
|
TaxonDescription taxonDescription = (TaxonDescription) result.getCdmEntity();
|
172
|
TaxonRowWrapperDTO taxonRowWrapper = CdmStore.getService(IDescriptiveDataSetService.class).createTaxonRowWrapper(taxonDescription.getUuid(), matrix.getDescriptiveDataSet().getUuid());
|
173
|
matrix.getDescriptions().add(taxonRowWrapper);
|
174
|
|
175
|
aggregateCategorcialHistogram(matrix.getFeatureToHistogramMap());
|
176
|
aggregateQuantitativeSummary(matrix.getFeatureToQuantDataStatisticsMap());
|
177
|
}
|
178
|
|
179
|
});
|
180
|
}
|
181
|
|
182
|
@SuppressWarnings("unchecked")
|
183
|
private void aggregateCategorcialHistogram(Map<Feature, CategoricalDataHistogram> featureToHistogramMap) {
|
184
|
featureToHistogramMap.clear();
|
185
|
matrix.getDescriptions().stream()
|
186
|
.filter(desc->desc instanceof SpecimenRowWrapperDTO)
|
187
|
.forEach(o -> ((SpecimenRowWrapperDTO) o).getDescription().getElements().stream()
|
188
|
.filter(descriptionElement -> descriptionElement instanceof CategoricalData)
|
189
|
.forEach(categoricalData -> {
|
190
|
Feature feature = ((CategoricalData) categoricalData).getFeature();
|
191
|
CategoricalDataHistogram dataHistogram = featureToHistogramMap.get(feature);
|
192
|
if(dataHistogram==null){
|
193
|
dataHistogram = new CategoricalDataHistogram(feature);
|
194
|
}
|
195
|
featureToHistogramMap.put(feature, dataHistogram);
|
196
|
((CategoricalData) categoricalData).getStateData()
|
197
|
.forEach(stateData -> featureToHistogramMap.get(feature).addState(stateData.getState()));
|
198
|
}));
|
199
|
}
|
200
|
|
201
|
@SuppressWarnings("unchecked")
|
202
|
private void aggregateQuantitativeSummary(Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap) {
|
203
|
featureToQuantDataStatisticsMap.clear();
|
204
|
matrix.getDescriptions().stream()
|
205
|
.filter(desc->desc instanceof SpecimenRowWrapperDTO)
|
206
|
.forEach(o -> ((SpecimenRowWrapperDTO) o).getDescription().getElements().stream()
|
207
|
.filter(descriptionElement -> descriptionElement instanceof QuantitativeData)
|
208
|
.forEach(quantData -> {
|
209
|
Feature feature = ((QuantitativeData) quantData).getFeature();
|
210
|
QuantitativeDataStatistics dataStatistics = featureToQuantDataStatisticsMap.get(feature);
|
211
|
if(dataStatistics==null){
|
212
|
dataStatistics = new QuantitativeDataStatistics();
|
213
|
}
|
214
|
featureToQuantDataStatisticsMap.put(feature, dataStatistics);
|
215
|
dataStatistics.addQuantitativeData((QuantitativeData) quantData);
|
216
|
}));
|
217
|
}
|
218
|
|
219
|
}
|