001 package org.kuali.ole.license.executor; 002 003 import org.kuali.ole.OLEConstants; 004 import org.kuali.ole.ingest.pojo.MatchBo; 005 import org.kuali.rice.core.api.exception.RiceRuntimeException; 006 import org.kuali.rice.core.api.util.xml.XmlHelper; 007 import org.kuali.rice.kew.engine.RouteContext; 008 import org.kuali.rice.kew.framework.support.krms.RulesEngineExecutor; 009 import org.kuali.rice.kew.impl.peopleflow.PeopleFlowBo; 010 import org.kuali.rice.kew.impl.peopleflow.PeopleFlowMemberBo; 011 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; 012 import org.kuali.rice.krad.service.KRADServiceLocator; 013 import org.kuali.rice.krms.api.engine.*; 014 import org.kuali.rice.krms.impl.repository.AgendaBo; 015 import org.w3c.dom.Document; 016 017 import javax.xml.xpath.XPath; 018 import javax.xml.xpath.XPathConstants; 019 import java.io.ByteArrayInputStream; 020 import java.util.HashMap; 021 import java.util.Iterator; 022 import java.util.List; 023 import java.util.Map; 024 025 /** 026 * OwnerRulesEngineExecutor validates rules from krms and executes people flow action. 027 */ 028 public class OwnerRulesEngineExecutor implements RulesEngineExecutor { 029 030 /** 031 * This method validates rules from krms and execute people flow action 032 * @param routeContext 033 * @param engine 034 * @return EngineResults 035 */ 036 @Override 037 public EngineResults execute(RouteContext routeContext, Engine engine) { 038 039 HashMap<String, Object> agendaValue = new HashMap<String, Object>(); 040 agendaValue.put("nm", "OWNER_POLICY"); 041 List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue); 042 AgendaBo agendaBo = agendaBos.get(0); 043 HashMap<String, String> map = new HashMap<String, String>(); 044 map.put("AGENDA_NAME", agendaBo.getName()); 045 List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map); 046 047 SelectionCriteria selectionCriteria = 048 SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()), getAgendaContext("OWNER_POLICY")); 049 050 ExecutionOptions executionOptions = new ExecutionOptions(); 051 executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true); 052 053 Facts.Builder factBuilder = Facts.Builder.create(); 054 055 String docContent = routeContext.getDocument().getDocContent(); 056 057 String licenseRequestWorkflowTypeCode = 058 getElementValue(docContent, "//newMaintainableObject/dataObject/licenseRequestWorkflowTypeCode"); 059 String owner = 060 getElementValue(docContent, "//newMaintainableObject/dataObject/assignee"); 061 062 String oleLicenseRequestStatusCode = 063 getElementValue(docContent, "//newMaintainableObject/dataObject/oleLicenseRequestStatus/code"); 064 065 HashMap<String,Object> termValues = new HashMap<String, Object>() ; 066 if((licenseRequestWorkflowTypeCode.equalsIgnoreCase("SIGO"))|| 067 (licenseRequestWorkflowTypeCode.equalsIgnoreCase("RVWO")) 068 || licenseRequestWorkflowTypeCode.equalsIgnoreCase("APPO") && !oleLicenseRequestStatusCode.equalsIgnoreCase("LC")){ 069 termValues.put("owner","route"); 070 }else{ 071 termValues.put("owner","NIL"); 072 } 073 074 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) { 075 MatchBo matchBo = matchBoIterator.next(); 076 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName()))); 077 } 078 EngineResults engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions); 079 String peopleFlowsSelected = (String)engineResult.getAttribute("peopleFlowsSelected"); 080 String peopleFlwid =peopleFlowsSelected!=null?peopleFlowsSelected.split(":")[1]:null; 081 if(peopleFlwid!=null)createPeopleFlowMemberToOwner(owner,peopleFlwid); 082 return engineResult; // 083 } 084 085 /** 086 * This method returns ElementValue using docContent and xpathExpression.. 087 * @param docContent 088 * @param xpathExpression 089 * @return value 090 */ 091 private String getElementValue(String docContent, String xpathExpression) { 092 try { 093 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes())); 094 095 XPath xpath = XPathHelper.newXPath(); 096 String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING); 097 098 return value; 099 100 } catch (Exception e) { 101 throw new RiceRuntimeException(); 102 } 103 } 104 105 /** 106 * This method returns SelectionContext using contextName. 107 * @param contextName 108 * @return Map 109 */ 110 protected Map<String, String> getSelectionContext(String contextName) { 111 Map<String, String> selector = new HashMap<String, String>(); 112 selector.put(OLEConstants.NAMESPACE_CODE_SELECTOR, "OLE"); 113 selector.put(OLEConstants.NAME_SELECTOR, contextName); 114 return selector; 115 } 116 117 /** 118 * This method returns AgendaContext using agendaName.. 119 * @param agendaName 120 * @return Map 121 */ 122 protected Map<String, String> getAgendaContext(String agendaName) { 123 Map<String, String> selector = new HashMap<String, String>(); 124 selector.put(OLEConstants.NAME_SELECTOR, agendaName); 125 return selector; 126 } 127 128 /** 129 * This method create people to owner using owner and peopleFlwId. 130 * @param owner 131 * @param peopleFlwId 132 */ 133 private void createPeopleFlowMemberToOwner(String owner,String peopleFlwId){ 134 PeopleFlowBo peopleFlow = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(PeopleFlowBo.class,peopleFlwId); 135 peopleFlow.getMembers(); 136 PeopleFlowMemberBo peopleFlowMemberBo = peopleFlow.getMembers().get(0); 137 peopleFlowMemberBo.setMemberId(owner); 138 KRADServiceLocator.getBusinessObjectService().save(peopleFlowMemberBo); 139 140 } 141 142 }