Merge branch 'release/3.12.0'
[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 import org.eclipse.jface.viewers.ViewerDropAdapter;
21
22 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
23 import eu.etaxonomy.cdm.model.common.OrderedTermBase;
24 import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
25 import eu.etaxonomy.cdm.model.common.TermBase;
26 import eu.etaxonomy.cdm.model.common.TermVocabulary;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
28 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
29 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
30 import eu.etaxonomy.taxeditor.store.StoreUtil;
31
32 /**
33 * @author l.morris
34 * @date 10 Jan 2012
35 *
36 */
37 public class MoveDefinedTermOperation extends AbstractPostTaxonOperation {
38
39 private final Collection<DefinedTermBase> sourceTerms;// the actual DefinedTermBase(s) we are moving
40 private final TermBase targetTermOrVocabulary;// the target VOCABULARY or DefinedTerm we are moving these to
41 private final int currentLocation;
42
43 /**
44 * @param label
45 * @param undoContext
46 * @param postOperationEnabled
47 */
48 public MoveDefinedTermOperation(String label,
49 IUndoContext undoContext,
50 TermBase target,
51 Collection<DefinedTermBase> sourceTerms,
52 IPostOperationEnabled postOperationEnabled,
53 int currentLocation) {
54 super(label, undoContext, postOperationEnabled);
55
56 this.targetTermOrVocabulary = target;
57 this.sourceTerms = sourceTerms;
58 this.currentLocation = currentLocation;
59 }
60
61 /* (non-Javadoc)
62 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
63 */
64 @Override
65 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
66 throws ExecutionException {
67
68 // need to make the moved DefinedTerm part of another DefinedTerm or Vocabulary (target)
69 // and remove it from old associations
70
71 //TODO move to ITermService
72
73 for (DefinedTermBase term : sourceTerms) {
74 // do nothing when moving it on itself
75 if(targetTermOrVocabulary.equals(term)){
76 Status status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Term can not be added to itself");
77 MessagingUtils.informationDialog("", status);
78 return status;
79 }
80
81 if (targetTermOrVocabulary instanceof TermVocabulary) {
82 TermVocabulary termVocabulary = (TermVocabulary)targetTermOrVocabulary;
83
84 // do nothing when term is top level and gets added to the same vocabulary
85 if(term.getPartOf() == null && termVocabulary.equals(term.getVocabulary())){
86 Status status = new Status(IStatus.CANCEL, StoreUtil.getPluginId(), "Term is already in this vocabulary");
87 MessagingUtils.informationDialog("", status);
88 return status;
89 }
90
91 cleanTerm(term);
92 termVocabulary.addTerm(term);
93
94 } else if (targetTermOrVocabulary instanceof DefinedTermBase) {
95 cleanTerm(term);
96 DefinedTermBase targetDefinedTerm = (DefinedTermBase) targetTermOrVocabulary;
97
98 if(targetTermOrVocabulary instanceof OrderedTermBase && term instanceof OrderedTermBase) {
99 TermVocabulary tVoc = ((DefinedTermBase) targetTermOrVocabulary).getVocabulary();
100 if(tVoc instanceof OrderedTermVocabulary) {
101 OrderedTermVocabulary otVoc = (OrderedTermVocabulary)tVoc;
102 // the link between the location and the add term (below / above)
103 // method is determined by the compare method in the
104 // DefinedTermEditor's ViewerSorter (DefinedTermSorter) class
105 if(currentLocation == ViewerDropAdapter.LOCATION_BEFORE) {
106 otVoc.addTermBelow((OrderedTermBase)term, (OrderedTermBase)targetTermOrVocabulary);
107 ((DefinedTermBase) targetTermOrVocabulary).getPartOf().addIncludes(term);
108 }
109
110 if(currentLocation == ViewerDropAdapter.LOCATION_AFTER) {
111 otVoc.addTermAbove((OrderedTermBase)term, (OrderedTermBase)targetTermOrVocabulary);
112 ((DefinedTermBase) targetTermOrVocabulary).getPartOf().addIncludes(term);
113 }
114 if(currentLocation == ViewerDropAdapter.LOCATION_ON) {
115 targetDefinedTerm.addIncludes(term);
116 targetDefinedTerm.getVocabulary().addTerm(term);
117 }
118 }
119 } else{
120 targetDefinedTerm.addIncludes(term);
121 targetDefinedTerm.getVocabulary().addTerm(term);
122 }
123
124 }
125
126 }
127 //return null;
128 return postExecute(targetTermOrVocabulary);
129 }
130
131 private DefinedTermBase cleanTerm(DefinedTermBase term){
132
133 DefinedTermBase partOf = term.getPartOf();
134 if(partOf != null){
135 partOf.removeIncludes(term);
136 }
137
138
139 TermVocabulary vocabulary = term.getVocabulary();
140 if(vocabulary != null){
141 vocabulary.removeTerm(term);
142 }
143
144 return term;
145 }
146
147
148 /* (non-Javadoc)
149 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
150 */
151 @Override
152 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
153 throws ExecutionException {
154 // TODO Auto-generated method stub
155 return null;
156 }
157
158 /* (non-Javadoc)
159 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
160 */
161 @Override
162 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
163 throws ExecutionException {
164 // TODO Auto-generated method stub
165 return null;
166 }
167
168 }