Coverage Report - org.kuali.rice.kew.routemodule.InlineRequestsRouteModule
 
Classes in this File Line Coverage Branch Coverage Complexity
InlineRequestsRouteModule
0%
0/76
0%
0/40
6.2
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.routemodule;
 17  
 
 18  
 import org.apache.log4j.Logger;
 19  
 import org.jdom.Document;
 20  
 import org.jdom.Element;
 21  
 import org.kuali.rice.core.api.impex.xml.XmlConstants;
 22  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 23  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 24  
 import org.kuali.rice.core.api.util.xml.XmlHelper;
 25  
 import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
 26  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 27  
 import org.kuali.rice.kew.engine.RouteContext;
 28  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 29  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 30  
 import org.kuali.rice.kew.rule.RuleExtensionBo;
 31  
 import org.kuali.rice.kew.rule.RuleResponsibilityBo;
 32  
 import org.kuali.rice.kew.rule.WorkflowRuleAttribute;
 33  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 34  
 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
 35  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 36  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 37  
 import org.kuali.rice.kew.api.KewApiConstants;
 38  
 import org.kuali.rice.kew.xml.RuleXmlParser;
 39  
 
 40  
 import javax.xml.xpath.XPath;
 41  
 import javax.xml.xpath.XPathConstants;
 42  
 import java.io.ByteArrayInputStream;
 43  
 import java.util.ArrayList;
 44  
 import java.util.List;
 45  
 
 46  
 
 47  
 /**
 48  
  * A RouteModule that generates requests for responsibilities statically defined
 49  
  * in the config block of the node.
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  0
 public class InlineRequestsRouteModule extends FlexRMAdapter {
 53  0
     private static final Logger LOG = Logger.getLogger(InlineRequestsRouteModule.class);
 54  
 
 55  
     /**
 56  
      * This overridden method is used to decipher the inline xpath and responsibilities of a route node definition and use
 57  
      * them to create action reqeusts
 58  
      * 
 59  
      * @see org.kuali.rice.kew.routemodule.FlexRMAdapter#findActionRequests(org.kuali.rice.kew.engine.RouteContext)
 60  
      */
 61  
     @Override
 62  
     public List<ActionRequestValue> findActionRequests(RouteContext context) throws Exception {
 63  
         // comment this out while implementing the meta-rules stuff
 64  
         // re-implement later
 65  0
         List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
 66  0
         RouteNodeInstance currentNode = context.getNodeInstance();
 67  0
         String contentFragment = currentNode.getRouteNode().getContentFragment();
 68  
         // parse with JDOM to reuse RuleXmlParser
 69  0
         Document doc = XmlHelper.trimSAXXml(new ByteArrayInputStream(contentFragment.getBytes()));
 70  0
         Element root = doc.getRootElement();
 71  0
         List<String> ruleAttributeNames = new ArrayList<String>();
 72  0
         List<String> ruleAttributeClassNames = new ArrayList<String>();
 73  0
         List<String> xpathExpressions = new ArrayList<String>();
 74  
         // get the list of ruleAttributes to use
 75  0
         Element ruleAttributes = root.getChild("ruleAttributes");
 76  0
         if (ruleAttributes != null) {
 77  0
             for (Object o : ruleAttributes.getChildren("name")) {
 78  0
                 Element e = (Element) o;
 79  0
                 ruleAttributeNames.add(e.getText());
 80  0
             }
 81  0
             for (Object o : ruleAttributes.getChildren("className")) {
 82  0
                 Element e = (Element) o;
 83  0
                 ruleAttributeClassNames.add(e.getText());
 84  0
             }
 85  
         }
 86  
         // get the list of xpath expressions to verify
 87  0
         for (Object o: root.getChildren("match")) {
 88  0
             Element e = (Element) o;
 89  0
             xpathExpressions.add(e.getText());
 90  0
         }
 91  0
         if ( (ruleAttributeNames.isEmpty()) && (ruleAttributeClassNames.isEmpty()) && (xpathExpressions.isEmpty()) ) {
 92  0
             throw new RuntimeException("Match xpath expression not specified (should be parse-time exception...)");
 93  
         }
 94  
 
 95  0
         List<WorkflowRuleAttribute> attributes = new ArrayList<WorkflowRuleAttribute>();
 96  0
         for (String attributeName : ruleAttributeNames) {
 97  0
             attributes.add(getRuleAttributeByName(attributeName));
 98  
         }
 99  0
         for (String attributeClassName : ruleAttributeClassNames) {
 100  0
             attributes.add(getRuleAttributeByClassName(attributeClassName));
 101  
         }
 102  
         
 103  
         // at this point if we have no xpath expressions or attributes we cannot match
 104  0
         if (attributes.isEmpty() && xpathExpressions.isEmpty()) {
 105  0
             return actionRequests;
 106  
         }
 107  
         
 108  0
         Boolean match = Boolean.TRUE;
 109  0
         if (!xpathExpressions.isEmpty()) {
 110  0
             XPath xpath = XPathHelper.newXPath();
 111  0
             for (String xpathExpression : xpathExpressions) {
 112  0
                 match &= (Boolean) xpath.evaluate(xpathExpression, context.getDocumentContent().getDocument(), XPathConstants.BOOLEAN);
 113  
             }
 114  
         }
 115  0
         for (WorkflowRuleAttribute workflowAttribute : attributes) {
 116  
             // no rule extensions to pass in below because we have no rule... simple attribute matching only
 117  0
             match &= workflowAttribute.isMatch(context.getDocumentContent(), new ArrayList<RuleExtensionBo>());
 118  
         }
 119  
         
 120  0
         if (match.booleanValue()) {
 121  
 //            LOG.debug("Expression '" + xpathExpression + "' matched document '" + context.getDocumentContent().getDocContent() + "'");
 122  
         } else {
 123  
             // return an empty list because we didn't find a match using the given xpath
 124  
 //            LOG.debug("Expression '" + xpathExpression + "' did NOT match document '" + context.getDocumentContent().getDocContent() + "'");
 125  0
             return actionRequests;
 126  
         }
 127  
 
 128  0
         List<org.kuali.rice.kew.api.rule.RuleResponsibility> responsibilities = new ArrayList<org.kuali.rice.kew.api.rule.RuleResponsibility>();
 129  0
         RuleXmlParser parser = new RuleXmlParser();
 130  0
         ActionRequestFactory arf = new ActionRequestFactory(context.getDocument(), currentNode);
 131  
         // this rule is only used to obtain description, forceAction flag, and the rulebasevalues id, which may be null
 132  0
         RuleBaseValues fakeRule = new RuleBaseValues();
 133  0
         fakeRule.setName("fakeRule");
 134  0
         fakeRule.setActive(Boolean.TRUE);
 135  0
         fakeRule.setCurrentInd(Boolean.TRUE);
 136  0
         fakeRule.setDescription("a fake rule");
 137  0
         fakeRule.setForceAction(Boolean.TRUE);
 138  0
         fakeRule.setId(null);
 139  
 
 140  0
         for (Object o: root.getChildren("responsibility", XmlConstants.RULE_NAMESPACE)) {
 141  0
             Element e = (Element) o;
 142  0
             RuleResponsibilityBo responsibility = parser.parseResponsibility(e, fakeRule);
 143  0
             responsibility.setResponsibilityId(KewApiConstants.MACHINE_GENERATED_RESPONSIBILITY_ID);
 144  0
             responsibilities.add(org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create(responsibility).build());
 145  0
         }
 146  0
         if (responsibilities.isEmpty()) {
 147  0
             throw new RuntimeException("No responsibilities found on node " + currentNode.getName());
 148  
         }
 149  
 
 150  0
         makeActionRequests(arf, responsibilities, context, RuleBaseValues.to(fakeRule), context.getDocument(), null, null);
 151  0
         actionRequests.addAll(arf.getRequestGraphs());
 152  0
         return actionRequests;
 153  
     }
 154  
     
 155  
     @Override
 156  
     public String toString() {
 157  0
         return "InlineRequestsRouteModule";
 158  
     }
 159  
 
 160  
     private WorkflowRuleAttribute getRuleAttributeByName(String ruleAttributeName) {
 161  0
         return materializeRuleAttribute(KEWServiceLocator.getRuleAttributeService().findByName(ruleAttributeName));
 162  
     }
 163  
     
 164  
     private WorkflowRuleAttribute getRuleAttributeByClassName(String ruleAttributeClassName) {
 165  0
         return materializeRuleAttribute(KEWServiceLocator.getRuleAttributeService().findByClassName(ruleAttributeClassName));
 166  
     }
 167  
     
 168  
     private WorkflowRuleAttribute materializeRuleAttribute(RuleAttribute ruleAttribute) {
 169  0
         if (ruleAttribute != null) {
 170  0
             if (KewApiConstants.RULE_ATTRIBUTE_TYPE.equals(ruleAttribute.getType())) {
 171  0
                 ObjectDefinition objDef = new ObjectDefinition(ruleAttribute.getResourceDescriptor(), ruleAttribute.getApplicationId());
 172  0
                 return (WorkflowRuleAttribute) GlobalResourceLoader.getObject(objDef);
 173  0
             } else if (KewApiConstants.RULE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType())) {
 174  0
                 ObjectDefinition objDef = new ObjectDefinition(ruleAttribute.getResourceDescriptor(), ruleAttribute.getApplicationId());
 175  0
                 WorkflowRuleAttribute workflowAttribute = (WorkflowRuleAttribute) GlobalResourceLoader.getObject(objDef);
 176  
                 //required to make it work because ruleAttribute XML is required to construct custom columns
 177  0
                 ((GenericXMLRuleAttribute) workflowAttribute).setExtensionDefinition(RuleAttribute.to(ruleAttribute));
 178  0
                 return workflowAttribute;
 179  
             }
 180  
         }
 181  0
         return null;
 182  
     }
 183  
     
 184  
 }