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