View Javadoc

1   /**
2    * Copyright 2005-2012 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.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   * A simple sample RulesEngineExecutor usable in the sample app which is hard-coded to select a context with
38   * namespaceCode="KR-SAP" and name="Travel Account ".  It also is hardcoded to select an agenda from the context with an
39   * event name of "workflow".
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
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          // extract facts from routeContext
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          factsBuilder.addFact("Subsidized Percent", Double.valueOf(subsidizedPercentStr));
67          factsBuilder.addFact("Account Type Code", accountTypeCode);
68          factsBuilder.addFact("Initiator Principal ID", initiator);
69  
70          return engine.execute(sectionCriteria, factsBuilder.build(), null);
71      }
72  
73      private String getElementValue(String docContent, String xpathExpression) {
74          try {
75              Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
76  
77              XPath xpath = XPathHelper.newXPath();
78              String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
79  
80              return value;
81  
82          } catch (Exception e) {
83              throw new RiceRuntimeException();
84          }
85      }
86  
87  //    public static void main(String [] args) throws Exception {
88  //
89  //        String sampleDocContent = FileUtils.readFileToString(new File("/home/gilesp/tmp/SampleDocContent.txt"));
90  //
91  //        RouteContext rc = new RouteContext();
92  //        DocumentRouteHeaderValue document = new DocumentRouteHeaderValue();
93  //        DocumentRouteHeaderValueContent content = new DocumentRouteHeaderValueContent();
94  //        content.setDocumentContent(sampleDocContent);
95  //        document.setDocumentContent(content);
96  //        rc.setDocument(document);
97  //
98  //        new TravelAccountRulesEngineExecutor().execute(rc, null);
99  //    }
100 }