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