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