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 java.util.Collection;
027 import java.util.HashMap;
028 import java.util.Iterator;
029 import java.util.Map;
030 import org.jboss.dna.common.util.CheckArg;
031 import org.jboss.dna.common.util.HashCode;
032 import org.jboss.dna.graph.GraphI18n;
033 import org.jboss.dna.graph.Location;
034 import org.jboss.dna.graph.connector.RepositoryConnection;
035 import org.jboss.dna.graph.property.Name;
036 import org.jboss.dna.graph.property.Property;
037
038 /**
039 * Instruction to read the properties and the number of children of the node at the specifed location.
040 *
041 * @author Randall Hauch
042 */
043 public class ReadAllPropertiesRequest extends CacheableRequest implements Iterable<Property> {
044
045 private static final long serialVersionUID = 1L;
046
047 public static final int UNKNOWN_NUMBER_OF_CHILDREN = -1;
048
049 private final Location at;
050 private final String workspaceName;
051 private final Map<Name, Property> properties = new HashMap<Name, Property>();
052 private int numberOfChildren = UNKNOWN_NUMBER_OF_CHILDREN;
053 private Location actualLocation;
054
055 /**
056 * Create a request to read the properties and number of children of a node at the supplied location.
057 *
058 * @param at the location of the node to be read
059 * @param workspaceName the name of the workspace containing the node
060 * @throws IllegalArgumentException if the location or workspace name is null
061 */
062 public ReadAllPropertiesRequest( Location at,
063 String workspaceName ) {
064 CheckArg.isNotNull(at, "at");
065 CheckArg.isNotNull(workspaceName, "workspaceName");
066 this.workspaceName = workspaceName;
067 this.at = at;
068 }
069
070 /**
071 * {@inheritDoc}
072 *
073 * @see org.jboss.dna.graph.request.Request#isReadOnly()
074 */
075 @Override
076 public boolean isReadOnly() {
077 return true;
078 }
079
080 /**
081 * Get the location defining the node that is to be read.
082 *
083 * @return the location of the node; never null
084 */
085 public Location at() {
086 return at;
087 }
088
089 /**
090 * Get the name of the workspace in which the node exists.
091 *
092 * @return the name of the workspace; never null
093 */
094 public String inWorkspace() {
095 return workspaceName;
096 }
097
098 /**
099 * Get the properties that were read from the {@link RepositoryConnection}.
100 *
101 * @return the properties, as a map of property name to property; never null
102 */
103 public Map<Name, Property> getPropertiesByName() {
104 return properties;
105 }
106
107 /**
108 * Get the properties that were read from the {@link RepositoryConnection}.
109 *
110 * @return the collection of properties; never null
111 */
112 public Collection<Property> getProperties() {
113 return properties.values();
114 }
115
116 /**
117 * {@inheritDoc}
118 *
119 * @see java.lang.Iterable#iterator()
120 */
121 public Iterator<Property> iterator() {
122 return getProperties().iterator();
123 }
124
125 /**
126 * Add a property that was read from the {@link RepositoryConnection}
127 *
128 * @param property the property that was read
129 * @return the previous property that had the same name, or null if there was no previously-recorded property with the same
130 * name
131 * @throws IllegalArgumentException if the property is null
132 * @throws IllegalStateException if the request is frozen
133 */
134 public Property addProperty( Property property ) {
135 checkNotFrozen();
136 return this.properties.put(property.getName(), property);
137 }
138
139 /**
140 * Add a property that was read from the {@link RepositoryConnection}
141 *
142 * @param properties the properties that were read
143 * @throws IllegalArgumentException if the property is null
144 * @throws IllegalStateException if the request is frozen
145 */
146 public void addProperties( Property... properties ) {
147 checkNotFrozen();
148 for (Property property : properties) {
149 this.properties.put(property.getName(), property);
150 }
151 }
152
153 /**
154 * Add a property that was read from the {@link RepositoryConnection}
155 *
156 * @param properties the properties that were read
157 * @throws IllegalArgumentException if the property is null
158 * @throws IllegalStateException if the request is frozen
159 */
160 public void addProperties( Iterable<Property> properties ) {
161 checkNotFrozen();
162 for (Property property : properties) {
163 this.properties.put(property.getName(), property);
164 }
165 }
166
167 /**
168 * Get the number of children for this node.
169 *
170 * @return the number of children, or {@link #UNKNOWN_NUMBER_OF_CHILDREN} if the number of children was not yet read
171 */
172 public int getNumberOfChildren() {
173 return numberOfChildren;
174 }
175
176 /**
177 * Set the number of children for this node
178 *
179 * @param numberOfChildren the number of children
180 * @throws IllegalArgumentException if the number of childre is negative
181 * @throws IllegalStateException if the request is frozen
182 */
183 public void setNumberOfChildren( int numberOfChildren ) {
184 checkNotFrozen();
185 CheckArg.isNonNegative(numberOfChildren, "numberOfChildren");
186 this.numberOfChildren = numberOfChildren;
187 }
188
189 /**
190 * Sets the actual and complete location of the node whose properties have been read. This method must be called when
191 * processing the request, and the actual location must have a {@link Location#getPath() path}.
192 *
193 * @param actual the actual location of the node being read, or null if the {@link #at() current location} should be used
194 * @throws IllegalArgumentException if the actual location does not represent the {@link Location#isSame(Location) same
195 * location} as the {@link #at() current location}, or if the actual location does not have a path.
196 * @throws IllegalStateException if the request is frozen
197 */
198 public void setActualLocationOfNode( Location actual ) {
199 checkNotFrozen();
200 if (!at.isSame(actual)) { // not same if actual is null
201 throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(actual, at));
202 }
203 assert actual != null;
204 if (!actual.hasPath()) {
205 throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
206 }
207 this.actualLocation = actual;
208 }
209
210 /**
211 * Get the actual location of the node whose properties were read.
212 *
213 * @return the actual location, or null if the actual location was not set
214 */
215 public Location getActualLocationOfNode() {
216 return actualLocation;
217 }
218
219 /**
220 * {@inheritDoc}
221 *
222 * @see org.jboss.dna.graph.request.Request#cancel()
223 */
224 @Override
225 public void cancel() {
226 super.cancel();
227 this.actualLocation = null;
228 }
229
230 /**
231 * {@inheritDoc}
232 *
233 * @see java.lang.Object#hashCode()
234 */
235 @Override
236 public int hashCode() {
237 return HashCode.compute(at, workspaceName);
238 }
239
240 /**
241 * {@inheritDoc}
242 *
243 * @see java.lang.Object#equals(java.lang.Object)
244 */
245 @Override
246 public boolean equals( Object obj ) {
247 if (obj == this) return true;
248 if (this.getClass().isInstance(obj)) {
249 ReadAllPropertiesRequest that = (ReadAllPropertiesRequest)obj;
250 if (!this.at().equals(that.at())) return false;
251 if (!this.inWorkspace().equals(that.inWorkspace())) return false;
252 return true;
253 }
254 return false;
255 }
256
257 /**
258 * {@inheritDoc}
259 *
260 * @see java.lang.Object#toString()
261 */
262 @Override
263 public String toString() {
264 return "read properties of " + at() + " in the \"" + workspaceName + "\" workspace";
265 }
266
267 }