View Javadoc

1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A simple sample RulesEngineExecutor usable in the sample app which is hard-coded to select a context with
39   * namespaceCode="KR-SAP" and name="Travel Account ".  It also is hardcoded to select an agenda from the context with an
40   * event name of "workflow".
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
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          // extract facts from routeContext
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  //    public static void main(String [] args) throws Exception {
91  //
92  //        String sampleDocContent = FileUtils.readFileToString(new File("/home/gilesp/tmp/SampleDocContent.txt"));
93  //
94  //        RouteContext rc = new RouteContext();
95  //        DocumentRouteHeaderValue document = new DocumentRouteHeaderValue();
96  //        DocumentRouteHeaderValueContent content = new DocumentRouteHeaderValueContent();
97  //        content.setDocumentContent(sampleDocContent);
98  //        document.setDocumentContent(content);
99  //        rc.setDocument(document);
100 //
101 //        new TravelAccountRulesEngineExecutor().execute(rc, null);
102 //    }
103 }