0d8024b9f642df38236396f8a1bc1567b26d5504
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / cdm / SwapSynonymAndTaxonAction.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.actions.cdm;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.PartInitException;
18
19 import eu.etaxonomy.cdm.model.description.TaxonDescription;
20 import eu.etaxonomy.cdm.model.taxon.Synonym;
21 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
22 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
25 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
26 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
27 import eu.etaxonomy.taxeditor.controller.EditorController;
28 import eu.etaxonomy.taxeditor.controller.GlobalController;
29 import eu.etaxonomy.taxeditor.model.CdmUtil;
30
31 /**
32 * @author p.ciardelli
33 * @created 09.06.2008
34 * @version 1.0
35 */
36 public class SwapSynonymAndTaxonAction extends Action {
37 private static final Logger logger = Logger
38 .getLogger(SwapSynonymAndTaxonAction.class);
39
40 private static String text = "Make synonym accepted name for this taxon";
41 private static ImageDescriptor image = TaxEditorPlugin.getDefault()
42 .getImageDescriptor(ITaxEditorConstants.SYNONYM_TO_TAXON_ICON);
43
44 private Synonym synonym;
45 private Taxon taxon;
46
47 public SwapSynonymAndTaxonAction(){
48 super(text, image);
49 }
50
51 public SwapSynonymAndTaxonAction(Taxon taxon, Synonym synonym) {
52 this();
53 this.synonym = synonym;
54 this.taxon = taxon;
55 }
56
57 public void run() {
58
59 // TODO: Call a taxon service method similar to makeTaxonSynonym()
60 // instead of implementing the logic here
61
62 try {
63 IEditorPart oldEditor = EditorController.getEditorByTaxon(taxon);
64 if (oldEditor.isDirty()) {
65 if (!MessageDialog.openConfirm(GlobalController.getShell(), "Save before proceeding",
66 "All changes must be saved before proceeding with this action.\n\n" +
67 "Press \"OK\" to save and continue, or \"Cancel\" to cancel this action.")) {
68 return;
69 }
70 oldEditor.doSave(null);
71 }
72 } catch (PartInitException e) {
73 e.printStackTrace();
74 MessageDialog.openError(GlobalController.getShell(), "Error", "Error swapping synonym and taxon");
75 return;
76 }
77
78 taxon.removeSynonym(synonym);
79
80 Taxon parentTaxon = taxon.getTaxonomicParent();
81 Taxon newTaxon = Taxon.NewInstance(synonym.getName(), taxon.getSec());
82 newTaxon.setTaxonomicParent(parentTaxon, null, null);
83
84 Synonym newSynonym = Synonym.NewInstance(taxon.getName(), taxon.getSec());
85
86 if (CdmUtil.isNameHomotypic(newSynonym.getName(), newTaxon)) {
87 newTaxon.addSynonym(newSynonym, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
88 } else {
89 newTaxon.addSynonym(newSynonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
90 }
91
92 // Move the other synonyms from old accepted taxon to new accepted taxon
93 for (Synonym synonym: taxon.getSynonyms()) {
94 SynonymRelationshipType synRelType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
95 if (CdmUtil.isNameHomotypic(synonym.getName(), newTaxon) != true) {
96 synRelType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
97 }
98 newTaxon.addSynonym(synonym, synRelType);
99 taxon.removeSynonym(synonym);
100 }
101
102 // Move misapplied names
103 for (Taxon misappliedNameTaxon: taxon.getMisappliedNames()) {
104 newTaxon.addMisappliedName(misappliedNameTaxon,
105 misappliedNameTaxon.getName().getCitation(),
106 ""); //TODO: Set the microcitation
107 taxon.removeTaxon(misappliedNameTaxon, TaxonRelationshipType.MISAPPLIED_NAME_FOR());
108 }
109
110 // Move descriptions
111 for(TaxonDescription taxDescription : taxon.getDescriptions()){
112 newTaxon.addDescription(taxDescription);
113 taxon.removeDescription(taxDescription);
114 }
115
116 firePropertyChange(ITaxEditorConstants.TAXON, null, newTaxon);
117 }
118 }