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.Set;
|
15
|
import java.util.UUID;
|
16
|
import java.util.stream.Collectors;
|
17
|
|
18
|
import org.eclipse.core.runtime.ICoreRunnable;
|
19
|
import org.eclipse.core.runtime.IProgressMonitor;
|
20
|
import org.eclipse.core.runtime.SubMonitor;
|
21
|
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
|
22
|
import org.eclipse.core.runtime.jobs.Job;
|
23
|
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
|
24
|
import org.eclipse.jface.layout.GridDataFactory;
|
25
|
import org.eclipse.jface.window.Window;
|
26
|
import org.eclipse.swt.SWT;
|
27
|
import org.eclipse.swt.events.SelectionAdapter;
|
28
|
import org.eclipse.swt.events.SelectionEvent;
|
29
|
import org.eclipse.swt.layout.RowLayout;
|
30
|
import org.eclipse.swt.widgets.Button;
|
31
|
import org.eclipse.swt.widgets.Composite;
|
32
|
|
33
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
34
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
35
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
36
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
37
|
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.AggregationMode;
|
38
|
import eu.etaxonomy.cdm.api.service.description.StructuredDescriptionAggregationConfiguration;
|
39
|
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
|
40
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
41
|
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
|
42
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
43
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
45
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
46
|
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
|
47
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
48
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
49
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
50
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
51
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
52
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
53
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
54
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
55
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
56
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonSelectionDialog;
|
57
|
|
58
|
/**
|
59
|
* @author pplitzner
|
60
|
* @since Jul 9, 2018
|
61
|
*/
|
62
|
public class CharacterMatrixBottomToolbar extends Composite{
|
63
|
|
64
|
private CharacterMatrix matrix;
|
65
|
|
66
|
public CharacterMatrixBottomToolbar(CharacterMatrix matrix, int style) {
|
67
|
super(matrix, style);
|
68
|
this.matrix = matrix;
|
69
|
|
70
|
init();
|
71
|
}
|
72
|
|
73
|
private void init() {
|
74
|
|
75
|
setLayout(new RowLayout());
|
76
|
GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
|
77
|
|
78
|
/**
|
79
|
* Add description button
|
80
|
*/
|
81
|
Button btnAddDescription = new Button(this, SWT.PUSH);
|
82
|
btnAddDescription.setImage(ImageResources.getImage(ImageResources.ADD_ICON_GREEN));
|
83
|
btnAddDescription.addSelectionListener(new SelectionAdapter() {
|
84
|
@Override
|
85
|
public void widgetSelected(SelectionEvent e) {
|
86
|
if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
|
87
|
return;
|
88
|
}
|
89
|
SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(matrix.getShell(), matrix);
|
90
|
if(dialog.open()==Window.OK){
|
91
|
Collection<SpecimenNodeWrapper> wrappers = dialog.getSpecimen();
|
92
|
if(wrappers.stream().anyMatch(wrapper->wrapper.getTaxonDescriptionUuid()==null)
|
93
|
&& !MessagingUtils.confirmDialog(
|
94
|
"Create Taxon Association",
|
95
|
"Some specimens are not linked with taxon via IndividualsAssociation yet.\n"
|
96
|
+ "Do you want to create this association?")){
|
97
|
return;
|
98
|
}
|
99
|
addRowsToMatrix(wrappers, matrix.getDescriptiveDataSet().getUuid());
|
100
|
wrappers.forEach(wrapper->matrix.getSpecimenCache().remove(wrapper));
|
101
|
}
|
102
|
}
|
103
|
});
|
104
|
/**
|
105
|
* Remove description button
|
106
|
*/
|
107
|
Button btnRemoveDescription = new Button(this, SWT.PUSH);
|
108
|
btnRemoveDescription.setImage(ImageResources.getImage(ImageResources.ACTIVE_DELETE_ICON));
|
109
|
btnRemoveDescription.addSelectionListener(new SelectionAdapter() {
|
110
|
@Override
|
111
|
public void widgetSelected(SelectionEvent e) {
|
112
|
if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
|
113
|
return;
|
114
|
}
|
115
|
if(!MessagingUtils.confirmDialog(Messages.CharacterMatrixBottomToolbar_CONFIRM_DELETE_TITLE, Messages.CharacterMatrixBottomToolbar_CONFIRM_DELETE_MESSAGE)){
|
116
|
return;
|
117
|
}
|
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
|
CdmStore.getService(IDescriptiveDataSetService.class).removeDescription(
|
129
|
rowToRemove.getDescription().getUuid(), matrix.getDescriptiveDataSet().getUuid());
|
130
|
});
|
131
|
}
|
132
|
});
|
133
|
/**
|
134
|
* Aggregate button
|
135
|
*/
|
136
|
Button btnAggregate = new Button(this, SWT.PUSH);
|
137
|
btnAggregate.setText(Messages.CharacterMatrixBottomToolbar_AGGREGATE);
|
138
|
btnAggregate.addSelectionListener(new SelectionAdapter() {
|
139
|
@Override
|
140
|
public void widgetSelected(SelectionEvent e) {
|
141
|
if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
|
142
|
return;
|
143
|
}
|
144
|
aggregatDescriptiveDataSet(matrix.getDescriptiveDataSet().getUuid());
|
145
|
}
|
146
|
});
|
147
|
if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_EXPERIMENTAL_FEATURES)){
|
148
|
/**
|
149
|
* Key generation button
|
150
|
*/
|
151
|
Button btnGenerateKey = new Button(this, SWT.PUSH);
|
152
|
btnGenerateKey.setText("Generate Polytomous Key");
|
153
|
btnGenerateKey.addSelectionListener(new SelectionAdapter() {
|
154
|
@Override
|
155
|
public void widgetSelected(SelectionEvent e) {
|
156
|
if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
|
157
|
return;
|
158
|
}
|
159
|
Set<TaxonNodeDto> nodeDtos = matrix.getDescriptiveDataSet().getTaxonSubtreeFilter().stream()
|
160
|
.map(node->new TaxonNodeDto(node))
|
161
|
.collect(Collectors.toSet());
|
162
|
TaxonNodeDto parentDto = CdmStore.getService(ITaxonNodeService.class).findCommonParentDto(nodeDtos);
|
163
|
UUID taxonUuid = parentDto.getTaxonUuid();
|
164
|
int response = MessagingUtils.confirmDialog(
|
165
|
"Choose taxonomic scope",
|
166
|
String.format("The common parent taxon of this data set is :\n%s\n\n"
|
167
|
+ "Do you want to use this as the taxonomic scope for the polytomous key?"
|
168
|
, parentDto.getTaxonTitleCache()), "Yes", "Choose different taxon", "Cancel");
|
169
|
if(response==2){
|
170
|
return;
|
171
|
}
|
172
|
else if(response==1){
|
173
|
Taxon taxon = TaxonSelectionDialog.selectTaxon(getShell(), null);
|
174
|
if(taxon==null){
|
175
|
return;
|
176
|
}
|
177
|
taxonUuid = taxon.getUuid();
|
178
|
}
|
179
|
generateKey(matrix.getDescriptiveDataSet().getUuid(), taxonUuid);
|
180
|
}
|
181
|
|
182
|
});
|
183
|
}
|
184
|
}
|
185
|
|
186
|
private void aggregatDescriptiveDataSet(UUID descriptiveDataSetUuid){
|
187
|
// FIXME: set taxon node filter for config (this is still not used in the aggregation)
|
188
|
StructuredDescriptionAggregationConfiguration config = StructuredDescriptionAggregationConfiguration.NewInstance(
|
189
|
null, null);
|
190
|
config.setDatasetUuid(matrix.getDescriptiveDataSet().getUuid());
|
191
|
config.setAggregateToHigherRanks(true);
|
192
|
config.setAggregationMode(AggregationMode.byAreasAndRanks());
|
193
|
config.setAdaptBatchSize(false);
|
194
|
|
195
|
TaxonNodeFilter filter = TaxonNodeFilter.NewRankInstance(matrix.getDescriptiveDataSet().getMinRank(),
|
196
|
matrix.getDescriptiveDataSet().getMaxRank());
|
197
|
for (NamedArea namedArea : matrix.getDescriptiveDataSet().getGeoFilter()) {
|
198
|
filter = filter.orArea(namedArea.getUuid());
|
199
|
}
|
200
|
for (TaxonNode taxonNode : matrix.getDescriptiveDataSet().getTaxonSubtreeFilter()) {
|
201
|
filter = filter.orSubtree(taxonNode);
|
202
|
}
|
203
|
config.setTaxonNodeFilter(filter);
|
204
|
|
205
|
UUID monitorUuid = CdmApplicationState.getLongRunningTasksService().invoke(config);
|
206
|
|
207
|
String jobLabel = "Aggregate Descriptive Data Set";
|
208
|
Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
|
209
|
SubMonitor subMonitor = SubMonitor.convert(monitor);
|
210
|
subMonitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
|
211
|
IRemotingProgressMonitor remotingMonitor;
|
212
|
try {
|
213
|
remotingMonitor = CdmStore.getProgressMonitorClientManager()
|
214
|
.pollMonitor(jobLabel,
|
215
|
monitorUuid,
|
216
|
50,
|
217
|
null,
|
218
|
(List)null,
|
219
|
subMonitor);
|
220
|
Object resultObject = remotingMonitor.getResult();
|
221
|
if(resultObject instanceof Exception){
|
222
|
MessagingUtils.errorDialog("Aggregation failed", this, "Aggregation was not successfull", TaxeditorEditorPlugin.PLUGIN_ID, (Exception)resultObject, true, true);
|
223
|
}
|
224
|
else if(resultObject instanceof UpdateResult){
|
225
|
DescriptiveDataSet dataSet = (DescriptiveDataSet) ((UpdateResult) resultObject).getCdmEntity();
|
226
|
dataSet = matrix.getCdmEntitiySession().load(dataSet, true);
|
227
|
// update local dataset
|
228
|
matrix.setDescriptiveDataSet(dataSet);
|
229
|
matrix.loadDescriptions(descriptiveDataSetUuid, false);
|
230
|
}
|
231
|
} catch (InterruptedException e) {
|
232
|
return;
|
233
|
}
|
234
|
monitor.done();
|
235
|
});
|
236
|
job.addJobChangeListener(new JobChangeAdapter(){
|
237
|
@Override
|
238
|
public void done(IJobChangeEvent event) {
|
239
|
CharacterMatrixBottomToolbar.this.getDisplay().asyncExec(()->{
|
240
|
matrix.redraw();
|
241
|
});
|
242
|
}
|
243
|
});
|
244
|
job.schedule();
|
245
|
}
|
246
|
|
247
|
private void addRowsToMatrix(Collection<SpecimenNodeWrapper> wrappers, UUID descriptiveDataSetUuid){
|
248
|
UUID monitorUuid = CdmApplicationState.getLongRunningTasksService().addRowWrapperToDataset(wrappers, descriptiveDataSetUuid);
|
249
|
|
250
|
String jobLabel = "Add specimens to matrix";
|
251
|
Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
|
252
|
SubMonitor subMonitor = SubMonitor.convert(monitor);
|
253
|
subMonitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
|
254
|
IRemotingProgressMonitor remotingMonitor;
|
255
|
try {
|
256
|
remotingMonitor = CdmStore.getProgressMonitorClientManager()
|
257
|
.pollMonitor(jobLabel,
|
258
|
monitorUuid,
|
259
|
50,
|
260
|
null,
|
261
|
(List)null,
|
262
|
subMonitor);
|
263
|
Object resultObject = remotingMonitor.getResult();
|
264
|
if(resultObject instanceof Exception){
|
265
|
MessagingUtils.errorDialog("Adding specimens failed", this, "Adding specimens was not successfull", TaxeditorEditorPlugin.PLUGIN_ID, (Exception)resultObject, true, true);
|
266
|
}
|
267
|
else if(resultObject instanceof UpdateResult){
|
268
|
UpdateResult result = (UpdateResult)resultObject;
|
269
|
if(!result.getExceptions().isEmpty()){
|
270
|
MessagingUtils.warningDialog(Messages.CharacterMatrixBottomToolbar_ERROR_ROW_CREATION_TITLE, this,
|
271
|
String.format(Messages.CharacterMatrixBottomToolbar_ERROR_ROW_CREATION_MESSAGE, result.getExceptions()
|
272
|
.stream().map(ex->ex.toString())
|
273
|
.collect(Collectors.joining("\n"))));
|
274
|
}
|
275
|
DescriptiveDataSet dataSet = (DescriptiveDataSet) result.getCdmEntity();
|
276
|
dataSet = matrix.getCdmEntitiySession().load(dataSet, true);
|
277
|
// update local dataset
|
278
|
matrix.setDescriptiveDataSet(dataSet);
|
279
|
matrix.loadDescriptions(descriptiveDataSetUuid, false);
|
280
|
}
|
281
|
} catch (InterruptedException e) {
|
282
|
return;
|
283
|
}
|
284
|
monitor.done();
|
285
|
});
|
286
|
job.addJobChangeListener(new JobChangeAdapter(){
|
287
|
@Override
|
288
|
public void done(IJobChangeEvent event) {
|
289
|
CharacterMatrixBottomToolbar.this.getDisplay().asyncExec(()->{
|
290
|
matrix.redraw();
|
291
|
});
|
292
|
}
|
293
|
});
|
294
|
job.schedule();
|
295
|
}
|
296
|
|
297
|
private void generateKey(UUID datasetUuid, UUID taxonUuid) {
|
298
|
|
299
|
UUID monitorUuid = CdmApplicationState.getLongRunningTasksService().generatePolytomousKey(datasetUuid, taxonUuid);
|
300
|
|
301
|
String jobLabel = "Generate polytomous key";
|
302
|
Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
|
303
|
SubMonitor subMonitor = SubMonitor.convert(monitor);
|
304
|
subMonitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
|
305
|
IRemotingProgressMonitor remotingMonitor;
|
306
|
try {
|
307
|
remotingMonitor = CdmStore.getProgressMonitorClientManager()
|
308
|
.pollMonitor(jobLabel,
|
309
|
monitorUuid,
|
310
|
50,
|
311
|
null,
|
312
|
(List)null,
|
313
|
subMonitor);
|
314
|
Object resultObject = remotingMonitor.getResult();
|
315
|
if(resultObject instanceof Exception){
|
316
|
MessagingUtils.errorDialog("Key generation failed", this, "Generating the polytomous key was not successfull", TaxeditorEditorPlugin.PLUGIN_ID, (Exception)resultObject, true, true);
|
317
|
}
|
318
|
} catch (InterruptedException e) {
|
319
|
return;
|
320
|
}
|
321
|
monitor.done();
|
322
|
});
|
323
|
job.schedule();
|
324
|
|
325
|
}
|
326
|
|
327
|
}
|