Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / operation / DeleteTermBaseOperation.java
1 /**
2 * Copyright (C) 2009 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 package eu.etaxonomy.taxeditor.editor.definedterm.operation;
10
11 import org.eclipse.core.commands.ExecutionException;
12 import org.eclipse.core.commands.operations.IUndoContext;
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.dialogs.MessageDialog;
18
19 import eu.etaxonomy.cdm.api.service.DeleteResult;
20 import eu.etaxonomy.cdm.api.service.ITermService;
21 import eu.etaxonomy.cdm.api.service.IVocabularyService;
22 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
23 import eu.etaxonomy.cdm.model.common.TermBase;
24 import eu.etaxonomy.cdm.model.common.TermVocabulary;
25 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermEditor;
26 import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
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 final TermEditorInput definedEditorInput;
41 private final 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 MessagingUtils.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 MessagingUtils.warningDialog("Vocabulary not empty", termBase, status);
77 return status;
78 }
79
80 definedEditorInput.getVocabularies().remove(termBase);
81
82 DeleteResult result = CdmStore.getService(IVocabularyService.class).delete(termBase.getUuid());
83 if (result.isError()){
84 StringBuffer errorString = new StringBuffer();
85 for (Exception e:result.getExceptions()){
86 errorString.append(e.getMessage() + "\\n");
87 }
88 MessageDialog.openError(null, "Delete failed", errorString.toString());
89 }
90
91 } else if (termBase instanceof DefinedTermBase) {
92
93
94 DefinedTermBase definedTermBase = (DefinedTermBase) termBase;
95
96 if (((DefinedTermBase)termBase).getCreatedBy() == null) {
97 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system defined term");
98 MessagingUtils.warningDialog("Cannot delete defined term", termBase, status);
99 return status;
100 }
101 if(!definedTermBase.getIncludes().isEmpty()){
102 IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This term includes other terms. Please delete the included terms before deleting this term.");
103 MessagingUtils.warningDialog("Term has included terms", termBase, status);
104 return status;
105 }
106
107
108 DefinedTermBase partOf = definedTermBase.getPartOf();
109 if(partOf != null){
110 partOf.removeIncludes(definedTermBase);
111 }
112
113 DefinedTermBase kindOf = definedTermBase.getKindOf();
114 if(kindOf != null){
115 definedTermBase.removeGeneralization(kindOf);
116 }
117
118 TermVocabulary vocabulary = definedTermBase.getVocabulary();
119 if(vocabulary != null){
120 vocabulary.removeTerm(definedTermBase);
121 }
122
123 DeleteResult result = CdmStore.getService(ITermService.class).delete(termBase.getUuid());
124
125 if (result.isError()){
126 StringBuffer errorString = new StringBuffer();
127 for (Exception e:result.getExceptions()){
128 errorString.append(e.getMessage() + "\\n");
129 }
130 MessageDialog.openError(null, "Delete failed", errorString.toString());
131 }
132 }
133
134 return postExecute(termBase);
135 }
136
137 /* (non-Javadoc)
138 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
139 */
140 @Override
141 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
142 throws ExecutionException {
143 // TODO Auto-generated method stub
144 return null;
145 }
146
147 /* (non-Javadoc)
148 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
149 */
150 @Override
151 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
152 throws ExecutionException {
153 // TODO Auto-generated method stub
154 return null;
155 }
156
157 }