001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package edu.sampleu.travel.workflow; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.exception.RiceRuntimeException; 020import org.kuali.rice.core.api.util.xml.XmlHelper; 021import org.kuali.rice.kew.engine.RouteContext; 022import org.kuali.rice.kew.framework.support.krms.RulesEngineExecutor; 023import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; 024import org.kuali.rice.krms.api.engine.Engine; 025import org.kuali.rice.krms.api.engine.EngineResults; 026import org.kuali.rice.krms.api.engine.Facts; 027import org.kuali.rice.krms.api.engine.SelectionCriteria; 028import org.w3c.dom.Document; 029 030import javax.xml.xpath.XPath; 031import javax.xml.xpath.XPathConstants; 032import java.io.ByteArrayInputStream; 033import java.util.Collections; 034import java.util.HashMap; 035import java.util.Map; 036 037/** 038 * A simple sample RulesEngineExecutor usable in the sample app which is hard-coded to select a context with 039 * namespaceCode="KR-SAP" and name="Travel Account ". It also is hardcoded to select an agenda from the context with an 040 * event name of "workflow". 041 * 042 * @author Kuali Rice Team (rice.collab@kuali.org) 043 */ 044public class TravelAccountRulesEngineExecutor implements RulesEngineExecutor { 045 046 private static final String CONTEXT_NAMESPACE_CODE = "KR-SAP"; 047 private static final String CONTEXT_NAME = "Travel Account"; 048 049 @Override 050 public EngineResults execute(RouteContext routeContext, Engine engine) { 051 Map<String, String> contextQualifiers = new HashMap<String, String>(); 052 contextQualifiers.put("namespaceCode", CONTEXT_NAMESPACE_CODE); 053 contextQualifiers.put("name", CONTEXT_NAME); 054 SelectionCriteria sectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers, 055 Collections.singletonMap("Campus", "BL")); 056 057 // extract facts from routeContext 058 String docContent = routeContext.getDocument().getDocContent(); 059 060 String subsidizedPercentStr = getElementValue(docContent, "//newMaintainableObject//subsidizedPercent"); 061 String accountTypeCode = 062 getElementValue(docContent, "//newMaintainableObject/dataObject/extension/accountTypeCode"); 063 String initiator = getElementValue(docContent, "//documentInitiator//principalId"); 064 065 Facts.Builder factsBuilder = Facts.Builder.create(); 066 067 if(StringUtils.isNotEmpty(subsidizedPercentStr)) { 068 factsBuilder.addFact("Subsidized Percent", Double.valueOf(subsidizedPercentStr)); 069 } 070 factsBuilder.addFact("Account Type Code", accountTypeCode); 071 factsBuilder.addFact("Initiator Principal ID", initiator); 072 073 return engine.execute(sectionCriteria, factsBuilder.build(), null); 074 } 075 076 private String getElementValue(String docContent, String xpathExpression) { 077 try { 078 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes())); 079 080 XPath xpath = XPathHelper.newXPath(); 081 String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING); 082 083 return value; 084 085 } catch (Exception e) { 086 throw new RiceRuntimeException(); 087 } 088 } 089 090// public static void main(String [] args) throws Exception { 091// 092// String sampleDocContent = FileUtils.readFileToString(new File("/home/gilesp/tmp/SampleDocContent.txt")); 093// 094// RouteContext rc = new RouteContext(); 095// DocumentRouteHeaderValue document = new DocumentRouteHeaderValue(); 096// DocumentRouteHeaderValueContent content = new DocumentRouteHeaderValueContent(); 097// content.setDocumentContent(sampleDocContent); 098// document.setDocumentContent(content); 099// rc.setDocument(document); 100// 101// new TravelAccountRulesEngineExecutor().execute(rc, null); 102// } 103}