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