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.property.Name;
31 import org.modeshape.graph.property.Path;
32
33
34
35
36
37 public class RenameNodeRequest extends ChangeRequest {
38
39 private static final long serialVersionUID = 1L;
40
41 private final Location at;
42 private final String workspaceName;
43 private final Name newName;
44 private Location actualOldLocation;
45 private Location actualNewLocation;
46
47
48
49
50
51
52
53
54
55 public RenameNodeRequest( Location at,
56 String workspaceName,
57 Name newName ) {
58 CheckArg.isNotNull(at, "at");
59 CheckArg.isNotNull(newName, "newName");
60 CheckArg.isNotNull(workspaceName, "workspaceName");
61 this.workspaceName = workspaceName;
62 this.at = at;
63 this.newName = newName;
64 }
65
66
67
68
69
70
71 @Override
72 public boolean isReadOnly() {
73 return false;
74 }
75
76
77
78
79
80
81 public Location at() {
82 return at;
83 }
84
85
86
87
88
89
90 public String inWorkspace() {
91 return workspaceName;
92 }
93
94
95
96
97
98
99 public Name toName() {
100 return newName;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114 public void setActualLocations( Location oldLocation,
115 Location newLocation ) {
116 checkNotFrozen();
117 CheckArg.isNotNull(oldLocation, "oldLocation");
118 CheckArg.isNotNull(newLocation, "newLocation");
119 if (!oldLocation.hasPath()) {
120 throw new IllegalArgumentException(GraphI18n.actualOldLocationMustHavePath.text(oldLocation));
121 }
122 if (!newLocation.hasPath()) {
123 throw new IllegalArgumentException(GraphI18n.actualNewLocationMustHavePath.text(newLocation));
124 }
125 Path newPath = newLocation.getPath();
126 if (!newPath.getLastSegment().getName().equals(toName())) {
127 String msg = GraphI18n.actualNewLocationMustHaveSameNameAsRequest.text(newLocation, toName());
128 throw new IllegalArgumentException(msg);
129 }
130 this.actualOldLocation = oldLocation;
131 this.actualNewLocation = newLocation;
132 }
133
134
135
136
137
138
139 public Location getActualLocationBefore() {
140 return actualOldLocation;
141 }
142
143
144
145
146
147
148 public Location getActualLocationAfter() {
149 return actualNewLocation;
150 }
151
152
153
154
155
156
157 @Override
158 public boolean changes( String workspace,
159 Path path ) {
160 return this.workspaceName.equals(workspace) && at.hasPath() && at.getPath().getParent().isAtOrBelow(path);
161 }
162
163
164
165
166
167
168
169
170
171 @Override
172 public Location changedLocation() {
173 return actualNewLocation != null ? actualNewLocation : at;
174 }
175
176
177
178
179
180
181 @Override
182 public String changedWorkspace() {
183 return workspaceName;
184 }
185
186
187
188
189
190
191 @Override
192 public void cancel() {
193 super.cancel();
194 this.actualNewLocation = null;
195 this.actualOldLocation = null;
196 }
197
198
199
200
201
202
203 @Override
204 public int hashCode() {
205 return HashCode.compute(at, workspaceName);
206 }
207
208
209
210
211
212
213 @Override
214 public boolean equals( Object obj ) {
215 if (obj == this) return true;
216 if (this.getClass().isInstance(obj)) {
217 RenameNodeRequest that = (RenameNodeRequest)obj;
218 if (!this.at().isSame(that.at())) return false;
219 if (!this.toName().equals(that.toName())) return false;
220 if (!this.inWorkspace().equals(that.inWorkspace())) return false;
221 return true;
222 }
223 return false;
224 }
225
226
227
228
229
230
231 @Override
232 public String toString() {
233 return "rename node at " + at() + " in the \"" + workspaceName + "\" workspace to " + toName();
234 }
235
236
237
238
239
240
241
242
243
244 @Override
245 public RenameNodeRequest clone() {
246 RenameNodeRequest request = new RenameNodeRequest(actualOldLocation != null ? actualOldLocation : at, workspaceName,
247 newName);
248 request.setActualLocations(actualOldLocation, actualNewLocation);
249 return request;
250 }
251
252 @Override
253 public RequestType getType() {
254 return RequestType.RENAME_NODE;
255 }
256 }