001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.util.documentserializer;
017
018import java.util.ArrayList;
019import java.util.Iterator;
020import java.util.List;
021
022/**
023 * A node in the trie.
024 *
025 */
026public class PropertySerializerTrieNode implements PropertySerializabilityMetadata {
027    private String pathString;
028    private String propertyNameComponent;
029    private PropertySerializability propertySerializability;
030
031    private List<PropertySerializerTrieNode> childNodes;
032
033    public PropertySerializerTrieNode(String pathString, String propertyNameComponent) {
034        this.pathString = pathString;
035        this.propertyNameComponent = propertyNameComponent;
036        this.childNodes = null;
037        this.propertySerializability = PropertySerializability.SERIALIZE_OBJECT;
038    }
039
040    public void addChildNode(PropertySerializerTrieNode child) {
041        if (childNodes == null) {
042            childNodes = new ArrayList<PropertySerializerTrieNode>();
043        }
044        childNodes.add(child);
045    }
046
047    /**
048     * The name of this property, relative to the parent node (i.e. the child node name relative to its parents).
049     *
050     * @return
051     */
052    public String getPropertyNameComponent() {
053        return propertyNameComponent;
054    }
055
056    /**
057     * Retrieves the child node with the given name
058     *
059     * @param propertyNameComponent
060     * @return
061     */
062    public PropertySerializerTrieNode getChildNode(String propertyNameComponent) {
063        if (childNodes == null) {
064            return null;
065        }
066        for (int i = 0; i < childNodes.size(); i++) {
067            PropertySerializerTrieNode childNode = childNodes.get(i);
068            if (childNode.getPropertyNameComponent().equals(propertyNameComponent)) {
069                return childNode;
070            }
071        }
072        return null;
073    }
074
075    /**
076     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityMetadata#getSerializableChildProperty(java.lang.String)
077     */
078    public PropertySerializabilityMetadata getSerializableChildProperty(String propertyNameComponent) {
079        return getChildNode(propertyNameComponent);
080    }
081
082    /**
083     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityMetadata#getPathString()
084     */
085    public String getPathString() {
086        return pathString;
087    }
088
089    /**
090     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityMetadata#getPropertySerializability()
091     */
092    public PropertySerializability getPropertySerializability() {
093        return propertySerializability;
094    }
095
096    /**
097     * Marks that all primitives of this object will be serialized.
098     */
099    public void setPropertySerializabilityToObjectAndAllPrimitives() {
100        this.propertySerializability = PropertySerializability.SERIALIZE_OBJECT_AND_ALL_PRIMITIVES;
101    }
102
103    @Override
104    public String toString() {
105        StringBuilder buf = new StringBuilder();
106        buf.append("Path String: ").append(pathString).append(" Name component: ").append(propertyNameComponent);
107        if (childNodes == null || childNodes.isEmpty()) {
108            buf.append(" No child nodes.");
109        }
110        else {
111            buf.append(" Child nodes: ");
112            for (Iterator<PropertySerializerTrieNode> i = childNodes.iterator(); i.hasNext();) {
113                buf.append(i.next().getPropertyNameComponent());
114                if (i.hasNext()) {
115                    buf.append(", ");
116                }
117            }
118        }
119        return super.toString();
120    }
121}