001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.graph.commands.basic;
023    
024    import java.util.Collection;
025    import java.util.LinkedList;
026    import net.jcip.annotations.NotThreadSafe;
027    import org.jboss.dna.common.util.StringUtil;
028    import org.jboss.dna.graph.commands.CreateNodeCommand;
029    import org.jboss.dna.graph.commands.NodeConflictBehavior;
030    import org.jboss.dna.graph.properties.Path;
031    import org.jboss.dna.graph.properties.Property;
032    
033    /**
034     * @author Randall Hauch
035     */
036    @NotThreadSafe
037    public class BasicCreateNodeCommand extends BasicGraphCommand implements CreateNodeCommand {
038    
039        /**
040         */
041        private static final long serialVersionUID = -5452285887178397354L;
042        private final Path path;
043        private final Collection<Property> properties;
044        private final NodeConflictBehavior conflictBehavior;
045    
046        /**
047         * @param path the path to the node; may not be null
048         */
049        public BasicCreateNodeCommand( Path path ) {
050            this(path, new LinkedList<Property>(), NodeConflictBehavior.UPDATE);
051        }
052    
053        /**
054         * @param path the path to the node; may not be null
055         * @param properties the properties of the node; may not be null
056         * @param conflictBehavior the desired behavior when a node exists at the <code>path</code>; may not be null
057         */
058        public BasicCreateNodeCommand( Path path,
059                                       Collection<Property> properties,
060                                       NodeConflictBehavior conflictBehavior ) {
061            super();
062            assert path != null;
063            assert properties != null;
064            assert conflictBehavior != null;
065            this.properties = properties;
066            this.path = path;
067            this.conflictBehavior = conflictBehavior;
068        }
069    
070        /**
071         * {@inheritDoc}
072         */
073        public Path getPath() {
074            return path;
075        }
076    
077        /**
078         * {@inheritDoc}
079         */
080        public Collection<Property> getProperties() {
081            return properties;
082        }
083    
084        /**
085         * {@inheritDoc}
086         */
087        public NodeConflictBehavior getConflictBehavior() {
088            return this.conflictBehavior;
089        }
090    
091        public void setProperty( Property property ) {
092            if (!properties.isEmpty()) {
093                for (Property existing : this.properties) {
094                    if (existing.getName().equals(property.getName())) {
095                        this.properties.remove(existing);
096                        break;
097                    }
098                }
099            }
100            this.properties.add(property);
101        }
102    
103        public void setProperties( Property... properties ) {
104            for (Property property : properties) {
105                setProperty(property);
106            }
107        }
108    
109        public void setProperties( Iterable<Property> properties ) {
110            for (Property property : properties) {
111                setProperty(property);
112            }
113        }
114    
115        /**
116         * {@inheritDoc}
117         */
118        public int compareTo( CreateNodeCommand that ) {
119            if (this == that) return 0;
120            return this.getPath().compareTo(that.getPath());
121        }
122    
123        /**
124         * {@inheritDoc}
125         */
126        @Override
127        public int hashCode() {
128            return path.hashCode();
129        }
130    
131        /**
132         * {@inheritDoc}
133         */
134        @Override
135        public boolean equals( Object obj ) {
136            if (obj == this) return true;
137            if (obj instanceof CreateNodeCommand) {
138                CreateNodeCommand that = (CreateNodeCommand)obj;
139                return this.path.equals(that.getPath());
140            }
141            return false;
142        }
143    
144        /**
145         * {@inheritDoc}
146         * 
147         * @see java.lang.Object#toString()
148         */
149        @Override
150        public String toString() {
151            StringBuilder sb = new StringBuilder();
152            sb.append(this.getClass().getSimpleName());
153            sb.append(" at ");
154            sb.append(this.getPath());
155            boolean firstProperty = true;
156            for (Property property : this.getProperties()) {
157                if (property.isEmpty()) continue;
158                if (firstProperty) {
159                    sb.append(" { ");
160                    firstProperty = false;
161                } else {
162                    sb.append("; ");
163                }
164                sb.append(property.getName());
165                sb.append("=");
166                if (property.isSingle()) {
167                    sb.append(StringUtil.readableString(property.getValues().next()));
168                } else {
169                    sb.append(StringUtil.readableString(property.getValuesAsArray()));
170                }
171            }
172            if (!firstProperty) {
173                sb.append(" }");
174            }
175            return sb.toString();
176        }
177    }