Project

General

Profile

Download (4.94 KB) Statistics
| Branch: | Tag: | Revision:
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.e4.DefinedTermEditorE4;
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

    
43
	public DeleteTermBaseOperation(String label,
44
			IUndoContext undoContext,
45
			TermBase termBase,
46
			TermEditorInput definedEditorInput,
47
			IPostOperationEnabled postOperationEnabled) {
48
		super(label, undoContext, postOperationEnabled);
49
		this.termBase = termBase;
50
		this.definedEditorInput = definedEditorInput;
51
	}
52

    
53
	@Override
54
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
55
			throws ExecutionException {
56
		if (termBase instanceof TermVocabulary) {
57
			if (((TermVocabulary)termBase).getCreatedBy() == null) {
58
				IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system vocabulary");
59
				MessagingUtils.warningDialog("Cannot delete vocabulary", termBase, status);
60
				return status;
61
			}
62

    
63
			if (!((TermVocabulary)termBase).getTerms().isEmpty()) {
64
				IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Delete all terms from this vocaulary before deleting the vocabulary.");
65
				MessagingUtils.warningDialog("Vocabulary not empty", termBase, status);
66
				return status;
67
			}
68

    
69
			definedEditorInput.getVocabularies().remove(termBase);
70

    
71
			DeleteResult result =	CdmStore.getService(IVocabularyService.class).delete(termBase.getUuid());
72
			if (result.isError()){
73
				StringBuffer errorString = new StringBuffer();
74
				for (Exception e:result.getExceptions()){
75
					errorString.append(e.getMessage() + "\\n");
76
				}
77
				MessageDialog.openError(null, "Delete failed", errorString.toString());
78
			}
79

    
80
		} else if (termBase instanceof DefinedTermBase) {
81

    
82

    
83
			DefinedTermBase definedTermBase = (DefinedTermBase) termBase;
84

    
85
			if (((DefinedTermBase)termBase).getCreatedBy() == null) {
86
				IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This is a CDM system defined term");
87
				MessagingUtils.warningDialog("Cannot delete defined term", termBase, status);
88
				return status;
89
			}
90
			if(!definedTermBase.getIncludes().isEmpty()){
91
				IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "This term includes other terms. Please delete the included terms before deleting this term.");
92
				MessagingUtils.warningDialog("Term has included terms", termBase, status);
93
				return status;
94
			}
95

    
96

    
97
			DefinedTermBase partOf = definedTermBase.getPartOf();
98
			if(partOf != null){
99
				partOf.removeIncludes(definedTermBase);
100
			}
101

    
102
			DefinedTermBase kindOf = definedTermBase.getKindOf();
103
			if(kindOf != null){
104
				definedTermBase.removeGeneralization(kindOf);
105
			}
106

    
107
			TermVocabulary vocabulary = definedTermBase.getVocabulary();
108
			if(vocabulary != null){
109
				vocabulary.removeTerm(definedTermBase);
110
			}
111

    
112
			DeleteResult result =	CdmStore.getService(ITermService.class).delete(termBase.getUuid());
113

    
114
			if (result.isError()){
115
				StringBuffer errorString = new StringBuffer();
116
				for (Exception e:result.getExceptions()){
117
					errorString.append(e.getMessage() + "\\n");
118
				}
119
				MessageDialog.openError(null, "Delete failed", errorString.toString());
120
			}
121
		}
122

    
123
		return postExecute(termBase);
124
	}
125

    
126
	@Override
127
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
128
			throws ExecutionException {
129
		// TODO Auto-generated method stub
130
		return null;
131
	}
132

    
133
	@Override
134
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
135
			throws ExecutionException {
136
		// TODO Auto-generated method stub
137
		return null;
138
	}
139

    
140
}
(3-3/4)