View Javadoc

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