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.Path; 31 32 /** 33 * Instruction to unlock an existing node or branch. Connectors that do not support locking must ignore this request. 34 */ 35 public class UnlockBranchRequest extends ChangeRequest { 36 37 private static final long serialVersionUID = 1L; 38 39 private final Location at; 40 private final String workspaceName; 41 private Location actualLocation; 42 43 /** 44 * Create a request to unlock the node or branch at the supplied location. 45 * 46 * @param at the location of the node to be unlocked 47 * @param workspaceName the name of the workspace containing the node 48 * @throws IllegalArgumentException if the location or workspace name is null 49 */ 50 public UnlockBranchRequest( Location at, 51 String workspaceName ) { 52 CheckArg.isNotNull(at, "at"); 53 CheckArg.isNotNull(workspaceName, "workspaceName"); 54 this.workspaceName = workspaceName; 55 this.at = at; 56 } 57 58 /** 59 * {@inheritDoc} 60 * 61 * @see org.modeshape.graph.request.Request#isReadOnly() 62 */ 63 @Override 64 public boolean isReadOnly() { 65 return false; 66 } 67 68 /** 69 * Get the location defining the node that is to be read. 70 * 71 * @return the location of the node; never null 72 */ 73 public Location at() { 74 return at; 75 } 76 77 /** 78 * Get the name of the workspace in which the node exists. 79 * 80 * @return the name of the workspace; never null 81 */ 82 public String inWorkspace() { 83 return workspaceName; 84 } 85 86 /** 87 * Sets the actual and complete location of the node being unlocked. This method must be called when processing the request, 88 * and the actual location must have a {@link Location#getPath() path}. 89 * 90 * @param actualLocation the actual location of the node before being unlocked 91 * @throws IllegalArgumentException if the either location is null or is missing its path 92 * @throws IllegalStateException if the request is frozen 93 */ 94 public void setActualLocation( Location actualLocation ) { 95 checkNotFrozen(); 96 CheckArg.isNotNull(actualLocation, "actualLocation"); 97 if (!actualLocation.hasPath()) { 98 throw new IllegalArgumentException(GraphI18n.actualOldLocationMustHavePath.text(actualLocation)); 99 } 100 this.actualLocation = actualLocation; 101 } 102 103 /** 104 * Get the actual location of the node that was unlocked. 105 * 106 * @return the actual location of the node being unlocked, or null if the actual location was not set 107 */ 108 public Location getActualLocation() { 109 return actualLocation; 110 } 111 112 /** 113 * {@inheritDoc} 114 * 115 * @see org.modeshape.graph.request.ChangeRequest#changes(java.lang.String, org.modeshape.graph.property.Path) 116 */ 117 @Override 118 public boolean changes( String workspace, 119 Path path ) { 120 return this.workspaceName.equals(workspace) && at.hasPath() && at.getPath().getParent().isAtOrBelow(path); 121 } 122 123 /** 124 * {@inheritDoc} 125 * 126 * @see org.modeshape.graph.request.ChangeRequest#changedLocation() 127 */ 128 @Override 129 public Location changedLocation() { 130 return actualLocation != null ? actualLocation : at; 131 } 132 133 /** 134 * {@inheritDoc} 135 * 136 * @see org.modeshape.graph.request.ChangeRequest#changedWorkspace() 137 */ 138 @Override 139 public String changedWorkspace() { 140 return workspaceName; 141 } 142 143 /** 144 * {@inheritDoc} 145 * 146 * @see org.modeshape.graph.request.Request#cancel() 147 */ 148 @Override 149 public void cancel() { 150 super.cancel(); 151 this.actualLocation = null; 152 } 153 154 /** 155 * {@inheritDoc} 156 * 157 * @see java.lang.Object#hashCode() 158 */ 159 @Override 160 public int hashCode() { 161 return HashCode.compute(at, workspaceName); 162 } 163 164 /** 165 * {@inheritDoc} 166 * 167 * @see java.lang.Object#equals(java.lang.Object) 168 */ 169 @Override 170 public boolean equals( Object obj ) { 171 if (obj == this) return true; 172 if (this.getClass().isInstance(obj)) { 173 UnlockBranchRequest that = (UnlockBranchRequest)obj; 174 if (!this.at().isSame(that.at())) return false; 175 if (!this.inWorkspace().equals(that.inWorkspace())) return false; 176 return true; 177 } 178 return false; 179 } 180 181 /** 182 * {@inheritDoc} 183 * 184 * @see java.lang.Object#toString() 185 */ 186 @Override 187 public String toString() { 188 return "unlock branch at " + at() + " in the \"" + workspaceName + "\" workspace"; 189 } 190 191 /** 192 * {@inheritDoc} 193 * <p> 194 * This method does not clone the results. 195 * </p> 196 * 197 * @see org.modeshape.graph.request.ChangeRequest#clone() 198 */ 199 @Override 200 public UnlockBranchRequest clone() { 201 UnlockBranchRequest request = new UnlockBranchRequest(actualLocation != null ? actualLocation : at, workspaceName); 202 request.setActualLocation(actualLocation); 203 return request; 204 } 205 206 @Override 207 public RequestType getType() { 208 return RequestType.UNLOCK_BRANCH; 209 } 210 }