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