Coverage Report - org.kuali.rice.kew.xml.RuleAttributeXmlParser
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleAttributeXmlParser
0%
0/83
0%
0/52
13.333
 
 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 static org.kuali.rice.core.api.impex.xml.XmlConstants.APPLICATION_ID;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import javax.xml.parsers.DocumentBuilderFactory;
 28  
 import javax.xml.xpath.XPath;
 29  
 import javax.xml.xpath.XPathConstants;
 30  
 import javax.xml.xpath.XPathExpressionException;
 31  
 
 32  
 import org.apache.commons.lang.StringUtils;
 33  
 import org.kuali.rice.core.api.impex.xml.XmlConstants;
 34  
 import org.kuali.rice.core.util.xml.XmlException;
 35  
 import org.kuali.rice.core.util.xml.XmlJotter;
 36  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 37  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 38  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 39  
 import org.kuali.rice.kew.util.KEWConstants;
 40  
 import org.w3c.dom.Element;
 41  
 import org.w3c.dom.Node;
 42  
 import org.w3c.dom.NodeList;
 43  
 import org.xml.sax.InputSource;
 44  
 
 45  
 
 46  
 /**
 47  
  * Parses {@link RuleAttribute}s from XML.
 48  
  *
 49  
  * @see RuleAttribute
 50  
  *
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 52  
  */
 53  0
 public class RuleAttributeXmlParser {
 54  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleAttributeXmlParser.class);
 55  
 
 56  
     
 57  
     private static final String XPATH_RULE_ATTRIBUTES = "//" + XmlConstants.RULE_ATTRIBUTES + "/" + XmlConstants.RULE_ATTRIBUTE;
 58  
         private static final String NAME = "name";
 59  
         private static final String CLASS_NAME = "className";
 60  
         private static final String LABEL = "label";
 61  
         private static final String DESCRIPTION = "description";
 62  
         private static final String TYPE = "type";
 63  
         private static final String CONFIG = "configuration";
 64  
         
 65  
         public List parseRuleAttributes(InputStream input) throws IOException, XmlException {
 66  
                 try {
 67  0
                         Element root = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(input)).getDocumentElement();
 68  0
                         return parseRuleAttributes(root);
 69  0
                 } catch (Exception e) {
 70  0
                         throw new XmlException("error parsing xml data", e);
 71  
                 }
 72  
         }
 73  
         
 74  
         public List parseRuleAttributes(Element element) throws XmlException {
 75  0
                 List ruleAttributes = new ArrayList();
 76  
                 try {
 77  0
                         XPath xpath = XPathHelper.newXPath();
 78  0
                         NodeList nodeList = (NodeList)xpath.evaluate(XPATH_RULE_ATTRIBUTES, element, XPathConstants.NODESET);
 79  0
                         for (int i = 0; i < nodeList.getLength(); i++) {
 80  0
                                 Node ruleAttributeNode = nodeList.item(i);
 81  0
                                 ruleAttributes.add(parseRuleAttribute(ruleAttributeNode));
 82  
                         }
 83  
                         
 84  0
                         for (Iterator iterator = ruleAttributes.iterator(); iterator.hasNext();) {
 85  0
                                 RuleAttribute ruleAttribute = (RuleAttribute) iterator.next();
 86  
                                 try {
 87  0
                     RuleAttribute existingAttribute = KEWServiceLocator.getRuleAttributeService().findByName(ruleAttribute.getName());
 88  0
                     if (existingAttribute != null) {
 89  0
                         ruleAttribute.setRuleAttributeId(existingAttribute.getRuleAttributeId());
 90  0
                         ruleAttribute.setVersionNumber(existingAttribute.getVersionNumber());
 91  
                     }
 92  0
                                     KEWServiceLocator.getRuleAttributeService().save(ruleAttribute);
 93  0
                                 } catch (Exception e) {
 94  0
                         LOG.error("Error saving rule attribute entered by XML", e);
 95  0
                                 }
 96  0
                         }
 97  0
                 } catch (XPathExpressionException e1) {
 98  0
                         throw new XmlException("Could not find a rule attribute.", e1);
 99  0
                 }
 100  0
                 return ruleAttributes;
 101  
         }
 102  
         
 103  
         private RuleAttribute parseRuleAttribute(Node ruleAttributeNode) throws XmlException {
 104  0
                 String name = "";
 105  0
                 String className = "";
 106  0
                 String label = "";
 107  0
                 String description = "";
 108  0
                 String type = "";
 109  0
                 String applicationId = null;
 110  0
                 Node xmlConfig = null;
 111  0
                 for (int i = 0; i < ruleAttributeNode.getChildNodes().getLength(); i++) {
 112  0
                         Node childNode = ruleAttributeNode.getChildNodes().item(i);
 113  0
                         if(NAME.equals(childNode.getNodeName())){
 114  0
                                 name = childNode.getFirstChild().getNodeValue();
 115  0
                         } else if(CLASS_NAME.equals(childNode.getNodeName())){
 116  0
                                 className = childNode.getFirstChild().getNodeValue();
 117  0
                         } else if(LABEL.equals(childNode.getNodeName())){
 118  0
                                 label = childNode.getFirstChild().getNodeValue();
 119  0
                         } else if(DESCRIPTION.equals(childNode.getNodeName())){
 120  0
                                 description = childNode.getFirstChild().getNodeValue();
 121  0
                         } else if(TYPE.equals(childNode.getNodeName())){
 122  0
                                 type = childNode.getFirstChild().getNodeValue();
 123  0
                         } else if(XmlConstants.ROUTING_CONFIG.equals(childNode.getNodeName()) || XmlConstants.SEARCHING_CONFIG.equals(childNode.getNodeName()) || 
 124  
                                         XmlConstants.SEARCH_RESULT_CONFIG.equals(childNode.getNodeName()) || XmlConstants.RESOLVER_CONFIG.equals(childNode.getNodeName()) ||
 125  
                                         CONFIG.equals(childNode.getNodeName())){
 126  0
                                 xmlConfig = childNode;
 127  0
                         } else if (XmlConstants.SERVICE_NAMESPACE.equals(childNode.getNodeName())) {
 128  0
                                 applicationId = childNode.getFirstChild().getNodeValue();
 129  0
                                 LOG.warn(XmlConstants.SERVICE_NAMESPACE + " element was set on rule attribute type XML but is deprecated and will be removed in a future version, please use " + APPLICATION_ID + " instead.");
 130  0
                         } else if (XmlConstants.APPLICATION_ID.equals(childNode.getNodeName())) {
 131  0
                                 applicationId = childNode.getFirstChild().getNodeValue();
 132  
                         }
 133  
                 }
 134  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(name)) {
 135  0
                         throw new XmlException("RuleAttribute must have a name");
 136  
                 }
 137  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(className)) {
 138  0
                         throw new XmlException("RuleAttribute must have a className");
 139  
                 }
 140  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(label)) {
 141  0
                         LOG.warn("Label empty defaulting to name");
 142  0
                         label = name;
 143  
                 }
 144  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(type)) {
 145  0
                         LOG.debug("No type specified, default to " + KEWConstants.RULE_ATTRIBUTE_TYPE);
 146  0
                         type = KEWConstants.RULE_ATTRIBUTE_TYPE;
 147  
                         //throw new XmlException("RuleAttribute must have an attribute type");
 148  
                 }
 149  0
                 RuleAttribute ruleAttribute = new RuleAttribute();
 150  0
                 ruleAttribute.setName(name.trim());
 151  0
                 ruleAttribute.setClassName(className.trim());
 152  0
                 ruleAttribute.setType(type.trim());
 153  0
                 ruleAttribute.setLabel(label.trim());
 154  
 //                 default description to label
 155  0
         if (StringUtils.isEmpty(description)) {
 156  0
             description = label;
 157  
         }
 158  0
                 ruleAttribute.setDescription(description.trim());
 159  0
                 if (applicationId != null)
 160  
                 {
 161  0
                         applicationId = applicationId.trim();
 162  
                 }
 163  0
                 ruleAttribute.setApplicationId(applicationId);
 164  
                 
 165  0
                 if(xmlConfig != null){
 166  0
                     ruleAttribute.setXmlConfigData(XmlJotter.jotNode(xmlConfig));
 167  
                 } else {
 168  0
                         if(KEWConstants.RULE_XML_ATTRIBUTE_TYPE.equals(type)){
 169  0
                                 throw new XmlException("A routing config must be present to be of type: "+type);
 170  0
                         } else if(KEWConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE.equals(type)){
 171  0
                                 throw new XmlException("A searching config must be present to be of type: "+type);
 172  0
                         } else if(KEWConstants.SEARCH_RESULT_XML_PROCESSOR_ATTRIBUTE_TYPE.equals(type)){
 173  0
                                 throw new XmlException("A searching config must be present to be of type: "+type);
 174  
                         }
 175  
                 }
 176  0
                 return ruleAttribute;
 177  
         }        
 178  
 }