merge
[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.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.model.MessagingUtils;
29 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
30 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32 import eu.etaxonomy.taxeditor.store.StoreUtil;
33
34 /**
35 * @author l.morris
36 * @date 22 Dec 2011
37 *
38 */
39 public class DeleteTermBaseOperation extends AbstractPostTaxonOperation {
40
41 private final TermEditorInput definedEditorInput;
42 private final TermBase termBase;
43 private DefinedTermEditor definedTermEditor;
44 /**
45 * @param label
46 * @param undoContext
47 * @param postOperationEnabled
48 */
49 public DeleteTermBaseOperation(String label,
50 IUndoContext undoContext,
51 TermBase termBase,
52 TermEditorInput definedEditorInput,
53 IPostOperationEnabled postOperationEnabled) {
54 super(label, undoContext, postOperationEnabled);
55 this.termBase = termBase;
56 this.definedEditorInput = definedEditorInput;
57 }
58
59 /* (non-Javadoc)
60 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
61 */
62 @Override
63 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
64 throws ExecutionException {
65
66
67
68 if (termBase instanceof TermVocabulary) {
69 if (((TermVocabulary)termBase).getCreatedBy() == null) {
70 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system vocabulary");
71 MessagingUtils.warningDialog("Cannot delete vocabulary", termBase, status);
72 return status;
73 }
74
75 if (!((TermVocabulary)termBase).getTerms().isEmpty()) {
76 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Delete all terms from this vocaulary before deleting the vocabulary.");
77 MessagingUtils.warningDialog("Vocabulary not empty", termBase, status);
78 return status;
79 }
80
81 definedEditorInput.getVocabularies().remove(termBase);
82
83 DeleteResult result = CdmStore.getService(IVocabularyService.class).delete(termBase.getUuid());
84 if (result.isError()){
85 StringBuffer errorString = new StringBuffer();
86 for (Exception e:result.getExceptions()){
87 errorString.append(e.getMessage() + "\\n");
88 }
89 MessageDialog.openError(null, "Delete failed", errorString.toString());
90 }
91
92 } else if (termBase instanceof DefinedTermBase) {
93
94
95 DefinedTermBase definedTermBase = (DefinedTermBase) termBase;
96
97 if (((DefinedTermBase)termBase).getCreatedBy() == null) {
98 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system defined term");
99 MessagingUtils.warningDialog("Cannot delete defined term", termBase, status);
100 return status;
101 }
102 if(!definedTermBase.getIncludes().isEmpty()){
103 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This term includes other terms. Please delete the included terms before deleting this term.");
104 MessagingUtils.warningDialog("Term has included terms", termBase, status);
105 return status;
106 }
107
108
109 DefinedTermBase partOf = definedTermBase.getPartOf();
110 if(partOf != null){
111 partOf.removeIncludes(definedTermBase);
112 }
113
114 DefinedTermBase kindOf = definedTermBase.getKindOf();
115 if(kindOf != null){
116 definedTermBase.removeGeneralization(kindOf);
117 }
118
119 TermVocabulary vocabulary = definedTermBase.getVocabulary();
120 if(vocabulary != null){
121 vocabulary.removeTerm(definedTermBase);
122 }
123
124 DeleteResult result = CdmStore.getService(ITermService.class).delete(termBase.getUuid());
125
126 if (result.isError()){
127 StringBuffer errorString = new StringBuffer();
128 for (Exception e:result.getExceptions()){
129 errorString.append(e.getMessage() + "\\n");
130 }
131 MessageDialog.openError(null, "Delete failed", errorString.toString());
132 }
133 }
134
135 return postExecute(termBase);
136 }
137
138 /* (non-Javadoc)
139 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
140 */
141 @Override
142 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
143 throws ExecutionException {
144 // TODO Auto-generated method stub
145 return null;
146 }
147
148 /* (non-Javadoc)
149 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
150 */
151 @Override
152 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
153 throws ExecutionException {
154 // TODO Auto-generated method stub
155 return null;
156 }
157
158 }