001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.graph.requests;
023    
024    import org.jboss.dna.common.util.CheckArg;
025    import org.jboss.dna.graph.GraphI18n;
026    import org.jboss.dna.graph.Location;
027    import org.jboss.dna.graph.connectors.RepositoryConnection;
028    import org.jboss.dna.graph.properties.Name;
029    import org.jboss.dna.graph.properties.Property;
030    
031    /**
032     * Instruction to read a single property on the node at the specified location.
033     * 
034     * @author Randall Hauch
035     */
036    public class ReadPropertyRequest extends CacheableRequest {
037    
038        private static final long serialVersionUID = 1L;
039    
040        private final Location on;
041        private final Name propertyName;
042        private Property property;
043        private Location actualLocation;
044    
045        /**
046         * Create a request to read the properties and number of children of a node at the supplied location.
047         * 
048         * @param on the location of the node to be read
049         * @param propertyName the name of the property to read
050         * @throws IllegalArgumentException if the location or property name are null
051         */
052        public ReadPropertyRequest( Location on,
053                                    Name propertyName ) {
054            CheckArg.isNotNull(on, "on");
055            CheckArg.isNotNull(propertyName, "propertyName");
056            this.on = on;
057            this.propertyName = propertyName;
058        }
059    
060        /**
061         * {@inheritDoc}
062         * 
063         * @see org.jboss.dna.graph.requests.Request#isReadOnly()
064         */
065        @Override
066        public boolean isReadOnly() {
067            return true;
068        }
069    
070        /**
071         * Get the location defining the node that is to be read.
072         * 
073         * @return the location of the node; never null
074         */
075        public Location on() {
076            return on;
077        }
078    
079        /**
080         * Get the name of the property that is to be read
081         * 
082         * @return the property name; never null
083         */
084        public Name named() {
085            return propertyName;
086        }
087    
088        /**
089         * Get the property that was read.
090         * 
091         * @return the property, or null if the property was not read or did not exist on the node
092         */
093        public Property getProperty() {
094            return property;
095        }
096    
097        /**
098         * Set the property on the node as read from the {@link RepositoryConnection}
099         * 
100         * @param property the property that was read
101         * @throws IllegalArgumentException if the property's name does not match the {@link #named() name of the property} that was
102         *         to be read
103         */
104        public void setProperty( Property property ) {
105            if (property != null) CheckArg.isEquals(property.getName(), "property's name", named(), "property name");
106            this.property = property;
107        }
108    
109        /**
110         * Sets the actual and complete location of the node whose property has been read. This method must be called when processing
111         * the request, and the actual location must have a {@link Location#getPath() path}.
112         * 
113         * @param actual the actual location of the node being read, or null if the {@link #on() current location} should be used
114         * @throws IllegalArgumentException if the actual location does not represent the {@link Location#isSame(Location) same
115         *         location} as the {@link #on() current location}, or if the actual location does not have a path.
116         */
117        public void setActualLocationOfNode( Location actual ) {
118            if (!on.isSame(actual)) { // not same if actual is null
119                throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(actual, on));
120            }
121            assert actual != null;
122            if (!actual.hasPath()) {
123                throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
124            }
125            this.actualLocation = actual;
126        }
127    
128        /**
129         * Get the actual location of the node whose property was read.
130         * 
131         * @return the actual location, or null if the actual location was not set
132         */
133        public Location getActualLocationOfNode() {
134            return actualLocation;
135        }
136    
137        /**
138         * {@inheritDoc}
139         * 
140         * @see java.lang.Object#equals(java.lang.Object)
141         */
142        @Override
143        public boolean equals( Object obj ) {
144            if (this.getClass().isInstance(obj)) {
145                ReadPropertyRequest that = (ReadPropertyRequest)obj;
146                if (!this.on().equals(that.on())) return false;
147                if (!this.named().equals(that.named())) return false;
148                return true;
149            }
150            return false;
151        }
152    
153        /**
154         * {@inheritDoc}
155         * 
156         * @see java.lang.Object#toString()
157         */
158        @Override
159        public String toString() {
160            return "read " + named() + " property at " + on();
161        }
162    
163    }