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 java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import java.util.LinkedList;
31 import java.util.List;
32 import org.modeshape.common.util.CheckArg;
33 import org.modeshape.common.util.HashCode;
34 import org.modeshape.graph.GraphI18n;
35 import org.modeshape.graph.Location;
36 import org.modeshape.graph.NodeConflictBehavior;
37 import org.modeshape.graph.property.Name;
38 import org.modeshape.graph.property.Path;
39 import org.modeshape.graph.property.Property;
40
41
42
43
44 public class CreateNodeRequest extends ChangeRequest implements Iterable<Property> {
45
46 private static final long serialVersionUID = 1L;
47
48 public static final NodeConflictBehavior DEFAULT_CONFLICT_BEHAVIOR = NodeConflictBehavior.APPEND;
49
50 private final Location under;
51 private final String workspaceName;
52 private final Name childName;
53 private final List<Property> properties;
54 private final NodeConflictBehavior conflictBehavior;
55 private Location actualLocation;
56
57
58
59
60
61
62
63
64
65
66
67 public CreateNodeRequest( Location parentLocation,
68 String workspaceName,
69 Name childName,
70 Property... properties ) {
71 this(parentLocation, workspaceName, childName, DEFAULT_CONFLICT_BEHAVIOR, properties);
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public CreateNodeRequest( Location parentLocation,
85 String workspaceName,
86 Name childName,
87 Iterable<Property> properties ) {
88 this(parentLocation, workspaceName, childName, DEFAULT_CONFLICT_BEHAVIOR, properties);
89 }
90
91
92
93
94
95
96
97
98
99
100
101 public CreateNodeRequest( Location parentLocation,
102 String workspaceName,
103 Name childName,
104 Iterator<Property> properties ) {
105 this(parentLocation, workspaceName, childName, DEFAULT_CONFLICT_BEHAVIOR, properties);
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public CreateNodeRequest( Location parentLocation,
121 String workspaceName,
122 Name childName,
123 NodeConflictBehavior conflictBehavior,
124 Property... properties ) {
125 CheckArg.isNotNull(parentLocation, "parentLocation");
126 CheckArg.isNotNull(workspaceName, "workspaceName");
127 CheckArg.isNotNull(conflictBehavior, "conflictBehavior");
128 CheckArg.isNotNull(childName, "childName");
129 this.under = parentLocation;
130 this.workspaceName = workspaceName;
131 this.childName = childName;
132 this.conflictBehavior = conflictBehavior;
133 int number = properties.length + (under.hasIdProperties() ? under.getIdProperties().size() : 0);
134 List<Property> props = new ArrayList<Property>(number);
135 for (Property property : properties) {
136 if (property != null) props.add(property);
137 }
138 this.properties = Collections.unmodifiableList(props);
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public CreateNodeRequest( Location parentLocation,
154 String workspaceName,
155 Name childName,
156 NodeConflictBehavior conflictBehavior,
157 Iterable<Property> properties ) {
158 CheckArg.isNotNull(parentLocation, "parentLocation");
159 CheckArg.isNotNull(workspaceName, "workspaceName");
160 CheckArg.isNotNull(conflictBehavior, "conflictBehavior");
161 CheckArg.isNotNull(childName, "childName");
162 this.under = parentLocation;
163 this.workspaceName = workspaceName;
164 this.childName = childName;
165 this.conflictBehavior = conflictBehavior;
166 List<Property> props = new LinkedList<Property>();
167 for (Property property : properties) {
168 if (property != null) props.add(property);
169 }
170 this.properties = Collections.unmodifiableList(props);
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public CreateNodeRequest( Location parentLocation,
186 String workspaceName,
187 Name childName,
188 NodeConflictBehavior conflictBehavior,
189 Iterator<Property> properties ) {
190 CheckArg.isNotNull(parentLocation, "parentLocation");
191 CheckArg.isNotNull(workspaceName, "workspaceName");
192 CheckArg.isNotNull(conflictBehavior, "conflictBehavior");
193 CheckArg.isNotNull(childName, "childName");
194 this.under = parentLocation;
195 this.workspaceName = workspaceName;
196 this.childName = childName;
197 this.conflictBehavior = conflictBehavior;
198 List<Property> props = new LinkedList<Property>();
199 while (properties.hasNext()) {
200 Property property = properties.next();
201 if (property != null) props.add(property);
202 }
203 this.properties = Collections.unmodifiableList(props);
204 }
205
206
207
208
209
210
211 public Location under() {
212 return under;
213 }
214
215
216
217
218
219
220 public String inWorkspace() {
221 return workspaceName;
222 }
223
224
225
226
227
228
229 public Name named() {
230 return childName;
231 }
232
233
234
235
236
237
238 public Iterator<Property> iterator() {
239 return this.properties.iterator();
240 }
241
242
243
244
245
246
247
248 public Collection<Property> properties() {
249 return properties;
250 }
251
252
253
254
255
256
257
258 public NodeConflictBehavior conflictBehavior() {
259 return conflictBehavior;
260 }
261
262
263
264
265
266
267 @Override
268 public boolean isReadOnly() {
269 return false;
270 }
271
272
273
274
275
276
277
278
279
280
281
282 public void setActualLocationOfNode( Location actual ) {
283 checkNotFrozen();
284 CheckArg.isNotNull(actual, "actual");
285 if (!under.equals(actual, false)) {
286 }
287 assert actual != null;
288 if (!actual.hasPath()) {
289 throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
290 }
291 assert actual.hasPath();
292 if (under.hasPath() && !under.getPath().equals(actual.getPath().getParent())) {
293 throw new IllegalArgumentException(GraphI18n.actualLocationNotEqualToInputLocation.text(actual, under));
294 }
295 this.actualLocation = actual;
296 }
297
298
299
300
301
302
303 public Location getActualLocationOfNode() {
304 return actualLocation;
305 }
306
307
308
309
310
311
312 @Override
313 public boolean changes( String workspace,
314 Path path ) {
315 return this.workspaceName.equals(workspace) && under.hasPath() && under.getPath().isAtOrBelow(path);
316 }
317
318
319
320
321
322
323 @Override
324 public Location changedLocation() {
325 return actualLocation != null ? actualLocation : under;
326 }
327
328
329
330
331
332
333 @Override
334 public String changedWorkspace() {
335 return workspaceName;
336 }
337
338
339
340
341
342
343 @Override
344 public void cancel() {
345 super.cancel();
346 this.actualLocation = null;
347 }
348
349
350
351
352
353
354 @Override
355 public int hashCode() {
356 return HashCode.compute(under, childName, workspaceName);
357 }
358
359
360
361
362
363
364 @Override
365 public boolean equals( Object obj ) {
366 if (obj == this) return true;
367 if (this.getClass().isInstance(obj)) {
368 CreateNodeRequest that = (CreateNodeRequest)obj;
369 if (!this.under().isSame(that.under())) return false;
370 if (!this.conflictBehavior().equals(that.conflictBehavior())) return false;
371 if (!this.inWorkspace().equals(that.conflictBehavior())) return false;
372 if (!this.properties().equals(that.properties())) return false;
373 return true;
374 }
375 return false;
376 }
377
378
379
380
381
382
383 @Override
384 public String toString() {
385 String parent = under() + "/";
386 if (under.hasPath() && under.getPath().isRoot()) parent = "/";
387 return "create in the \"" + workspaceName + "\" workspace the node \"" + parent + childName + "\" with properties "
388 + properties();
389 }
390
391
392
393
394
395
396
397
398
399 @Override
400 public CreateNodeRequest clone() {
401 CreateNodeRequest request = new CreateNodeRequest(under, workspaceName, childName, conflictBehavior, properties);
402 request.setActualLocationOfNode(actualLocation);
403 return request;
404 }
405
406 @Override
407 public RequestType getType() {
408 return RequestType.CREATE_NODE;
409 }
410 }