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