1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.sampleu.travel.workflow;
17
18 import org.kuali.rice.core.api.exception.RiceRuntimeException;
19 import org.kuali.rice.core.api.util.xml.XmlHelper;
20 import org.kuali.rice.kew.engine.RouteContext;
21 import org.kuali.rice.kew.framework.support.krms.RulesEngineExecutor;
22 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
23 import org.kuali.rice.krms.api.engine.Engine;
24 import org.kuali.rice.krms.api.engine.EngineResults;
25 import org.kuali.rice.krms.api.engine.Facts;
26 import org.kuali.rice.krms.api.engine.SelectionCriteria;
27 import org.w3c.dom.Document;
28
29 import javax.xml.xpath.XPath;
30 import javax.xml.xpath.XPathConstants;
31 import java.io.ByteArrayInputStream;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Map;
35
36
37
38
39
40
41
42
43 public class TravelAccountRulesEngineExecutor implements RulesEngineExecutor {
44
45 private static final String CONTEXT_NAMESPACE_CODE = "KR-SAP";
46 private static final String CONTEXT_NAME = "Travel Account";
47
48 @Override
49 public EngineResults execute(RouteContext routeContext, Engine engine) {
50 Map<String, String> contextQualifiers = new HashMap<String, String>();
51 contextQualifiers.put("namespaceCode", CONTEXT_NAMESPACE_CODE);
52 contextQualifiers.put("name", CONTEXT_NAME);
53 SelectionCriteria sectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers,
54 Collections.singletonMap("Campus", "BL"));
55
56
57 String docContent = routeContext.getDocument().getDocContent();
58
59 String subsidizedPercentStr = getElementValue(docContent, "//newMaintainableObject//subsidizedPercent");
60 String accountTypeCode =
61 getElementValue(docContent, "//newMaintainableObject/dataObject/extension/accountTypeCode");
62 String initiator = getElementValue(docContent, "//documentInitiator//principalId");
63
64 Facts.Builder factsBuilder = Facts.Builder.create();
65
66 try {
67 factsBuilder.addFact("Subsidized Percent", Double.valueOf(subsidizedPercentStr));
68 } catch (NumberFormatException e) {
69 factsBuilder.addFact("Subsidized Percent", 0);
70 }
71 factsBuilder.addFact("Account Type Code", accountTypeCode);
72 factsBuilder.addFact("Initiator Principal ID", initiator);
73
74 return engine.execute(sectionCriteria, factsBuilder.build(), null);
75 }
76
77 private String getElementValue(String docContent, String xpathExpression) {
78 try {
79 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
80
81 XPath xpath = XPathHelper.newXPath();
82 String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
83
84 return value;
85
86 } catch (Exception e) {
87 throw new RiceRuntimeException();
88 }
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 }