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