fix delete of taxon which is in two classifications and has children
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / operation / ChangeAcceptedTaxonToSynonymOperation.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 org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.IUndoContext;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19
20 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
21 import eu.etaxonomy.cdm.api.service.DeleteResult;
22 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
23 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
24 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28 import eu.etaxonomy.taxeditor.model.MessagingUtils;
29 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
30 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32
33 /**
34 * <p>ChangeAcceptedTaxonToSynonymOperation class.</p>
35 *
36 * @author n.hoffmann
37 * @created Jan 4, 2010
38 * @version 1.0
39 */
40 public class ChangeAcceptedTaxonToSynonymOperation extends
41 DeleteOperation {
42
43 private final TaxonNode newAcceptedTaxonNode;
44
45 //private TaxonNode oldTaxonNode;
46
47 /**
48 * <p>Constructor for ChangeAcceptedTaxonToSynonymOperation.</p>
49 *
50 * @param label a {@link java.lang.String} object.
51 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
52 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
53 * @param oldTaxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
54 * @param newAcceptedTaxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
55 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
56 */
57 public ChangeAcceptedTaxonToSynonymOperation(String label,
58 IUndoContext undoContext, ITaxonTreeNode oldTaxonNode, TaxonNode newAcceptedTaxonNode,
59 IPostOperationEnabled postOperationEnabled, IConversationEnabled conversationEnabled) {
60 super(label, undoContext, oldTaxonNode, new TaxonDeletionConfigurator(),postOperationEnabled, conversationEnabled);
61 this.newAcceptedTaxonNode = newAcceptedTaxonNode;
62 }
63
64
65 /* (non-Javadoc)
66 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
67 */
68 /** {@inheritDoc} */
69 @Override
70 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
71 throws ExecutionException {
72
73 monitor.worked(20);
74 bind();
75 Taxon oldTaxon = HibernateProxyHelper.deproxy(((TaxonNode) taxonNode).getTaxon(), Taxon.class);
76 Taxon newAcceptedTaxon = HibernateProxyHelper.deproxy(newAcceptedTaxonNode.getTaxon(), Taxon.class);
77
78 try {
79 DeleteResult result = CdmStore.getService(ITaxonNodeService.class).makeTaxonNodeASynonymOfAnotherTaxonNode((TaxonNode) taxonNode, newAcceptedTaxonNode, null, null, null);
80
81 if (!result.getExceptions().isEmpty() && result.isOk()){
82 String separator = ", ";
83 String exceptionString = "";
84 for (Exception exception : result.getExceptions()) {
85 exceptionString += exception.getLocalizedMessage()+separator;
86 }
87
88 MessagingUtils.informationDialog("Synonym created but taxon is not deleted.", exceptionString);
89 } else if (result.isAbort() || result.isError()){
90 MessagingUtils.errorDialog("Synonym could not created", null, result.toString(), TaxeditorNavigationPlugin.PLUGIN_ID, null, true);
91 }
92 } catch (IllegalArgumentException e) {
93 MessagingUtils.errorDialog("Operation failed", this, e.getMessage(), TaxeditorNavigationPlugin.PLUGIN_ID, e, false);
94 return Status.CANCEL_STATUS;
95 }
96
97 monitor.worked(40);
98
99 return postExecute(oldTaxon);
100 }
101
102 /* (non-Javadoc)
103 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
104 */
105 /** {@inheritDoc} */
106 @Override
107 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
108 throws ExecutionException {
109 // TODO Auto-generated method stub
110 return null;
111 }
112
113 /* (non-Javadoc)
114 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
115 */
116 /** {@inheritDoc} */
117 @Override
118 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
119 throws ExecutionException {
120 // TODO Auto-generated method stub
121 return null;
122 }
123 }