06a3013b87f9c25cb86ea782bb1d34bba62d9f5c
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / operation / ChangeHomotypicGroupOperation.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.hibernate.HibernateProxyHelper;
19 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
20 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
21 import eu.etaxonomy.cdm.model.taxon.Synonym;
22 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
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 * Change the homotypical group of a given synonym.
29 *
30 * @author n.hoffmann
31 * @created 19.01.2009
32 * @version 1.0
33 */
34 public class ChangeHomotypicGroupOperation extends AbstractPostTaxonOperation {
35
36 /**
37 * The synonym to be moved.
38 */
39 private final Synonym synonym;
40 /**
41 * The former homotypical group the synonym belonged to
42 */
43 private HomotypicalGroup oldHomotypicalGroup;
44 /**
45 * The homotypical group the synonym is to be moved to
46 */
47 private HomotypicalGroup newHomotypicalGroup;
48
49 /**
50 * <p>Constructor for ChangeHomotypicGroupOperation.</p>
51 *
52 * @param label a {@link java.lang.String} object.
53 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
54 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
55 * @param synonym a {@link eu.etaxonomy.cdm.model.taxon.Synonym} object.
56 * @param newHomotypicalGroup a {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup} object.
57 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
58 */
59 public ChangeHomotypicGroupOperation(String label, IUndoContext undoContext,
60 Taxon taxon, Synonym synonym, HomotypicalGroup newHomotypicalGroup, IPostOperationEnabled postOperationEnabled) {
61 super(label, undoContext, taxon, postOperationEnabled);
62
63 this.synonym = synonym;
64 if(synonym == null){
65 throw new IllegalArgumentException(
66 "A null synonym was provided.");
67 }
68
69 this.oldHomotypicalGroup = synonym.getHomotypicGroup();
70 this.newHomotypicalGroup = newHomotypicalGroup != null ? newHomotypicalGroup : HomotypicalGroup.NewInstance();
71 }
72
73 /* (non-Javadoc)
74 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
75 */
76 /** {@inheritDoc} */
77 @Override
78 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
79 throws ExecutionException {
80
81 // Get synonym name
82 TaxonNameBase<?, ?> synonymName = synonym.getName();
83 monitor.worked(20);
84
85 // TODO pass in homotypical group's taxon in case we are dragging from one editor to another
86
87 // Switch groups
88 oldHomotypicalGroup.removeTypifiedName(synonymName);
89 monitor.worked(40);
90 newHomotypicalGroup = HibernateProxyHelper.deproxy(newHomotypicalGroup, HomotypicalGroup.class);
91 newHomotypicalGroup.addTypifiedName(synonymName);
92
93 if(! synonym.getAcceptedTaxa().contains(element)){
94 for(Taxon acceptedTaxon : synonym.getAcceptedTaxa()){
95 acceptedTaxon.removeSynonym(synonym);
96 }
97
98 SynonymRelationshipType type = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
99 if(newHomotypicalGroup.getTypifiedNames().contains(element.getName())){
100 type = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
101 }
102
103 element.addSynonym(synonym, type);
104 }
105
106 // Redraw editor if it exists
107 return postExecute(synonym);
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
112 */
113 /** {@inheritDoc} */
114 @Override
115 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
116 throws ExecutionException {
117 return execute(monitor, info);
118 }
119
120 /* (non-Javadoc)
121 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
122 */
123 /** {@inheritDoc} */
124 @Override
125 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
126 throws ExecutionException {
127
128 // Get synonym name
129 TaxonNameBase<?, ?> synonymName = this.synonym.getName();
130 if(synonymName == null){
131 // TODO
132 }
133
134 // TODO pass in homotypical group's taxon in case we are dragging from one editor to another
135
136 // Switch groups
137 newHomotypicalGroup.removeTypifiedName(synonymName);
138 oldHomotypicalGroup.addTypifiedName(synonymName);
139
140 // Redraw editor if it exists
141 return postExecute(synonym);
142 }
143 }