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