0b992d1d0c96aefb9d381212e84b8dea43752557
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / operations / DeleteConceptRelationOperation.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.store.operations;
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.reference.ReferenceBase;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
21 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
22 import eu.etaxonomy.taxeditor.store.model.TaxonUtil;
23
24 /**
25 * Deletes a relation between two taxa.
26 *
27 * TODO we assume that there is only one kind of relation between two taxa.
28 * If there are more, this operation breaks
29 *
30 * @author p.ciardelli
31 * @author n.hoffmann
32 * @created 29.01.2009
33 * @version 1.0
34 */
35 public class DeleteConceptRelationOperation extends AbstractPostOperation {
36
37 private Taxon relatedTaxon;
38 private TaxonRelationshipType relationshipType;
39 private ReferenceBase<?> citation;
40 private String microcitation;
41 private TaxonRelationship relationship;
42
43 public DeleteConceptRelationOperation(String label,
44 IUndoContext undoContext, Taxon taxon, Taxon relatedTaxon,
45 IPostOperationEnabled postOperationEnabled) {
46 super(label, undoContext, taxon, postOperationEnabled);
47
48 this.relatedTaxon = relatedTaxon;
49
50 relationship = TaxonUtil.getRelationshipBetweenTwoTaxa(relatedTaxon, taxon);
51
52 relationshipType = relationship.getType();
53
54 citation = relationship.getCitation();
55 microcitation = relationship.getCitationMicroReference();
56 }
57
58 /* (non-Javadoc)
59 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
60 */
61 @Override
62 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
63 throws ExecutionException {
64
65 // Find relation, save citation information
66 for (TaxonRelationship relationship : taxon.getTaxonRelations()) {
67 if (relationship.getType().equals(relationshipType)
68 && relationship.getFromTaxon().equals(relatedTaxon)) {
69 citation = relationship.getCitation();
70 microcitation = relationship.getCitationMicroReference();
71 this.relationship = relationship;
72 }
73 }
74
75 // Remove relation from taxon
76 taxon.removeTaxonRelation(relationship);
77
78 return postExecute(taxon);
79 }
80
81 /* (non-Javadoc)
82 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
83 */
84 @Override
85 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
86 throws ExecutionException {
87 return execute(monitor, info);
88 }
89
90 /* (non-Javadoc)
91 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
92 */
93 @Override
94 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
95 throws ExecutionException {
96
97 // relatedTaxon.addTaxonRelation(relationship); // don't do nothin'
98 taxon.addTaxonRelation(relatedTaxon, relationshipType, citation, microcitation);
99
100 return postExecute(relatedTaxon);
101 }
102 }