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 org.apache.commons.collections.CollectionUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
21 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
22 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
23 | |
import org.kuali.rice.core.api.CoreConstants; |
24 | |
import org.kuali.rice.core.api.mo.ModelBuilder; |
25 | |
import org.kuali.rice.core.api.mo.ModelObjectComplete; |
26 | |
import org.kuali.rice.kim.api.common.attribute.KimAttributeData; |
27 | |
import org.kuali.rice.kim.api.common.attribute.KimAttributeDataContract; |
28 | |
import org.kuali.rice.kim.api.common.template.Template; |
29 | |
import org.w3c.dom.Element; |
30 | |
|
31 | |
import javax.xml.bind.annotation.XmlAccessType; |
32 | |
import javax.xml.bind.annotation.XmlAccessorType; |
33 | |
import javax.xml.bind.annotation.XmlAnyElement; |
34 | |
import javax.xml.bind.annotation.XmlElement; |
35 | |
import javax.xml.bind.annotation.XmlElementWrapper; |
36 | |
import javax.xml.bind.annotation.XmlRootElement; |
37 | |
import javax.xml.bind.annotation.XmlType; |
38 | |
import java.io.Serializable; |
39 | |
import java.util.ArrayList; |
40 | |
import java.util.Collection; |
41 | |
import java.util.Collections; |
42 | |
import java.util.List; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 1 | @XmlRootElement(name = Permission.Constants.ROOT_ELEMENT_NAME) |
52 | |
@XmlAccessorType(XmlAccessType.NONE) |
53 | |
@XmlType(name = Permission.Constants.TYPE_NAME, propOrder = { |
54 | |
Permission.Elements.ID, |
55 | |
Permission.Elements.NAMESPACE_CODE, |
56 | |
Permission.Elements.NAME, |
57 | |
Permission.Elements.DESCRIPTION, |
58 | |
Permission.Elements.TEMPLATE, |
59 | |
Permission.Elements.ACTIVE, |
60 | |
Permission.Elements.ATTRIBUTES, |
61 | |
CoreConstants.CommonElements.VERSION_NUMBER, |
62 | |
CoreConstants.CommonElements.OBJECT_ID, |
63 | |
CoreConstants.CommonElements.FUTURE_ELEMENTS |
64 | |
}) |
65 | 3 | public final class Permission implements PermissionContract, ModelObjectComplete{ |
66 | |
|
67 | |
private static final long serialVersionUID = 1L; |
68 | |
|
69 | |
@XmlElement(name = Permission.Elements.ID, required = true) |
70 | |
private final String id; |
71 | |
|
72 | |
@XmlElement(name = Permission.Elements.NAMESPACE_CODE, required = true) |
73 | |
private final String namespaceCode; |
74 | |
|
75 | |
@XmlElement(name = Permission.Elements.NAME, required = true) |
76 | |
private final String name; |
77 | |
|
78 | |
@XmlElement(name = Permission.Elements.DESCRIPTION, required = false) |
79 | |
private final String description; |
80 | |
|
81 | |
@XmlElement(name = Permission.Elements.TEMPLATE, required = true) |
82 | |
private final Template template; |
83 | |
|
84 | |
@XmlElementWrapper(name = Elements.ATTRIBUTES, required = false) |
85 | |
@XmlElement(name = Elements.ATTRIBUTE, required = false) |
86 | |
private final List<KimAttributeData> attributes; |
87 | |
|
88 | |
@XmlElement(name = Permission.Elements.ACTIVE, required = false) |
89 | |
private boolean active; |
90 | |
|
91 | |
@XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false) |
92 | |
private final Long versionNumber; |
93 | |
|
94 | |
@XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false) |
95 | |
private final String objectId; |
96 | |
|
97 | 5 | @SuppressWarnings("unused") |
98 | |
@XmlAnyElement |
99 | |
private final Collection<Element> _futureElements = null; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | 2 | private Permission() { |
106 | 2 | this.id = null; |
107 | 2 | this.namespaceCode = null; |
108 | 2 | this.name = null; |
109 | 2 | this.description = null; |
110 | 2 | this.template = null; |
111 | 2 | this.attributes = null; |
112 | 2 | this.active = false; |
113 | 2 | this.versionNumber = Long.valueOf(1L); |
114 | 2 | this.objectId = null; |
115 | 2 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | 3 | private Permission(Builder builder) { |
123 | 3 | this.id = builder.getId(); |
124 | 3 | this.namespaceCode = builder.getNamespaceCode(); |
125 | 3 | this.name = builder.getName(); |
126 | 3 | this.description = builder.getDescription(); |
127 | 3 | this.template = builder.getTemplate().build(); |
128 | 3 | final List<KimAttributeData> temp = new ArrayList<KimAttributeData>(); |
129 | 3 | if (!CollectionUtils.isEmpty(builder.getAttributes())) { |
130 | 1 | for (KimAttributeData.Builder attribute : builder.getAttributes()) { |
131 | 1 | temp.add(attribute.build()); |
132 | |
} |
133 | |
} |
134 | 3 | this.attributes = Collections.unmodifiableList(temp); |
135 | 3 | this.active = builder.isActive(); |
136 | 3 | this.versionNumber = builder.getVersionNumber(); |
137 | 3 | this.objectId = builder.getObjectId(); |
138 | 3 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
@Override |
144 | |
public String getId() { |
145 | 1 | return id; |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
@Override |
152 | |
public String getNamespaceCode() { |
153 | 1 | return namespaceCode; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
@Override |
160 | |
public String getName() { |
161 | 1 | return name; |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
@Override |
168 | |
public String getDescription() { |
169 | 1 | return description; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
@Override |
176 | |
public Template getTemplate() { |
177 | 1 | return template; |
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
@Override |
184 | |
public boolean isActive() { |
185 | 1 | return active; |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
@Override |
194 | |
public List<KimAttributeData> getAttributes() { |
195 | 1 | return this.attributes; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
@Override |
202 | |
public Long getVersionNumber() { |
203 | 1 | return versionNumber; |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
@Override |
210 | |
public String getObjectId() { |
211 | 1 | return objectId; |
212 | |
} |
213 | |
|
214 | |
@Override |
215 | |
public int hashCode() { |
216 | 0 | return HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE); |
217 | |
} |
218 | |
|
219 | |
@Override |
220 | |
public boolean equals(Object obj) { |
221 | 2 | return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE); |
222 | |
} |
223 | |
|
224 | |
@Override |
225 | |
public String toString() { |
226 | 0 | return ToStringBuilder.reflectionToString(this); |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | 3 | public static final class Builder implements PermissionContract, ModelBuilder, Serializable { |
233 | |
private String id; |
234 | |
private String namespaceCode; |
235 | |
private String name; |
236 | |
private String description; |
237 | |
private Template.Builder template; |
238 | |
private List<KimAttributeData.Builder> attributes; |
239 | 6 | private Long versionNumber = 1L; |
240 | |
private String objectId; |
241 | |
private boolean active; |
242 | |
|
243 | 6 | private Builder(String id, String namespaceCode, String name, Template.Builder template) { |
244 | 6 | setId(id); |
245 | 6 | setNamespaceCode(namespaceCode); |
246 | 6 | setName(name); |
247 | 6 | setTemplate(template); |
248 | 6 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public static Builder create(String id, String namespaceCode, String name, Template.Builder template) { |
254 | 4 | return new Builder(id, namespaceCode, name, template); |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public static Builder create(PermissionContract contract) { |
261 | 2 | Builder builder = new Builder(contract.getId(), contract.getNamespaceCode(), contract.getName(), Template.Builder.create(contract.getTemplate())); |
262 | 2 | builder.setDescription(contract.getDescription()); |
263 | 2 | List<KimAttributeData.Builder> attributeBuilders = new ArrayList<KimAttributeData.Builder>(); |
264 | 2 | if (!CollectionUtils.isEmpty(contract.getAttributes())) { |
265 | 1 | for (KimAttributeDataContract attributeContract : contract.getAttributes()) { |
266 | 1 | KimAttributeData.Builder attributeBuilder = KimAttributeData.Builder.create(attributeContract); |
267 | 1 | attributeBuilders.add(attributeBuilder); |
268 | 1 | } |
269 | |
} |
270 | 2 | builder.setAttributes(Collections.unmodifiableList(attributeBuilders)); |
271 | 2 | builder.setActive(contract.isActive()); |
272 | 2 | builder.setVersionNumber(contract.getVersionNumber()); |
273 | 2 | builder.setObjectId(contract.getObjectId()); |
274 | |
|
275 | 2 | return builder; |
276 | |
} |
277 | |
|
278 | |
@Override |
279 | |
public String getId() { |
280 | 3 | return id; |
281 | |
} |
282 | |
|
283 | |
public void setId(final String id) { |
284 | 6 | if (StringUtils.isBlank(id)) { |
285 | 0 | throw new IllegalArgumentException("id is blank"); |
286 | |
} |
287 | 6 | this.id = id; |
288 | 6 | } |
289 | |
|
290 | |
@Override |
291 | |
public String getNamespaceCode() { |
292 | 3 | return namespaceCode; |
293 | |
} |
294 | |
|
295 | |
public void setNamespaceCode(final String namespaceCode) { |
296 | 6 | if (StringUtils.isBlank(namespaceCode)) { |
297 | 0 | throw new IllegalArgumentException("namespaceCode is blank"); |
298 | |
} |
299 | 6 | this.namespaceCode = namespaceCode; |
300 | 6 | } |
301 | |
|
302 | |
@Override |
303 | |
public String getName() { |
304 | 3 | return name; |
305 | |
} |
306 | |
|
307 | |
public void setName(final String name) { |
308 | 6 | if (StringUtils.isBlank(name)) { |
309 | 0 | throw new IllegalArgumentException("name is blank"); |
310 | |
} |
311 | 6 | this.name = name; |
312 | 6 | } |
313 | |
|
314 | |
@Override |
315 | |
public String getDescription() { |
316 | 3 | return description; |
317 | |
} |
318 | |
|
319 | |
public void setDescription(final String description) { |
320 | 3 | this.description = description; |
321 | 3 | } |
322 | |
|
323 | |
@Override |
324 | |
public Template.Builder getTemplate() { |
325 | 3 | return template; |
326 | |
} |
327 | |
|
328 | |
public void setTemplate(final Template.Builder template) { |
329 | 6 | if (template == null) { |
330 | 0 | throw new IllegalArgumentException("template is null"); |
331 | |
} |
332 | 6 | this.template = template; |
333 | 6 | } |
334 | |
|
335 | |
@Override |
336 | |
public boolean isActive() { |
337 | 3 | return active; |
338 | |
} |
339 | |
|
340 | |
public void setActive(final boolean active) { |
341 | 2 | this.active = active; |
342 | 2 | } |
343 | |
|
344 | |
@Override |
345 | |
public Long getVersionNumber() { |
346 | 3 | return versionNumber; |
347 | |
} |
348 | |
|
349 | |
public void setVersionNumber(final Long versionNumber) { |
350 | 4 | if (versionNumber == null || versionNumber <= 0) { |
351 | 2 | throw new IllegalArgumentException("versionNumber is invalid"); |
352 | |
} |
353 | 2 | this.versionNumber = versionNumber; |
354 | 2 | } |
355 | |
|
356 | |
@Override |
357 | |
public String getObjectId() { |
358 | 3 | return objectId; |
359 | |
} |
360 | |
|
361 | |
public void setObjectId(final String objectId) { |
362 | 2 | this.objectId = objectId; |
363 | 2 | } |
364 | |
|
365 | |
@Override |
366 | |
public List<KimAttributeData.Builder> getAttributes() { |
367 | 4 | return attributes; |
368 | |
} |
369 | |
|
370 | |
public void setAttributes(List<KimAttributeData.Builder> attributes) { |
371 | 2 | this.attributes = attributes; |
372 | 2 | } |
373 | |
|
374 | |
@Override |
375 | |
public Permission build() { |
376 | 3 | return new Permission(this); |
377 | |
} |
378 | |
} |
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | 0 | static class Constants { |
384 | |
static final String ROOT_ELEMENT_NAME = "permission"; |
385 | |
static final String TYPE_NAME = "PermissionType"; |
386 | 1 | static final String[] HASH_CODE_EQUALS_EXCLUDE = {CoreConstants.CommonElements.FUTURE_ELEMENTS}; |
387 | |
} |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | 0 | static class Elements { |
394 | |
static final String ID = "id"; |
395 | |
static final String NAMESPACE_CODE = "namespaceCode"; |
396 | |
static final String NAME = "name"; |
397 | |
static final String DESCRIPTION = "description"; |
398 | |
static final String TEMPLATE = "template"; |
399 | |
static final String ATTRIBUTES = "attributes"; |
400 | |
static final String ATTRIBUTE = "attribute"; |
401 | |
static final String ACTIVE = "active"; |
402 | |
} |
403 | |
} |