Project

General

Profile

Download (7.06 KB) Statistics
| Branch: | Tag: | Revision:
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.Set;
14

    
15
import org.eclipse.core.commands.ExecutionException;
16
import org.eclipse.core.commands.operations.IUndoContext;
17
import org.eclipse.core.runtime.IAdaptable;
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.jface.dialogs.MessageDialog;
21

    
22
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
23
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
24
import eu.etaxonomy.cdm.api.service.DeleteResult;
25
import eu.etaxonomy.cdm.api.service.IClassificationService;
26
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
27
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.taxon.Classification;
30
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32
import eu.etaxonomy.taxeditor.model.MessagingUtils;
33
import eu.etaxonomy.taxeditor.operation.AbstractPersistentPostOperation;
34
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36

    
37
/**
38
 * <p>DeleteTreeNodeOperation class.</p>
39
 *
40
 * @author n.hoffmann
41
 * @created Jan 20, 2010
42
 * @version 1.0
43
 */
44
public class DeleteOperation extends AbstractPersistentPostOperation{
45

    
46
	private Set<ITaxonTreeNode> treeNodes;
47
	protected final TaxonDeletionConfigurator config;
48

    
49

    
50
	/**
51
	 * <p>Constructor for DeleteTreeNodeOperation.</p>
52
	 *
53
	 * @param label a {@link java.lang.String} object.
54
	 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
55
	 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
56
	 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
57
	 * @param treeNodes a {@link java.util.Set} object.
58
	 */
59
	public DeleteOperation(String label, IUndoContext undoContext,
60
			ITaxonTreeNode taxonNode, TaxonDeletionConfigurator config,
61
			IPostOperationEnabled postOperationEnabled,
62
			IConversationEnabled conversationEnabled) {
63
		super(label, undoContext, postOperationEnabled, conversationEnabled);
64
		this.taxonNode = (ITaxonTreeNode)CdmBase.deproxy(taxonNode, CdmBase.class);
65
		this.config = config;
66
	}
67

    
68
	/**
69
	 * <p>Constructor for DeleteTreeNodeOperation.</p>
70
	 *
71
	 * @param label a {@link java.lang.String} object.
72
	 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
73
	 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
74
	 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
75
	 * @param treeNodes a {@link java.util.Set} object.
76
	 */
77
	public DeleteOperation(String label, IUndoContext undoContext,
78
			Set<ITaxonTreeNode> treeNodes, TaxonDeletionConfigurator config,
79
			IPostOperationEnabled postOperationEnabled,
80
			IConversationEnabled conversationEnabled) {
81
		super(label, undoContext, postOperationEnabled, conversationEnabled);
82
		this.treeNodes = treeNodes;
83
		this.config = config;
84
	}
85

    
86

    
87
	/* (non-Javadoc)
88
	 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
89
	 */
90
	/** {@inheritDoc} */
91
	@Override
92
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
93
			throws ExecutionException {
94

    
95
		bind();
96
		monitor.worked(20);
97
		ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
98
		ITaxonNodeService service = controller.getTaxonNodeService();
99
			if(taxonNode != null && taxonNode instanceof TaxonNode){
100
				//((TaxonNode) treeNode).delete();
101

    
102
				element = ((TaxonNode)taxonNode).getTaxon();
103

    
104
				DeleteResult result = service.deleteTaxonNode((TaxonNode)taxonNode, config);
105
				if (result.isError() && !result.getExceptions().isEmpty()){
106
					//TODO:Error message!
107
					MessageDialog.openError(null, "Delete failed", result.getExceptions().iterator().next().getMessage());
108
				} else if (!result.getExceptions().isEmpty()){
109
					String separator = ", ";
110
				    String exceptionString = "";
111
				    int count = 1;
112
				    for (Exception exception : result.getExceptions()) {
113
				        exceptionString += exception.getLocalizedMessage();
114
				        if (count < result.getExceptions().size()){
115
				            exceptionString += separator;
116
				        }
117
			        }
118
					MessagingUtils.informationDialog("Delete of the node was successful but the taxon could not be deleted.", exceptionString);
119
				}
120

    
121

    
122
			}else if(taxonNode != null && taxonNode instanceof Classification){
123
				Classification taxonomicTree = (Classification) taxonNode;
124
				/*if(taxonomicTree.hasChildNodes()){
125
					if(! MessageDialog.openConfirm(null, "Confirm Deletion", "The selected tree has children, do yu realy want to delete the whole tree with its children?")){
126
						return null;
127
					}
128
				}*/
129

    
130
				DeleteResult result = CdmStore.getService(IClassificationService.class).delete(taxonomicTree);
131
				if (result.isError() && !result.getExceptions().isEmpty()){
132
				    String separator = ", ";
133
                    String exceptionString = "";
134
                    for (Exception exception : result.getExceptions()) {
135
                        exceptionString += exception.getLocalizedMessage()+separator;
136
                    }
137
				    MessagingUtils.error(null, exceptionString, result.getExceptions().iterator().next());
138
				}
139

    
140
				/*}else{
141
					try{
142
					CdmStore.getService(IClassificationService.class).delete(taxonomicTree);
143
					}catch(ReferencedObjectUndeletableException e){
144
						throw new ExecutionException(e.getMessage());
145
					}
146
				}*/
147
			} else {
148

    
149
				DeleteResult result =service.deleteTaxonNodes(treeNodes, config);
150
				if (result.isError() && !result.getExceptions().isEmpty()){
151
				    String separator = ", ";
152
                    String exceptionString = "";
153
                    for (Exception exception : result.getExceptions()) {
154
                        exceptionString += exception.getLocalizedMessage()+separator;
155
                    }
156
                    MessagingUtils.error(null, exceptionString, result.getExceptions().iterator().next());
157
				}
158
			}
159

    
160
		monitor.worked(40);
161
		return postExecute(null);
162
	}
163

    
164
	/* (non-Javadoc)
165
	 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
166
	 */
167
	/** {@inheritDoc} */
168
	@Override
169
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
170
			throws ExecutionException {
171
		return null;
172
	}
173

    
174
	/* (non-Javadoc)
175
	 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
176
	 */
177
	/** {@inheritDoc} */
178
	@Override
179
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
180
			throws ExecutionException {
181
		return null;
182
	}
183
}
(3-3/5)