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