001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package org.kuali.rice.krms.api.repository;
006
007import java.io.Serializable;
008import java.util.ArrayList;
009import java.util.Collection;
010import java.util.Collections;
011import java.util.List;
012import javax.xml.bind.annotation.XmlAccessType;
013import javax.xml.bind.annotation.XmlAccessorType;
014import javax.xml.bind.annotation.XmlAnyElement;
015import javax.xml.bind.annotation.XmlElement;
016import javax.xml.bind.annotation.XmlRootElement;
017import javax.xml.bind.annotation.XmlType;
018import org.kuali.rice.core.api.CoreConstants;
019import org.kuali.rice.core.api.mo.ModelBuilder;
020import org.kuali.rice.krms.api.KrmsConstants;
021
022/**
023 * Concrete model object implementation of a natural language tree immutable.
024 * Instances of natural language tree can be (un)marshalled to and from XML.
025 *
026 * @see NaturalLanguageTreeContract
027 */
028@XmlRootElement(name = NaturalLanguageTree.Constants.ROOT_ELEMENT_NAME)
029@XmlAccessorType(XmlAccessType.NONE)
030@XmlType(name = NaturalLanguageTree.Constants.TYPE_NAME, propOrder = {
031    NaturalLanguageTree.Elements.NATURAL_LANGUAGE,
032    NaturalLanguageTree.Elements.CHILDREN,
033    CoreConstants.CommonElements.FUTURE_ELEMENTS
034})
035public class NaturalLanguageTree implements NaturalLanguageTreeContract {
036
037    private static final long serialVersionUID = 2783959459503209577L;
038    @XmlElement(name = NaturalLanguageTree.Elements.NATURAL_LANGUAGE, required = false)
039    private String naturalLanguage;
040    @XmlElement(name = NaturalLanguageTree.Elements.CHILDREN, required = false)
041    private List<NaturalLanguageTree> children;
042    @SuppressWarnings("unused")
043    @XmlAnyElement
044    private final Collection<org.w3c.dom.Element> _futureElements = null;
045
046    /**
047     * This constructor should never be called. It is only present for use
048     * during JAXB unmarshalling.
049     */
050    public NaturalLanguageTree() {
051        this.naturalLanguage = null;
052        this.children = null;
053    }
054
055    @Override
056    public String getNaturalLanguage() {
057        return naturalLanguage;
058    }
059
060    @Override
061    public List<NaturalLanguageTree> getChildren() {
062        return children;
063    }
064
065    /**
066     * Constructs a KRMS Repository Agenda object from the given builder. This
067     * constructor is private and should only ever be invoked from the builder.
068     *
069     * @param builder the Builder from which to construct the Agenda
070     */
071    private NaturalLanguageTree(Builder builder) {
072        this.naturalLanguage = builder.getNaturalLanguage();
073        List<NaturalLanguageTree> list = null;
074        if (builder.getChildren() != null) {
075            list = new ArrayList<NaturalLanguageTree>(builder.getChildren().size());
076            for (NaturalLanguageTreeContract nltree : builder.getChildren()) {
077                list.add(Builder.create(nltree).build());
078            }
079            this.children = Collections.unmodifiableList(list);
080        }
081    }
082
083    /**
084     * This builder is used to construct instances of KRMS Repository Agenda. It
085     * enforces the constraints of the {@link NaturalLanguageTreeContract}.
086     */
087    public static class Builder implements NaturalLanguageTreeContract, ModelBuilder, Serializable {
088
089        private static final long serialVersionUID = -8862851720709537839L;
090        private String naturalLanguage;
091        private List<? extends NaturalLanguageTreeContract> children;
092 
093        /**
094         * Private constructor for creating a builder with all of it's required
095         * attributes.
096         */
097        private Builder() {
098        }        
099        
100        /**
101         * Private constructor for creating a builder with all of it's required
102         * attributes.
103         */
104        private Builder(String naturalLanguage, List<? extends NaturalLanguageTreeContract> children) {
105            setNaturalLanguage(naturalLanguage);
106            setChildren(children);
107        }
108
109        @Override
110        public String getNaturalLanguage() {
111            return naturalLanguage;
112        }
113
114        public void setNaturalLanguage(String naturalLanguage) {
115            this.naturalLanguage = naturalLanguage;
116        }
117
118        @Override
119        public List<? extends NaturalLanguageTreeContract> getChildren() {
120            return children;
121        }
122
123        public void setChildren(List<? extends NaturalLanguageTreeContract> children) {
124            this.children = children;
125        }
126
127        
128        /**
129         * Creates a builder by populating it with data from the given
130         * {@link NaturalLanguageTreeContract}.
131         *
132         * @param contract the contract from which to populate this builder
133         * @return an instance of the builder populated with data from the
134         * contract
135         * @throws IllegalArgumentException if the contract is null
136         */
137        public static NaturalLanguageTree.Builder create() {
138            NaturalLanguageTree.Builder builder = new NaturalLanguageTree.Builder();
139            return builder;
140        }
141        /**
142         * Creates a builder by populating it with data from the given
143         * {@link NaturalLanguageTreeContract}.
144         *
145         * @param contract the contract from which to populate this builder
146         * @return an instance of the builder populated with data from the
147         * contract
148         * @throws IllegalArgumentException if the contract is null
149         */
150        public static NaturalLanguageTree.Builder create(NaturalLanguageTreeContract contract) {
151            if (contract == null) {
152                throw new IllegalArgumentException("contract is null");
153            }
154            NaturalLanguageTree.Builder builder = new NaturalLanguageTree.Builder(contract.getNaturalLanguage(), contract.getChildren());
155
156            return builder;
157        }
158
159        /**
160         * Builds an instance of a Natural Language Tree based on the current state of the
161         * builder.
162         *
163         * @return the fully-constructed Agenda
164         */
165        @Override
166        public NaturalLanguageTree build() {
167            return new NaturalLanguageTree(this);
168        }
169    }
170
171    /**
172     * Defines some constants used on this class.
173     */
174    public static class Constants {
175
176        final static String ROOT_ELEMENT_NAME = "naturalLanguageTree";
177        final static String TYPE_NAME = "NaturalLanguageTreeType";
178        final static String[] HASH_CODE_EQUALS_EXCLUDE = {"_futureElements"};
179        public final static String EVENT = "Event";   // key for event attribute
180    }
181
182    /**
183     * A private class which exposes constants which define the XML element
184     * names to use when this object is marshalled to XML.
185     */
186    public static class Elements {
187
188        final static String NATURAL_LANGUAGE = "naturalLanguage";
189        final static String CHILDREN = "children";
190    }
191
192    public static class Cache {
193
194        public static final String NAME = KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0 + "/" + NaturalLanguageTree.Constants.TYPE_NAME;
195    }
196}