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