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