- fixed saving problem due to cloning of FieldUnit
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / operation / MoveDefinedTermOperation.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 java.util.Collection;
13
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20
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.cdm.model.description.DescriptionBase;
25 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
27 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
28 import eu.etaxonomy.taxeditor.store.StoreUtil;
29
30 /**
31 * @author l.morris
32 * @date 10 Jan 2012
33 *
34 */
35 public class MoveDefinedTermOperation extends AbstractPostOperation {
36
37 private Collection<DefinedTermBase> sourceTerms;// the actual DefinedTermBase(s) we are moving
38 private TermBase targetTermOrVocabulary;// the target VOCABULARY or DefinedTerm we are moving these to
39
40 /**
41 * @param label
42 * @param undoContext
43 * @param postOperationEnabled
44 */
45 public MoveDefinedTermOperation(String label, IUndoContext undoContext, TermBase target, Collection<DefinedTermBase> sourceTerms,
46 IPostOperationEnabled postOperationEnabled) {
47 super(label, undoContext, postOperationEnabled);
48
49 this.targetTermOrVocabulary = target;
50 this.sourceTerms = sourceTerms;
51 }
52
53 /* (non-Javadoc)
54 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
55 */
56 @Override
57 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
58 throws ExecutionException {
59
60 // need to make the moved DefinedTerm part of another DefinedTerm or Vocabulary (target)
61 // and remove it from old associations
62
63 //TODO move to ITermService
64
65 for (DefinedTermBase term : sourceTerms){
66 // do nothing when moving it on itself
67 if(targetTermOrVocabulary.equals(term)){
68 Status status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Term can not be added to itself");
69 StoreUtil.informationDialog("", status);
70 return status;
71 }
72
73 if (targetTermOrVocabulary instanceof TermVocabulary) {
74 TermVocabulary termVocabulary = (TermVocabulary)targetTermOrVocabulary;
75
76 // do nothing when term is top level and gets added to the same vocabulary
77 if(term.getPartOf() == null && termVocabulary.equals(term.getVocabulary())){
78 Status status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Term is already in this vocabulary");
79 StoreUtil.informationDialog("", status);
80 return status;
81 }
82
83 cleanTerm(term);
84 termVocabulary.addTerm(term);
85
86 } else if (targetTermOrVocabulary instanceof DefinedTermBase) {
87 cleanTerm(term);
88 DefinedTermBase targetDefinedTerm = (DefinedTermBase) targetTermOrVocabulary;
89 targetDefinedTerm.addIncludes(term);
90 targetDefinedTerm.getVocabulary().addTerm(term);
91 }
92
93 }
94 //return null;
95 return postExecute(targetTermOrVocabulary);
96 }
97
98 private DefinedTermBase cleanTerm(DefinedTermBase term){
99
100 DefinedTermBase partOf = term.getPartOf();
101 if(partOf != null){
102 partOf.removeIncludes(term);
103 }
104
105
106 TermVocabulary vocabulary = term.getVocabulary();
107 if(vocabulary != null){
108 vocabulary.removeTerm(term);
109 }
110
111 return term;
112 }
113
114
115 /* (non-Javadoc)
116 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
117 */
118 @Override
119 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
120 throws ExecutionException {
121 // TODO Auto-generated method stub
122 return null;
123 }
124
125 /* (non-Javadoc)
126 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
127 */
128 @Override
129 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
130 throws ExecutionException {
131 // TODO Auto-generated method stub
132 return null;
133 }
134
135 }