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