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.rice.core.api.exception.RiceRuntimeException;
6 import org.kuali.rice.core.api.util.xml.XmlHelper;
7 import org.kuali.rice.kew.engine.RouteContext;
8 import org.kuali.rice.kew.framework.support.krms.RulesEngineExecutor;
9 import org.kuali.rice.kew.impl.peopleflow.PeopleFlowBo;
10 import org.kuali.rice.kew.impl.peopleflow.PeopleFlowMemberBo;
11 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
12 import org.kuali.rice.krad.service.KRADServiceLocator;
13 import org.kuali.rice.krms.api.engine.*;
14 import org.kuali.rice.krms.impl.repository.AgendaBo;
15 import org.w3c.dom.Document;
16
17 import javax.xml.xpath.XPath;
18 import javax.xml.xpath.XPathConstants;
19 import java.io.ByteArrayInputStream;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25
26
27
28 public class OwnerRulesEngineExecutor implements RulesEngineExecutor {
29
30
31
32
33
34
35
36 @Override
37 public EngineResults execute(RouteContext routeContext, Engine engine) {
38
39 HashMap<String, Object> agendaValue = new HashMap<String, Object>();
40 agendaValue.put("nm", "OWNER_POLICY");
41 List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
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()), getAgendaContext("OWNER_POLICY"));
49
50 ExecutionOptions executionOptions = new ExecutionOptions();
51 executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
52
53 Facts.Builder factBuilder = Facts.Builder.create();
54
55 String docContent = routeContext.getDocument().getDocContent();
56
57 String licenseRequestWorkflowTypeCode =
58 getElementValue(docContent, "//newMaintainableObject/dataObject/licenseRequestWorkflowTypeCode");
59 String owner =
60 getElementValue(docContent, "//newMaintainableObject/dataObject/assignee");
61
62 String oleLicenseRequestStatusCode =
63 getElementValue(docContent, "//newMaintainableObject/dataObject/oleLicenseRequestStatus/code");
64
65 HashMap<String,Object> termValues = new HashMap<String, Object>() ;
66 if((licenseRequestWorkflowTypeCode.equalsIgnoreCase("SIGO"))||
67 (licenseRequestWorkflowTypeCode.equalsIgnoreCase("RVWO"))
68 || licenseRequestWorkflowTypeCode.equalsIgnoreCase("APPO") && !oleLicenseRequestStatusCode.equalsIgnoreCase("LC")){
69 termValues.put("owner","route");
70 }else{
71 termValues.put("owner","NIL");
72 }
73
74 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
75 MatchBo matchBo = matchBoIterator.next();
76 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
77 }
78 EngineResults engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
79 String peopleFlowsSelected = (String)engineResult.getAttribute("peopleFlowsSelected");
80 String peopleFlwid =peopleFlowsSelected!=null?peopleFlowsSelected.split(":")[1]:null;
81 if(peopleFlwid!=null)createPeopleFlowMemberToOwner(owner,peopleFlwid);
82 return engineResult;
83 }
84
85
86
87
88
89
90
91 private String getElementValue(String docContent, String xpathExpression) {
92 try {
93 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
94
95 XPath xpath = XPathHelper.newXPath();
96 String value = (String)xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
97
98 return value;
99
100 } catch (Exception e) {
101 throw new RiceRuntimeException();
102 }
103 }
104
105
106
107
108
109
110 protected Map<String, String> getSelectionContext(String contextName) {
111 Map<String, String> selector = new HashMap<String, String>();
112 selector.put(OLEConstants.NAMESPACE_CODE_SELECTOR, "OLE");
113 selector.put(OLEConstants.NAME_SELECTOR, contextName);
114 return selector;
115 }
116
117
118
119
120
121
122 protected Map<String, String> getAgendaContext(String agendaName) {
123 Map<String, String> selector = new HashMap<String, String>();
124 selector.put(OLEConstants.NAME_SELECTOR, agendaName);
125 return selector;
126 }
127
128
129
130
131
132
133 private void createPeopleFlowMemberToOwner(String owner,String peopleFlwId){
134 PeopleFlowBo peopleFlow = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(PeopleFlowBo.class,peopleFlwId);
135 peopleFlow.getMembers();
136 PeopleFlowMemberBo peopleFlowMemberBo = peopleFlow.getMembers().get(0);
137 peopleFlowMemberBo.setMemberId(owner);
138 KRADServiceLocator.getBusinessObjectService().save(peopleFlowMemberBo);
139
140 }
141
142 }