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
38
39
40
41
42 public class CopyBranchRequest extends ChangeRequest {
43
44 private static final long serialVersionUID = 1L;
45
46 public static final NodeConflictBehavior DEFAULT_NODE_CONFLICT_BEHAVIOR = NodeConflictBehavior.APPEND;
47
48 private final Location from;
49 private final Location into;
50 private final String fromWorkspace;
51 private final String intoWorkspace;
52 private final Name desiredNameForCopy;
53 private final NodeConflictBehavior nodeConflictBehavior;
54 private Location actualFromLocation;
55 private Location actualIntoLocation;
56
57
58
59
60
61
62
63
64
65
66
67 public CopyBranchRequest( Location from,
68 String fromWorkspace,
69 Location into,
70 String intoWorkspace ) {
71 this(from, fromWorkspace, into, intoWorkspace, null, DEFAULT_NODE_CONFLICT_BEHAVIOR);
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85 public CopyBranchRequest( Location from,
86 String fromWorkspace,
87 Location into,
88 String intoWorkspace,
89 Name nameForCopy ) {
90 this(from, fromWorkspace, into, intoWorkspace, nameForCopy, DEFAULT_NODE_CONFLICT_BEHAVIOR);
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public CopyBranchRequest( Location from,
107 String fromWorkspace,
108 Location into,
109 String intoWorkspace,
110 Name nameForCopy,
111 NodeConflictBehavior nodeConflictBehavior ) {
112 CheckArg.isNotNull(from, "from");
113 CheckArg.isNotNull(into, "into");
114 CheckArg.isNotNull(fromWorkspace, "fromWorkspace");
115 CheckArg.isNotNull(intoWorkspace, "intoWorkspace");
116 CheckArg.isNotNull(nodeConflictBehavior, "nodeConflictBehavior");
117 this.from = from;
118 this.into = into;
119 this.fromWorkspace = fromWorkspace;
120 this.intoWorkspace = intoWorkspace;
121 this.desiredNameForCopy = nameForCopy;
122 this.nodeConflictBehavior = nodeConflictBehavior;
123 }
124
125
126
127
128
129
130 public Location from() {
131 return from;
132 }
133
134
135
136
137
138
139 public Location into() {
140 return into;
141 }
142
143
144
145
146
147
148 public String fromWorkspace() {
149 return fromWorkspace;
150 }
151
152
153
154
155
156
157 public String intoWorkspace() {
158 return intoWorkspace;
159 }
160
161
162
163
164
165
166
167 public boolean isSameWorkspace() {
168 return fromWorkspace.equals(intoWorkspace);
169 }
170
171
172
173
174
175
176 public Name desiredName() {
177 return desiredNameForCopy;
178 }
179
180
181
182
183
184
185 @Override
186 public boolean isReadOnly() {
187 return false;
188 }
189
190
191
192
193
194
195
196 public NodeConflictBehavior nodeConflictBehavior() {
197 return nodeConflictBehavior;
198 }
199
200
201
202
203
204
205
206
207
208
209 public void setActualLocations( Location fromLocation,
210 Location intoLocation ) {
211 checkNotFrozen();
212 CheckArg.isNotNull(fromLocation, "intoLocation");
213 CheckArg.isNotNull(intoLocation, "intoLocation");
214 if (!fromLocation.hasPath()) {
215 throw new IllegalArgumentException(GraphI18n.actualOldLocationMustHavePath.text(fromLocation));
216 }
217 if (!intoLocation.hasPath()) {
218 throw new IllegalArgumentException(GraphI18n.actualNewLocationMustHavePath.text(intoLocation));
219 }
220 this.actualFromLocation = fromLocation;
221 this.actualIntoLocation = intoLocation;
222 }
223
224
225
226
227
228
229 public Location getActualLocationBefore() {
230 return actualFromLocation;
231 }
232
233
234
235
236
237
238 public Location getActualLocationAfter() {
239 return actualIntoLocation;
240 }
241
242
243
244
245
246
247 @Override
248 public boolean changes( String workspace,
249 Path path ) {
250 return this.intoWorkspace.equals(workspace) && into.hasPath() && into.getPath().isAtOrBelow(path);
251 }
252
253
254
255
256
257
258 @Override
259 public Location changedLocation() {
260 return actualIntoLocation != null ? actualIntoLocation : into;
261 }
262
263
264
265
266
267
268 @Override
269 public String changedWorkspace() {
270 return intoWorkspace();
271 }
272
273
274
275
276
277
278 @Override
279 public int hashCode() {
280 return HashCode.compute(from, fromWorkspace, into, intoWorkspace);
281 }
282
283
284
285
286
287
288 @Override
289 public void cancel() {
290 super.cancel();
291 this.actualFromLocation = null;
292 this.actualIntoLocation = null;
293 }
294
295
296
297
298
299
300 @Override
301 public boolean equals( Object obj ) {
302 if (obj == this) return true;
303 if (this.getClass().isInstance(obj)) {
304 CopyBranchRequest that = (CopyBranchRequest)obj;
305 if (!this.from().isSame(that.from())) return false;
306 if (!this.into().isSame(that.into())) return false;
307 if (!this.nodeConflictBehavior().equals(that.nodeConflictBehavior())) return false;
308 if (!this.fromWorkspace.equals(that.fromWorkspace)) return false;
309 if (!this.intoWorkspace.equals(that.intoWorkspace)) return false;
310 return true;
311 }
312 return false;
313 }
314
315
316
317
318
319
320 @Override
321 public String toString() {
322 if (fromWorkspace.equals(intoWorkspace)) {
323 if (desiredNameForCopy != null) {
324 return "copy branch " + from() + " in the \"" + fromWorkspace + "\" workspace into " + into() + " with name "
325 + desiredNameForCopy;
326 }
327 return "copy branch " + from() + " in the \"" + fromWorkspace + "\" workspace into " + into();
328 }
329 if (desiredNameForCopy != null) {
330 return "copy branch " + from() + " in the \"" + fromWorkspace + "\" workspace into " + into() + " with name "
331 + desiredNameForCopy + " in the \"" + intoWorkspace + "\" workspace";
332 }
333 return "copy branch " + from() + " in the \"" + fromWorkspace + "\" workspace into " + into() + " in the \""
334 + intoWorkspace + "\" workspace";
335 }
336
337
338
339
340
341
342
343
344
345 @Override
346 public CopyBranchRequest clone() {
347 CopyBranchRequest result = new CopyBranchRequest(actualFromLocation != null ? actualFromLocation : from, fromWorkspace,
348 actualIntoLocation != null ? actualIntoLocation : into, intoWorkspace,
349 desiredNameForCopy, nodeConflictBehavior);
350 result.setActualLocations(actualFromLocation, actualIntoLocation);
351 return result;
352 }
353
354 @Override
355 public RequestType getType() {
356 return RequestType.COPY_BRANCH;
357 }
358 }