1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.taxeditor.editor.view.derivate.handler;
|
11
|
|
12
|
import javax.inject.Named;
|
13
|
|
14
|
import org.eclipse.core.commands.ParameterizedCommand;
|
15
|
import org.eclipse.core.runtime.IStatus;
|
16
|
import org.eclipse.core.runtime.Status;
|
17
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
18
|
import org.eclipse.e4.core.di.annotations.Execute;
|
19
|
import org.eclipse.e4.core.di.annotations.Optional;
|
20
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
21
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
22
|
import org.eclipse.jface.viewers.TreeNode;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
25
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
26
|
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
|
27
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
30
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
31
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
32
|
import eu.etaxonomy.taxeditor.editor.AppModelId;
|
33
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
34
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
35
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
36
|
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
|
37
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
38
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
39
|
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
|
40
|
|
41
|
/**
|
42
|
*
|
43
|
* @author pplitzner
|
44
|
* @date Oct 21, 2014
|
45
|
*
|
46
|
*/
|
47
|
public class DeleteDerivateHandler {
|
48
|
|
49
|
|
50
|
private SpecimenDeleteConfigurator deleteConfigurator;
|
51
|
|
52
|
@Execute
|
53
|
public Object execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode treeNode, ParameterizedCommand command) {
|
54
|
deleteConfigurator = new SpecimenDeleteConfigurator();
|
55
|
if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){
|
56
|
deleteConfigurator.setDeleteChildren(true);
|
57
|
}
|
58
|
|
59
|
DerivateView derivateView = (DerivateView) part.getObject();
|
60
|
Object value = treeNode.getValue();
|
61
|
IStatus allowStatus = allowOperations(derivateView, treeNode);
|
62
|
if(allowStatus.isOK()) {
|
63
|
DeleteResult deleteResult;
|
64
|
if(value instanceof SingleRead
|
65
|
&& treeNode.getParent()!=null
|
66
|
&& treeNode.getParent().getValue() instanceof Sequence){
|
67
|
deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)value).getUuid(),
|
68
|
((Sequence) treeNode.getParent().getValue()).getUuid());
|
69
|
} else if(value instanceof Sequence){
|
70
|
deleteResult = CdmStore.getService(ISequenceService.class).delete(((Sequence) value).getUuid());
|
71
|
} else {
|
72
|
deleteResult = CdmStore.getService(IOccurrenceService.class).delete(((CdmBase) value).getUuid(), deleteConfigurator);
|
73
|
}
|
74
|
|
75
|
if (!deleteResult.isOk()) {
|
76
|
MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString());
|
77
|
|
78
|
return Status.CANCEL_STATUS;
|
79
|
}
|
80
|
if(derivateView.postOperation(null)){
|
81
|
derivateView.remove(treeNode);
|
82
|
|
83
|
}
|
84
|
return Status.OK_STATUS;
|
85
|
}
|
86
|
return null;
|
87
|
}
|
88
|
|
89
|
public IStatus allowOperations(DerivateView derivateView, TreeNode treeNode) {
|
90
|
String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE;
|
91
|
if(deleteConfigurator.isDeleteChildren()){
|
92
|
confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN;
|
93
|
}
|
94
|
|
95
|
if(derivateView.isDirty()){
|
96
|
MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
|
97
|
return Status.CANCEL_STATUS;
|
98
|
}
|
99
|
confirmationQuestion += "?"; //$NON-NLS-1$
|
100
|
if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){
|
101
|
return Status.CANCEL_STATUS;
|
102
|
}
|
103
|
DeleteResult deleteResult;
|
104
|
Object value = treeNode.getValue();
|
105
|
if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
|
106
|
if (value instanceof Sequence || value instanceof SingleRead){
|
107
|
deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
|
108
|
} else{
|
109
|
deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator);
|
110
|
}
|
111
|
if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;}
|
112
|
else{
|
113
|
if (!deleteResult.isOk()){
|
114
|
DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
|
115
|
} else {
|
116
|
if (!deleteResult.getExceptions().isEmpty()){
|
117
|
DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
return Status.CANCEL_STATUS;
|
122
|
}
|
123
|
}
|
124
|
return Status.CANCEL_STATUS;
|
125
|
}
|
126
|
|
127
|
@CanExecute
|
128
|
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node){
|
129
|
return node !=null;
|
130
|
}
|
131
|
|
132
|
}
|