001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024 package org.jboss.dna.graph.request;
025
026 import org.jboss.dna.common.util.CheckArg;
027 import org.jboss.dna.common.util.HashCode;
028 import org.jboss.dna.graph.GraphI18n;
029 import org.jboss.dna.graph.Location;
030 import org.jboss.dna.graph.property.Name;
031 import org.jboss.dna.graph.property.Path;
032
033 /**
034 * Instruction to rename an existing node (but keep it under the same parent). The same-name-sibling index will be determined
035 * automatically, based upon it's current location within the list of children.
036 *
037 * @author Randall Hauch
038 */
039 public class RenameNodeRequest extends ChangeRequest {
040
041 private static final long serialVersionUID = 1L;
042
043 private final Location at;
044 private final String workspaceName;
045 private final Name newName;
046 private Location actualOldLocation;
047 private Location actualNewLocation;
048
049 /**
050 * Create a request to rename the node at the supplied location.
051 *
052 * @param at the location of the node to be read
053 * @param workspaceName the name of the workspace containing the node
054 * @param newName the new name for the node
055 * @throws IllegalArgumentException if the location or workspace name is null
056 */
057 public RenameNodeRequest( Location at,
058 String workspaceName,
059 Name newName ) {
060 CheckArg.isNotNull(at, "at");
061 CheckArg.isNotNull(newName, "newName");
062 CheckArg.isNotNull(workspaceName, "workspaceName");
063 this.workspaceName = workspaceName;
064 this.at = at;
065 this.newName = newName;
066 }
067
068 /**
069 * {@inheritDoc}
070 *
071 * @see org.jboss.dna.graph.request.Request#isReadOnly()
072 */
073 @Override
074 public boolean isReadOnly() {
075 return false;
076 }
077
078 /**
079 * Get the location defining the node that is to be read.
080 *
081 * @return the location of the node; never null
082 */
083 public Location at() {
084 return at;
085 }
086
087 /**
088 * Get the name of the workspace in which the node exists.
089 *
090 * @return the name of the workspace; never null
091 */
092 public String inWorkspace() {
093 return workspaceName;
094 }
095
096 /**
097 * Get the new name for the node.
098 *
099 * @return the new name; never null
100 */
101 public Name toName() {
102 return newName;
103 }
104
105 /**
106 * Sets the actual and complete location of the node being renamed and its new location. This method must be called when
107 * processing the request, and the actual location must have a {@link Location#getPath() path}.
108 *
109 * @param oldLocation the actual location of the node before being renamed
110 * @param newLocation the actual location of the node after being renamed
111 * @throws IllegalArgumentException if the either location is null or is missing its path, if the old location does not
112 * represent the {@link Location#isSame(Location) same location} as the {@link #at() current location}, if the new
113 * location does not have the same parent as the old location, or if the new location does not have the same
114 * {@link Path.Segment#getName() name} on {@link Path#getLastSegment() last segment} as that {@link #toName()
115 * specified on the request}
116 * @throws IllegalStateException if the request is frozen
117 */
118 public void setActualLocations( Location oldLocation,
119 Location newLocation ) {
120 checkNotFrozen();
121 if (!at.isSame(oldLocation)) { // not same if actual is null
122 throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(oldLocation, at));
123 }
124 assert oldLocation != null;
125 if (newLocation == null) {
126 throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(newLocation, at));
127 }
128 if (!oldLocation.hasPath()) {
129 throw new IllegalArgumentException(GraphI18n.actualOldLocationMustHavePath.text(oldLocation));
130 }
131 if (!newLocation.hasPath()) {
132 throw new IllegalArgumentException(GraphI18n.actualNewLocationMustHavePath.text(newLocation));
133 }
134 Path newPath = newLocation.getPath();
135 if (!newPath.getParent().equals(oldLocation.getPath().getParent())) {
136 String msg = GraphI18n.actualNewLocationMustHaveSameParentAsOldLocation.text(newLocation, oldLocation);
137 throw new IllegalArgumentException(msg);
138 }
139 if (!newPath.getLastSegment().getName().equals(toName())) {
140 String msg = GraphI18n.actualNewLocationMustHaveSameNameAsRequest.text(newLocation, toName());
141 throw new IllegalArgumentException(msg);
142 }
143 this.actualNewLocation = newLocation;
144 }
145
146 /**
147 * Get the actual location of the node before being renamed.
148 *
149 * @return the actual location of the node before being renamed, or null if the actual location was not set
150 */
151 public Location getActualLocationBefore() {
152 return actualOldLocation;
153 }
154
155 /**
156 * Get the actual location of the node after being renamed.
157 *
158 * @return the actual location of the node after being renamed, or null if the actual location was not set
159 */
160 public Location getActualLocationAfter() {
161 return actualNewLocation;
162 }
163
164 /**
165 * {@inheritDoc}
166 *
167 * @see org.jboss.dna.graph.request.ChangeRequest#changes(java.lang.String, org.jboss.dna.graph.property.Path)
168 */
169 @Override
170 public boolean changes( String workspace,
171 Path path ) {
172 return this.workspaceName.equals(workspace) && at.hasPath() && at.getPath().getParent().isAtOrBelow(path);
173 }
174
175 /**
176 * {@inheritDoc}
177 *
178 * @see org.jboss.dna.graph.request.ChangeRequest#changedLocation()
179 */
180 @Override
181 public Location changedLocation() {
182 return at;
183 }
184
185 /**
186 * {@inheritDoc}
187 *
188 * @see org.jboss.dna.graph.request.ChangeRequest#changedWorkspace()
189 */
190 @Override
191 public String changedWorkspace() {
192 return workspaceName;
193 }
194
195 /**
196 * {@inheritDoc}
197 *
198 * @see org.jboss.dna.graph.request.Request#cancel()
199 */
200 @Override
201 public void cancel() {
202 super.cancel();
203 this.actualNewLocation = null;
204 this.actualOldLocation = null;
205 }
206
207 /**
208 * {@inheritDoc}
209 *
210 * @see java.lang.Object#hashCode()
211 */
212 @Override
213 public int hashCode() {
214 return HashCode.compute(at, workspaceName);
215 }
216
217 /**
218 * {@inheritDoc}
219 *
220 * @see java.lang.Object#equals(java.lang.Object)
221 */
222 @Override
223 public boolean equals( Object obj ) {
224 if (obj == this) return true;
225 if (this.getClass().isInstance(obj)) {
226 RenameNodeRequest that = (RenameNodeRequest)obj;
227 if (!this.at().equals(that.at())) return false;
228 if (!this.toName().equals(that.toName())) return false;
229 if (!this.inWorkspace().equals(that.inWorkspace())) return false;
230 return true;
231 }
232 return false;
233 }
234
235 /**
236 * {@inheritDoc}
237 *
238 * @see java.lang.Object#toString()
239 */
240 @Override
241 public String toString() {
242 return "rename node at " + at() + " in the \"" + workspaceName + "\" workspace to " + toName();
243 }
244
245 }