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