View Javadoc

1   /**
2    * Copyright 2005-2011 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 org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
23  import org.kuali.rice.krms.api.repository.BuilderUtils;
24  
25  import javax.xml.bind.annotation.XmlAccessType;
26  import javax.xml.bind.annotation.XmlAccessorType;
27  import javax.xml.bind.annotation.XmlAnyElement;
28  import javax.xml.bind.annotation.XmlElement;
29  import javax.xml.bind.annotation.XmlElementWrapper;
30  import javax.xml.bind.annotation.XmlRootElement;
31  import javax.xml.bind.annotation.XmlType;
32  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
33  import java.io.Serializable;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.Map;
37  import java.util.Set;
38  
39  /**
40   * This is a description of what this class does - gilesp don't forget to fill this in. 
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  @XmlRootElement(name = TermResolverDefinition.Constants.ROOT_ELEMENT_NAME)
46  @XmlAccessorType(XmlAccessType.NONE)
47  @XmlType(name = TermResolverDefinition.Constants.TYPE_NAME, propOrder = {
48  		TermResolverDefinition.Elements.ID,
49  		TermResolverDefinition.Elements.NAME,
50          TermResolverDefinition.Elements.NAMESPACE,
51  		TermResolverDefinition.Elements.TYPE_ID,
52  		TermResolverDefinition.Elements.OUTPUT,
53  		TermResolverDefinition.Elements.PREREQUISITES,
54  		TermResolverDefinition.Elements.ATTRIBUTES,
55  		TermResolverDefinition.Elements.PARAMETER_NAMES,
56          CoreConstants.CommonElements.VERSION_NUMBER,
57  		CoreConstants.CommonElements.FUTURE_ELEMENTS
58  })
59  public final class TermResolverDefinition extends AbstractDataTransferObject implements TermResolverDefinitionContract {
60  	
61      private static final long serialVersionUID = 1L;
62  	
63  	@XmlElement(name = Elements.ID, required=false)
64  	private final String id;
65  	@XmlElement(name = Elements.NAMESPACE, required=true)
66  	private final String namespace;
67  	@XmlElement(name = Elements.NAME, required=true)
68  	private final String name;
69      @XmlElement(name = Elements.TYPE_ID, required=true)
70      private final String typeId;
71  	@XmlElement(name = Elements.OUTPUT, required=false)
72  	private final TermSpecificationDefinition output;
73  
74  	@XmlElement(name = "termSpecificationDefinition", required=false)
75  	@XmlElementWrapper(name = Elements.PREREQUISITES, required=false)
76  	private final Set<TermSpecificationDefinition> prerequisites;
77  
78  	@XmlElement(name = Elements.ATTRIBUTES, required = false)
79  	@XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
80  	private final Map<String, String> attributes;
81  
82  	@XmlElementWrapper(name = Elements.PARAMETER_NAMES, required=false)
83  	@XmlElement(name = "parameterName")
84  	private final Set<String> parameterNames;
85  
86  	@XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
87      private final Long versionNumber;	
88  	
89      @SuppressWarnings("unused")
90  	@XmlAnyElement
91  	private final Collection<org.w3c.dom.Element> _futureElements = null;
92      
93  	/**
94  	 * This private constructor is for JAXB use only, don't invoke directly.
95  	 */
96  	private TermResolverDefinition() {
97  		id = null;
98  		namespace = null;
99  		name = null;
100 		typeId = null;
101 		output = null;
102 		prerequisites = null;
103 		attributes = null;
104 		parameterNames = null;
105         versionNumber = null;
106 	}
107 	
108 	private TermResolverDefinition(Builder builder) {
109 		this.id = builder.getId();
110 		this.namespace = builder.getNamespace();
111 		this.name = builder.getName();
112 		this.typeId = builder.getTypeId();
113 		this.output = builder.getOutput().build();
114 		this.prerequisites = BuilderUtils.convertFromBuilderSet(builder.getPrerequisites());
115 		this.parameterNames = Collections.unmodifiableSet(builder.getParameterNames());
116 		this.versionNumber = builder.getVersionNumber();
117         if (builder.attributes != null){
118         	this.attributes = Collections.unmodifiableMap(builder.getAttributes());
119         } else {
120         	this.attributes = null;
121         }
122 	}
123 	
124 	public static class Builder implements TermResolverDefinitionContract, ModelBuilder, 
125 		Serializable {
126 		
127 		private static final long serialVersionUID = 1L;
128 		
129 		private String id;
130 		private String namespace;
131 		private String name;
132         private String typeId;
133 		private TermSpecificationDefinition.Builder output;
134 		private Set<TermSpecificationDefinition.Builder> prerequisites;
135         private Map<String, String> attributes;
136 		private Set<String> parameterNames;
137         private Long versionNumber;
138 		
139 		private Builder(String id, String namespaceCode, String name, String typeId, TermSpecificationDefinition.Builder output,
140                 Set<TermSpecificationDefinition.Builder> prerequisites, Map<String, String> attributes, Set<String> parameterNames) {
141 			setId(id);
142 			setNamespace(namespaceCode);
143 			setName(name);
144 			setTypeId(typeId);
145 			setOutput(output);
146 			setPrerequisites(prerequisites);
147 			setAttributes(attributes);
148 			setParameterNames(parameterNames);
149 		}
150 		
151 		
152 		
153 		private Builder(TermResolverDefinitionContract termResolver) {
154 			setId(termResolver.getId());
155 			setNamespace(termResolver.getNamespace());
156 			setName(termResolver.getName());
157 			setTypeId(termResolver.getTypeId());
158 			setOutput(TermSpecificationDefinition.Builder.create(termResolver.getOutput()));
159 			setPrerequisites(BuilderUtils.transform(termResolver.getPrerequisites(), TermSpecificationDefinition.Builder.toBuilder));
160 			setAttributes(termResolver.getAttributes());
161 			this.setParameterNames(termResolver.getParameterNames());
162 			this.setVersionNumber(termResolver.getVersionNumber());
163 		}
164 		
165 		public static Builder create(TermResolverDefinitionContract termResolver) {
166 			return new Builder(termResolver);
167 		}
168 		
169 		public static Builder create(String id, String namespaceCode, String name, String typeId,
170                 TermSpecificationDefinition.Builder output, Set<TermSpecificationDefinition.Builder> prerequisites, Map<String, String> attributes,
171                 Set<String> parameterNames) {
172 			return new Builder(id, namespaceCode, name, typeId, output, prerequisites, attributes, parameterNames);
173 		}
174 
175 		// Builder setters:
176 		// TODO: proper validation & javadocs
177 		
178 		/**
179 		 * @param id the id to set
180 		 */
181 		public void setId(String id) {
182 			if (id != null && StringUtils.isBlank(id)) {
183 				throw new IllegalArgumentException(/* TODO */);
184 			}
185 			this.id = id;
186 		}
187 
188 		/**
189 		 * @param namespace the namespace to set
190 		 */
191 		public void setNamespace(String namespace) {
192 			if (StringUtils.isBlank(namespace)) {
193 				throw new IllegalArgumentException(/* TODO */);
194 			}
195 			this.namespace = namespace;
196 		}
197 
198 		/**
199 		 * @param name the name to set
200 		 */
201 		public void setName(String name) {
202 			if (StringUtils.isBlank(name)) {
203 				throw new IllegalArgumentException(/* TODO */);
204 			}
205 			this.name = name;
206 		}
207 
208 		/**
209 		 * @param typeId the typeId to set
210 		 */
211 		public void setTypeId(String typeId) {
212 			if (StringUtils.isBlank(typeId)) {
213 				throw new IllegalArgumentException(/* TODO */);
214 			}
215 			this.typeId = typeId;
216 		}
217 
218 		/**
219 		 * @param output the output to set
220 		 */
221 		public void setOutput(TermSpecificationDefinition.Builder output) {
222 			if (output == null) {
223 				throw new IllegalArgumentException(/* TODO */);
224 			}
225 			this.output = output;
226 		}
227 
228 		/**
229 		 * @param prerequisites the prerequisites to set
230 		 */
231 		public void setPrerequisites(
232 				Set<TermSpecificationDefinition.Builder> prerequisites) {
233 			this.prerequisites = prerequisites;
234 		}
235 
236 		/**
237 		 * @param attributes the attributes to set
238 		 */
239 		public void setAttributes(Map<String, String> attributes){
240 			if (attributes == null){
241 				this.attributes = Collections.emptyMap();
242 			} else {
243 				this.attributes = Collections.unmodifiableMap(attributes);
244 			}
245 		}
246 
247 		/**
248 		 * @param parameterNames the parameterNames to set
249 		 */
250 		public void setParameterNames(Set<String> parameterNames) {
251 			this.parameterNames = parameterNames;
252 		}		
253 		
254 		/**
255 		 * @param versionNumber the versionNumber to set.  May be null.
256 		 */
257         public void setVersionNumber(Long versionNumber){
258             this.versionNumber = versionNumber;
259         }
260         
261 		// Builder getters:
262 
263 		/**
264 		 * @return the id
265 		 */
266 		public String getId() {
267 			return this.id;
268 		}
269 		
270 		/**
271 		 * @return the namespace
272 		 */
273 		public String getNamespace() {
274 			return this.namespace;
275 		}
276 		/**
277 		 * @return the name
278 		 */
279 		public String getName() {
280 			return this.name;
281 		}
282 		/**
283 		 * @return the typeId
284 		 */
285 		public String getTypeId() {
286 			return this.typeId;
287 		}
288 		/**
289 		 * @return the output
290 		 */
291 		public TermSpecificationDefinition.Builder getOutput() {
292 			return this.output;
293 		}
294 		/**
295 		 * @return the prerequisites
296 		 */
297 		public Set<TermSpecificationDefinition.Builder> getPrerequisites() {
298 			return this.prerequisites;
299 		}
300 		/**
301 		 * @return the attributes
302 		 */
303 		public Map<String, String> getAttributes() {
304 			return this.attributes;
305 		}
306 		/**
307 		 * @return the parameterNames
308 		 */
309 		public Set<String> getParameterNames() {
310 			return (parameterNames == null) ? Collections.<String>emptySet() : parameterNames;
311 		}
312 		
313 		/**
314 		 * @return the version number
315 		 */
316         @Override
317         public Long getVersionNumber() {
318             return this.versionNumber;
319         }
320 
321 		/**
322 		 * This overridden method ...
323 		 * 
324 		 * @see org.kuali.rice.core.api.mo.ModelBuilder#build()
325 		 */
326 		@Override
327 		public TermResolverDefinition build() {
328 			return new TermResolverDefinition(this);
329 		}
330 		
331 	}
332 	
333 	/**
334 	 * @return the id
335 	 */
336 	@Override
337 	public String getId() {
338 		return this.id;
339 	}
340 	
341 	/**
342 	 * @return the namespace
343 	 */
344 	@Override
345 	public String getNamespace() {
346 		return this.namespace;
347 	}
348 	
349 	/**
350 	 * @return the name
351 	 */
352 	@Override
353 	public String getName() {
354 		return this.name;
355 	}
356 	
357 	/**
358 	 * @return the typeId
359 	 */
360 	@Override
361 	public String getTypeId() {
362 		return this.typeId;
363 	}
364 	
365 	/**
366 	 * @return the specification
367 	 */
368 	@Override
369 	public TermSpecificationDefinition getOutput() {
370 		return this.output;
371 	}
372 	
373 	/**
374 	 * @return the prerequisites
375 	 */
376 	@Override
377 	public Set<TermSpecificationDefinition> getPrerequisites() {
378 		return this.prerequisites;
379 	}
380 	/**
381 	 * @return the attributes
382 	 */
383 	@Override
384 	public Map<String, String> getAttributes() {
385 		return this.attributes;
386 	}
387 
388 	/**
389 	 * @return the parameterNames
390 	 */
391 	@Override
392 	public Set<String> getParameterNames() {
393 		return this.parameterNames;
394 	}
395 	
396 	/**
397 	 * @see org.kuali.rice.core.api.mo.common.Versioned#getVersionNumber()
398 	 */
399     @Override
400     public Long getVersionNumber() {
401         return versionNumber;
402     }
403 	
404 	/**
405 	 * Defines some internal constants used on this class.
406 	 */
407 	static class Constants {
408 		final static String ROOT_ELEMENT_NAME = "termResolverDefinition";
409 		final static String TYPE_NAME = "termResolverDefinitionType";
410 	}
411 	
412 	static class Elements {
413 		public static final String ID = "id";
414 		public static final String NAMESPACE = "namespace";
415 		public static final String NAME = "name";
416         public static final String TYPE_ID = "typeId";
417 		public static final String OUTPUT = "output";
418 		public static final String PREREQUISITES = "prerequisites";
419 		public static final String ATTRIBUTES = "attributes";
420 		public static final String PARAMETER_NAMES = "parameterNames";
421 	}
422 }