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