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