1
|
/**
|
2
|
* Copyright (C) 2020 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.handler;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Iterator;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import javax.inject.Named;
|
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.Job;
|
22
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
23
|
import org.eclipse.e4.core.di.annotations.Execute;
|
24
|
import org.eclipse.e4.ui.di.UISynchronize;
|
25
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
26
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
27
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
28
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
29
|
import org.eclipse.jface.window.Window;
|
30
|
import org.eclipse.jface.wizard.WizardDialog;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
33
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
34
|
import eu.etaxonomy.cdm.api.service.description.AggregationMode;
|
35
|
import eu.etaxonomy.cdm.api.service.description.StructuredDescriptionAggregationConfiguration;
|
36
|
import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
|
37
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
38
|
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
|
39
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
40
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
41
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
42
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
43
|
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
|
44
|
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrixPart;
|
45
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
46
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
47
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
48
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
49
|
import eu.etaxonomy.taxeditor.ui.dialog.configurator.StructuredAggregationConfigurationWizard;
|
50
|
|
51
|
/**
|
52
|
* @author k.luther
|
53
|
* @since Jun 4, 2020
|
54
|
*/
|
55
|
public class AggregationHandler {
|
56
|
@Execute
|
57
|
public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart, UISynchronize sync) {
|
58
|
|
59
|
// dependent on the selection the specimens are filtered
|
60
|
CharacterMatrixPart matrixPart = (CharacterMatrixPart) activePart.getObject();
|
61
|
|
62
|
CharacterMatrix matrix = matrixPart.getMatrix();
|
63
|
|
64
|
StructuredDescriptionAggregationConfiguration config = StructuredDescriptionAggregationConfiguration.NewInstance(
|
65
|
null, null);
|
66
|
IStructuredSelection sel = matrix.getSelection();
|
67
|
// Object o = sel.getFirstElement();
|
68
|
List<TaxonNodeDto> nodeDtos = new ArrayList<>();
|
69
|
Iterator iterator = sel.iterator();
|
70
|
while (iterator.hasNext()){
|
71
|
Object o = iterator.next();
|
72
|
if (o instanceof TaxonNodeDto){
|
73
|
nodeDtos.add((TaxonNodeDto)o);
|
74
|
}
|
75
|
}
|
76
|
TaxonNodeFilter filter = TaxonNodeFilter.NewRankInstance(matrix.getDescriptiveDataSet().getMinRank(),
|
77
|
matrix.getDescriptiveDataSet().getMaxRank());
|
78
|
for (NamedArea namedArea : matrix.getDescriptiveDataSet().getGeoFilter()) {
|
79
|
filter = filter.orArea(namedArea.getUuid());
|
80
|
}
|
81
|
|
82
|
|
83
|
if (!nodeDtos.isEmpty()){
|
84
|
for (TaxonNodeDto dto: nodeDtos){
|
85
|
filter = filter.orSubtree(dto.getUuid());
|
86
|
}
|
87
|
}else{
|
88
|
for (TaxonNode taxonNode : matrix.getDescriptiveDataSet().getTaxonSubtreeFilter()) {
|
89
|
filter = filter.orSubtree(taxonNode);
|
90
|
}
|
91
|
}
|
92
|
config.setTaxonNodeFilter(filter);
|
93
|
|
94
|
config.setDatasetUuid(matrix.getDescriptiveDataSet().getUuid());
|
95
|
config.setAggregateToHigherRanks(true);
|
96
|
config.setAggregationMode(AggregationMode.byAreasAndRanks());
|
97
|
config.setAdaptBatchSize(false);
|
98
|
TaxonNodeDto node = null;
|
99
|
|
100
|
StructuredAggregationConfigurationWizard aggregationWizard = new StructuredAggregationConfigurationWizard(config, matrix.getDescriptiveDataSet(), nodeDtos);
|
101
|
WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
|
102
|
aggregationWizard);
|
103
|
|
104
|
int open = dialog.open();
|
105
|
if (open != Window.OK){
|
106
|
return;
|
107
|
}
|
108
|
UUID monitorUuid = CdmApplicationState.getLongRunningTasksService().invoke(config);
|
109
|
|
110
|
String jobLabel = "Aggregate Descriptive Data Set";
|
111
|
Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
|
112
|
SubMonitor subMonitor = SubMonitor.convert(monitor);
|
113
|
subMonitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
|
114
|
IRemotingProgressMonitor remotingMonitor;
|
115
|
try {
|
116
|
remotingMonitor = CdmStore.getProgressMonitorClientManager()
|
117
|
.pollMonitor(jobLabel,
|
118
|
monitorUuid,
|
119
|
50,
|
120
|
null,
|
121
|
(List)null,
|
122
|
subMonitor);
|
123
|
Object resultObject = remotingMonitor.getResult();
|
124
|
if(resultObject instanceof Exception){
|
125
|
MessagingUtils.errorDialog("Aggregation failed", this, "Aggregation was not successfull", TaxeditorEditorPlugin.PLUGIN_ID, (Exception)resultObject, true, true);
|
126
|
}
|
127
|
else if(resultObject instanceof UpdateResult){
|
128
|
DescriptiveDataSet dataSet = (DescriptiveDataSet) ((UpdateResult) resultObject).getCdmEntity();
|
129
|
dataSet = matrix.getCdmEntitiySession().load(dataSet, true);
|
130
|
// update local dataset
|
131
|
matrix.setDescriptiveDataSet(dataSet);
|
132
|
matrix.loadDescriptions(false, false);
|
133
|
}
|
134
|
} catch (InterruptedException e) {
|
135
|
return;
|
136
|
}
|
137
|
monitor.done();
|
138
|
});
|
139
|
// job.addJobChangeListener(new JobChangeAdapter(){
|
140
|
// @Override
|
141
|
// public void done(IJobChangeEvent event) {
|
142
|
// CharacterMatrixBottomToolbar.this.getDisplay().asyncExec(()->{
|
143
|
// matrix.redraw();
|
144
|
// });
|
145
|
// }
|
146
|
// });
|
147
|
job.schedule();
|
148
|
}
|
149
|
|
150
|
@CanExecute
|
151
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
|
152
|
MHandledMenuItem menuItem){
|
153
|
CharacterMatrixPart matrixPart = (CharacterMatrixPart) activePart.getObject();
|
154
|
IStructuredSelection selection = matrixPart.getSelection();
|
155
|
boolean canExecute = (selection.isEmpty() ||
|
156
|
!(selection.getFirstElement() instanceof TaxonRowWrapperDTO) );
|
157
|
|
158
|
return canExecute;
|
159
|
|
160
|
}
|
161
|
}
|