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