Coverage Report - org.kuali.rice.kew.xml.RuleExtensionXmlParser
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleExtensionXmlParser
0%
0/53
0%
0/24
6
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.xml;
 18  
 
 19  
 import org.jdom.Element;
 20  
 import org.jdom.Namespace;
 21  
 import org.kuali.rice.core.util.xml.XmlException;
 22  
 import org.kuali.rice.core.util.xml.XmlHelper;
 23  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 24  
 import org.kuali.rice.kew.rule.RuleExtension;
 25  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 26  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 27  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 28  
 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
 29  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 30  
 
 31  
 import java.util.ArrayList;
 32  
 import java.util.Collection;
 33  
 import java.util.Iterator;
 34  
 import java.util.List;
 35  
 
 36  
 
 37  
 /**
 38  
  * Parses {@link RuleExtension}s from XML.
 39  
  *
 40  
  * @see RuleExtension
 41  
  * @see RuleExtensionValue
 42  
  *
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 44  
  */
 45  0
 public class RuleExtensionXmlParser {
 46  
 
 47  0
     private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/Rule");
 48  
     private static final String RULE_EXTENSION = "ruleExtension";
 49  
     private static final String ATTRIBUTE = "attribute";
 50  
     private static final String RULE_TEMPLATE = "ruleTemplate";
 51  
     private static final String RULE_EXTENSION_VALUES = "ruleExtensionValues";
 52  
     private static final String RULE_EXTENSION_VALUE = "ruleExtensionValue";
 53  
     private static final String KEY = "key";
 54  
     private static final String VALUE = "value";
 55  
 
 56  
     public List parseRuleExtensions(Element element, RuleBaseValues rule) throws XmlException {
 57  0
         List ruleExtensions = new ArrayList();
 58  0
         Collection<Element> ruleElements = XmlHelper.findElements(element, RULE_EXTENSION);
 59  0
         for (Iterator iterator = ruleElements.iterator(); iterator.hasNext();) {
 60  0
             ruleExtensions.add(parseRuleExtension((Element) iterator.next(), rule));
 61  
         }
 62  0
         return ruleExtensions;
 63  
     }
 64  
 
 65  
     private RuleExtension parseRuleExtension(Element element, RuleBaseValues rule) throws XmlException {
 66  0
         String attributeName = element.getChildText(ATTRIBUTE, NAMESPACE);
 67  0
         String templateName = element.getChildText(RULE_TEMPLATE, NAMESPACE);
 68  0
         Element valuesElement = element.getChild(RULE_EXTENSION_VALUES, NAMESPACE);
 69  0
         if (attributeName == null) {
 70  0
             throw new XmlException("Rule extension must have a valid attribute.");
 71  
         }
 72  0
         if (templateName == null) {
 73  0
             throw new XmlException("Rule extension must have a valid rule template.");
 74  
         }
 75  0
         RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(attributeName);
 76  0
         if (ruleAttribute == null) {
 77  0
             throw new XmlException("Could not locate attribute for the given name '" + attributeName + "'");
 78  
         }
 79  0
         RuleTemplate ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(templateName);
 80  0
         if (ruleTemplate == null) {
 81  0
             throw new XmlException("Could not locate rule template for the given name '" + templateName + "'");
 82  
         }
 83  0
         RuleExtension extension = new RuleExtension();
 84  0
         extension.setRuleBaseValues(rule);
 85  0
         boolean attributeFound = false;
 86  0
         for (Iterator iter = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iter.hasNext();) {
 87  0
             RuleTemplateAttribute templateAttribute = (RuleTemplateAttribute) iter.next();
 88  0
             if (templateAttribute.getRuleAttributeId().equals(ruleAttribute.getRuleAttributeId())) {
 89  0
                 extension.setRuleTemplateAttribute(templateAttribute);
 90  0
                 extension.setRuleTemplateAttributeId(templateAttribute.getRuleTemplateAttributeId());
 91  0
                 attributeFound = true;
 92  0
                 break;
 93  
             }
 94  0
         }
 95  
 
 96  0
         if (!attributeFound) {
 97  
             // TODO: need test case for this
 98  0
             throw new XmlException("Attribute '" + attributeName + "' not found on template '" + ruleTemplate.getName() + "'");
 99  
         }
 100  
 
 101  0
         extension.setExtensionValues(parseRuleExtensionValues(valuesElement, extension));
 102  0
         return extension;
 103  
     }
 104  
 
 105  
     private List parseRuleExtensionValues(Element element, RuleExtension ruleExtension) throws XmlException {
 106  0
         List values = new ArrayList();
 107  0
         if (element == null) {
 108  0
             return values;
 109  
         }
 110  0
         Collection<Element> valueElements = XmlHelper.findElements(element, RULE_EXTENSION_VALUE);
 111  0
         for (Iterator iterator = valueElements.iterator(); iterator.hasNext();) {
 112  0
             Element valueElement = (Element) iterator.next();
 113  0
             values.add(parseRuleExtensionValue(valueElement, ruleExtension));
 114  0
         }
 115  0
         return values;
 116  
     }
 117  
 
 118  
     private RuleExtensionValue parseRuleExtensionValue(Element element, RuleExtension ruleExtension) throws XmlException {
 119  0
         String key = element.getChildText(KEY, NAMESPACE);
 120  0
         String value = element.getChildText(VALUE, NAMESPACE);
 121  0
         if (org.apache.commons.lang.StringUtils.isEmpty(key)) {
 122  0
             throw new XmlException("RuleExtensionValue must have a non-empty key.");
 123  
         }
 124  0
         if (value == null) {
 125  0
             throw new XmlException("RuleExtensionValue must have a non-null value.");
 126  
         }
 127  0
         RuleExtensionValue extensionValue = new RuleExtensionValue(key, value);
 128  0
         extensionValue.setExtension(ruleExtension);
 129  0
         return extensionValue;
 130  
     }
 131  
 
 132  
 }