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.HashMap;
026 import java.util.List;
027 import java.util.Map;
028 import net.jcip.annotations.NotThreadSafe;
029 import org.jboss.dna.common.util.StringUtil;
030 import org.jboss.dna.graph.commands.GetNodeCommand;
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 BasicGetNodeCommand extends BasicGetChildrenCommand implements GetNodeCommand {
040
041 private static final long serialVersionUID = 5355669032301356873L;
042 private final Map<Name, Property> properties = new HashMap<Name, Property>();
043
044 /**
045 * @param path
046 */
047 public BasicGetNodeCommand( Path path ) {
048 super(path);
049
050 }
051
052 /**
053 * {@inheritDoc}
054 */
055 public void setProperty( Property property ) {
056 if (property != null) {
057 properties.put(property.getName(), property);
058 }
059 }
060
061 public void setProperties( Map<Name, Property> properties ) {
062 this.properties.clear();
063 if (properties != null) this.properties.putAll(properties);
064 }
065
066 /**
067 * Get the property values that were added to the command
068 *
069 * @return the map of property name to values
070 */
071 public Collection<Property> getProperties() {
072 return this.properties.values();
073 }
074
075 public Map<Name, Property> getPropertiesByName() {
076 return this.properties;
077 }
078
079 /**
080 * {@inheritDoc}
081 *
082 * @see java.lang.Object#toString()
083 */
084 @Override
085 public String toString() {
086 StringBuilder sb = new StringBuilder();
087 sb.append(this.getClass().getSimpleName());
088 sb.append(" at ");
089 sb.append(this.getPath());
090 boolean firstProperty = true;
091 for (Property property : this.getProperties()) {
092 if (property.isEmpty()) continue;
093 if (firstProperty) {
094 sb.append(" { ");
095 firstProperty = false;
096 } else {
097 sb.append("; ");
098 }
099 sb.append(property.getName());
100 sb.append("=");
101 if (property.isSingle()) {
102 sb.append(StringUtil.readableString(property.getValues().next()));
103 } else {
104 sb.append(StringUtil.readableString(property.getValuesAsArray()));
105 }
106 }
107 if (!firstProperty) {
108 sb.append(" }");
109 }
110 List<Path.Segment> children = this.getChildren();
111 if (children != null && children.size() > 0) {
112 sb.append(" with ").append(children.size()).append(" children: ");
113 sb.append(StringUtil.readableString(children));
114 }
115 return sb.toString();
116 }
117
118 }