17c6d8850ad79e57429094cde5beb41a4c8f1dc0
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / operations / MoveTaxonOperation.java
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.operations;
11
12 import java.util.UUID;
13
14 import org.apache.log4j.Logger;
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.core.runtime.Status;
21
22 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
23 import eu.etaxonomy.cdm.model.taxon.ITreeNode;
24 import eu.etaxonomy.cdm.model.taxon.IllegalAncestryException;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.taxeditor.store.CdmStore;
27 import eu.etaxonomy.taxeditor.store.StoreUtil;
28
29 /**
30 * Change the taxonomic parent of a given taxon.
31 *
32 * @author n.hoffmann
33 * @created 16.01.2009
34 * @version 1.0
35 */
36 public class MoveTaxonOperation extends AbstractPostOperation {
37
38 @SuppressWarnings("unused")
39 private static final Logger logger = Logger.getLogger(MoveTaxonOperation.class);
40 /**
41 * A reference to the new taxonomical parent.
42 */
43 private ITreeNode newParentTreeNode;
44 /**
45 * A reference to the former taxonomical parent
46 */
47 private ITreeNode oldParentTreeNode;
48
49 public MoveTaxonOperation(String label, IUndoContext undoContext,
50 TaxonNode taxonNode, ITreeNode newParentTreeNode, IPostOperationEnabled postOperationEnabled) {
51 super(label, undoContext, taxonNode, null);
52
53 this.newParentTreeNode = newParentTreeNode;
54
55 // Save old parent ITreeNode for undo
56 this.oldParentTreeNode = taxonNode.getParent();
57 }
58
59 /* (non-Javadoc)
60 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
61 */
62 @Override
63 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
64 throws ExecutionException {
65
66 TaxonNode newTaxonNode = moveTaxonBaseIsolated(taxonNode.getUuid(), newParentTreeNode.getUuid());
67
68 return postExecute(newTaxonNode);
69 }
70
71 /* (non-Javadoc)
72 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
73 */
74 @Override
75 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
76 throws ExecutionException {
77 return execute(monitor, info);
78 }
79
80 /* (non-Javadoc)
81 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
82 */
83 @Override
84 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
85 throws ExecutionException {
86
87 moveTaxonBaseIsolated(taxonNode.getUuid(), oldParentTreeNode.getUuid());
88
89 return Status.OK_STATUS;
90 }
91
92 /**
93 *
94 * @param taxonNodeUuid
95 * @param newParentTaxonNodeUuid
96 * @return
97 */
98 private TaxonNode moveTaxonBaseIsolated(UUID taxonNodeUuid, UUID newParentTreeNodeUuid){
99
100 // get a new conversation
101 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
102 try{
103 // find the node
104 TaxonNode taxonNode = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNodeUuid);
105
106 // find the new parent node
107 ITreeNode newParentTreeNode = CdmStore.getTaxonTreeService().getTreeNodeByUuid(newParentTreeNodeUuid);
108 // add node to new parent
109 try{
110 TaxonNode newChild = newParentTreeNode.addChildNode(taxonNode, newParentTreeNode.getReference(), newParentTreeNode.getMicroReference(), taxonNode.getSynonymToBeUsed());
111 // commit the conversation
112 conversation.commit(true);
113 return newChild;
114 }catch(IllegalAncestryException e){
115 StoreUtil.warningDialog("Illegal ancestry", e.getMessage());
116 }
117 return null;
118 }finally{
119 // FIXME closing this conversation also closes the conversation of the
120 // taxonomic tree. Removing the close call for sage of a smooth presentation
121 // conversation.close();
122 }
123 }
124 }