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.property.Path;
030    
031    /**
032     * Instruction that a branch be deleted.
033     * 
034     * @author Randall Hauch
035     */
036    public class DeleteBranchRequest extends Request implements ChangeRequest {
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 delete a branch.
046         * 
047         * @param at the location of the top node in the existing branch that is to be deleted
048         * @param workspaceName the name of the workspace containing the parent
049         * @throws IllegalArgumentException if the location or workspace name is null
050         */
051        public DeleteBranchRequest( 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         * Get the location defining the top of the branch to be deleted
061         * 
062         * @return the location of the branch; never null
063         */
064        public Location at() {
065            return at;
066        }
067    
068        /**
069         * Get the name of the workspace in which the branch exists.
070         * 
071         * @return the name of the workspace; never null
072         */
073        public String inWorkspace() {
074            return workspaceName;
075        }
076    
077        /**
078         * {@inheritDoc}
079         * 
080         * @see org.jboss.dna.graph.request.Request#isReadOnly()
081         */
082        @Override
083        public boolean isReadOnly() {
084            return false;
085        }
086    
087        /**
088         * Sets the actual and complete location of the node being deleted. This method must be called when processing the request,
089         * and the actual location must have a {@link Location#getPath() path}.
090         * 
091         * @param actual the actual location of the node being deleted, 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         */
095        public void setActualLocationOfNode( Location actual ) {
096            if (!at.isSame(actual)) { // not same if actual is null
097                throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(actual, at));
098            }
099            assert actual != null;
100            if (!actual.hasPath()) {
101                throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
102            }
103            this.actualLocation = actual;
104        }
105    
106        /**
107         * Get the actual location of the node that was deleted.
108         * 
109         * @return the actual location, or null if the actual location was not set
110         */
111        public Location getActualLocationOfNode() {
112            return actualLocation;
113        }
114    
115        /**
116         * {@inheritDoc}
117         * 
118         * @see org.jboss.dna.graph.request.ChangeRequest#changes(java.lang.String, org.jboss.dna.graph.property.Path)
119         */
120        public boolean changes( String workspace,
121                                Path path ) {
122            return this.workspaceName.equals(workspace) && at.hasPath() && at.getPath().isAtOrBelow(path);
123        }
124    
125        /**
126         * {@inheritDoc}
127         * 
128         * @see org.jboss.dna.graph.request.ChangeRequest#changedLocation()
129         */
130        public Location changedLocation() {
131            return at;
132        }
133    
134        /**
135         * {@inheritDoc}
136         * 
137         * @see java.lang.Object#equals(java.lang.Object)
138         */
139        @Override
140        public boolean equals( Object obj ) {
141            if (obj == this) return true;
142            if (this.getClass().isInstance(obj)) {
143                DeleteBranchRequest that = (DeleteBranchRequest)obj;
144                if (!this.at().equals(that.at())) return false;
145                if (!this.inWorkspace().equals(that.inWorkspace())) return false;
146                return true;
147            }
148            return false;
149        }
150    
151        /**
152         * {@inheritDoc}
153         * 
154         * @see java.lang.Object#toString()
155         */
156        @Override
157        public String toString() {
158            return "delete branch " + at() + " in the \"" + workspaceName + "\" workspace";
159        }
160    }