001 /** 002 * Copyright 2005-2011 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 */ 016 package edu.sampleu.travel.workflow; 017 018 import org.kuali.rice.core.api.exception.RiceRuntimeException; 019 import org.kuali.rice.core.api.util.xml.XmlHelper; 020 import org.kuali.rice.kew.engine.RouteContext; 021 import org.kuali.rice.kew.framework.support.krms.RulesEngineExecutor; 022 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; 023 import org.kuali.rice.krms.api.engine.Engine; 024 import org.kuali.rice.krms.api.engine.EngineResults; 025 import org.kuali.rice.krms.api.engine.Facts; 026 import org.kuali.rice.krms.api.engine.SelectionCriteria; 027 import org.w3c.dom.Document; 028 029 import javax.xml.xpath.XPath; 030 import javax.xml.xpath.XPathConstants; 031 import java.io.ByteArrayInputStream; 032 import java.util.Collections; 033 import java.util.HashMap; 034 import java.util.Map; 035 036 /** 037 * A simple sample RulesEngineExecutor usable in the sample app which is hard-coded to select a context with 038 * namespaceCode="KR-SAP" and name="Travel Account ". It also is hardcoded to select an agenda from the context with an 039 * event name of "workflow". 040 * 041 * @author Kuali Rice Team (rice.collab@kuali.org) 042 */ 043 public class TravelAccountRulesEngineExecutor implements RulesEngineExecutor { 044 045 private static final String CONTEXT_NAMESPACE_CODE = "KR-SAP"; 046 private static final String CONTEXT_NAME = "Travel Account"; 047 048 @Override 049 public EngineResults execute(RouteContext routeContext, Engine engine) { 050 Map<String, String> contextQualifiers = new HashMap<String, String>(); 051 contextQualifiers.put("namespaceCode", CONTEXT_NAMESPACE_CODE); 052 contextQualifiers.put("name", CONTEXT_NAME); 053 SelectionCriteria sectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers, 054 Collections.singletonMap("Campus", "BL")); 055 056 // extract facts from routeContext 057 String docContent = routeContext.getDocument().getDocContent(); 058 059 String subsidizedPercentStr = getElementValue(docContent, "//newMaintainableObject//subsidizedPercent"); 060 String accountTypeCode = 061 getElementValue(docContent, "//newMaintainableObject/dataObject/extension/accountTypeCode"); 062 String initiator = getElementValue(docContent, "//documentInitiator//principalId"); 063 064 Facts.Builder factsBuilder = Facts.Builder.create(); 065 066 factsBuilder.addFact("Subsidized Percent", Double.valueOf(subsidizedPercentStr)); 067 factsBuilder.addFact("Account Type Code", accountTypeCode); 068 factsBuilder.addFact("Initiator Principal ID", initiator); 069 070 return engine.execute(sectionCriteria, factsBuilder.build(), null); 071 } 072 073 private String getElementValue(String docContent, String xpathExpression) { 074 try { 075 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes())); 076 077 XPath xpath = XPathHelper.newXPath(); 078 String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING); 079 080 return value; 081 082 } catch (Exception e) { 083 throw new RiceRuntimeException(); 084 } 085 } 086 087 // public static void main(String [] args) throws Exception { 088 // 089 // String sampleDocContent = FileUtils.readFileToString(new File("/home/gilesp/tmp/SampleDocContent.txt")); 090 // 091 // RouteContext rc = new RouteContext(); 092 // DocumentRouteHeaderValue document = new DocumentRouteHeaderValue(); 093 // DocumentRouteHeaderValueContent content = new DocumentRouteHeaderValueContent(); 094 // content.setDocumentContent(sampleDocContent); 095 // document.setDocumentContent(content); 096 // rc.setDocument(document); 097 // 098 // new TravelAccountRulesEngineExecutor().execute(rc, null); 099 // } 100 }