6a8187a4d94f6ba11ea8247765b5cba59edbc45c
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / operation / 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.navigation.navigator.operation;
11
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22
23 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
24 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
25 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
26 import eu.etaxonomy.cdm.model.taxon.IllegalAncestryException;
27 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeEvent.EventType;
29 import eu.etaxonomy.taxeditor.model.MessagingUtils;
30 import eu.etaxonomy.taxeditor.operation.AbstractPersistentPostOperation;
31 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
32 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * Change the taxonomic parent of a given taxon.
37 *
38 * @author n.hoffmann
39 * @created 16.01.2009
40 * @version 1.0
41 */
42 public class MoveTaxonOperation extends AbstractPersistentPostOperation {
43
44 /**
45 * A reference to the new taxonomical parent.
46 */
47 private final ITaxonTreeNode newParentTreeNode;
48 /**
49 * A reference to the former taxonomical parents
50 */
51 private final Map<TaxonNode, ITaxonTreeNode> oldParentTreeNodes;
52
53 private final Set<TaxonNode> taxonNodes;
54
55 /**
56 * <p>Constructor for MoveTaxonOperation.</p>
57 *
58 * @param label a {@link java.lang.String} object.
59 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
60 * @param taxonNodes a {@link java.util.Set} object.
61 * @param newParentTreeNode a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
62 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
63 * @param conversationEnabled a {@link eu.etaxonomy.cdm.api.conversation.IConversationEnabled} object.
64 */
65 public MoveTaxonOperation(String label,
66 IUndoContext undoContext,
67 Set<TaxonNode> taxonNodes,
68 ITaxonTreeNode newParentTreeNode,
69 IPostOperationEnabled postOperationEnabled,
70 IConversationEnabled conversationEnabled,
71 ICdmEntitySessionEnabled cdmEntitySessionEnabled) {
72 super(label, undoContext, postOperationEnabled, conversationEnabled, cdmEntitySessionEnabled);
73
74 this.taxonNodes = taxonNodes;
75
76 this.newParentTreeNode = newParentTreeNode;
77
78 // Save old parent ITaxonTreeNodes for undo
79 oldParentTreeNodes = new HashMap<TaxonNode, ITaxonTreeNode>();
80 for(TaxonNode taxonNode : taxonNodes){
81 this.oldParentTreeNodes.put(taxonNode, taxonNode.getParent());
82 }
83 }
84
85 /* (non-Javadoc)
86 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
87 */
88 /** {@inheritDoc} */
89 @Override
90 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
91 throws ExecutionException {
92 bind();
93 monitor.worked(20);
94
95 try {
96 for (TaxonNode taxonNode : taxonNodes){
97 TaxonNode newTaxonNode = newParentTreeNode.addChildNode(taxonNode,
98 newParentTreeNode.getReference(), newParentTreeNode.getMicroReference());
99 taxonNodes.add(newTaxonNode);
100
101 monitor.worked(2);
102 }
103 CdmStore.getService( ITaxonNodeService.class).merge((TaxonNode)newParentTreeNode);
104 CdmStore.getCurrentSessionManager().getActiveSession().addEvent(newParentTreeNode, EventType.UPDATE);
105 } catch(IllegalAncestryException e) {
106 MessagingUtils.warningDialog("Illegal ancestry", this, e.getMessage());
107 }
108 monitor.worked(40);
109
110 return postExecute(null);
111 }
112
113 /* (non-Javadoc)
114 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
115 */
116 /** {@inheritDoc} */
117 @Override
118 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
119 throws ExecutionException {
120 return execute(monitor, info);
121 }
122
123 /* (non-Javadoc)
124 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
125 */
126 /** {@inheritDoc} */
127 @Override
128 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
129 throws ExecutionException {
130 MessagingUtils.warn(this.getClass(), "Not implemented yet.");
131
132 // iterate over oldParentTreeNodes, delete each TaxonNode from its actual parent and add to its former parent
133
134 return Status.OK_STATUS;
135 }
136 }