merged campanula branch to trunk. Main features are: BioCase Query via Imports, Deriv...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / operation / DeleteTermBaseOperation.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.editor.definedterm.operation;
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 import org.eclipse.core.runtime.Status;
18
19 import eu.etaxonomy.cdm.api.service.ITermService;
20 import eu.etaxonomy.cdm.api.service.IVocabularyService;
21 import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
22 import eu.etaxonomy.cdm.model.common.DefinedTerm;
23 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24 import eu.etaxonomy.cdm.model.common.TermBase;
25 import eu.etaxonomy.cdm.model.common.TermVocabulary;
26 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermEditor;
27 import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
28 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
29 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31 import eu.etaxonomy.taxeditor.store.StoreUtil;
32
33 /**
34 * @author l.morris
35 * @date 22 Dec 2011
36 *
37 */
38 public class DeleteTermBaseOperation extends AbstractPostTaxonOperation {
39
40 private TermEditorInput definedEditorInput;
41 private TermBase termBase;
42 private DefinedTermEditor definedTermEditor;
43 /**
44 * @param label
45 * @param undoContext
46 * @param postOperationEnabled
47 */
48 public DeleteTermBaseOperation(String label,
49 IUndoContext undoContext,
50 TermBase termBase,
51 TermEditorInput definedEditorInput,
52 IPostOperationEnabled postOperationEnabled) {
53 super(label, undoContext, postOperationEnabled);
54 this.termBase = termBase;
55 this.definedEditorInput = definedEditorInput;
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
66
67 if (termBase instanceof TermVocabulary) {
68 if (((TermVocabulary)termBase).getCreatedBy() == null) {
69 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system vocabulary");
70 StoreUtil.warningDialog("Cannot delete vocabulary", termBase, status);
71 return status;
72 }
73
74 if (!((TermVocabulary)termBase).getTerms().isEmpty()) {
75 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Delete all terms from this vocaulary before deleting the vocabulary.");
76 StoreUtil.warningDialog("Vocabulary not empty", termBase, status);
77 return status;
78 }
79
80 definedEditorInput.getVocabularies().remove((TermVocabulary)termBase);
81 try{
82 CdmStore.getService(IVocabularyService.class).delete((TermVocabulary)termBase);
83 } catch (DataChangeNoRollbackException e) {
84
85 throw new ExecutionException(e.getMessage());
86
87 }
88
89 } else if (termBase instanceof DefinedTermBase) {
90
91
92 DefinedTermBase definedTermBase = (DefinedTermBase) termBase;
93
94 if (((DefinedTermBase)termBase).getCreatedBy() == null) {
95 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system defined term");
96 StoreUtil.warningDialog("Cannot delete defined term", termBase, status);
97 return status;
98 }
99 if(!definedTermBase.getIncludes().isEmpty()){
100 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This term includes other terms. Please delete the included terms before deleting this term.");
101 StoreUtil.warningDialog("Term has included terms", termBase, status);
102 return status;
103 }
104
105
106 DefinedTermBase partOf = definedTermBase.getPartOf();
107 if(partOf != null){
108 partOf.removeIncludes(definedTermBase);
109 }
110
111 DefinedTermBase kindOf = definedTermBase.getKindOf();
112 if(kindOf != null){
113 definedTermBase.removeGeneralization(kindOf);
114 }
115
116 TermVocabulary vocabulary = definedTermBase.getVocabulary();
117 if(vocabulary != null){
118 vocabulary.removeTerm(definedTermBase);
119 }
120 try{
121 CdmStore.getService(ITermService.class).delete((DefinedTermBase)termBase);
122 } catch (DataChangeNoRollbackException e) {
123
124 throw new ExecutionException(e.getMessage());
125
126 }
127 }
128
129 return postExecute(termBase);
130 }
131
132 /* (non-Javadoc)
133 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
134 */
135 @Override
136 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
137 throws ExecutionException {
138 // TODO Auto-generated method stub
139 return null;
140 }
141
142 /* (non-Javadoc)
143 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
144 */
145 @Override
146 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
147 throws ExecutionException {
148 // TODO Auto-generated method stub
149 return null;
150 }
151
152 }