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