1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
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
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
116
117
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
133
134 @Override
135 public String getId() {
136 return id;
137 }
138
139
140
141
142 @Override
143 public String getNamespaceCode() {
144 return namespaceCode;
145 }
146
147
148
149
150 @Override
151 public String getName() {
152 return name;
153 }
154
155
156
157
158 @Override
159 public String getDescription() {
160 return description;
161 }
162
163
164
165
166 @Override
167 public Template getTemplate() {
168 return template;
169 }
170
171
172
173
174 @Override
175 public boolean isActive() {
176 return active;
177 }
178
179
180
181
182
183 @Override
184 public Map<String, String> getAttributes() {
185 return this.attributes;
186 }
187
188
189
190
191 @Override
192 public Long getVersionNumber() {
193 return versionNumber;
194 }
195
196
197
198
199 @Override
200 public String getObjectId() {
201 return objectId;
202 }
203
204
205
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
225
226 public static Builder create(String namespaceCode, String name) {
227 return new Builder(namespaceCode, name);
228 }
229
230
231
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 && contract.getTemplate().getName() != null
245 && contract.getTemplate().getNamespaceCode() != null) {
246 builder.setTemplate(Template.Builder.create(contract.getTemplate()));
247 }
248
249 return builder;
250 }
251
252 @Override
253 public String getId() {
254 return id;
255 }
256
257 public void setId(final String id) {
258 this.id = id;
259 }
260
261 @Override
262 public String getNamespaceCode() {
263 return namespaceCode;
264 }
265
266 public void setNamespaceCode(final String namespaceCode) {
267 if (StringUtils.isBlank(namespaceCode)) {
268 throw new IllegalArgumentException("namespaceCode is blank");
269 }
270 this.namespaceCode = namespaceCode;
271 }
272
273 @Override
274 public String getName() {
275 return name;
276 }
277
278 public void setName(final String name) {
279 if (StringUtils.isBlank(name)) {
280 throw new IllegalArgumentException("name is blank");
281 }
282 this.name = name;
283 }
284
285 @Override
286 public String getDescription() {
287 return description;
288 }
289
290 public void setDescription(final String description) {
291 this.description = description;
292 }
293
294 @Override
295 public Template.Builder getTemplate() {
296 return template;
297 }
298
299 public void setTemplate(final Template.Builder template) {
300 if (template == null) {
301 throw new IllegalArgumentException("template is null");
302 }
303 if (StringUtils.isNotBlank(template.getName())
304 && StringUtils.isNotBlank(template.getNamespaceCode())) {
305 this.template = template;
306 } else {
307 this.template = null;
308 }
309 }
310
311 @Override
312 public boolean isActive() {
313 return active;
314 }
315
316 public void setActive(final boolean active) {
317 this.active = active;
318 }
319
320 @Override
321 public Long getVersionNumber() {
322 return versionNumber;
323 }
324
325 public void setVersionNumber(final Long versionNumber) {
326
327 if (versionNumber != null && versionNumber <= 0) {
328 throw new IllegalArgumentException("versionNumber is invalid");
329 }
330 this.versionNumber = versionNumber;
331 }
332
333 @Override
334 public String getObjectId() {
335 return objectId;
336 }
337
338 public void setObjectId(final String objectId) {
339 this.objectId = objectId;
340 }
341
342 @Override
343 public Map<String, String> getAttributes() {
344 return attributes;
345 }
346
347 public void setAttributes(Map<String, String> attributes) {
348 this.attributes = Collections.unmodifiableMap(Maps.newHashMap(attributes));
349 }
350
351 @Override
352 public Permission build() {
353 return new Permission(this);
354 }
355 }
356
357
358
359
360 static class Constants {
361 static final String ROOT_ELEMENT_NAME = "permission";
362 static final String TYPE_NAME = "PermissionType";
363 }
364
365
366
367
368
369 static class Elements {
370 static final String ID = "id";
371 static final String NAMESPACE_CODE = "namespaceCode";
372 static final String NAME = "name";
373 static final String DESCRIPTION = "description";
374 static final String TEMPLATE = "template";
375 static final String ATTRIBUTES = "attributes";
376 static final String ACTIVE = "active";
377 }
378
379 public static class Cache {
380 public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + Permission.Constants.TYPE_NAME;
381 }
382 }