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.graph.GraphI18n; 028 import org.jboss.dna.graph.Location; 029 import org.jboss.dna.graph.property.Name; 030 import org.jboss.dna.graph.property.Path; 031 032 /** 033 * Instruction to rename an existing node (but keep it under the same parent). The same-name-sibling index will be determined 034 * automatically, based upon it's current location within the list of children. 035 * 036 * @author Randall Hauch 037 */ 038 public class RenameNodeRequest extends Request implements ChangeRequest { 039 040 private static final long serialVersionUID = 1L; 041 042 private final Location at; 043 private final String workspaceName; 044 private final Name newName; 045 private Location actualOldLocation; 046 private Location actualNewLocation; 047 048 /** 049 * Create a request to rename the node at the supplied location. 050 * 051 * @param at the location of the node to be read 052 * @param workspaceName the name of the workspace containing the node 053 * @param newName the new name for the node 054 * @throws IllegalArgumentException if the location or workspace name is null 055 */ 056 public RenameNodeRequest( Location at, 057 String workspaceName, 058 Name newName ) { 059 CheckArg.isNotNull(at, "at"); 060 CheckArg.isNotNull(newName, "newName"); 061 CheckArg.isNotNull(workspaceName, "workspaceName"); 062 this.workspaceName = workspaceName; 063 this.at = at; 064 this.newName = newName; 065 } 066 067 /** 068 * {@inheritDoc} 069 * 070 * @see org.jboss.dna.graph.request.Request#isReadOnly() 071 */ 072 @Override 073 public boolean isReadOnly() { 074 return false; 075 } 076 077 /** 078 * Get the location defining the node that is to be read. 079 * 080 * @return the location of the node; never null 081 */ 082 public Location at() { 083 return at; 084 } 085 086 /** 087 * Get the name of the workspace in which the node exists. 088 * 089 * @return the name of the workspace; never null 090 */ 091 public String inWorkspace() { 092 return workspaceName; 093 } 094 095 /** 096 * Get the new name for the node. 097 * 098 * @return the new name; never null 099 */ 100 public Name toName() { 101 return newName; 102 } 103 104 /** 105 * Sets the actual and complete location of the node being renamed and its new location. This method must be called when 106 * processing the request, and the actual location must have a {@link Location#getPath() path}. 107 * 108 * @param oldLocation the actual location of the node before being renamed 109 * @param newLocation the actual location of the node after being renamed 110 * @throws IllegalArgumentException if the either location is null or is missing its path, if the old location does not 111 * represent the {@link Location#isSame(Location) same location} as the {@link #at() current location}, if the new 112 * location does not have the same parent as the old location, or if the new location does not have the same 113 * {@link Path.Segment#getName() name} on {@link Path#getLastSegment() last segment} as that {@link #toName() 114 * specified on the request} 115 */ 116 public void setActualLocations( Location oldLocation, 117 Location newLocation ) { 118 if (!at.isSame(oldLocation)) { // not same if actual is null 119 throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(oldLocation, at)); 120 } 121 assert oldLocation != null; 122 if (newLocation == null) { 123 throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(newLocation, at)); 124 } 125 if (!oldLocation.hasPath()) { 126 throw new IllegalArgumentException(GraphI18n.actualOldLocationMustHavePath.text(oldLocation)); 127 } 128 if (!newLocation.hasPath()) { 129 throw new IllegalArgumentException(GraphI18n.actualNewLocationMustHavePath.text(newLocation)); 130 } 131 Path newPath = newLocation.getPath(); 132 if (!newPath.getParent().equals(oldLocation.getPath().getParent())) { 133 String msg = GraphI18n.actualNewLocationMustHaveSameParentAsOldLocation.text(newLocation, oldLocation); 134 throw new IllegalArgumentException(msg); 135 } 136 if (!newPath.getLastSegment().getName().equals(toName())) { 137 String msg = GraphI18n.actualNewLocationMustHaveSameNameAsRequest.text(newLocation, toName()); 138 throw new IllegalArgumentException(msg); 139 } 140 this.actualNewLocation = newLocation; 141 } 142 143 /** 144 * Get the actual location of the node before being renamed. 145 * 146 * @return the actual location of the node before being renamed, or null if the actual location was not set 147 */ 148 public Location getActualLocationBefore() { 149 return actualOldLocation; 150 } 151 152 /** 153 * Get the actual location of the node after being renamed. 154 * 155 * @return the actual location of the node after being renamed, or null if the actual location was not set 156 */ 157 public Location getActualLocationAfter() { 158 return actualNewLocation; 159 } 160 161 /** 162 * {@inheritDoc} 163 * 164 * @see org.jboss.dna.graph.request.ChangeRequest#changes(java.lang.String, org.jboss.dna.graph.property.Path) 165 */ 166 public boolean changes( String workspace, 167 Path path ) { 168 return this.workspaceName.equals(workspace) && at.hasPath() && at.getPath().getParent().isAtOrBelow(path); 169 } 170 171 /** 172 * {@inheritDoc} 173 * 174 * @see org.jboss.dna.graph.request.ChangeRequest#changedLocation() 175 */ 176 public Location changedLocation() { 177 return at; 178 } 179 180 /** 181 * {@inheritDoc} 182 * 183 * @see java.lang.Object#equals(java.lang.Object) 184 */ 185 @Override 186 public boolean equals( Object obj ) { 187 if (obj == this) return true; 188 if (this.getClass().isInstance(obj)) { 189 RenameNodeRequest that = (RenameNodeRequest)obj; 190 if (!this.at().equals(that.at())) return false; 191 if (!this.toName().equals(that.toName())) return false; 192 if (!this.inWorkspace().equals(that.inWorkspace())) return false; 193 return true; 194 } 195 return false; 196 } 197 198 /** 199 * {@inheritDoc} 200 * 201 * @see java.lang.Object#toString() 202 */ 203 @Override 204 public String toString() { 205 return "rename node at " + at() + " in the \"" + workspaceName + "\" workspace to " + toName(); 206 } 207 208 }