View Javadoc
1   package org.kuali.ole.deliver.executor;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
6   import org.kuali.ole.ingest.pojo.MatchBo;
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.krad.service.BusinessObjectService;
13  import org.kuali.rice.krad.service.KRADServiceLocator;
14  import org.kuali.rice.krad.util.GlobalVariables;
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   * OleLicenseRulesEngineExecutor validates rules from krms and executes people flow action.
26   */
27  public class OleDeliverRequestEngineExecutor implements RulesEngineExecutor {
28      private List<OleDeliverRequestBo> recallList = new ArrayList<OleDeliverRequestBo>();
29      private List<OleDeliverRequestBo> holdList = new ArrayList<OleDeliverRequestBo>();
30      private List<OleDeliverRequestBo> pageList = new ArrayList<OleDeliverRequestBo>();
31      private List<OleDeliverRequestBo> requestsByBorrower = new ArrayList<OleDeliverRequestBo>();
32      private BusinessObjectService businessObjectService;
33      private static final Logger LOG = Logger.getLogger(OleDeliverRequestEngineExecutor.class);
34  
35      public BusinessObjectService getBusinessObjectService() {
36          if (businessObjectService == null) {
37              return KRADServiceLocator.getBusinessObjectService();
38          }
39          return businessObjectService;
40      }
41  
42      /**
43       * This method validates rules from krms and execute people flow action
44       *
45       * @param routeContext
46       * @param engine
47       * @return EngineResults
48       */
49      @Override
50      public EngineResults execute(RouteContext routeContext, Engine engine) {
51          EngineResults engineResult = null;
52          HashMap<String, Object> agendaValue = new HashMap<String, Object>();
53          agendaValue.put(OLEConstants.NAME_NM, OLEConstants.REQUEST_AGENDA_NM);
54          List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
55          if (agendaBos != null && agendaBos.size() > 0) {
56              AgendaBo agendaBo = agendaBos.get(0);
57              HashMap<String, String> map = new HashMap<String, String>();
58              map.put(OLEConstants.AGENDA_NAME, agendaBo.getName());
59              List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map);
60  
61              SelectionCriteria selectionCriteria =
62                      SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()),
63                              getAgendaContext(OLEConstants.REQUEST_AGENDA_NM));
64  
65              ExecutionOptions executionOptions = new ExecutionOptions();
66              executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
67  
68              Facts.Builder factBuilder = Facts.Builder.create();
69  
70              String docContent = routeContext.getDocument().getDocContent();
71  
72              String borrowerType =
73                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.PATRON_TYPE_NAME);
74              String itemType =
75                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.ITEM_TYPE_NM);
76              String requestTypeId =
77                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID);
78              String requestTypeCd =
79                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.OleDeliverRequest.REQUEST_TYPE_CD);
80              String location =
81                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.SHELVING_LOCATION);
82              String itemCollection =
83                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.ITEM_COLLECTION);
84              String itemLibrary =
85                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.ITEM_LIBRARY);
86              String itemCampus =
87                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.ITEM_CAMPUS);
88              String itemInstitution =
89                      getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.ITEM_INSTITUTION);
90              String itemUUID = getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.OleDeliverRequest.ITEM_UUID);
91              String borrowerId = getElementValue(docContent, OLEConstants.DATA_OBJECT + OLEConstants.OleDeliverRequest.BORROWER_ID);
92              Map<String, String> requestMap = new HashMap<String, String>();
93              requestMap.put(OLEConstants.ITEM_UUID, itemUUID);
94              if (requestTypeId.equals("1") || (requestTypeId.equals("2"))) {
95                  requestMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID, "1");
96                  recallList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap);
97                  requestMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID, "2");
98                  recallList.addAll((List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap));
99              } else if (requestTypeId.equals("3") || requestTypeId.equals("4")) {
100                 //  holdList = (List<OleDeliverRequestBo>)getBusinessObjectService().findMatching(OleDeliverRequestBo.class,requestMap);
101                 requestMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID, "3");
102                 holdList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap);
103                 requestMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID, "4");
104                 holdList.addAll((List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap));
105             } else if (requestTypeId.equals("5") || requestTypeId.equals("6")) {
106                 // pageList = (List<OleDeliverRequestBo>)getBusinessObjectService().findMatching(OleDeliverRequestBo.class,requestMap);
107                 requestMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID, "5");
108                 pageList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap);
109                 requestMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID, "6");
110                 pageList.addAll((List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestMap));
111             }
112             Map<String, String> requestByBorrower = new HashMap<String, String>();
113             requestByBorrower.put(OLEConstants.OleDeliverRequest.BORROWER_ID, borrowerId);
114             requestsByBorrower = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, requestByBorrower);
115             HashMap<String, Object> termValues = new HashMap<String, Object>();
116             termValues.put(OLEConstants.BORROWER_TYPE, borrowerType);
117             termValues.put(OLEConstants.ITEM_TYPE, itemType);
118             termValues.put(OLEConstants.LOCATION, location);
119             termValues.put(OLEConstants.ITEM_SHELVING, location);
120             termValues.put(OLEConstants.ITEM_COLLECTION, itemCollection);
121             termValues.put(OLEConstants.ITEM_LIBRARY, itemLibrary);
122             termValues.put(OLEConstants.ITEM_CAMPUS, itemCampus);
123             termValues.put(OLEConstants.ITEM_INSTITUTION, itemInstitution);
124             termValues.put(OLEConstants.MAX_NO_OF_RECALL_REQUEST, new Integer(recallList.size()) + 1);
125             termValues.put(OLEConstants.MAX_NO_OF_HOLD_REQUEST, new Integer(holdList.size()) + 1);
126             termValues.put(OLEConstants.MAX_NO_OF_PAGE_REQUEST, new Integer(pageList.size()) + 1);
127             // termValues.put("maxNumberOfRequestByBorrower",requestsByBorrower.size());
128             termValues.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_ID, requestTypeId);
129             termValues.put(OLEConstants.REQUEST_TYPE, requestTypeCd);
130 
131 
132             for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
133                 MatchBo matchBo = matchBoIterator.next();
134                 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
135             }
136 
137 
138             engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
139             List<String> errorMessage = (List<String>) engineResult.getAttribute(OLEConstants.ERROR_ACTION);
140             StringBuffer failures = new StringBuffer();
141             if (errorMessage != null && errorMessage.size() > 0) {
142                 int i = 1;
143                 for (String errMsg : errorMessage) {
144                     failures.append(i++ + ". " + errMsg + OLEConstants.BREAK);
145                 }
146             }
147             if (!failures.toString().isEmpty()) {
148                 GlobalVariables.getMessageMap().putError("Error", failures.toString());
149 
150             }
151 
152         }
153         return engineResult;  //To change body of implemented methods use File | Settings | File Templates.
154     }
155 
156 
157     /**
158      * This method returns ElementValue using docContent and xpathExpression..
159      *
160      * @param docContent
161      * @param xpathExpression
162      * @return value
163      */
164     private String getElementValue(String docContent, String xpathExpression) {
165         try {
166             Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
167 
168             XPath xpath = XPathHelper.newXPath();
169             String value = (String) xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
170 
171             return value;
172 
173         } catch (Exception e) {
174             LOG.error("Exception", e);
175             throw new RiceRuntimeException();
176         }
177     }
178 
179     /**
180      * This method returns SelectionContext using contextName.
181      *
182      * @param contextName
183      * @return Map
184      */
185     protected Map<String, String> getSelectionContext(String contextName) {
186         Map<String, String> selector = new HashMap<String, String>();
187         selector.put(OLEConstants.NAMESPACE_CODE, OLEConstants.OLE_NAMESPACE);
188         selector.put(OLEConstants.NAME, contextName);
189         return selector;
190     }
191 
192     /**
193      * This method returns AgendaContext using agendaName..
194      *
195      * @param agendaName
196      * @return Map
197      */
198     protected Map<String, String> getAgendaContext(String agendaName) {
199         Map<String, String> selector = new HashMap<String, String>();
200         selector.put(OLEConstants.NAME, agendaName);
201         return selector;
202     }
203 
204 
205     private void populateRequestList(String requestTypeId, String itemId) {
206 
207 
208     }
209 
210 
211 }