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.HashMap;
025    import java.util.Map;
026    import net.jcip.annotations.NotThreadSafe;
027    import org.jboss.dna.common.util.StringUtil;
028    import org.jboss.dna.graph.cache.CachePolicy;
029    import org.jboss.dna.graph.commands.GetPropertiesCommand;
030    import org.jboss.dna.graph.properties.DateTime;
031    import org.jboss.dna.graph.properties.Name;
032    import org.jboss.dna.graph.properties.Path;
033    import org.jboss.dna.graph.properties.Property;
034    
035    /**
036     * @author Randall Hauch
037     */
038    @NotThreadSafe
039    public class BasicGetPropertiesCommand extends BasicGraphCommand implements GetPropertiesCommand {
040    
041        /**
042         */
043        private static final long serialVersionUID = -7816393217506909521L;
044        private final Map<Name, Property> properties = new HashMap<Name, Property>();
045        private final Path path;
046        private CachePolicy cachePolicy;
047        private DateTime timeLoaded;
048    
049        /**
050         * @param path the path to the node; may not be null
051         */
052        public BasicGetPropertiesCommand( Path path ) {
053            super();
054            assert path != null;
055            this.path = path;
056        }
057    
058        /**
059         * {@inheritDoc}
060         */
061        public void setProperty( Property property ) {
062            if (property != null) {
063                properties.put(property.getName(), property);
064            }
065        }
066    
067        public void setProperties( Map<Name, Property> properties ) {
068            this.properties.clear();
069            if (properties != null) this.properties.putAll(properties);
070        }
071    
072        /**
073         * Get the property values that were added to the command
074         * 
075         * @return the map of property name to values
076         */
077        public Iterable<Property> getProperties() {
078            return this.properties.values();
079        }
080    
081        public Map<Name, Property> getPropertiesByName() {
082            return this.properties;
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public Path getPath() {
089            return path;
090        }
091    
092        /**
093         * {@inheritDoc}
094         */
095        public CachePolicy getCachePolicy() {
096            return cachePolicy;
097        }
098    
099        /**
100         * {@inheritDoc}
101         */
102        public DateTime getTimeLoaded() {
103            return timeLoaded;
104        }
105    
106        /**
107         * @param timeLoaded Sets timeLoaded to the specified value.
108         */
109        public void setTimeLoaded( DateTime timeLoaded ) {
110            this.timeLoaded = timeLoaded;
111        }
112    
113        /**
114         * {@inheritDoc}
115         */
116        public void setCachePolicy( CachePolicy cachePolicy ) {
117            this.cachePolicy = cachePolicy;
118        }
119    
120        /**
121         * {@inheritDoc}
122         * 
123         * @see java.lang.Object#toString()
124         */
125        @Override
126        public String toString() {
127            StringBuilder sb = new StringBuilder();
128            sb.append(this.getClass().getSimpleName());
129            sb.append(" at ");
130            sb.append(this.getPath());
131            boolean firstProperty = true;
132            for (Property property : this.getProperties()) {
133                if (property.isEmpty()) continue;
134                if (firstProperty) {
135                    sb.append(" { ");
136                    firstProperty = false;
137                } else {
138                    sb.append("; ");
139                }
140                sb.append(property.getName());
141                sb.append("=");
142                if (property.isSingle()) {
143                    sb.append(StringUtil.readableString(property.getValues().next()));
144                } else {
145                    sb.append(StringUtil.readableString(property.getValuesAsArray()));
146                }
147            }
148            if (!firstProperty) {
149                sb.append(" }");
150            }
151            return sb.toString();
152        }
153    }