51ff8ca90269c09c7744c205119682bada564bf1
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / operation / 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.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.reference.Reference;
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
23 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
24 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
25 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
26 import eu.etaxonomy.taxeditor.store.StoreUtil;
27
28 /**
29 * Deletes a relation between two taxa.
30 *
31 * TODO we assume that there is only one kind of relation between two taxa.
32 * If there are more, this operation breaks
33 *
34 * @author p.ciardelli
35 * @author n.hoffmann
36 * @created 29.01.2009
37 * @version 1.0
38 */
39 public class DeleteConceptRelationOperation extends AbstractPostOperation {
40
41 private Taxon relatedTaxon;
42 private TaxonRelationshipType relationshipType;
43 private Reference<?> citation;
44 private String microcitation;
45 private TaxonRelationship taxonRelationship;
46
47 /**
48 * <p>Constructor for DeleteConceptRelationOperation.</p>
49 *
50 * @param label a {@link java.lang.String} object.
51 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
52 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
53 * @param relatedTaxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
54 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
55 */
56 public DeleteConceptRelationOperation(String label,
57 IUndoContext undoContext, Taxon taxon, Taxon relatedTaxon,
58 IPostOperationEnabled postOperationEnabled) {
59 super(label, undoContext, taxon, postOperationEnabled);
60
61 this.relatedTaxon = relatedTaxon;
62
63 Set<TaxonRelationship> taxonRelationships = taxon.getTaxonRelations(relatedTaxon);
64
65 if(taxonRelationships.size() > 1){
66 StoreUtil.warningDialog("Multiple relations between taxa", this, "There are multiple relations between the " +
67 "accepted and the related taxon. This case is not handled by the software yet");
68 return;
69 }
70
71 taxonRelationship = taxonRelationships.iterator().next();
72
73 relationshipType = taxonRelationship.getType();
74
75 citation = taxonRelationship.getCitation();
76 microcitation = taxonRelationship.getCitationMicroReference();
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
81 */
82 /** {@inheritDoc} */
83 @Override
84 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
85 throws ExecutionException {
86
87 // Find relation, save citation information
88 for (TaxonRelationship relationship : taxon.getTaxonRelations()) {
89 if (relationship.getType().equals(relationshipType)
90 && relationship.getFromTaxon().equals(relatedTaxon)) {
91 citation = relationship.getCitation();
92 microcitation = relationship.getCitationMicroReference();
93 taxonRelationship = relationship;
94 }
95 }
96 monitor.worked(20);
97
98 // Remove relation from taxon
99 taxon.removeTaxonRelation(taxonRelationship);
100 monitor.worked(40);
101
102 return postExecute(taxon);
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 return execute(monitor, info);
113 }
114
115 /* (non-Javadoc)
116 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
117 */
118 /** {@inheritDoc} */
119 @Override
120 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
121 throws ExecutionException {
122
123 // relatedTaxon.addTaxonRelation(relationship); // don't do nothin'
124 taxon.addTaxonRelation(relatedTaxon, relationshipType, citation, microcitation);
125
126 return postExecute(relatedTaxon);
127 }
128 }