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    
030    /**
031     * Instruction to verify the existance of a node at the specified location. This request also returns the actual location.
032     * 
033     * @author Randall Hauch
034     */
035    public class VerifyNodeExistsRequest extends CacheableRequest {
036    
037        private static final long serialVersionUID = 1L;
038    
039        private final Location at;
040        private final String workspaceName;
041        private Location actualLocation;
042    
043        /**
044         * Create a request to verify the existance and location of a node at the supplied location.
045         * 
046         * @param at the location of the node to be verified
047         * @param workspaceName the name of the workspace containing the node
048         * @throws IllegalArgumentException if the location or workspace name is null
049         */
050        public VerifyNodeExistsRequest( Location at,
051                                        String workspaceName ) {
052            CheckArg.isNotNull(at, "at");
053            CheckArg.isNotNull(workspaceName, "workspaceName");
054            this.workspaceName = workspaceName;
055            this.at = at;
056        }
057    
058        /**
059         * {@inheritDoc}
060         * 
061         * @see org.jboss.dna.graph.request.Request#isReadOnly()
062         */
063        @Override
064        public boolean isReadOnly() {
065            return true;
066        }
067    
068        /**
069         * Get the location defining the node that is to be read.
070         * 
071         * @return the location of the node; never null
072         */
073        public Location at() {
074            return at;
075        }
076    
077        /**
078         * Get the name of the workspace in which the node exists.
079         * 
080         * @return the name of the workspace; never null
081         */
082        public String inWorkspace() {
083            return workspaceName;
084        }
085    
086        /**
087         * Sets the actual and complete location of the node whose properties have been read. This method must be called when
088         * processing the request, and the actual location must have a {@link Location#getPath() path}.
089         * 
090         * @param actual the actual location of the node being read, or null if the {@link #at() current location} should be used
091         * @throws IllegalArgumentException if the actual location does not represent the {@link Location#isSame(Location) same
092         *         location} as the {@link #at() current location}, or if the actual location does not have a path.
093         */
094        public void setActualLocationOfNode( Location actual ) {
095            if (!at.isSame(actual)) { // not same if actual is null
096                throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(actual, at));
097            }
098            assert actual != null;
099            if (!actual.hasPath()) {
100                throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
101            }
102            this.actualLocation = actual;
103        }
104    
105        /**
106         * Get the actual location of the node whose properties were read.
107         * 
108         * @return the actual location, or null if the actual location was not set
109         */
110        public Location getActualLocationOfNode() {
111            return actualLocation;
112        }
113    
114        /**
115         * Return whether this node is known to exist. If the request has been processed, it will have an
116         * {@link #getActualLocationOfNode() actual location} or an {@link #getError() error}.
117         * 
118         * @return true if this node is known to exist
119         * @see #getActualLocationOfNode()
120         * @see #getError()
121         */
122        public boolean exists() {
123            return actualLocation != null;
124        }
125    
126        /**
127         * {@inheritDoc}
128         * 
129         * @see java.lang.Object#equals(java.lang.Object)
130         */
131        @Override
132        public boolean equals( Object obj ) {
133            if (obj == this) return true;
134            if (this.getClass().isInstance(obj)) {
135                VerifyNodeExistsRequest that = (VerifyNodeExistsRequest)obj;
136                if (!this.at().equals(that.at())) return false;
137                if (!this.inWorkspace().equals(that.inWorkspace())) return false;
138                return true;
139            }
140            return false;
141        }
142    
143        /**
144         * {@inheritDoc}
145         * 
146         * @see java.lang.Object#toString()
147         */
148        @Override
149        public String toString() {
150            return "verify node exists at " + at() + " in the \"" + workspaceName + "\" workspace";
151        }
152    }