Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / operation / CreateDefinedTermOperation.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
19 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
20 import eu.etaxonomy.cdm.model.common.TermBase;
21 import eu.etaxonomy.cdm.model.common.TermVocabulary;
22 import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
23 import eu.etaxonomy.taxeditor.model.MessagingUtils;
24 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
25 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
26 import eu.etaxonomy.taxeditor.store.CdmStore;
27 import eu.etaxonomy.taxeditor.store.StoreUtil;
28
29 /**
30 * @author l.morris
31 * @date 21 Dec 2011
32 *
33 */
34 public class CreateDefinedTermOperation extends AbstractPostTaxonOperation {
35
36
37
38 private final TermBase parentTermBase;
39 private final TermEditorInput definedTermInput;
40
41 /**
42 * @param label
43 * @param undoContext
44 * @param postOperationEnabled
45 */
46 public CreateDefinedTermOperation(String label,
47 IUndoContext undoContext,
48 TermBase termBase,
49 TermEditorInput definedTermInput,
50 IPostOperationEnabled postOperationEnabled) {
51 super(label, undoContext, postOperationEnabled);
52 this.parentTermBase = termBase;
53 this.definedTermInput = definedTermInput;
54 }
55
56 /* (non-Javadoc)
57 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
58 */
59 @Override
60 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
61 throws ExecutionException {
62
63 DefinedTermBase newTerm = definedTermInput.getTermType().getEmptyDefinedTermBase();
64 if (newTerm == null) {
65 IStatus status =
66 new Status(IStatus.CANCEL,
67 StoreUtil.getPluginId(),
68 "Creation of term corresponding to type '" + definedTermInput.getTermType().getMessage() + "' is not yet supported");
69 MessagingUtils.warningDialog("Cannot create term", newTerm, status);
70 return status;
71 }
72 newTerm = CdmStore.getCurrentApplicationConfiguration().getTermService().save(newTerm);
73
74 if (parentTermBase instanceof TermVocabulary){
75 TermVocabulary vocabulary = (TermVocabulary) parentTermBase;
76 vocabulary.addTerm(newTerm);
77 } else if (parentTermBase instanceof DefinedTermBase) {
78 DefinedTermBase parent = (DefinedTermBase) parentTermBase;
79 parent.addIncludes(newTerm);
80 TermVocabulary vocabulary = parent.getVocabulary();
81 vocabulary.addTerm(newTerm);
82 }
83
84 return postExecute(newTerm);
85 }
86
87 /* (non-Javadoc)
88 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
89 */
90 @Override
91 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
92 throws ExecutionException {
93 // TODO Auto-generated method stub
94 return null;
95 }
96
97 /* (non-Javadoc)
98 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
99 */
100 @Override
101 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
102 throws ExecutionException {
103 // TODO Auto-generated method stub
104 return null;
105 }
106
107 }