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.function;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAnyElement;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.rice.core.api.CoreConstants;
30  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
31  import org.kuali.rice.core.api.mo.ModelBuilder;
32  import org.w3c.dom.Element;
33  
34  /**
35   * An immutable representation of a function parameter definition.
36   * 
37   * @see FunctionParameterDefinitionContract
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  @XmlRootElement(name = FunctionParameterDefinition.Constants.ROOT_ELEMENT_NAME)
43  @XmlAccessorType(XmlAccessType.NONE)
44  @XmlType(name = FunctionParameterDefinition.Constants.TYPE_NAME, propOrder = {
45  		FunctionParameterDefinition.Elements.ID,
46  		FunctionParameterDefinition.Elements.NAME,
47  		FunctionParameterDefinition.Elements.DESCRIPTION,
48  		FunctionParameterDefinition.Elements.PARAMETER_TYPE,
49  		FunctionParameterDefinition.Elements.SEQUENCE,
50  		FunctionParameterDefinition.Elements.FUNCTION_ID,
51          CoreConstants.CommonElements.VERSION_NUMBER,
52          CoreConstants.CommonElements.FUTURE_ELEMENTS
53  })
54  public class FunctionParameterDefinition extends AbstractDataTransferObject implements FunctionParameterDefinitionContract {
55  
56  	private static final long serialVersionUID = 1391030685309770560L;
57  
58  	@XmlElement(name = Elements.ID, required = false)
59  	private final String id;
60  		
61  	@XmlElement(name = Elements.NAME, required = true)
62  	private final String name;
63  	
64  	@XmlElement(name = Elements.DESCRIPTION, required = false)
65  	private final String description;
66  	
67  	@XmlElement(name = Elements.TYPE, required = true)
68  	private final String parameterType;
69  	
70  	@XmlElement(name = Elements.FUNCTION_ID, required = true)
71  	private final String functionId;
72  	
73  	@XmlElement(name = Elements.SEQUENCE, required=true)
74  	private Integer sequenceNumber;
75  
76  	@XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
77  	private final Long versionNumber;
78  	
79  	@SuppressWarnings("unused")
80      @XmlAnyElement
81      private final Collection<Element> _futureElements = null;
82  	
83  	/**
84       * Private constructor used only by JAXB.
85       */
86      private FunctionParameterDefinition() {
87      	this.id = null;
88      	this.name = null;
89      	this.description = null;
90      	this.parameterType = null;
91      	this.functionId = null;
92      	this.sequenceNumber = null;
93      	this.versionNumber = null;
94      }
95  
96      /**
97       * Constructs a FunctionParameterDefinition from the given builder.  This constructor is private and should only
98       * ever be invoked from the builder.
99       *
100      * @param builder the Builder from which to construct the FunctionParameterDefinition
101      */
102     private FunctionParameterDefinition(Builder builder) {
103     	this.id = builder.getId();
104     	this.name = builder.getName();
105     	this.description = builder.getDescription();
106     	this.parameterType = builder.getParameterType();
107     	this.functionId = builder.getFunctionId();
108     	this.sequenceNumber = builder.getSequenceNumber();
109     	this.versionNumber = builder.getVersionNumber();
110     }
111     
112 	@Override
113 	public String getId() {
114 		return id;
115 	}
116 
117 	@Override
118 	public String getName() {
119 		return name;
120 	}
121 
122 	@Override
123 	public String getDescription() {
124 		return description;
125 	}
126 	
127 	@Override
128 	public String getParameterType() {
129 		return parameterType;
130 	}
131 	
132 	@Override
133 	public String getFunctionId() {
134 		return functionId;
135 	}
136 	
137 	@Override
138 	public Long getVersionNumber() {
139 		return versionNumber;
140 	}
141 
142 	@Override
143 	public Integer getSequenceNumber() {
144 		return sequenceNumber;
145 	}
146 
147 	/**
148 	 * A builder which can be used to construct {@link FunctionParameterDefinition}
149 	 * instances.  Enforces the constraints of the {@link FunctionParameterDefinitionContract}.
150 	 * 
151 	 * @author Kuali Rice Team (rice.collab@kuali.org)
152 	 *
153 	 */
154 	public static final class Builder implements FunctionParameterDefinitionContract, ModelBuilder, Serializable  {
155     	    	
156     	private static final long serialVersionUID = -4470376239998290245L;
157     	
158 		private String id;
159     	private String name;
160     	private String description;
161     	private String functionId;
162     	private String parameterType;
163     	private Integer sequenceNumber;
164     	private Long versionNumber;
165 
166         /**
167          * Private constructor use the create method.
168          * @param name to use when building
169          * @param type to use when building
170          * @param sequenceNumber to use when building
171          */
172         private Builder(String name, String type, Integer sequenceNumber) {
173         	setName(name);
174         	setParameterType(type);
175         	setSequenceNumber(sequenceNumber);
176         }
177         
178         /**
179          * Creates a function parameter definition builder with the given required values.  This builder
180          * is the only means by which a {@link FunctionParameterDefinition} object should be created.
181          * 
182          * @param name the name of the function parameter definition to create, must not be null or blank
183          * @param type the type of the function parameter definition to create, must not be null or blank
184          * 
185          * @return a builder with the required values already initialized
186          * 
187          * @throws IllegalArgumentException if any of the given arguments is null or blank
188          */
189         public static Builder create(String name, String type, Integer sequenceNumber) {
190         	return new Builder(name, type, sequenceNumber);
191         }
192         
193         /**
194          * Creates and populates a builder with the data on the given {@link FunctionParameterDefinitionContract}.
195          * This is similar in nature to a "copy constructor" for {@link FunctionParameterDefinition}.
196          * 
197          * @param contract an object implementing the {@link FunctionParameterDefinitionContract} from which
198          * to copy property values
199          *  
200          * @return a builder with the values from the contract already initialized
201          * 
202          * @throws IllegalArgumentException if the given contract is null
203          */
204         public static Builder create(FunctionParameterDefinitionContract contract) {
205         	if (contract == null) {
206         		throw new IllegalArgumentException("contract was null");
207         	}
208         	Builder builder = create(contract.getName(), contract.getParameterType(), contract.getSequenceNumber());
209         	builder.setId(contract.getId());
210         	builder.setDescription(contract.getDescription());
211         	builder.setParameterType(contract.getParameterType());
212         	builder.setFunctionId(contract.getFunctionId());
213         	builder.setVersionNumber(contract.getVersionNumber());
214         	return builder;
215         }
216 
217         @Override
218         public FunctionParameterDefinition build() {
219         	return new FunctionParameterDefinition(this);
220         }
221         
222         @Override
223 		public String getId() {
224 			return this.id;
225 		}
226 
227         /**
228          * Sets the id for the function parameter definition that will be returned by this builder.
229          * 
230          * @param id the function parameter definition id to set
231          */
232 		public void setId(String id) {
233 			this.id = id;
234 		}
235 
236 		@Override
237 		public String getName() {
238 			return this.name;
239 		}
240 
241 		/**
242          * Sets the name for the function parameter definition that will be returned by this builder.
243          * The name must not be null or blank.
244          * 
245          * @param name the name to set on this builder, must not be null or blank
246          */
247 		public void setName(String name) {
248 			if (StringUtils.isBlank(name)) {
249 				throw new IllegalArgumentException("name was blank");
250 			}
251 			this.name = name;
252 		}
253 		
254 		@Override
255 		public String getDescription() {
256 			return this.description;
257 		}
258 
259         /**
260          * Sets the description for the function parameter definition that will be returned by this builder.
261          * 
262          * @param description the description to set on this builder
263          */
264 		public void setDescription(String description) {
265 			this.description = description;
266 		}
267 
268 		@Override
269 		public String getParameterType() {
270 			return this.parameterType;
271 		}
272 
273 		/**
274          * Sets the type for the function parameter definition that will be
275          * returned by this builder.  This can be one of a set of "built-in"
276          * data types or a custom datatype represented as a fully qualified
277          * java class name.  The type must not be null or blank.
278          * 
279          * @param type the type to set on this builder, must not be null or blank
280          */
281 		public void setParameterType(String type) {
282 			if (StringUtils.isBlank(type)) {
283 				throw new IllegalArgumentException("type was blank");
284 			}
285 			this.parameterType = type;
286 		}
287 
288 		@Override
289 		public String getFunctionId() {
290 			return this.functionId;
291 		}
292 
293 		/**
294          * Sets the type for the function id
295          * If provided, the function id must be non-blank.
296          * Must allow id to be null, to prevent chicken/egg problems.
297          * 
298          * @param functionId the functionId to set on this builder, must be either null or non-blank
299          */
300 		public void setFunctionId(String functionId) {
301 			if (functionId != null && StringUtils.isBlank(functionId)) {
302 				throw new IllegalArgumentException("functionId must be null or non-blank");
303 			}
304 			this.functionId = functionId;
305 		}
306 
307 		@Override
308 		public Integer getSequenceNumber() {
309 			return this.sequenceNumber;
310 		}
311 
312 		/**
313          * Sets the sequence number for the function parameter definition that
314          * will be returned by this builder. This is the position in the functions
315          * parameter list.
316          * 
317          * @param sequenceNumber the position of the parameter in the function parameter list
318          */
319 		public void setSequenceNumber(Integer sequenceNumber) {
320 			this.sequenceNumber = sequenceNumber;
321 		}
322 
323 		@Override
324 		public Long getVersionNumber() {
325 			return this.versionNumber;
326 		}
327 
328 		/**
329          * Sets the version number for the function parameter definition that
330          * will be returned by this builder.
331          * 
332          * <p>In general, this value should not be manually set on the builder,
333          * but rather copied from an existing {@link FunctionParameterDefinitionContract} when
334          * invoking {@link Builder#create(FunctionParameterDefinitionContract)}.
335          * 
336          * @param versionNumber the version number to set
337          */
338 		public void setVersionNumber(Long versionNumber) {
339 			this.versionNumber = versionNumber;
340 		}
341 
342 	}
343 	
344 	/**
345      * Defines some internal constants used on this class.
346      */
347     static class Constants {
348         final static String ROOT_ELEMENT_NAME = "functionParameter";
349         final static String TYPE_NAME = "FunctionParameterType";
350     }
351 
352     /**
353      * A private class which exposes constants which define the XML element names to use
354      * when this object is marshalled to XML.
355      */
356     static class Elements {
357         final static String ID = "id";
358         final static String NAME = "name";
359         final static String DESCRIPTION = "description";
360         final static String PARAMETER_TYPE = "parameterType";
361         final static String TYPE = "type";
362         final static String FUNCTION_ID = "functionId";
363 		final static String SEQUENCE = "sequenceNumber";
364     }
365     
366 }