View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * Instruction to rename an existing node (but keep it under the same parent). The same-name-sibling index will be determined
35   * automatically, based upon it's current location within the list of children.
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       * Create a request to rename the node at the supplied location.
49       * 
50       * @param at the location of the node to be read
51       * @param workspaceName the name of the workspace containing the node
52       * @param newName the new name for the node
53       * @throws IllegalArgumentException if the location or workspace name is null
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       * {@inheritDoc}
68       * 
69       * @see org.modeshape.graph.request.Request#isReadOnly()
70       */
71      @Override
72      public boolean isReadOnly() {
73          return false;
74      }
75  
76      /**
77       * Get the location defining the node that is to be read.
78       * 
79       * @return the location of the node; never null
80       */
81      public Location at() {
82          return at;
83      }
84  
85      /**
86       * Get the name of the workspace in which the node exists.
87       * 
88       * @return the name of the workspace; never null
89       */
90      public String inWorkspace() {
91          return workspaceName;
92      }
93  
94      /**
95       * Get the new name for the node.
96       * 
97       * @return the new name; never null
98       */
99      public Name toName() {
100         return newName;
101     }
102 
103     /**
104      * Sets the actual and complete location of the node being renamed and its new location. This method must be called when
105      * processing the request, and the actual location must have a {@link Location#getPath() path}.
106      * 
107      * @param oldLocation the actual location of the node before being renamed
108      * @param newLocation the actual location of the node after being renamed
109      * @throws IllegalArgumentException if the either location is null or is missing its path, or if the new location does not
110      *         have the same {@link Path.Segment#getName() name} on {@link Path#getLastSegment() last segment} as that
111      *         {@link #toName() specified on the request}
112      * @throws IllegalStateException if the request is frozen
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      * Get the actual location of the node before being renamed.
136      * 
137      * @return the actual location of the node before being renamed, or null if the actual location was not set
138      */
139     public Location getActualLocationBefore() {
140         return actualOldLocation;
141     }
142 
143     /**
144      * Get the actual location of the node after being renamed.
145      * 
146      * @return the actual location of the node after being renamed, or null if the actual location was not set
147      */
148     public Location getActualLocationAfter() {
149         return actualNewLocation;
150     }
151 
152     /**
153      * {@inheritDoc}
154      * 
155      * @see org.modeshape.graph.request.ChangeRequest#changes(java.lang.String, org.modeshape.graph.property.Path)
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      * {@inheritDoc}
165      * <p>
166      * This method returns the {@link #getActualLocationAfter()} location, or if null the {@link #at()} location.
167      * </p>
168      * 
169      * @see org.modeshape.graph.request.ChangeRequest#changedLocation()
170      */
171     @Override
172     public Location changedLocation() {
173         return actualNewLocation != null ? actualNewLocation : at;
174     }
175 
176     /**
177      * {@inheritDoc}
178      * 
179      * @see org.modeshape.graph.request.ChangeRequest#changedWorkspace()
180      */
181     @Override
182     public String changedWorkspace() {
183         return workspaceName;
184     }
185 
186     /**
187      * {@inheritDoc}
188      * 
189      * @see org.modeshape.graph.request.Request#cancel()
190      */
191     @Override
192     public void cancel() {
193         super.cancel();
194         this.actualNewLocation = null;
195         this.actualOldLocation = null;
196     }
197 
198     /**
199      * {@inheritDoc}
200      * 
201      * @see java.lang.Object#hashCode()
202      */
203     @Override
204     public int hashCode() {
205         return HashCode.compute(at, workspaceName);
206     }
207 
208     /**
209      * {@inheritDoc}
210      * 
211      * @see java.lang.Object#equals(java.lang.Object)
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      * {@inheritDoc}
228      * 
229      * @see java.lang.Object#toString()
230      */
231     @Override
232     public String toString() {
233         return "rename node at " + at() + " in the \"" + workspaceName + "\" workspace to " + toName();
234     }
235 
236     /**
237      * {@inheritDoc}
238      * <p>
239      * This method does not clone the results.
240      * </p>
241      * 
242      * @see org.modeshape.graph.request.ChangeRequest#clone()
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 }