24865969ae937a3694ebfa64c496682567b410a4
[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 import org.eclipse.jface.dialogs.MessageDialog;
19
20 import eu.etaxonomy.cdm.api.service.DeleteResult;
21 import eu.etaxonomy.cdm.api.service.ITermService;
22 import eu.etaxonomy.cdm.api.service.IVocabularyService;
23 import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
24 import eu.etaxonomy.cdm.model.common.DefinedTerm;
25 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
26 import eu.etaxonomy.cdm.model.common.TermBase;
27 import eu.etaxonomy.cdm.model.common.TermVocabulary;
28 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermEditor;
29 import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
30 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
31 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
32 import eu.etaxonomy.taxeditor.store.CdmStore;
33 import eu.etaxonomy.taxeditor.store.StoreUtil;
34
35 /**
36 * @author l.morris
37 * @date 22 Dec 2011
38 *
39 */
40 public class DeleteTermBaseOperation extends AbstractPostTaxonOperation {
41
42 private TermEditorInput definedEditorInput;
43 private TermBase termBase;
44 private DefinedTermEditor definedTermEditor;
45 /**
46 * @param label
47 * @param undoContext
48 * @param postOperationEnabled
49 */
50 public DeleteTermBaseOperation(String label,
51 IUndoContext undoContext,
52 TermBase termBase,
53 TermEditorInput definedEditorInput,
54 IPostOperationEnabled postOperationEnabled) {
55 super(label, undoContext, postOperationEnabled);
56 this.termBase = termBase;
57 this.definedEditorInput = definedEditorInput;
58 }
59
60 /* (non-Javadoc)
61 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
62 */
63 @Override
64 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
65 throws ExecutionException {
66
67
68
69 if (termBase instanceof TermVocabulary) {
70 if (((TermVocabulary)termBase).getCreatedBy() == null) {
71 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system vocabulary");
72 StoreUtil.warningDialog("Cannot delete vocabulary", termBase, status);
73 return status;
74 }
75
76 if (!((TermVocabulary)termBase).getTerms().isEmpty()) {
77 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Delete all terms from this vocaulary before deleting the vocabulary.");
78 StoreUtil.warningDialog("Vocabulary not empty", termBase, status);
79 return status;
80 }
81
82 definedEditorInput.getVocabularies().remove((TermVocabulary)termBase);
83
84 DeleteResult result = CdmStore.getService(IVocabularyService.class).delete((TermVocabulary)termBase);
85 if (result.isError()){
86 //TODO: Error message!
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
121 DeleteResult result = CdmStore.getService(ITermService.class).delete((DefinedTermBase)termBase);
122
123 if (result.isError()){
124 MessageDialog.openError(null, "Delete failed", result.getExceptions().get(0).getMessage());
125 }
126 }
127
128 return postExecute(termBase);
129 }
130
131 /* (non-Javadoc)
132 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
133 */
134 @Override
135 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
136 throws ExecutionException {
137 // TODO Auto-generated method stub
138 return null;
139 }
140
141 /* (non-Javadoc)
142 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
143 */
144 @Override
145 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
146 throws ExecutionException {
147 // TODO Auto-generated method stub
148 return null;
149 }
150
151 }