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