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 public class RuleExtensionXmlParser {
45
46 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 List ruleExtensions = new ArrayList();
57 Collection<Element> ruleElements = XmlHelper.findElements(element, RULE_EXTENSION);
58 for (Iterator iterator = ruleElements.iterator(); iterator.hasNext();) {
59 ruleExtensions.add(parseRuleExtension((Element) iterator.next(), rule));
60 }
61 return ruleExtensions;
62 }
63
64 private RuleExtensionBo parseRuleExtension(Element element, RuleBaseValues rule) throws XmlException {
65 String attributeName = element.getChildText(ATTRIBUTE, NAMESPACE);
66 String templateName = element.getChildText(RULE_TEMPLATE, NAMESPACE);
67 Element valuesElement = element.getChild(RULE_EXTENSION_VALUES, NAMESPACE);
68 if (attributeName == null) {
69 throw new XmlException("Rule extension must have a valid attribute.");
70 }
71 if (templateName == null) {
72 throw new XmlException("Rule extension must have a valid rule template.");
73 }
74 RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(attributeName);
75 if (ruleAttribute == null) {
76 throw new XmlException("Could not locate attribute for the given name '" + attributeName + "'");
77 }
78 RuleTemplateBo ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(templateName);
79 if (ruleTemplate == null) {
80 throw new XmlException("Could not locate rule template for the given name '" + templateName + "'");
81 }
82 RuleExtensionBo extension = new RuleExtensionBo();
83 extension.setRuleBaseValues(rule);
84 boolean attributeFound = false;
85 for (Iterator iter = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iter.hasNext();) {
86 RuleTemplateAttributeBo templateAttribute = (RuleTemplateAttributeBo) iter.next();
87 if (templateAttribute.getRuleAttributeId().equals(ruleAttribute.getId())) {
88 extension.setRuleTemplateAttribute(templateAttribute);
89 extension.setRuleTemplateAttributeId(templateAttribute.getId());
90 attributeFound = true;
91 break;
92 }
93 }
94
95 if (!attributeFound) {
96
97 throw new XmlException("Attribute '" + attributeName + "' not found on template '" + ruleTemplate.getName() + "'");
98 }
99
100 extension.setExtensionValues(parseRuleExtensionValues(valuesElement, extension));
101 return extension;
102 }
103
104 private List parseRuleExtensionValues(Element element, RuleExtensionBo ruleExtension) throws XmlException {
105 List values = new ArrayList();
106 if (element == null) {
107 return values;
108 }
109 Collection<Element> valueElements = XmlHelper.findElements(element, RULE_EXTENSION_VALUE);
110 for (Iterator iterator = valueElements.iterator(); iterator.hasNext();) {
111 Element valueElement = (Element) iterator.next();
112 values.add(parseRuleExtensionValue(valueElement, ruleExtension));
113 }
114 return values;
115 }
116
117 private RuleExtensionValue parseRuleExtensionValue(Element element, RuleExtensionBo ruleExtension) throws XmlException {
118 String key = element.getChildText(KEY, NAMESPACE);
119 String value = element.getChildText(VALUE, NAMESPACE);
120 if (org.apache.commons.lang.StringUtils.isEmpty(key)) {
121 throw new XmlException("RuleExtensionValue must have a non-empty key.");
122 }
123 if (value == null) {
124 throw new XmlException("RuleExtensionValue must have a non-null value.");
125 }
126 RuleExtensionValue extensionValue = new RuleExtensionValue(key, value);
127 extensionValue.setExtension(ruleExtension);
128 return extensionValue;
129 }
130
131 }