| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.routemodule; |
| 18 | |
|
| 19 | |
import java.io.ByteArrayInputStream; |
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.List; |
| 22 | |
|
| 23 | |
import javax.xml.xpath.XPath; |
| 24 | |
import javax.xml.xpath.XPathConstants; |
| 25 | |
|
| 26 | |
import org.apache.commons.lang.StringUtils; |
| 27 | |
import org.apache.log4j.Logger; |
| 28 | |
import org.jdom.Document; |
| 29 | |
import org.jdom.Element; |
| 30 | |
import org.kuali.rice.core.reflect.ObjectDefinition; |
| 31 | |
import org.kuali.rice.core.resourceloader.GlobalResourceLoader; |
| 32 | |
import org.kuali.rice.kew.actionrequest.ActionRequestFactory; |
| 33 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
| 34 | |
import org.kuali.rice.kew.engine.RouteContext; |
| 35 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
| 36 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
| 37 | |
import org.kuali.rice.kew.rule.RuleExtension; |
| 38 | |
import org.kuali.rice.kew.rule.RuleResponsibility; |
| 39 | |
import org.kuali.rice.kew.rule.WorkflowAttribute; |
| 40 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
| 41 | |
import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute; |
| 42 | |
import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; |
| 43 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 44 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 45 | |
import org.kuali.rice.kew.util.XmlHelper; |
| 46 | |
import org.kuali.rice.kew.xml.RuleXmlParser; |
| 47 | |
import org.kuali.rice.kew.xml.XmlConstants; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 0 | public class InlineRequestsRouteModule extends FlexRMAdapter { |
| 56 | 0 | private static final Logger LOG = Logger.getLogger(InlineRequestsRouteModule.class); |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public List<ActionRequestValue> findActionRequests(RouteContext context) throws Exception { |
| 66 | |
|
| 67 | |
|
| 68 | 0 | List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>(); |
| 69 | 0 | RouteNodeInstance currentNode = context.getNodeInstance(); |
| 70 | 0 | String contentFragment = currentNode.getRouteNode().getContentFragment(); |
| 71 | |
|
| 72 | 0 | Document doc = XmlHelper.trimSAXXml(new ByteArrayInputStream(contentFragment.getBytes())); |
| 73 | 0 | Element root = doc.getRootElement(); |
| 74 | 0 | List<String> ruleAttributeNames = new ArrayList<String>(); |
| 75 | 0 | List<String> ruleAttributeClassNames = new ArrayList<String>(); |
| 76 | 0 | List<String> xpathExpressions = new ArrayList<String>(); |
| 77 | |
|
| 78 | 0 | Element ruleAttributes = root.getChild("ruleAttributes"); |
| 79 | 0 | if (ruleAttributes != null) { |
| 80 | 0 | for (Object o : ruleAttributes.getChildren("name")) { |
| 81 | 0 | Element e = (Element) o; |
| 82 | 0 | ruleAttributeNames.add(e.getText()); |
| 83 | 0 | } |
| 84 | 0 | for (Object o : ruleAttributes.getChildren("className")) { |
| 85 | 0 | Element e = (Element) o; |
| 86 | 0 | ruleAttributeClassNames.add(e.getText()); |
| 87 | 0 | } |
| 88 | |
} |
| 89 | |
|
| 90 | 0 | for (Object o: root.getChildren("match")) { |
| 91 | 0 | Element e = (Element) o; |
| 92 | 0 | xpathExpressions.add(e.getText()); |
| 93 | 0 | } |
| 94 | 0 | if ( (ruleAttributeNames.isEmpty()) && (ruleAttributeClassNames.isEmpty()) && (xpathExpressions.isEmpty()) ) { |
| 95 | 0 | throw new RuntimeException("Match xpath expression not specified (should be parse-time exception...)"); |
| 96 | |
} |
| 97 | |
|
| 98 | 0 | List<WorkflowAttribute> attributes = new ArrayList<WorkflowAttribute>(); |
| 99 | 0 | for (String attributeName : ruleAttributeNames) { |
| 100 | 0 | attributes.add(getRuleAttributeByName(attributeName)); |
| 101 | |
} |
| 102 | 0 | for (String attributeClassName : ruleAttributeClassNames) { |
| 103 | 0 | attributes.add(getRuleAttributeByClassName(attributeClassName)); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | 0 | if (attributes.isEmpty() && xpathExpressions.isEmpty()) { |
| 108 | 0 | return actionRequests; |
| 109 | |
} |
| 110 | |
|
| 111 | 0 | Boolean match = Boolean.TRUE; |
| 112 | 0 | if (!xpathExpressions.isEmpty()) { |
| 113 | 0 | XPath xpath = XPathHelper.newXPath(); |
| 114 | 0 | for (String xpathExpression : xpathExpressions) { |
| 115 | 0 | match &= (Boolean) xpath.evaluate(xpathExpression, context.getDocumentContent().getDocument(), XPathConstants.BOOLEAN); |
| 116 | |
} |
| 117 | |
} |
| 118 | 0 | for (WorkflowAttribute workflowAttribute : attributes) { |
| 119 | |
|
| 120 | 0 | match &= workflowAttribute.isMatch(context.getDocumentContent(), new ArrayList<RuleExtension>()); |
| 121 | |
} |
| 122 | |
|
| 123 | 0 | if (match.booleanValue()) { |
| 124 | |
|
| 125 | |
} else { |
| 126 | |
|
| 127 | |
|
| 128 | 0 | return actionRequests; |
| 129 | |
} |
| 130 | |
|
| 131 | 0 | List<RuleResponsibility> responsibilities = new ArrayList<RuleResponsibility>(); |
| 132 | 0 | RuleXmlParser parser = new RuleXmlParser(); |
| 133 | 0 | ActionRequestFactory arf = new ActionRequestFactory(context.getDocument(), currentNode); |
| 134 | |
|
| 135 | 0 | RuleBaseValues fakeRule = new RuleBaseValues(); |
| 136 | 0 | fakeRule.setActiveInd(Boolean.TRUE); |
| 137 | 0 | fakeRule.setCurrentInd(Boolean.TRUE); |
| 138 | 0 | fakeRule.setDescription("a fake rule"); |
| 139 | 0 | fakeRule.setForceAction(Boolean.TRUE); |
| 140 | 0 | fakeRule.setRuleBaseValuesId(null); |
| 141 | |
|
| 142 | 0 | for (Object o: root.getChildren("responsibility", XmlConstants.RULE_NAMESPACE)) { |
| 143 | 0 | Element e = (Element) o; |
| 144 | 0 | RuleResponsibility responsibility = parser.parseResponsibility(e, fakeRule); |
| 145 | 0 | responsibility.setResponsibilityId(KEWConstants.MACHINE_GENERATED_RESPONSIBILITY_ID); |
| 146 | 0 | responsibilities.add(responsibility); |
| 147 | 0 | } |
| 148 | 0 | if (responsibilities.size() == 0) { |
| 149 | 0 | throw new RuntimeException("No responsibilities found on node " + currentNode.getName()); |
| 150 | |
} |
| 151 | |
|
| 152 | 0 | makeActionRequests(arf, responsibilities, context, fakeRule, context.getDocument(), null, null); |
| 153 | 0 | actionRequests.addAll(arf.getRequestGraphs()); |
| 154 | 0 | return actionRequests; |
| 155 | |
} |
| 156 | |
|
| 157 | |
@Override |
| 158 | |
public String toString() { |
| 159 | 0 | return "InlineRequestsRouteModule"; |
| 160 | |
} |
| 161 | |
|
| 162 | |
private WorkflowAttribute getRuleAttributeByName(String ruleAttributeName) { |
| 163 | 0 | return materializeRuleAttribute(KEWServiceLocator.getRuleAttributeService().findByName(ruleAttributeName)); |
| 164 | |
} |
| 165 | |
|
| 166 | |
private WorkflowAttribute getRuleAttributeByClassName(String ruleAttributeClassName) { |
| 167 | 0 | return materializeRuleAttribute(KEWServiceLocator.getRuleAttributeService().findByClassName(ruleAttributeClassName)); |
| 168 | |
} |
| 169 | |
|
| 170 | |
private WorkflowAttribute materializeRuleAttribute(RuleAttribute ruleAttribute) { |
| 171 | 0 | if (ruleAttribute != null) { |
| 172 | 0 | if (KEWConstants.RULE_ATTRIBUTE_TYPE.equals(ruleAttribute.getType())) { |
| 173 | 0 | ObjectDefinition objDef = new ObjectDefinition(ruleAttribute.getClassName(), ruleAttribute.getServiceNamespace()); |
| 174 | 0 | return (WorkflowAttribute) GlobalResourceLoader.getObject(objDef); |
| 175 | 0 | } else if (KEWConstants.RULE_XML_ATTRIBUTE_TYPE.equals(ruleAttribute.getType())) { |
| 176 | 0 | ObjectDefinition objDef = new ObjectDefinition(ruleAttribute.getClassName(), ruleAttribute.getServiceNamespace()); |
| 177 | 0 | WorkflowAttribute workflowAttribute = (WorkflowAttribute) GlobalResourceLoader.getObject(objDef); |
| 178 | |
|
| 179 | 0 | ((GenericXMLRuleAttribute) workflowAttribute).setRuleAttribute(ruleAttribute); |
| 180 | 0 | return workflowAttribute; |
| 181 | |
} |
| 182 | |
} |
| 183 | 0 | return null; |
| 184 | |
} |
| 185 | |
|
| 186 | |
} |