View Javadoc
1   /**
2    * Copyright 2005-2016 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.kim.api.common.template;
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.kim.api.KimConstants;
23  import org.w3c.dom.Element;
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.XmlRootElement;
30  import javax.xml.bind.annotation.XmlType;
31  import java.io.Serializable;
32  import java.util.Collection;
33  
34  /**
35   * An immutable representation of a {@link TemplateContract}.
36   *
37   * <p>To construct an instance of a Template, use the {@link Template.Builder} class.<p/>
38   */
39  @XmlRootElement(name = Template.Constants.ROOT_ELEMENT_NAME)
40  @XmlAccessorType(XmlAccessType.NONE)
41  @XmlType(name = Template.Constants.TYPE_NAME, propOrder = {
42  		Template.Elements.ID,
43  		Template.Elements.NAMESPACE_CODE,
44  		Template.Elements.NAME,
45  		Template.Elements.DESCRIPTION,
46  		Template.Elements.KIM_TYPE_ID,
47          Template.Elements.ACTIVE,
48          CoreConstants.CommonElements.VERSION_NUMBER,
49          CoreConstants.CommonElements.OBJECT_ID,
50          CoreConstants.CommonElements.FUTURE_ELEMENTS
51  })
52  public final class Template extends AbstractDataTransferObject implements TemplateContract {
53  
54  	private static final long serialVersionUID = 1L;
55  	
56      @XmlElement(name = Template.Elements.ID, required = false)
57      private final String id;
58  
59      @XmlElement(name = Template.Elements.NAMESPACE_CODE, required = true)
60      private final String namespaceCode;
61  
62      @XmlElement(name = Template.Elements.NAME, required = true)
63      private final String name;
64  
65      @XmlElement(name = Template.Elements.DESCRIPTION, required = false)
66      private final String description;
67  
68      @XmlElement(name = Template.Elements.KIM_TYPE_ID, required = true)
69      private final String kimTypeId;
70      
71      @XmlElement(name = Template.Elements.ACTIVE, required = false)
72      private final boolean active;
73  
74      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
75      private final Long versionNumber;
76  
77      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
78      private final String objectId;
79      
80      @SuppressWarnings("unused")
81      @XmlAnyElement
82      private final Collection<Element> _futureElements = null;
83      
84      /**
85  	 *  A constructor to be used only by JAXB unmarshalling.
86  	 * 
87  	 */
88  	private Template() {
89  		this.id = null;
90          this.namespaceCode = null;
91          this.name = null;
92          this.description = null;
93          this.kimTypeId = null;
94          this.active = false;
95          this.versionNumber = Long.valueOf(1L);
96          this.objectId = null;
97  	}
98  	
99      /**
100 	 * A constructor using the Builder.
101 	 * 
102 	 * @param builder
103 	 */
104 	private Template(Builder builder) {
105 		this.id = builder.getId();
106         this.namespaceCode = builder.getNamespaceCode();
107         this.name = builder.getName();
108         this.description = builder.getDescription();
109         this.kimTypeId = builder.getKimTypeId();
110         
111         this.active = builder.isActive();
112         this.versionNumber = builder.getVersionNumber();
113         this.objectId = builder.getObjectId();
114 	}
115 
116 	/**
117 	 * @see TemplateContract#getId()
118 	 */
119 	@Override
120 	public String getId() {
121 		return id;
122 	}
123 
124 	/**
125 	 * @see TemplateContract#getNamespaceCode()
126 	 */
127 	@Override
128 	public String getNamespaceCode() {
129 		return namespaceCode;
130 	}
131 
132 	/**
133 	 * @see TemplateContract#getName()
134 	 */
135 	@Override
136 	public String getName() {
137 		return name;
138 	}
139 
140 	/**
141 	 * @see TemplateContract#getDescription()
142 	 */
143 	@Override
144 	public String getDescription() {
145 		return description;
146 	}
147 
148 	/**
149 	 * @see TemplateContract#getKimTypeId()
150 	 */
151 	@Override
152 	public String getKimTypeId() {
153 		return kimTypeId;
154 	}
155 	
156 	/**
157 	 * @see TemplateContract#isActive()
158 	 */
159 	@Override
160 	public boolean isActive() {
161 		return active;
162 	}
163 	
164 	/**
165 	 * @see org.kuali.rice.core.api.mo.common.Versioned#getVersionNumber()
166 	 */
167 	@Override
168 	public Long getVersionNumber() {
169 		return versionNumber;
170 	}
171 
172 	/**
173 	 * @see org.kuali.rice.core.api.mo.common.GloballyUnique#getObjectId()
174 	 */
175 	@Override
176 	public String getObjectId() {
177 		return objectId;
178     }
179 	
180     /**
181      * This builder constructs a Template enforcing the constraints of the {@link TemplateContract}.
182      */
183     public static final class Builder implements TemplateContract, ModelBuilder, Serializable {
184         private String id;
185         private String namespaceCode;
186         private String name;
187         private String description;
188         private String kimTypeId;
189         private Long versionNumber = 1L;
190         private String objectId;
191         private boolean active;
192         
193         private Builder(String namespaceCode, String name, String kimTypeId) {
194             setNamespaceCode(namespaceCode);
195             setName(name);
196             setKimTypeId(kimTypeId);
197         }
198 
199         /**
200          * creates a KimPermission with the required fields.
201          */
202         public static Builder create(String namespaceCode, String name, String kimTypeId) {
203             return new Builder(namespaceCode, name, kimTypeId);
204         }
205 
206         /**
207          * creates a KimPermission from an existing {@link TemplateContract}.
208          */
209         public static Builder create(TemplateContract contract) {
210             Builder builder = new Builder(contract.getNamespaceCode(), contract.getName(), contract.getKimTypeId());
211             builder.setId(contract.getId());
212             builder.setDescription(contract.getDescription());
213             
214             builder.setActive(contract.isActive());
215             builder.setVersionNumber(contract.getVersionNumber());
216             builder.setObjectId(contract.getObjectId());
217 
218             return builder;
219         }
220 
221         @Override
222         public String getId() {
223             return id;
224         }
225 
226         public void setId(final String id) {
227         	this.id = id;
228         }
229         
230         @Override
231         public String getNamespaceCode() {
232             return namespaceCode;
233         }
234 
235         public void setNamespaceCode(final String namespaceCode) {
236         	if (StringUtils.isEmpty(namespaceCode)) {
237                 throw new IllegalArgumentException("namespaceCode is blank");
238             }
239         	this.namespaceCode = namespaceCode;
240         }
241 
242         @Override
243         public String getName() {
244             return name;
245         }
246 
247         public void setName(final String name) {
248         	if (StringUtils.isEmpty(name)) {
249                 throw new IllegalArgumentException("name is blank");
250             }
251         	this.name = name;
252         }
253 
254 		@Override
255 		public String getDescription() {
256 			return description;
257 		}
258 		
259 		public void setDescription(final String description) {
260 			this.description = description;
261 		}
262 
263 		@Override
264 		public String getKimTypeId() {
265 			if (StringUtils.isEmpty(kimTypeId)) {
266                 throw new IllegalArgumentException("kimTypeId is blank");
267             }
268 			return kimTypeId;
269 		}
270 		
271 		public void setKimTypeId(final String kimTypeId) {
272 			this.kimTypeId = kimTypeId;
273 		}
274 		
275 		@Override
276 		public boolean isActive() {
277 			return active;
278 		}
279 		
280 		public void setActive(final boolean active) {
281             this.active = active;
282         }
283 
284 		@Override
285 		public Long getVersionNumber() {
286 			return versionNumber;
287 		}
288 
289 		public void setVersionNumber(final Long versionNumber) {
290 			if (versionNumber <= 0) {
291 	            throw new IllegalArgumentException("versionNumber is invalid");
292 	        }
293 			this.versionNumber = versionNumber;
294 	    }
295 		 
296 		@Override
297 		public String getObjectId() {
298 			return objectId;
299 		}
300 
301         public void setObjectId(final String objectId) {
302             this.objectId = objectId;
303         }
304         
305         @Override
306         public Template build() {
307             return new Template(this);
308         }
309     }
310     
311     /**
312      * Defines some internal constants used on this class.
313      */
314     static class Constants {
315         static final String ROOT_ELEMENT_NAME = "template";
316         static final String TYPE_NAME = "TemplateType";
317     }
318 
319     /**
320      * A private class which exposes constants which define the XML element names to use
321      * when this object is marshalled to XML.
322      */
323     static class Elements {
324         static final String ID = "id";
325         static final String NAMESPACE_CODE = "namespaceCode";
326         static final String NAME = "name";
327         static final String DESCRIPTION = "description";
328         static final String KIM_TYPE_ID = "kimTypeId";        
329         static final String ACTIVE = "active";
330     }
331 
332     public static class Cache {
333         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + Template.Constants.TYPE_NAME;
334     }
335 }