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