Project

General

Profile

Download (5.04 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

    
18
import eu.etaxonomy.cdm.api.service.DeleteResult;
19
import eu.etaxonomy.cdm.api.service.ITermService;
20
import eu.etaxonomy.cdm.api.service.IVocabularyService;
21
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
22
import eu.etaxonomy.cdm.model.common.TermBase;
23
import eu.etaxonomy.cdm.model.common.TermVocabulary;
24
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
25
import eu.etaxonomy.taxeditor.l10n.Messages;
26
import eu.etaxonomy.taxeditor.model.MessagingUtils;
27
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
28
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
29
import eu.etaxonomy.taxeditor.store.CdmStore;
30
import eu.etaxonomy.taxeditor.store.StoreUtil;
31
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
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(), Messages.DeleteTermBaseOperation_SYSTEM_VOC);
59
				MessagingUtils.warningDialog(Messages.DeleteTermBaseOperation_CANNOT_DELETE_VOC, termBase, status);
60
				return status;
61
			}
62

    
63
			if (!((TermVocabulary)termBase).getTerms().isEmpty()) {
64
				IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), Messages.DeleteTermBaseOperation_DELETE_ALL_TERMS_BEFORE);
65
				MessagingUtils.warningDialog(Messages.DeleteTermBaseOperation_VOC_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.isOk()){
73
			    return showErrorMessage(result);
74
			}
75

    
76
		} else if (termBase instanceof DefinedTermBase) {
77

    
78

    
79
			DefinedTermBase definedTermBase = (DefinedTermBase) termBase;
80

    
81
			if (((DefinedTermBase)termBase).getCreatedBy() == null) {
82
				IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), Messages.DeleteTermBaseOperation_SYSTEM_TERM);
83
				MessagingUtils.warningDialog(Messages.DeleteTermBaseOperation_CANNOT_DELETE_TERM, termBase, status);
84
				return status;
85
			}
86
			if(!definedTermBase.getIncludes().isEmpty()){
87
				IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), Messages.DeleteTermBaseOperation_TERM_INCLUDES_OTHERS);
88
				MessagingUtils.warningDialog(Messages.DeleteTermBaseOperation_TERM_INLCUDES, termBase, status);
89
				return status;
90
			}
91

    
92
			DeleteResult result =	CdmStore.getService(ITermService.class).delete(termBase.getUuid());
93
			if (!result.isOk()){
94
			    return showErrorMessage(result);
95
            }
96
		}
97

    
98
		return postExecute(termBase);
99
	}
100

    
101
	private IStatus showErrorMessage(DeleteResult result) {
102
	    StringBuffer errorString = new StringBuffer();
103
        for (Exception e:result.getExceptions()){
104
            errorString.append(e.getMessage() + System.lineSeparator());
105
        }
106
        IStatus status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), errorString.toString());
107
        String delete_failed = Messages.DeleteTermBaseOperation_DELETE_FAILED;
108
        if (result.isAbort()){
109
            MessagingUtils.warningDialog(delete_failed, this, status);
110
        }else{
111
            if (result.getExceptions().iterator().hasNext()){
112
                MessagingUtils.errorDialog(delete_failed, this.getClass(),null, TaxeditorStorePlugin.PLUGIN_ID, result.getExceptions().iterator().next(), true);
113
            }else{
114
                MessagingUtils.warningDialog(delete_failed, this, status);
115
            }
116
        }
117
        return status;
118
    }
119

    
120
    @Override
121
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
122
			throws ExecutionException {
123
		return null;
124
	}
125

    
126
	@Override
127
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
128
			throws ExecutionException {
129
		return null;
130
	}
131

    
132
}
(3-3/4)