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