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    import org.jboss.dna.graph.property.Property;
031    
032    /**
033     * Instruction to set a particular property on the node at the specified location. This request <i>never</i> removes the node,
034     * even if the property is empty.
035     * 
036     * @author Randall Hauch
037     */
038    public class SetPropertyRequest extends Request implements ChangeRequest {
039    
040        private static final long serialVersionUID = 1L;
041    
042        private final Location on;
043        private final String workspaceName;
044        private final Property property;
045        private Location actualLocation;
046    
047        /**
048         * Create a request to set the property on the node at the supplied location.
049         * 
050         * @param on the location of the node to be read
051         * @param workspaceName the name of the workspace containing the node
052         * @param property the new property on the node
053         * @throws IllegalArgumentException if the location, workspace name, or property is null
054         */
055        public SetPropertyRequest( Location on,
056                                   String workspaceName,
057                                   Property property ) {
058            CheckArg.isNotNull(on, "on");
059            CheckArg.isNotNull(property, "property");
060            CheckArg.isNotNull(workspaceName, "workspaceName");
061            this.workspaceName = workspaceName;
062            this.on = on;
063            this.property = property;
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 that is to be updated.
078         * 
079         * @return the location of the node; never null
080         */
081        public Location on() {
082            return on;
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 property that is being set.
096         * 
097         * @return the new property; never null
098         */
099        public Property property() {
100            return property;
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 #on() 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 #on() current location}, or if the actual location does not have a path.
110         */
111        public void setActualLocationOfNode( Location actual ) {
112            if (!on.isSame(actual)) { // not same if actual is null
113                throw new IllegalArgumentException(GraphI18n.actualLocationIsNotSameAsInputLocation.text(actual, on));
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) && on.hasPath() && on.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                SetPropertyRequest that = (SetPropertyRequest)obj;
151                if (!this.on().equals(that.on())) return false;
152                if (!this.property().equals(that.property())) 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 on;
166        }
167    
168        /**
169         * {@inheritDoc}
170         * 
171         * @see java.lang.Object#toString()
172         */
173        @Override
174        public String toString() {
175            return "set property " + property().getName() + " on " + on() + " in the \"" + workspaceName + "\" workspace to "
176                   + property().getValuesAsArray();
177        }
178    }