Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / operation / CreateSynonymInExistingHomotypicalGroupOperation.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.editor.name.operation;
11
12 import org.eclipse.core.commands.ExecutionException;
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17
18 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
19 import eu.etaxonomy.cdm.model.name.NonViralName;
20 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
23 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
24
25 /**
26 * <p>CreateSynonymInExistingHomotypicalGroupOperation class.</p>
27 *
28 * @author n.hoffmann
29 * @created 02.02.2009
30 * @version 1.0
31 */
32 public class CreateSynonymInExistingHomotypicalGroupOperation extends
33 AbstractPostTaxonOperation {
34
35 private final HomotypicalGroup group;
36 private final NonViralName newSynonymName;
37
38 private SynonymRelationship synonymRelationship;
39
40 /**
41 * <p>Constructor for CreateSynonymInExistingHomotypicalGroupOperation.</p>
42 *
43 * @param label a {@link java.lang.String} object.
44 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
45 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
46 * @param group a {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup} object.
47 * @param newSynonymName a {@link eu.etaxonomy.cdm.model.name.TaxonNameBase} object.
48 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
49 */
50 public CreateSynonymInExistingHomotypicalGroupOperation(String label,
51 IUndoContext undoContext, Taxon taxon, HomotypicalGroup group, NonViralName newSynonymName, IPostOperationEnabled postOperationEnabled) {
52 super(label, undoContext, taxon, postOperationEnabled);
53 this.group = group;
54 this.newSynonymName = newSynonymName;
55 }
56
57 /* (non-Javadoc)
58 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
59 */
60 /** {@inheritDoc} */
61 @Override
62 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
63 throws ExecutionException {
64
65 // Add name to given homotypic group
66 group.addTypifiedName(newSynonymName);
67 monitor.worked(20);
68
69 // Create a new synonym for the taxon
70 // TODO add citations
71 if(group.equals(element.getHomotypicGroup())){
72 synonymRelationship = element.addHomotypicSynonymName(newSynonymName, null, null);
73 }else{
74 synonymRelationship = element.addHeterotypicSynonymName(newSynonymName);
75 }
76 monitor.worked(40);
77
78 // Synonym affectedSynonym = null;
79 //
80 // for(Synonym synonym : group.getSynonymsInGroup(taxon.getSec())){
81 // if(synonym.getName() == synonymName){
82 // affectedSynonym = synonym;
83 // break;
84 // }
85 // }
86
87 return postExecute(synonymRelationship.getSynonym());
88 }
89
90 /* (non-Javadoc)
91 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
92 */
93 /** {@inheritDoc} */
94 @Override
95 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
96 throws ExecutionException {
97 return execute(monitor, info);
98 }
99
100 /* (non-Javadoc)
101 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
102 */
103 /** {@inheritDoc} */
104 @Override
105 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
106 throws ExecutionException {
107
108 element.removeSynonymRelation(synonymRelationship);
109
110 return postExecute(null);
111 }
112 }