View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.api.repository.term;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlAnyElement;
25  import javax.xml.bind.annotation.XmlElement;
26  import javax.xml.bind.annotation.XmlElementWrapper;
27  import javax.xml.bind.annotation.XmlRootElement;
28  import javax.xml.bind.annotation.XmlType;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.jdom.IllegalAddException;
32  import org.kuali.rice.core.api.CoreConstants;
33  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
34  import org.kuali.rice.core.api.mo.ModelBuilder;
35  import org.kuali.rice.krms.api.KrmsConstants;
36  import org.kuali.rice.krms.api.repository.BuilderUtils;
37  
38  /**
39   * Immutable DTO for Terms.  Construction must be done via the {@link Builder} inner class.
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   */
44  @XmlRootElement(name = TermDefinition.Constants.ROOT_ELEMENT_NAME)
45  @XmlAccessorType(XmlAccessType.NONE)
46  @XmlType(name = TermDefinition.Constants.TYPE_NAME, propOrder = {
47  		TermDefinition.Elements.ID,
48          TermDefinition.Elements.SPECIFICATION,
49          TermDefinition.Elements.DESCRIPTION,
50  		TermDefinition.Elements.PARAMETERS,
51          CoreConstants.CommonElements.VERSION_NUMBER,
52  		CoreConstants.CommonElements.FUTURE_ELEMENTS
53  })
54  public final class TermDefinition extends AbstractDataTransferObject implements TermDefinitionContract {
55  	
56  	private static final long serialVersionUID = 1L;
57  	
58  	@XmlElement(name = Elements.ID, required=false)
59  	private final String id;
60  	@XmlElement(name = Elements.SPECIFICATION, required=true)
61  	private final TermSpecificationDefinition specification;
62      @XmlElement(name = Elements.DESCRIPTION, required=false)
63      private final String description;
64  	@XmlElementWrapper(name = Elements.PARAMETERS, required=false)
65  	@XmlElement(name = "parameter", required=false)
66  	private final List<TermParameterDefinition> parameters;
67      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
68      private final Long versionNumber;
69  	
70  	@SuppressWarnings("unused")
71      @XmlAnyElement
72      private final Collection<org.w3c.dom.Element> _futureElements = null;
73  	
74  	/**
75  	 * This constructor is for JAXB only.  Do not invoke directly.
76  	 */
77  	private TermDefinition() {
78  		id = null;
79  		specification = null;
80          description = null;
81  		parameters = null;
82          versionNumber = null;
83  	}
84  	
85  	private TermDefinition(Builder builder) {
86  		id = builder.getId();
87  		specification = builder.getSpecification().build();
88          description = builder.getDescription();
89  		parameters = BuilderUtils.convertFromBuilderList(builder.getParameters());
90  		versionNumber = builder.getVersionNumber();
91  	}
92  	
93  	/**
94  	 * {@link ModelBuilder} for {@link TermDefinition}s.
95  	 * 
96  	 * @author Kuali Rice Team (rice.collab@kuali.org)
97  	 *
98  	 */
99  	public static class Builder implements TermDefinitionContract, ModelBuilder, Serializable {
100 		
101 		private static final long serialVersionUID = 1L;
102 		
103 		private String id;
104         private String description;
105 		private TermSpecificationDefinition.Builder specification;
106 		private List<TermParameterDefinition.Builder> parameters;
107         private Long versionNumber;
108 		
109 		private Builder(String id, TermSpecificationDefinition.Builder termSpecificationDefinition, 
110 				List<TermParameterDefinition.Builder> termParameters) {
111 			setId(id);
112 			setSpecification(termSpecificationDefinition);
113 			setParameters(termParameters);
114 		}
115 
116 		/**
117 		 * static factory for creating a {@link Builder}.
118 		 * 
119 		 * @param id may be null.
120 		 * @param termSpecification must not be null.
121 		 * @param termParameters may be null.
122 		 */
123 		public static Builder create(String id, TermSpecificationDefinition.Builder termSpecification, 
124 				List<TermParameterDefinition.Builder> termParameters) {
125 			return new Builder(id, termSpecification, termParameters);
126 		}
127 		
128 		/**
129 		 * static factory for creating a {@link Builder} from a {@link TermDefinitionContract}.
130 		 * 
131 		 * @param term must be non-null.
132 		 */
133 		public static Builder create(TermDefinitionContract term) {
134 			if (term == null) throw new IllegalAddException("term may not be null");
135 			
136 			// Convert TermParameterDefinitionContract to TermParameterDefinition:
137 			List<TermParameterDefinition.Builder> outParams =
138 				BuilderUtils.transform(term.getParameters(), TermParameterDefinition.Builder.toBuilder);
139 
140 			Builder builder = create(term.getId(), 
141 					// doing my TermSpecificationDefinitionContract conversion inline:
142 					TermSpecificationDefinition.Builder.create(term.getSpecification()),
143 					// this is made immutable in the setter
144 					outParams 
145 					);
146             builder.setDescription(term.getDescription());
147 			builder.setVersionNumber(term.getVersionNumber());
148 			return builder;
149 		}
150 
151         public void setDescription(String description) {
152             this.description = description;
153         }
154 
155         // Builder setters:
156 		
157 		/**
158 		 * @param id the id to set.  Should be null to build {@link TermDefinition}s for creation operations.
159 		 * @throws IllegalArgumentException if the id is non-null and only contains whitespace
160 		 */
161 		public void setId(String id) {
162 			if (id != null && StringUtils.isBlank(id)) {
163 				throw new IllegalArgumentException("id must contain non-whitespace chars");
164 			}
165 			this.id = id;
166 		}
167 		
168 		/**
169 		 * @param termSpecification the termSpecification to set
170 		 * @throws IllegalArgumentException if termSpecification is null
171 		 */
172 		public void setSpecification(TermSpecificationDefinition.Builder termSpecification) {
173 			if (termSpecification == null) {
174 				throw new IllegalArgumentException("termSpecification must not be null");
175 			}
176 			this.specification = termSpecification;
177 		}
178 		
179 		/**
180 		 * @param parameters the termParameters to set.  May be null, or empty.
181 		 */
182 		public void setParameters(List<TermParameterDefinition.Builder> parameters) {
183 			this.parameters = parameters;
184 		}
185 
186 		/**
187 		 * @param versionNumber the versionNumber to set.  May be null.
188 		 */
189         public void setVersionNumber(Long versionNumber){
190             this.versionNumber = versionNumber;
191         }
192         
193 		// Builder getters:
194 		
195 		/**
196 		 * @return the id
197 		 */
198 		@Override
199 		public String getId() {
200 			return id;
201 		}
202 
203 		/**
204 		 * @return the termSpecification
205 		 */
206 		@Override
207 		public TermSpecificationDefinition.Builder getSpecification() {
208 			return specification;
209 		}
210 
211         @Override
212         public String getDescription() {
213             return description;
214         }
215 
216         /**
217 		 * @return the termParameters
218 		 */
219 		@Override
220 		public List<TermParameterDefinition.Builder> getParameters() {
221 			return parameters;
222 		}
223 		
224 		/**
225 		 * @return the version number
226 		 */
227         @Override
228         public Long getVersionNumber() {
229             return this.versionNumber;
230         }
231         
232 		/**
233 		 * Builds the {@link TermDefinition}, or dies trying.
234 		 * 
235 		 * @see org.kuali.rice.core.api.mo.ModelBuilder#build()
236 		 * @throws IllegalStateException if builder validation fails
237 		 */
238 		@Override
239 		public TermDefinition build() {
240 			return new TermDefinition(this);
241 		}
242 	}
243 	
244 	/**
245 	 * @return the termId.  May be null if this {@link TermDefinition} hasn't been persisted.
246 	 */
247 	@Override
248 	public String getId() {
249 		return this.id;
250 	}
251 	/**
252 	 * @return the specification.  Will never be null.
253 	 */
254 	@Override
255 	public TermSpecificationDefinition getSpecification() {
256 		return this.specification;
257 	}
258 
259     @Override
260     public String getDescription() {
261         return description;
262     }
263 
264     /**
265 	 * @return the parameters.  May be empty, but will never be null.
266 	 */
267 	@Override
268 	public List<TermParameterDefinition> getParameters() {
269 		return this.parameters;
270 	}
271 	
272 	/**
273 	 * @see org.kuali.rice.core.api.mo.common.Versioned#getVersionNumber()
274 	 */
275     @Override
276     public Long getVersionNumber() {
277         return versionNumber;
278     }
279 
280 	static class Constants {
281 		public static final String ROOT_ELEMENT_NAME = "termDefinition";
282 		public static final String TYPE_NAME = "termDefinitionType";
283 	}
284 
285 	static class Elements {
286 		public static final String ID = "id";
287 		public static final String SPECIFICATION = "specification";
288 		public static final String PARAMETERS = "parameters";
289         public static final String DESCRIPTION = "description";
290     }
291 
292     public static class Cache {
293         public static final String NAME = KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0 + "/" + TermDefinition.Constants.TYPE_NAME;
294     }
295 }