View Javadoc

1   package org.kuali.ole.license.executor;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.agreement.bo.OleAgreementMethod;
5   import org.kuali.ole.ingest.pojo.MatchBo;
6   import org.kuali.ole.license.bo.OleLicenseRequestType;
7   import org.kuali.ole.license.bo.OleLicenseRequestWorkflowType;
8   import org.kuali.rice.core.api.exception.RiceRuntimeException;
9   import org.kuali.rice.core.api.util.xml.XmlHelper;
10  import org.kuali.rice.kew.engine.RouteContext;
11  import org.kuali.rice.kew.framework.support.krms.RulesEngineExecutor;
12  import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
13  import org.kuali.rice.kim.impl.role.RoleBo;
14  import org.kuali.rice.kim.impl.role.RoleMemberBo;
15  import org.kuali.rice.krad.service.KRADServiceLocator;
16  import org.kuali.rice.krms.api.engine.*;
17  import org.kuali.rice.krms.impl.repository.AgendaBo;
18  import org.w3c.dom.Document;
19  
20  import javax.xml.xpath.XPath;
21  import javax.xml.xpath.XPathConstants;
22  import java.io.ByteArrayInputStream;
23  import java.util.*;
24  
25  /**
26   * OleLicenseRulesEngineExecutor validates rules from krms and executes people flow action.
27   */
28  public class OleLicenseRulesEngineExecutor implements RulesEngineExecutor {
29      private static final String NAMESPACE_CODE_SELECTOR = "namespaceCode";
30      private static final String NAME_SELECTOR = "name";
31  
32      /**
33       * This method validates rules from krms and execute people flow action
34       * @param routeContext
35       * @param engine
36       * @return EngineResults
37       */
38      @Override
39      public EngineResults execute(RouteContext routeContext, Engine engine) {
40          EngineResults engineResult =null;
41          HashMap<String, Object> agendaValue = new HashMap<String, Object>();
42          agendaValue.put("nm", OLEConstants.OleLicenseRequest.LICENSE_AGENDA_NM);
43          List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
44          if(agendaBos!=null && agendaBos.size()>0){
45              AgendaBo agendaBo = agendaBos.get(0);
46              HashMap<String, String> map = new HashMap<String, String>();
47              map.put("AGENDA_NAME", agendaBo.getName());
48              List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map);
49  
50              SelectionCriteria selectionCriteria =
51                      SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()),
52                              getAgendaContext(OLEConstants.OleLicenseRequest.LICENSE_AGENDA_NM));
53  
54              ExecutionOptions executionOptions = new ExecutionOptions();
55              executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
56  
57              Facts.Builder factBuilder = Facts.Builder.create();
58  
59              String docContent = routeContext.getDocument().getDocContent();
60  
61              String licenseRequestWorkflowTypeCode =
62                      getElementValue(docContent, "//newMaintainableObject/dataObject/licenseRequestWorkflowTypeCode");
63  
64              String agreementMethodId =
65                      getElementValue(docContent, "//newMaintainableObject/dataObject/agreementMethodId");
66              String licenseRequestTypeId =
67                      getElementValue(docContent, "//newMaintainableObject/dataObject/licenseRequestTypeId");
68              String owner =
69                      getElementValue(docContent, "//newMaintainableObject/dataObject/assignee");
70  
71              String licenseType = getLicenseType(licenseRequestTypeId);
72              String  workflowName = getWorkFlowName(licenseRequestWorkflowTypeCode) ;
73              String agreementMethod = getAgreementMethod(agreementMethodId);
74  
75              HashMap<String,Object> termValues = new HashMap<String, Object>() ;
76              termValues.put("licenseType",licenseType);
77              termValues.put("agreementMethod",agreementMethod);
78              termValues.put("workflowName",workflowName);
79  
80  
81              for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
82                  MatchBo matchBo = matchBoIterator.next();
83                  factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
84              }
85  
86  
87              engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
88              changeLicenseMangerRoleToOwner(owner);
89          }
90          return engineResult;  //To change body of implemented methods use File | Settings | File Templates.
91      }
92  
93      private String getAgreementMethod(String agreementMethodId) {
94          OleAgreementMethod agreementMethod = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleAgreementMethod.class, agreementMethodId);
95          return agreementMethod.getAgreementMethodName();
96      }
97  
98      /**
99       * This method returns LicenseType using licenseRequestTypeId.
100      * @param licenseRequestTypeId
101      * @return oleLicenseRequestType
102      */
103     private String getLicenseType(String licenseRequestTypeId){
104         OleLicenseRequestType oleLicenseRequestType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleLicenseRequestType.class, licenseRequestTypeId);
105         return oleLicenseRequestType.getName();
106     }
107 
108     /**
109      *  This method returns WorkFlowName using licenseRequestWorkflowTypeCode.
110      * @param licenseRequestWorkflowTypeCode
111      * @return oleLicenseRequestWorkflowType
112      */
113     private String getWorkFlowName(String licenseRequestWorkflowTypeCode){
114         OleLicenseRequestWorkflowType oleLicenseRequestWorkflowType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleLicenseRequestWorkflowType.class, licenseRequestWorkflowTypeCode);
115         return oleLicenseRequestWorkflowType.getName();
116     }
117 
118     /**
119      * This method returns ElementValue using docContent and xpathExpression..
120      * @param docContent
121      * @param xpathExpression
122      * @return value
123      */
124     private String getElementValue(String docContent, String xpathExpression) {
125         try {
126             Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
127 
128             XPath xpath = XPathHelper.newXPath();
129             String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
130 
131             return value;
132 
133         } catch (Exception e) {
134             throw new RiceRuntimeException();
135         }
136     }
137 
138     /**
139      *  This method returns SelectionContext using contextName.
140      * @param contextName
141      * @return Map
142      */
143     protected Map<String, String> getSelectionContext(String contextName) {
144         Map<String, String> selector = new HashMap<String, String>();
145         selector.put(NAMESPACE_CODE_SELECTOR, OLEConstants.OLE_NAMESPACE);
146         selector.put(NAME_SELECTOR, contextName);
147         return selector;
148     }
149 
150     /**
151      *  This method returns AgendaContext using agendaName..
152      * @param agendaName
153      * @return Map
154      */
155     protected Map<String, String> getAgendaContext(String agendaName) {
156         Map<String, String> selector = new HashMap<String, String>();
157         selector.put(NAME_SELECTOR, agendaName);
158         return selector;
159     }
160 
161     /**
162      *   This method change the license role using owner.
163      * @param owner
164      */
165     private void changeLicenseMangerRoleToOwner(String owner){
166         HashMap<String, String> map = new HashMap<String, String>();
167         map.put("ROLE_NM", OLEConstants.OleLicenseRequest.LICENSE_MNGR_ROLE_NM);
168         List<RoleBo> roleBos = (List<RoleBo>)KRADServiceLocator.getBusinessObjectService().findMatching(RoleBo.class,map);
169         List<RoleMemberBo> roleMemberBos = roleBos!=null && roleBos.size()>0 ? roleBos.get(0).getMembers() : new ArrayList<RoleMemberBo>();
170         RoleMemberBo roleMemberBo = roleMemberBos.size()>0? roleMemberBos.get(0): null;
171         if(roleMemberBo!=null){
172             roleMemberBo.setMemberId(owner);
173             KRADServiceLocator.getBusinessObjectService().save(roleMemberBo);
174         }
175     }
176 }