minor
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / handler / GeneratePolytomousKeyHandler.java
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.List;
12 import java.util.Set;
13 import java.util.UUID;
14
15 import javax.inject.Named;
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.Job;
21 import org.eclipse.e4.core.di.annotations.CanExecute;
22 import org.eclipse.e4.core.di.annotations.Execute;
23 import org.eclipse.e4.ui.di.UISynchronize;
24 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
25 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
26 import org.eclipse.e4.ui.services.IServiceConstants;
27
28 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
29 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
30 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
31 import eu.etaxonomy.cdm.model.taxon.Taxon;
32 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
33 import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
34 import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrixPart;
35 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38 import eu.etaxonomy.taxeditor.store.StoreUtil;
39 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonSelectionDialog;
40
41 /**
42 * @author k.luther
43 * @since Jun 9, 2020
44 */
45 public class GeneratePolytomousKeyHandler {
46
47 @Execute
48 public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart, UISynchronize sync) {
49 // dependent on the selection the specimens are filtered
50 CharacterMatrixPart matrixPart = (CharacterMatrixPart) activePart.getObject();
51
52 CharacterMatrix matrix = matrixPart.getMatrix();
53 if(StoreUtil.promptCheckIsDirty(matrix.getPart())){
54 return;
55 }
56 Set<TaxonNodeDto> nodeDtos = matrix.getDescriptiveDataSet().getSubTreeFilter();
57
58 TaxonNodeDto parentDto = CdmStore.getService(ITaxonNodeService.class).findCommonParentDto(nodeDtos);
59 UUID taxonUuid = parentDto.getTaxonUuid();
60 int response = MessagingUtils.confirmDialog(
61 "Choose taxonomic scope",
62 String.format("The common parent taxon of this dataset is :\n%s\n\n"
63 + "Do you want to use this as the taxonomic scope for the polytomous key?"
64 , parentDto.getTaxonTitleCache()), "Yes", "Choose different taxon", "Cancel");
65 if(response==2){
66 return;
67 }
68 else if(response==1){
69 Taxon taxon = TaxonSelectionDialog.selectTaxon(matrix.getShell(), null);
70 if(taxon==null){
71 return;
72 }
73 taxonUuid = taxon.getUuid();
74 }
75
76
77 UUID datasetUuid = matrix.getDescriptiveDataSet().getUuid();
78 UUID monitorUuid = CdmApplicationState.getLongRunningTasksService().generatePolytomousKey(datasetUuid, taxonUuid);
79
80 String jobLabel = "Generate polytomous key";
81 Job job = Job.create(jobLabel, (ICoreRunnable) monitor -> {
82 SubMonitor subMonitor = SubMonitor.convert(monitor);
83 subMonitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
84 IRemotingProgressMonitor remotingMonitor;
85 try {
86 remotingMonitor = CdmStore.getProgressMonitorClientManager()
87 .pollMonitor(jobLabel,
88 monitorUuid,
89 50,
90 null,
91 (List)null,
92 subMonitor);
93 Object resultObject = remotingMonitor.getResult();
94 if(resultObject instanceof Exception){
95 MessagingUtils.errorDialog("Key generation failed", this, "Generating the polytomous key was not successfull", TaxeditorEditorPlugin.PLUGIN_ID, (Exception)resultObject, true, true);
96 }
97 } catch (InterruptedException e) {
98 return;
99 }
100 monitor.done();
101 });
102 job.schedule();
103 }
104
105 @CanExecute
106 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
107 MHandledMenuItem menuItem){
108 return true;
109 }
110 }