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.connector.RepositoryConnection;
31 import org.modeshape.graph.property.Name;
32 import org.modeshape.graph.property.Property;
33
34 /**
35 * Instruction to read a single property on the node at the specified location.
36 */
37 public class ReadPropertyRequest extends CacheableRequest {
38
39 private static final long serialVersionUID = 1L;
40
41 private final Location on;
42 private final String workspaceName;
43 private final Name propertyName;
44 private Property property;
45 private Location actualLocation;
46
47 /**
48 * Create a request to read the properties and number of children of a node at the supplied location.
49 *
50 * @param on the location of the node to be read
51 * @param workspaceName the name of the workspace containing the node
52 * @param propertyName the name of the property to read
53 * @throws IllegalArgumentException if the location, workspace name, or property name are null
54 */
55 public ReadPropertyRequest( Location on,
56 String workspaceName,
57 Name propertyName ) {
58 CheckArg.isNotNull(on, "on");
59 CheckArg.isNotNull(propertyName, "propertyName");
60 CheckArg.isNotNull(workspaceName, "workspaceName");
61 this.workspaceName = workspaceName;
62 this.on = on;
63 this.propertyName = propertyName;
64 }
65
66 /**
67 * {@inheritDoc}
68 *
69 * @see org.modeshape.graph.request.Request#isReadOnly()
70 */
71 @Override
72 public boolean isReadOnly() {
73 return true;
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 on() {
82 return on;
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 name of the property that is to be read
96 *
97 * @return the property name; never null
98 */
99 public Name named() {
100 return propertyName;
101 }
102
103 /**
104 * Get the property that was read.
105 *
106 * @return the property, or null if the property was not read or did not exist on the node
107 */
108 public Property getProperty() {
109 return property;
110 }
111
112 /**
113 * Set the property on the node as read from the {@link RepositoryConnection}
114 *
115 * @param property the property that was read
116 * @throws IllegalArgumentException if the property's name does not match the {@link #named() name of the property} that was
117 * to be read
118 * @throws IllegalStateException if the request is frozen
119 */
120 public void setProperty( Property property ) {
121 checkNotFrozen();
122 if (property != null) CheckArg.isEquals(property.getName(), "property's name", named(), "property name");
123 this.property = property;
124 }
125
126 /**
127 * Sets the actual and complete location of the node whose property has been read. This method must be called when processing
128 * the request, and the actual location must have a {@link Location#getPath() path}.
129 *
130 * @param actual the actual location of the node being read, or null if the {@link #on() current location} should be used
131 * @throws IllegalArgumentException if the actual location is not {@link Location#equals(Object) equal to} the {@link #on()
132 * current location}, or if the actual location does not have a path.
133 * @throws IllegalStateException if the request is frozen
134 */
135 public void setActualLocationOfNode( Location actual ) {
136 checkNotFrozen();
137 if (!on.equals(actual)) { // not same if actual is null
138 throw new IllegalArgumentException(GraphI18n.actualLocationNotEqualToInputLocation.text(actual, on));
139 }
140 assert actual != null;
141 if (!actual.hasPath()) {
142 throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
143 }
144 this.actualLocation = actual;
145 }
146
147 /**
148 * Get the actual location of the node whose property was read.
149 *
150 * @return the actual location, or null if the actual location was not set
151 */
152 public Location getActualLocationOfNode() {
153 return actualLocation;
154 }
155
156 /**
157 * {@inheritDoc}
158 *
159 * @see org.modeshape.graph.request.Request#cancel()
160 */
161 @Override
162 public void cancel() {
163 super.cancel();
164 this.actualLocation = null;
165 this.property = null;
166 }
167
168 /**
169 * {@inheritDoc}
170 *
171 * @see java.lang.Object#hashCode()
172 */
173 @Override
174 public int hashCode() {
175 return HashCode.compute(on, workspaceName, propertyName);
176 }
177
178 /**
179 * {@inheritDoc}
180 *
181 * @see java.lang.Object#equals(java.lang.Object)
182 */
183 @Override
184 public boolean equals( Object obj ) {
185 if (obj == this) return true;
186 if (this.getClass().isInstance(obj)) {
187 ReadPropertyRequest that = (ReadPropertyRequest)obj;
188 if (!this.on().isSame(that.on())) return false;
189 if (!this.named().equals(that.named())) return false;
190 if (!this.inWorkspace().equals(that.inWorkspace())) return false;
191 return true;
192 }
193 return false;
194 }
195
196 /**
197 * {@inheritDoc}
198 *
199 * @see java.lang.Object#toString()
200 */
201 @Override
202 public String toString() {
203 return "read " + named() + " property on " + on() + " in the \"" + workspaceName + "\" workspace";
204 }
205
206 @Override
207 public RequestType getType() {
208 return RequestType.READ_PROPERTY;
209 }
210 }