CdmTransientEntityCacher : overriding cache field only in the case of initialised...
[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 import org.eclipse.jface.dialogs.MessageDialog;
23
24 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
25 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
26 import eu.etaxonomy.cdm.api.service.DeleteResult;
27 import eu.etaxonomy.cdm.api.service.IClassificationService;
28 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
29 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.taxon.Classification;
32 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
33 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34 import eu.etaxonomy.taxeditor.operation.AbstractPersistentPostOperation;
35 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
36 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38
39 /**
40 * <p>DeleteTreeNodeOperation class.</p>
41 *
42 * @author n.hoffmann
43 * @created Jan 20, 2010
44 * @version 1.0
45 */
46 public class DeleteOperation extends AbstractPersistentPostOperation{
47
48 private Set<ITaxonTreeNode> treeNodes;
49 protected final TaxonDeletionConfigurator config;
50 private final ICdmEntitySessionEnabled cdmEntitySessionEnabled;
51
52 /**
53 * <p>Constructor for DeleteTreeNodeOperation.</p>
54 *
55 * @param label a {@link java.lang.String} object.
56 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
57 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
58 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
59 * @param treeNodes a {@link java.util.Set} object.
60 */
61 public DeleteOperation(String label, IUndoContext undoContext,
62 ITaxonTreeNode taxonNode, TaxonDeletionConfigurator config,
63 IPostOperationEnabled postOperationEnabled,
64 IConversationEnabled conversationEnabled,
65 ICdmEntitySessionEnabled cdmEntitySessionEnabled) {
66 super(label, undoContext, postOperationEnabled, conversationEnabled, cdmEntitySessionEnabled);
67 this.taxonNode = (ITaxonTreeNode)CdmBase.deproxy(taxonNode, CdmBase.class);
68 this.config = config;
69 this.cdmEntitySessionEnabled = cdmEntitySessionEnabled;
70 }
71
72 /**
73 * <p>Constructor for DeleteTreeNodeOperation.</p>
74 *
75 * @param label a {@link java.lang.String} object.
76 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
77 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
78 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
79 * @param treeNodes a {@link java.util.Set} object.
80 */
81 public DeleteOperation(String label, IUndoContext undoContext,
82 Set<ITaxonTreeNode> treeNodes, TaxonDeletionConfigurator config,
83 IPostOperationEnabled postOperationEnabled,
84 IConversationEnabled conversationEnabled,
85 ICdmEntitySessionEnabled cdmEntitySessionEnabled) {
86 super(label, undoContext, postOperationEnabled, conversationEnabled, cdmEntitySessionEnabled);
87 this.treeNodes = treeNodes;
88 this.config = config;
89 this.cdmEntitySessionEnabled = cdmEntitySessionEnabled;
90 }
91
92
93 /* (non-Javadoc)
94 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
95 */
96 /** {@inheritDoc} */
97 @Override
98 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
99 throws ExecutionException {
100
101 bind();
102 monitor.worked(20);
103 ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
104 ITaxonNodeService service = controller.getTaxonNodeService();
105 Set<CdmBase> affectedObjects = new HashSet<CdmBase>();
106
107 if(taxonNode != null && taxonNode instanceof TaxonNode){
108 //((TaxonNode) treeNode).delete();
109
110 element = ((TaxonNode)taxonNode).getTaxon();
111
112 DeleteResult result = service.deleteTaxonNode(taxonNode.getUuid(), config);
113 if (result.isError()){
114 //TODO:Error message!
115 MessageDialog.openError(null, "Delete failed", result.getExceptions().get(0).getMessage());
116 } else {
117 affectedObjects.add(((TaxonNode)taxonNode).getParent());
118 cdmEntitySessionEnabled.getCdmEntitySession().delete(taxonNode, affectedObjects);
119 }
120
121 }else if(taxonNode != null && taxonNode instanceof Classification){
122 Classification taxonomicTree = (Classification) taxonNode;
123 /*if(taxonomicTree.hasChildNodes()){
124 if(! MessageDialog.openConfirm(null, "Confirm Deletion", "The selected tree has children, do yu realy want to delete the whole tree with its children?")){
125 return null;
126 }
127 }*/
128
129 DeleteResult result = CdmStore.getService(IClassificationService.class).delete(taxonomicTree);
130 if (result.isError()){
131 //TODO:Error message!
132 MessageDialog.openError(null, "Delete failed", result.getExceptions().get(0).getMessage());
133 } else {
134 affectedObjects.add(((TaxonNode)taxonNode).getParent());
135 cdmEntitySessionEnabled.getCdmEntitySession().delete(taxonomicTree, affectedObjects);
136 }
137 } else {
138 Set<UUID> treeNodeUuids = new HashSet<UUID>();
139 for(ITaxonTreeNode treeNode : treeNodes) {
140 treeNodeUuids.add(treeNode.getUuid());
141 }
142 DeleteResult result = service.deleteTaxonNodes(treeNodeUuids, config);
143 if (result.isError()){
144 //TODO:Error message!
145 MessageDialog.openError(null, "Delete failed", result.getExceptions().get(0).getMessage());
146 }
147
148 for(ITaxonTreeNode treeNode : treeNodes) {
149 affectedObjects.add(((TaxonNode)treeNode).getParent());
150 }
151 cdmEntitySessionEnabled.getCdmEntitySession().delete(treeNodes, affectedObjects);
152 }
153
154 monitor.worked(40);
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 }