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 org.jboss.dna.common.util.CheckArg;
027    import org.jboss.dna.common.util.HashCode;
028    import org.jboss.dna.graph.GraphI18n;
029    import org.jboss.dna.graph.Location;
030    import org.jboss.dna.graph.connector.RepositoryConnection;
031    import org.jboss.dna.graph.property.Name;
032    import org.jboss.dna.graph.property.Property;
033    
034    /**
035     * Instruction to read a single property on the node at the specified location.
036     * 
037     * @author Randall Hauch
038     */
039    public class ReadPropertyRequest extends CacheableRequest {
040    
041        private static final long serialVersionUID = 1L;
042    
043        private final Location on;
044        private final String workspaceName;
045        private final Name propertyName;
046        private Property property;
047        private Location actualLocation;
048    
049        /**
050         * Create a request to read the properties and number of children of a node at the supplied location.
051         * 
052         * @param on the location of the node to be read
053         * @param workspaceName the name of the workspace containing the node
054         * @param propertyName the name of the property to read
055         * @throws IllegalArgumentException if the location, workspace name, or property name are null
056         */
057        public ReadPropertyRequest( Location on,
058                                    String workspaceName,
059                                    Name propertyName ) {
060            CheckArg.isNotNull(on, "on");
061            CheckArg.isNotNull(propertyName, "propertyName");
062            CheckArg.isNotNull(workspaceName, "workspaceName");
063            this.workspaceName = workspaceName;
064            this.on = on;
065            this.propertyName = propertyName;
066        }
067    
068        /**
069         * {@inheritDoc}
070         * 
071         * @see org.jboss.dna.graph.request.Request#isReadOnly()
072         */
073        @Override
074        public boolean isReadOnly() {
075            return true;
076        }
077    
078        /**
079         * Get the location defining the node that is to be read.
080         * 
081         * @return the location of the node; never null
082         */
083        public Location on() {
084            return on;
085        }
086    
087        /**
088         * Get the name of the workspace in which the node exists.
089         * 
090         * @return the name of the workspace; never null
091         */
092        public String inWorkspace() {
093            return workspaceName;
094        }
095    
096        /**
097         * Get the name of the property that is to be read
098         * 
099         * @return the property name; never null
100         */
101        public Name named() {
102            return propertyName;
103        }
104    
105        /**
106         * Get the property that was read.
107         * 
108         * @return the property, or null if the property was not read or did not exist on the node
109         */
110        public Property getProperty() {
111            return property;
112        }
113    
114        /**
115         * Set the property on the node as read from the {@link RepositoryConnection}
116         * 
117         * @param property the property that was read
118         * @throws IllegalArgumentException if the property's name does not match the {@link #named() name of the property} that was
119         *         to be read
120         * @throws IllegalStateException if the request is frozen
121         */
122        public void setProperty( Property property ) {
123            checkNotFrozen();
124            if (property != null) CheckArg.isEquals(property.getName(), "property's name", named(), "property name");
125            this.property = property;
126        }
127    
128        /**
129         * Sets the actual and complete location of the node whose property has been read. This method must be called when processing
130         * the request, and the actual location must have a {@link Location#getPath() path}.
131         * 
132         * @param actual the actual location of the node being read, or null if the {@link #on() current location} should be used
133         * @throws IllegalArgumentException if the actual location does not represent the {@link Location#isSame(Location) same
134         *         location} as the {@link #on() current location}, or if the actual location does not have a path.
135         * @throws IllegalStateException if the request is frozen
136         */
137        public void setActualLocationOfNode( Location actual ) {
138            checkNotFrozen();
139            if (!on.isSame(actual)) { // not same if actual is null
140                throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(actual, on));
141            }
142            assert actual != null;
143            if (!actual.hasPath()) {
144                throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
145            }
146            this.actualLocation = actual;
147        }
148    
149        /**
150         * Get the actual location of the node whose property was read.
151         * 
152         * @return the actual location, or null if the actual location was not set
153         */
154        public Location getActualLocationOfNode() {
155            return actualLocation;
156        }
157    
158        /**
159         * {@inheritDoc}
160         * 
161         * @see org.jboss.dna.graph.request.Request#cancel()
162         */
163        @Override
164        public void cancel() {
165            super.cancel();
166            this.actualLocation = null;
167            this.property = null;
168        }
169    
170        /**
171         * {@inheritDoc}
172         * 
173         * @see java.lang.Object#hashCode()
174         */
175        @Override
176        public int hashCode() {
177            return HashCode.compute(on, workspaceName, propertyName);
178        }
179    
180        /**
181         * {@inheritDoc}
182         * 
183         * @see java.lang.Object#equals(java.lang.Object)
184         */
185        @Override
186        public boolean equals( Object obj ) {
187            if (obj == this) return true;
188            if (this.getClass().isInstance(obj)) {
189                ReadPropertyRequest that = (ReadPropertyRequest)obj;
190                if (!this.on().equals(that.on())) return false;
191                if (!this.named().equals(that.named())) return false;
192                if (!this.inWorkspace().equals(that.inWorkspace())) return false;
193                return true;
194            }
195            return false;
196        }
197    
198        /**
199         * {@inheritDoc}
200         * 
201         * @see java.lang.Object#toString()
202         */
203        @Override
204        public String toString() {
205            return "read " + named() + " property on " + on() + " in the \"" + workspaceName + "\" workspace";
206        }
207    
208    }