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