Fixed #812 - Add "Year" to ZoologicalNamePropertySource
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / operations / DeleteTaxonOperation.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 org.apache.log4j.Logger;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.IUndoContext;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.swt.widgets.Shell;
21
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.taxeditor.model.TaxonUtil;
24
25 /**
26 * Delete a taxon from the model.
27 *
28 * TODO: Currently all data in session data repository has to be saved before deleting
29 * a taxon and the whole logic is happening here. This way we would have to implement
30 * the same logic in another place if we want to delete outside of an operation context
31 * (NOT undoable). Therefore it is desirable to have the logic implemented in another
32 * place and merely call a method here.
33 *
34 * @author n.hoffmann
35 * @created 16.01.2009
36 * @version 1.0
37 */
38 public class DeleteTaxonOperation extends AbstractPostOperation {
39
40 @SuppressWarnings("unused")
41 private static Logger logger = Logger.getLogger(DeleteTaxonOperation.class);
42
43 /**
44 * The taxonomical parent of the taxon to be deleted.
45 */
46 private Taxon parentTaxon;
47
48 public DeleteTaxonOperation(String text, IUndoContext undoContext,
49 Taxon taxon) {
50 super(text, undoContext, taxon, null);
51
52 parentTaxon = taxon.getTaxonomicParent();
53 }
54
55 /* (non-Javadoc)
56 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
57 */
58 @Override
59 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
60 throws ExecutionException {
61
62 // Get taxon name - watch out for null names
63 String taxonName = "";
64 if (taxon.getName() != null) {
65 taxonName = taxon.getName().getTitleCache();
66 }
67
68 // Prompt user for confirmation
69 if(! MessageDialog.openConfirm((Shell) info.getAdapter(Shell.class), "Confirm Deletion", "Are you sure you want to delete taxon '" + taxonName + "'?")){
70 monitor.done();
71 return Status.CANCEL_STATUS;
72 }
73
74
75 // If the taxon has children, cancel operation
76 // TODO add option to continue, and delete children
77 if (taxon.hasTaxonomicChildren()) {
78 MessageDialog.openInformation((Shell) info.getAdapter(Shell.class), "Cannot delete taxon",
79 "'" + taxonName + "' has taxonomic children. " +
80 "These must be manually deleted before their parent.");
81 monitor.done();
82 return Status.CANCEL_STATUS;
83 }
84
85 // delete the taxon in an isolated conversation
86 TaxonUtil.deleteTaxonBaseIsolated(taxon.getUuid());
87
88
89 return postExecute(null);
90 }
91
92 /* (non-Javadoc)
93 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
94 */
95 @Override
96 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
97 throws ExecutionException {
98 return execute(monitor, info);
99 }
100
101 /* (non-Javadoc)
102 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
103 */
104 @Override
105 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
106 throws ExecutionException {
107 // FIXME we have to add old citation
108 parentTaxon.addTaxonomicChild(taxon, null, null);
109 // FIXME readding has to take place in an isolated conversation as well
110 // so that all the mediation magic does the rest for us
111 return postExecute(null);
112 }
113 }