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