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