1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.xml; |
17 | |
|
18 | |
import org.jdom.Element; |
19 | |
import org.jdom.Namespace; |
20 | |
import org.kuali.rice.core.api.util.xml.XmlException; |
21 | |
import org.kuali.rice.core.api.util.xml.XmlHelper; |
22 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
23 | |
import org.kuali.rice.kew.rule.RuleExtensionBo; |
24 | |
import org.kuali.rice.kew.rule.RuleExtensionValue; |
25 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
26 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateBo; |
27 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo; |
28 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
29 | |
|
30 | |
import java.util.ArrayList; |
31 | |
import java.util.Collection; |
32 | |
import java.util.Iterator; |
33 | |
import java.util.List; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class RuleExtensionXmlParser { |
45 | |
|
46 | 0 | private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/Rule"); |
47 | |
private static final String RULE_EXTENSION = "ruleExtension"; |
48 | |
private static final String ATTRIBUTE = "attribute"; |
49 | |
private static final String RULE_TEMPLATE = "ruleTemplate"; |
50 | |
private static final String RULE_EXTENSION_VALUES = "ruleExtensionValues"; |
51 | |
private static final String RULE_EXTENSION_VALUE = "ruleExtensionValue"; |
52 | |
private static final String KEY = "key"; |
53 | |
private static final String VALUE = "value"; |
54 | |
|
55 | |
public List parseRuleExtensions(Element element, RuleBaseValues rule) throws XmlException { |
56 | 0 | List ruleExtensions = new ArrayList(); |
57 | 0 | Collection<Element> ruleElements = XmlHelper.findElements(element, RULE_EXTENSION); |
58 | 0 | for (Iterator iterator = ruleElements.iterator(); iterator.hasNext();) { |
59 | 0 | ruleExtensions.add(parseRuleExtension((Element) iterator.next(), rule)); |
60 | |
} |
61 | 0 | return ruleExtensions; |
62 | |
} |
63 | |
|
64 | |
private RuleExtensionBo parseRuleExtension(Element element, RuleBaseValues rule) throws XmlException { |
65 | 0 | String attributeName = element.getChildText(ATTRIBUTE, NAMESPACE); |
66 | 0 | String templateName = element.getChildText(RULE_TEMPLATE, NAMESPACE); |
67 | 0 | Element valuesElement = element.getChild(RULE_EXTENSION_VALUES, NAMESPACE); |
68 | 0 | if (attributeName == null) { |
69 | 0 | throw new XmlException("Rule extension must have a valid attribute."); |
70 | |
} |
71 | 0 | if (templateName == null) { |
72 | 0 | throw new XmlException("Rule extension must have a valid rule template."); |
73 | |
} |
74 | 0 | RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(attributeName); |
75 | 0 | if (ruleAttribute == null) { |
76 | 0 | throw new XmlException("Could not locate attribute for the given name '" + attributeName + "'"); |
77 | |
} |
78 | 0 | RuleTemplateBo ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(templateName); |
79 | 0 | if (ruleTemplate == null) { |
80 | 0 | throw new XmlException("Could not locate rule template for the given name '" + templateName + "'"); |
81 | |
} |
82 | 0 | RuleExtensionBo extension = new RuleExtensionBo(); |
83 | 0 | extension.setRuleBaseValues(rule); |
84 | 0 | boolean attributeFound = false; |
85 | 0 | for (Iterator iter = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iter.hasNext();) { |
86 | 0 | RuleTemplateAttributeBo templateAttribute = (RuleTemplateAttributeBo) iter.next(); |
87 | 0 | if (templateAttribute.getRuleAttributeId().equals(ruleAttribute.getId())) { |
88 | 0 | extension.setRuleTemplateAttribute(templateAttribute); |
89 | 0 | extension.setRuleTemplateAttributeId(templateAttribute.getId()); |
90 | 0 | attributeFound = true; |
91 | 0 | break; |
92 | |
} |
93 | 0 | } |
94 | |
|
95 | 0 | if (!attributeFound) { |
96 | |
|
97 | 0 | throw new XmlException("Attribute '" + attributeName + "' not found on template '" + ruleTemplate.getName() + "'"); |
98 | |
} |
99 | |
|
100 | 0 | extension.setExtensionValues(parseRuleExtensionValues(valuesElement, extension)); |
101 | 0 | return extension; |
102 | |
} |
103 | |
|
104 | |
private List parseRuleExtensionValues(Element element, RuleExtensionBo ruleExtension) throws XmlException { |
105 | 0 | List values = new ArrayList(); |
106 | 0 | if (element == null) { |
107 | 0 | return values; |
108 | |
} |
109 | 0 | Collection<Element> valueElements = XmlHelper.findElements(element, RULE_EXTENSION_VALUE); |
110 | 0 | for (Iterator iterator = valueElements.iterator(); iterator.hasNext();) { |
111 | 0 | Element valueElement = (Element) iterator.next(); |
112 | 0 | values.add(parseRuleExtensionValue(valueElement, ruleExtension)); |
113 | 0 | } |
114 | 0 | return values; |
115 | |
} |
116 | |
|
117 | |
private RuleExtensionValue parseRuleExtensionValue(Element element, RuleExtensionBo ruleExtension) throws XmlException { |
118 | 0 | String key = element.getChildText(KEY, NAMESPACE); |
119 | 0 | String value = element.getChildText(VALUE, NAMESPACE); |
120 | 0 | if (org.apache.commons.lang.StringUtils.isEmpty(key)) { |
121 | 0 | throw new XmlException("RuleExtensionValue must have a non-empty key."); |
122 | |
} |
123 | 0 | if (value == null) { |
124 | 0 | throw new XmlException("RuleExtensionValue must have a non-null value."); |
125 | |
} |
126 | 0 | RuleExtensionValue extensionValue = new RuleExtensionValue(key, value); |
127 | 0 | extensionValue.setExtension(ruleExtension); |
128 | 0 | return extensionValue; |
129 | |
} |
130 | |
|
131 | |
} |