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 null or does not have a path.
132 * @throws IllegalStateException if the request is frozen
133 */
134 public void setActualLocationOfNode( Location actual ) {
135 checkNotFrozen();
136 CheckArg.isNotNull(actual, "actual");
137 if (!actual.hasPath()) {
138 throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
139 }
140 this.actualLocation = actual;
141 }
142
143 /**
144 * Get the actual location of the node whose property was read.
145 *
146 * @return the actual location, or null if the actual location was not set
147 */
148 public Location getActualLocationOfNode() {
149 return actualLocation;
150 }
151
152 /**
153 * {@inheritDoc}
154 *
155 * @see org.modeshape.graph.request.Request#cancel()
156 */
157 @Override
158 public void cancel() {
159 super.cancel();
160 this.actualLocation = null;
161 this.property = null;
162 }
163
164 /**
165 * {@inheritDoc}
166 *
167 * @see java.lang.Object#hashCode()
168 */
169 @Override
170 public int hashCode() {
171 return HashCode.compute(on, workspaceName, propertyName);
172 }
173
174 /**
175 * {@inheritDoc}
176 *
177 * @see java.lang.Object#equals(java.lang.Object)
178 */
179 @Override
180 public boolean equals( Object obj ) {
181 if (obj == this) return true;
182 if (this.getClass().isInstance(obj)) {
183 ReadPropertyRequest that = (ReadPropertyRequest)obj;
184 if (!this.on().isSame(that.on())) return false;
185 if (!this.named().equals(that.named())) return false;
186 if (!this.inWorkspace().equals(that.inWorkspace())) return false;
187 return true;
188 }
189 return false;
190 }
191
192 /**
193 * {@inheritDoc}
194 *
195 * @see java.lang.Object#toString()
196 */
197 @Override
198 public String toString() {
199 return "read " + named() + " property on " + on() + " in the \"" + workspaceName + "\" workspace";
200 }
201
202 @Override
203 public RequestType getType() {
204 return RequestType.READ_PROPERTY;
205 }
206 }