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