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