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
26
27 public class OleLicenseRulesEngineExecutor implements RulesEngineExecutor {
28 private static final String NAMESPACE_CODE_SELECTOR = "namespaceCode";
29 private static final String NAME_SELECTOR = "name";
30
31
32
33
34
35
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 agreementMethod =
64 getElementValue(docContent, "//newMaintainableObject/dataObject/agreementMethod");
65 String licenseRequestTypeId =
66 getElementValue(docContent, "//newMaintainableObject/dataObject/licenseRequestTypeId");
67 String owner =
68 getElementValue(docContent, "//newMaintainableObject/dataObject/assignee");
69
70 String licenseType = getLicenseType(licenseRequestTypeId);
71 String workflowName = getWorkFlowName(licenseRequestWorkflowTypeCode) ;
72
73 HashMap<String,Object> termValues = new HashMap<String, Object>() ;
74 termValues.put("licenseType",licenseType);
75 termValues.put("agreementMethod",agreementMethod);
76 termValues.put("workflowName",workflowName);
77
78
79 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
80 MatchBo matchBo = matchBoIterator.next();
81 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
82 }
83
84
85 engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
86 changeLicenseMangerRoleToOwner(owner);
87 }
88 return engineResult;
89 }
90
91
92
93
94
95
96 private String getLicenseType(String licenseRequestTypeId){
97 OleLicenseRequestType oleLicenseRequestType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleLicenseRequestType.class, licenseRequestTypeId);
98 return oleLicenseRequestType.getName();
99 }
100
101
102
103
104
105
106 private String getWorkFlowName(String licenseRequestWorkflowTypeCode){
107 OleLicenseRequestWorkflowType oleLicenseRequestWorkflowType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleLicenseRequestWorkflowType.class, licenseRequestWorkflowTypeCode);
108 return oleLicenseRequestWorkflowType.getName();
109 }
110
111
112
113
114
115
116
117 private String getElementValue(String docContent, String xpathExpression) {
118 try {
119 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
120
121 XPath xpath = XPathHelper.newXPath();
122 String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
123
124 return value;
125
126 } catch (Exception e) {
127 throw new RiceRuntimeException();
128 }
129 }
130
131
132
133
134
135
136 protected Map<String, String> getSelectionContext(String contextName) {
137 Map<String, String> selector = new HashMap<String, String>();
138 selector.put(NAMESPACE_CODE_SELECTOR, OLEConstants.OLE_NAMESPACE);
139 selector.put(NAME_SELECTOR, contextName);
140 return selector;
141 }
142
143
144
145
146
147
148 protected Map<String, String> getAgendaContext(String agendaName) {
149 Map<String, String> selector = new HashMap<String, String>();
150 selector.put(NAME_SELECTOR, agendaName);
151 return selector;
152 }
153
154
155
156
157
158 private void changeLicenseMangerRoleToOwner(String owner){
159 HashMap<String, String> map = new HashMap<String, String>();
160 map.put("ROLE_NM", OLEConstants.OleLicenseRequest.LICENSE_MNGR_ROLE_NM);
161 List<RoleBo> roleBos = (List<RoleBo>)KRADServiceLocator.getBusinessObjectService().findMatching(RoleBo.class,map);
162 List<RoleMemberBo> roleMemberBos = roleBos!=null && roleBos.size()>0 ? roleBos.get(0).getMembers() : new ArrayList<RoleMemberBo>();
163 RoleMemberBo roleMemberBo = roleMemberBos.size()>0? roleMemberBos.get(0): null;
164 if(roleMemberBo!=null){
165 roleMemberBo.setMemberId(owner);
166 KRADServiceLocator.getBusinessObjectService().save(roleMemberBo);
167 }
168 }
169 }