merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / operation / ChangeConceptToSynonymOperation.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 java.util.Set;
13
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19
20 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
21 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
22 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
25 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
26 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
27 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
28 import eu.etaxonomy.taxeditor.store.StoreUtil;
29
30 /**
31 * <p>ChangeConceptToSynonymOperation class.</p>
32 *
33 * @author p.ciardelli
34 * @author n.hoffmann
35 * @created 15.01.2009
36 * @version 1.0
37 */
38 public class ChangeConceptToSynonymOperation extends
39 AbstractPostTaxonOperation {
40
41 private Taxon concept;
42
43 private HomotypicalGroup homotypicalGroup;
44
45 private TaxonRelationship taxonRelationship;
46 private TaxonRelationshipType oldRelationshipType;
47
48 private SynonymRelationship newSynonymRelationship;
49
50 /**
51 * <p>Constructor for ChangeConceptToSynonymOperation.</p>
52 *
53 * @param label a {@link java.lang.String} object.
54 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
55 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
56 * @param concept a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
57 * @param homotypicalGroup a {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup} object.
58 * @param editor a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
59 */
60 public ChangeConceptToSynonymOperation(String label,
61 IUndoContext undoContext, Taxon taxon, Taxon concept, HomotypicalGroup homotypicalGroup,
62 IPostOperationEnabled editor) {
63 super(label, undoContext, taxon, editor);
64
65 Set<TaxonRelationship> taxonRelationships = taxon.getTaxonRelations(concept);
66
67 if(taxonRelationships.size() > 1){
68 StoreUtil.warningDialog("Multiple relations between taxa", this, "There are multiple relations between the " +
69 "accepted and the related taxon. This case is not handled by the software yet");
70 return;
71 }
72
73 this.taxonRelationship = taxonRelationships.iterator().next();
74 this.oldRelationshipType = taxonRelationship.getType();
75
76 this.concept = concept;
77 this.homotypicalGroup = homotypicalGroup != null ? homotypicalGroup : HomotypicalGroup.NewInstance();
78 }
79
80 /* (non-Javadoc)
81 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
82 */
83 /** {@inheritDoc} */
84 @Override
85 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
86 throws ExecutionException {
87
88 // Create new synonym using concept name
89 TaxonNameBase<?, ?> synonymName = concept.getName();
90
91 // Remove concept relation from taxon
92 element.removeTaxon(concept, oldRelationshipType);
93 monitor.worked(20);
94
95 // Add name to new homotypic group
96 homotypicalGroup.addTypifiedName(synonymName);
97 monitor.worked(40);
98
99 // Create a new synonym for the taxon
100 newSynonymRelationship = element.addHeterotypicSynonymName(synonymName);
101
102 return postExecute(newSynonymRelationship.getSynonym());
103 }
104
105 /* (non-Javadoc)
106 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
107 */
108 /** {@inheritDoc} */
109 @Override
110 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
111 throws ExecutionException {
112 // TODO redo for change misapplied name ...
113 return null;
114 }
115
116 /* (non-Javadoc)
117 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
118 */
119 /** {@inheritDoc} */
120 @Override
121 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
122 throws ExecutionException {
123 // TODO undo for change misapplied name ...
124 return null;
125 }
126 }