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