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.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
37 @Override
38 public EngineResults execute(RouteContext routeContext, Engine engine) {
39
40 HashMap<String, Object> agendaValue = new HashMap<String, Object>();
41 agendaValue.put("nm", "OWNER_POLICY");
42 List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
43 AgendaBo agendaBo = agendaBos.get(0);
44 HashMap<String, String> map = new HashMap<String, String>();
45 map.put("AGENDA_NAME", agendaBo.getName());
46 List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map);
47
48 SelectionCriteria selectionCriteria =
49 SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()), getAgendaContext("OWNER_POLICY"));
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 String owner =
61 getElementValue(docContent, "//newMaintainableObject/dataObject/assignee");
62
63 String oleLicenseRequestStatusCode =
64 getElementValue(docContent, "//newMaintainableObject/dataObject/oleLicenseRequestStatus/code");
65
66 HashMap<String, Object> termValues = new HashMap<String, Object>();
67 if ((licenseRequestWorkflowTypeCode.equalsIgnoreCase("SIGO")) ||
68 (licenseRequestWorkflowTypeCode.equalsIgnoreCase("RVWO"))
69 || licenseRequestWorkflowTypeCode.equalsIgnoreCase("APPO") && !oleLicenseRequestStatusCode.equalsIgnoreCase("LC")) {
70 termValues.put("owner", "route");
71 } else {
72 termValues.put("owner", "NIL");
73 }
74
75 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
76 MatchBo matchBo = matchBoIterator.next();
77 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
78 }
79 EngineResults engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
80 String peopleFlowsSelected = (String) engineResult.getAttribute("peopleFlowsSelected");
81 String peopleFlwid = peopleFlowsSelected != null ? peopleFlowsSelected.split(":")[1] : null;
82 if (peopleFlwid != null) createPeopleFlowMemberToOwner(owner, peopleFlwid);
83 return engineResult;
84 }
85
86
87
88
89
90
91
92
93 private String getElementValue(String docContent, String xpathExpression) {
94 try {
95 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
96
97 XPath xpath = XPathHelper.newXPath();
98 String value = (String) xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
99
100 return value;
101
102 } catch (Exception e) {
103 throw new RiceRuntimeException();
104 }
105 }
106
107
108
109
110
111
112
113 protected Map<String, String> getSelectionContext(String contextName) {
114 Map<String, String> selector = new HashMap<String, String>();
115 selector.put(OLEConstants.NAMESPACE_CODE_SELECTOR, "OLE");
116 selector.put(OLEConstants.NAME_SELECTOR, contextName);
117 return selector;
118 }
119
120
121
122
123
124
125
126 protected Map<String, String> getAgendaContext(String agendaName) {
127 Map<String, String> selector = new HashMap<String, String>();
128 selector.put(OLEConstants.NAME_SELECTOR, agendaName);
129 return selector;
130 }
131
132
133
134
135
136
137
138 private void createPeopleFlowMemberToOwner(String owner, String peopleFlwId) {
139 PeopleFlowBo peopleFlow = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(PeopleFlowBo.class, peopleFlwId);
140 peopleFlow.getMembers();
141 PeopleFlowMemberBo peopleFlowMemberBo = peopleFlow.getMembers().get(0);
142 peopleFlowMemberBo.setMemberId(owner);
143 KRADServiceLocator.getBusinessObjectService().save(peopleFlowMemberBo);
144
145 }
146
147 }