1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.request;
25
26 import org.modeshape.common.util.CheckArg;
27 import org.modeshape.common.util.HashCode;
28 import org.modeshape.graph.GraphI18n;
29 import org.modeshape.graph.Location;
30 import org.modeshape.graph.NodeConflictBehavior;
31 import org.modeshape.graph.property.Name;
32 import org.modeshape.graph.property.Path;
33
34
35
36
37 public class MoveBranchRequest extends ChangeRequest {
38
39 private static final long serialVersionUID = 1L;
40
41 public static final NodeConflictBehavior DEFAULT_CONFLICT_BEHAVIOR = NodeConflictBehavior.APPEND;
42
43 private final Location from;
44 private final Location into;
45 private final Location before;
46 private final String workspaceName;
47 private final Name desiredNameForNode;
48 private final NodeConflictBehavior conflictBehavior;
49 private Location actualOldLocation;
50 private Location actualNewLocation;
51
52
53
54
55
56
57
58
59
60 public MoveBranchRequest( Location from,
61 Location into,
62 String workspaceName ) {
63 this(from, into, null, workspaceName, null, DEFAULT_CONFLICT_BEHAVIOR);
64 }
65
66
67
68
69
70
71
72
73
74
75 public MoveBranchRequest( Location from,
76 Location into,
77 String workspaceName,
78 Name newNameForMovedNode ) {
79 this(from, into, null, workspaceName, newNameForMovedNode, DEFAULT_CONFLICT_BEHAVIOR);
80 }
81
82
83
84
85
86
87
88
89
90
91
92 public MoveBranchRequest( Location from,
93 Location into,
94 String workspaceName,
95 NodeConflictBehavior conflictBehavior ) {
96 this(from, into, null, workspaceName, null, conflictBehavior);
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public MoveBranchRequest( Location from,
113 Location into,
114 Location before,
115 String workspaceName,
116 Name newNameForMovedNode,
117 NodeConflictBehavior conflictBehavior ) {
118 CheckArg.isNotNull(from, "from");
119
120 CheckArg.isNotNull(workspaceName, "workspaceName");
121 CheckArg.isNotNull(conflictBehavior, "conflictBehavior");
122 this.from = from;
123 this.into = into;
124 this.before = before;
125 this.workspaceName = workspaceName;
126 this.desiredNameForNode = newNameForMovedNode;
127 this.conflictBehavior = conflictBehavior;
128 }
129
130
131
132
133
134
135 public Location from() {
136 return from;
137 }
138
139
140
141
142
143
144 public Location into() {
145 return into;
146 }
147
148
149
150
151
152
153 public Location before() {
154 return before;
155 }
156
157
158
159
160
161
162 public String inWorkspace() {
163 return workspaceName;
164 }
165
166
167
168
169
170
171 public Name desiredName() {
172 return desiredNameForNode;
173 }
174
175
176
177
178
179
180
181 public NodeConflictBehavior conflictBehavior() {
182 return conflictBehavior;
183 }
184
185
186
187
188
189
190 @Override
191 public boolean isReadOnly() {
192 return false;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public boolean hasNoEffect() {
212 if (into != null && into.hasPath() && into.hasIdProperties() == false && from.hasPath()) {
213 if (!from.getPath().getParent().equals(into.getPath())) return false;
214 if (desiredName() != null && !desiredName().equals(from.getPath().getLastSegment().getName())) return false;
215 if (before != null) return false;
216 return true;
217 }
218
219 return false;
220 }
221
222
223
224
225
226
227
228
229
230
231 public void setActualLocations( Location oldLocation,
232 Location newLocation ) {
233 checkNotFrozen();
234 CheckArg.isNotNull(oldLocation, "oldLocation");
235 CheckArg.isNotNull(newLocation, "newLocation");
236 if (!oldLocation.hasPath()) {
237 throw new IllegalArgumentException(GraphI18n.actualOldLocationMustHavePath.text(oldLocation));
238 }
239 if (!newLocation.hasPath()) {
240 throw new IllegalArgumentException(GraphI18n.actualNewLocationMustHavePath.text(newLocation));
241 }
242 Name actualNewName = newLocation.getPath().getLastSegment().getName();
243 Name expectedNewName = desiredName() != null ? desiredName() : oldLocation.getPath().getLastSegment().getName();
244 if (!actualNewName.equals(expectedNewName)) {
245 throw new IllegalArgumentException(GraphI18n.actualLocationNotEqualToInputLocation.text(newLocation, into));
246 }
247 this.actualOldLocation = oldLocation;
248 this.actualNewLocation = newLocation;
249 }
250
251
252
253
254
255
256 public Location getActualLocationBefore() {
257 return actualOldLocation;
258 }
259
260
261
262
263
264
265 public Location getActualLocationAfter() {
266 return actualNewLocation;
267 }
268
269
270
271
272
273
274 @Override
275 public boolean changes( String workspace,
276 Path path ) {
277 if (this.into() != null) {
278 return this.workspaceName.equals(workspace)
279 && (into.hasPath() && into.getPath().isAtOrBelow(path) || from.hasPath() && from.getPath().isAtOrBelow(path));
280 }
281
282 assert before() != null;
283 return this.workspaceName.equals(workspace)
284 && (before.hasPath() && before.getPath().getParent().isAtOrBelow(path) || from.hasPath()
285 && from.getPath().isAtOrBelow(path));
286
287 }
288
289
290
291
292
293
294
295
296
297 @Override
298 public Location changedLocation() {
299 return actualNewLocation != null ? actualNewLocation : into != null ? into : before;
300 }
301
302
303
304
305
306
307 @Override
308 public String changedWorkspace() {
309 return workspaceName;
310 }
311
312
313
314
315
316
317 @Override
318 public void cancel() {
319 super.cancel();
320 this.actualOldLocation = null;
321 this.actualNewLocation = null;
322 }
323
324
325
326
327
328
329 @Override
330 public int hashCode() {
331 return HashCode.compute(from, workspaceName, into);
332 }
333
334
335
336
337
338
339 @Override
340 public boolean equals( Object obj ) {
341 if (obj == this) return true;
342 if (this.getClass().isInstance(obj)) {
343 MoveBranchRequest that = (MoveBranchRequest)obj;
344 if (!this.from().isSame(that.from())) return false;
345 if (!this.into().isSame(that.into())) return false;
346 if (!this.conflictBehavior().equals(that.conflictBehavior())) return false;
347 if (!this.workspaceName.equals(that.workspaceName)) return false;
348 return true;
349 }
350 return false;
351 }
352
353
354
355
356
357
358 @Override
359 public String toString() {
360 if (desiredName() != null) {
361 return "move branch " + from() + " in the \"" + inWorkspace() + "\" workspace "
362 + (into() == null ? "before " + before() : "into " + into()) + " with name " + desiredName();
363 }
364 return "move branch " + from() + " in the \"" + inWorkspace() + "\" workspace into "
365 + (into() == null ? "before " + before() : "into " + into());
366 }
367
368
369
370
371
372
373
374
375
376 @Override
377 public MoveBranchRequest clone() {
378 MoveBranchRequest request = new MoveBranchRequest(actualOldLocation != null ? actualOldLocation : from, into, before,
379 workspaceName, desiredNameForNode, conflictBehavior);
380 request.setActualLocations(actualOldLocation, actualNewLocation);
381 return request;
382 }
383
384 @Override
385 public RequestType getType() {
386 return RequestType.MOVE_BRANCH;
387 }
388 }