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