View Javadoc

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