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  
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.kuali.rice.krms.api.repository.BuilderUtils.Transformer;
33  
34  /**
35   * Immutable DTO for TermParameters.  An instance represents a single parameter on a Term. 
36   * Construction must be done via the {@link Builder} inner class.
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  @XmlRootElement(name = TermParameterDefinition.Constants.ROOT_ELEMENT_NAME)
42  @XmlAccessorType(XmlAccessType.NONE)
43  @XmlType(name = TermParameterDefinition.Constants.TYPE_NAME, propOrder = {
44  		TermParameterDefinition.Elements.ID,
45  		TermParameterDefinition.Elements.NAME,
46  		TermParameterDefinition.Elements.VALUE,
47          CoreConstants.CommonElements.VERSION_NUMBER,
48  		CoreConstants.CommonElements.FUTURE_ELEMENTS
49  })
50  public final class TermParameterDefinition extends AbstractDataTransferObject implements TermParameterDefinitionContract {
51  
52  	private static final long serialVersionUID = 1L;
53  	
54  	@XmlElement(name = Elements.ID, required=true)
55  	private final String id;
56  	
57  	private final String termId;
58  
59  	@XmlElement(name = Elements.NAME, required=true)
60  	private final String name;
61  	
62  	@XmlElement(name = Elements.VALUE)
63  	private final String value;
64  	
65      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
66      private final Long versionNumber;
67  
68      @SuppressWarnings("unused")
69      @XmlAnyElement
70      private final Collection<org.w3c.dom.Element> _futureElements = null;
71  	
72  	// For JAXB use only, shouldn't be invoked directly
73  	private TermParameterDefinition() {
74  		id = null;
75  		termId = null;
76  		name = null;
77  		value = null;
78          versionNumber = null;
79  	}
80  	
81  	private TermParameterDefinition(Builder builder) {
82  		id = builder.getId(); 
83  		termId = builder.getTermId();
84  		name = builder.getName();
85  		value = builder.getValue();
86  		versionNumber = builder.getVersionNumber();
87  	}
88  	
89  	public static class Builder implements TermParameterDefinitionContract, ModelBuilder, Serializable {
90  
91  		private static final long serialVersionUID = 1L;
92  		
93  		private String id;
94  		private String termId;
95  		private String name;
96  		private String value;
97          private Long versionNumber;
98  		
99  		private static final String NON_NULL_NON_EMPTY_ERROR =  
100 			" must be non-null and must contain non-whitespace chars";
101 		
102 		public static final Transformer<TermParameterDefinitionContract, TermParameterDefinition.Builder> toBuilder = 
103 			new Transformer<TermParameterDefinitionContract, TermParameterDefinition.Builder>() {
104 			public Builder transform(TermParameterDefinitionContract input) {
105 				return Builder.create(input);
106 			};
107 		};
108 		
109 		private Builder(String id, String termId, String name, String value) {
110 			setId(id);
111 			setTermId(termId);
112 			setName(name);
113 			setValue(value);
114 		}
115 		
116 		/**
117 		 * static factory to create a {@link Builder} from fields
118 		 * 
119 		 * @param id must be null, or contain non-whitespace
120 		 * @param termId must be null, or contain non-whitespace
121 		 * @param name must be non-null
122 		 * @param value
123 		 */
124 		public static Builder create(String id, String termId, String name, String value) {
125 			return new Builder(id, termId, name, value);
126 		}
127 		
128 		/**
129 		 * static factory to create a {@link Builder} from a {@link TermParameterDefinitionContract} 
130 		 * 
131 		 * @param termParameterDefinition
132 		 */
133 		public static Builder create(TermParameterDefinitionContract termParameterDefinition) {
134 			Builder builder = new Builder(termParameterDefinition.getId(), 
135 					termParameterDefinition.getTermId(),
136 					termParameterDefinition.getName(), 
137 					termParameterDefinition.getValue());
138 			builder.setVersionNumber(termParameterDefinition.getVersionNumber());
139 			return builder;
140 		}
141 		
142 		// Setters:
143 		
144 		/**
145 		 * @param id the id to set.  for {@link TermParameterDefinition}s used in creational 
146 		 * service methods, it must be null.  Otherwise, it must be non-null and contain non-whitespace chars.
147 		 * @throws IllegalArgumentException if id is all whitespace chars
148 		 */
149 		public void setId(String id) {
150 			if (id != null && StringUtils.isBlank(id)) {
151 				throw new IllegalArgumentException("id must contain non-whitespace chars");
152 			}
153 			this.id = id;
154 		}
155 		
156 		/**
157 		 * @param termId the termId to set
158 		 */
159 		public void setTermId(String termId) {
160 			if (termId != null && StringUtils.isBlank(termId)) {
161 				throw new IllegalArgumentException("termId must contain non-whitespace chars");
162 			}
163 			this.termId = termId;
164 		}
165 		
166 		/**
167 		 * @param name the name to set.  Must be non-null and contain non-whitespace chars.
168 		 * @throws IllegalArgumentException if name is null or is all whitespace chars
169 		 */
170 		public void setName(String name) {
171 			if (StringUtils.isBlank(name)) {
172 				throw new IllegalArgumentException("name" + NON_NULL_NON_EMPTY_ERROR);
173 			}
174 			this.name = name;
175 		}
176 		
177 		/**
178 		 * @param value the value to set.  May be null or empty.
179 		 */
180 		public void setValue(String value) {
181 			this.value = value;
182 		}
183 		
184 		/**
185 		 * @param versionNumber the versionNumber to set.  May be null.
186 		 */
187         public void setVersionNumber(Long versionNumber){
188             this.versionNumber = versionNumber;
189         }
190         
191 		// Getters:
192 		
193 		/**
194 		 * @return the id
195 		 */
196 		@Override
197 		public String getId() {
198 			return this.id;
199 		}
200 		
201 		/**
202 		 * @return the termId
203 		 */
204 		@Override
205 		public String getTermId() {
206 			return this.termId;
207 		}
208 		
209 		/**
210 		 * @return the name
211 		 */
212 		@Override
213 		public String getName() {
214 			return this.name;
215 		}
216 		
217 		/**
218 		 * @return the value
219 		 */
220 		@Override
221 		public String getValue() {
222 			return this.value;
223 		}		
224 		
225 		/**
226 		 * @return the version number
227 		 */
228         @Override
229         public Long getVersionNumber() {
230             return this.versionNumber;
231         }
232 
233 		/**
234 		 * return a {@link TermParameterDefinition} instance constructed from this {@link Builder} 
235 		 * @see org.kuali.rice.core.api.mo.ModelBuilder#build()
236 		 */
237 		@Override
238 		public TermParameterDefinition build() {
239 			return new TermParameterDefinition(this);
240 		}
241 		
242 	}
243 	
244 	
245 	/**
246 	 * @return the id
247 	 */
248 	@Override
249 	public String getId() {
250 		return this.id;
251 	}
252 	/**
253 	 * @return the termId
254 	 */
255 	@Override
256 	public String getTermId() {
257 		return termId;
258 	}
259 	/**
260 	 * @return the name
261 	 */
262 	@Override
263 	public String getName() {
264 		return this.name;
265 	}
266 	/**
267 	 * @return the value
268 	 */
269 	@Override
270 	public String getValue() {
271 		return this.value;
272 	}
273 	
274 	/**
275 	 * @see org.kuali.rice.core.api.mo.common.Versioned#getVersionNumber()
276 	 */
277     @Override
278     public Long getVersionNumber() {
279         return versionNumber;
280     }
281 	
282 	public static class Constants {
283 		public static final String ROOT_ELEMENT_NAME = "TermParameterDefinition";
284 		public static final String TYPE_NAME = "TermParameterDefinitionType";
285 	}
286 	
287 	public static class Elements {
288 		public static final String ID = "id";
289 		public static final String TERM_ID = "termId";
290 		public static final String NAME = "name";
291 		public static final String VALUE = "value";
292 	}
293 }