cd0fbef49e3ea40493493963de799a531c2337fe
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / operation / DeleteOperation.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.navigation.navigator.operation;
12
13 import java.util.HashSet;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22
23 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
24 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
25 import eu.etaxonomy.cdm.api.service.DeleteResult;
26 import eu.etaxonomy.cdm.api.service.IClassificationService;
27 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
28 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.taxon.Classification;
31 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
32 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
34 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
35 import eu.etaxonomy.taxeditor.operation.AbstractPersistentPostOperation;
36 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
37 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
38 import eu.etaxonomy.taxeditor.store.CdmStore;
39
40 /**
41 * <p>DeleteTreeNodeOperation class.</p>
42 *
43 * @author n.hoffmann
44 * @created Jan 20, 2010
45 * @version 1.0
46 */
47 public class DeleteOperation extends AbstractPersistentPostOperation{
48
49 private Set<ITaxonTreeNode> treeNodes;
50 protected final TaxonDeletionConfigurator config;
51 private final ICdmEntitySessionEnabled cdmEntitySessionEnabled;
52
53 /**
54 * <p>Constructor for DeleteTreeNodeOperation.</p>
55 *
56 * @param label a {@link java.lang.String} object.
57 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
58 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
59 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
60 * @param treeNodes a {@link java.util.Set} object.
61 */
62 public DeleteOperation(String label, IUndoContext undoContext,
63 ITaxonTreeNode taxonNode, TaxonDeletionConfigurator config,
64 IPostOperationEnabled postOperationEnabled,
65 IConversationEnabled conversationEnabled,
66 ICdmEntitySessionEnabled cdmEntitySessionEnabled) {
67 super(label, undoContext, postOperationEnabled, conversationEnabled, cdmEntitySessionEnabled);
68 this.taxonNode = (ITaxonTreeNode)CdmBase.deproxy(taxonNode, CdmBase.class);
69 this.config = config;
70 this.cdmEntitySessionEnabled = cdmEntitySessionEnabled;
71 }
72
73 /**
74 * <p>Constructor for DeleteTreeNodeOperation.</p>
75 *
76 * @param label a {@link java.lang.String} object.
77 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
78 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
79 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
80 * @param treeNodes a {@link java.util.Set} object.
81 */
82 public DeleteOperation(String label, IUndoContext undoContext,
83 Set<ITaxonTreeNode> treeNodes, TaxonDeletionConfigurator config,
84 IPostOperationEnabled postOperationEnabled,
85 IConversationEnabled conversationEnabled,
86 ICdmEntitySessionEnabled cdmEntitySessionEnabled) {
87 super(label, undoContext, postOperationEnabled, conversationEnabled, cdmEntitySessionEnabled);
88 this.treeNodes = treeNodes;
89 this.config = config;
90 this.cdmEntitySessionEnabled = cdmEntitySessionEnabled;
91 }
92
93
94
95
96 /* (non-Javadoc)
97 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
98 */
99 /** {@inheritDoc} */
100 @Override
101 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
102 throws ExecutionException {
103
104 bind();
105 monitor.worked(20);
106 ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
107 ITaxonNodeService service = controller.getTaxonNodeService();
108 if(taxonNode != null && taxonNode instanceof TaxonNode){
109 //((TaxonNode) treeNode).delete();
110
111 element = ((TaxonNode)taxonNode).getTaxon();
112
113 DeleteResult result = service.deleteTaxonNode((TaxonNode)taxonNode, config);
114 if (result.isError() && !result.getExceptions().isEmpty()){
115
116 DeleteResultMessagingUtils.messageDialogWithDetails(result, "TaxonNode could not be deleted.", TaxeditorNavigationPlugin.PLUGIN_ID);
117 } else if (!result.getExceptions().isEmpty()){
118 String separator = ", ";
119 String exceptionString = "";
120 for (Exception exception : result.getExceptions()) {
121 exceptionString += exception.getLocalizedMessage()+separator;
122 }
123 DeleteResultMessagingUtils.messageDialogWithDetails(result, "TaxonNode was successfully deleted, but related object(s) could not be deleted", TaxeditorNavigationPlugin.PLUGIN_ID);
124 }
125
126
127 }else if(taxonNode != null && taxonNode instanceof Classification){
128 Classification taxonomicTree = (Classification) taxonNode;
129
130 DeleteResult result = CdmStore.getService(IClassificationService.class).delete(taxonomicTree.getUuid());
131 if (result.isError() && !result.getExceptions().isEmpty()){
132 //TODO:Error message!
133 DeleteResultMessagingUtils.messageDialogWithDetails(result, "TaxonNode could not be deleted.", TaxeditorNavigationPlugin.PLUGIN_ID);
134 }
135 } else {
136 Set<UUID> treeNodeUuids = new HashSet<UUID>();
137 ITaxonTreeNode entity = null;
138 for(ITaxonTreeNode treeNode : treeNodes) {
139 if(entity == null) {
140 entity = treeNode;
141 }
142 treeNodeUuids.add(treeNode.getUuid());
143 }
144
145 DeleteResult result = service.deleteTaxonNodes(treeNodeUuids, config);
146 if (result.isError() && !result.getExceptions().isEmpty()){
147 //TODO:Error message!
148 DeleteResultMessagingUtils.messageDialogWithDetails(result, "TaxonNode could not be deleted.", TaxeditorNavigationPlugin.PLUGIN_ID);
149 }
150
151 }
152
153 monitor.worked(40);
154
155 return postExecute(null);
156 }
157
158 /* (non-Javadoc)
159 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
160 */
161 /** {@inheritDoc} */
162 @Override
163 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
164 throws ExecutionException {
165 return null;
166 }
167
168 /* (non-Javadoc)
169 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
170 */
171 /** {@inheritDoc} */
172 @Override
173 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
174 throws ExecutionException {
175 return null;
176 }
177 }