001package org.kuali.ole.deliver.controller; 002 003import org.apache.commons.collections.CollectionUtils; 004import org.apache.commons.lang3.StringUtils; 005import org.apache.log4j.Logger; 006import org.kuali.ole.OLEConstants; 007import org.kuali.ole.deliver.bo.OlePatronDocument; 008import org.kuali.ole.deliver.bo.OleProxyPatronDocument; 009import org.kuali.ole.deliver.controller.checkout.CircUtilController; 010import org.kuali.ole.deliver.drools.DroolsConstants; 011import org.kuali.ole.deliver.drools.DroolsExchange; 012import org.kuali.ole.deliver.form.OleLoanForm; 013import org.kuali.ole.deliver.util.DroolsResponse; 014import org.kuali.ole.deliver.util.ErrorMessage; 015import org.kuali.ole.deliver.util.OlePatronRecordUtil; 016import org.kuali.ole.sys.context.SpringContext; 017import org.kuali.ole.utility.OleStopWatch; 018import org.kuali.rice.core.api.config.property.ConfigContext; 019 020import java.util.Iterator; 021import java.util.List; 022 023/** 024 * Created by pvsubrah on 6/4/15. 025 */ 026 027public abstract class PatronLookupCircBaseController extends CircUtilController { 028 029 private static final Logger LOG = Logger.getLogger(PatronLookupCircBaseController.class); 030 031 public abstract OlePatronDocument getPatronDocument(DroolsExchange droolsExchange); 032 033 public abstract void setPatronDocument(DroolsExchange droolsExchange, OlePatronDocument patronDocument); 034 035 public abstract String getPatronBarcode(DroolsExchange droolsExchange); 036 037 public abstract void setErrorMessage(DroolsExchange droolsExchange, ErrorMessage errorMessage); 038 039 public abstract void setProxyCheckDone(DroolsExchange droolsExchange, boolean proxyCheckDone); 040 041 private OlePatronRecordUtil olePatronRecordUtil; 042 043 public DroolsResponse searchPatron(DroolsExchange droolsExchange) { 044 DroolsResponse droolsResponse = null; 045 OleStopWatch oleStopWatch = new OleStopWatch(); 046 oleStopWatch.start(); 047 try { 048 OlePatronDocument patronDocument = getPatronDocument(droolsExchange); 049 if (null != patronDocument && StringUtils.isBlank(patronDocument.getBarcode())) { 050 patronDocument = getOlePatronRecordUtil().getPatronRecordByBarcode(getPatronBarcode(droolsExchange)); 051 patronDocument.setRequestedItemRecordsCount(patronDocument.getOleDeliverRequestBos().size()); 052 patronDocument.setTempCirculationHistoryCount(patronDocument.getOleTemporaryCirculationHistoryRecords().size()); 053 if(StringUtils.isBlank(patronDocument.getPhoneNumber())){ 054 patronDocument.setPhoneNumber(patronDocument.getOlePatronEntityViewBo() != null ? patronDocument.getOlePatronEntityViewBo().getPhoneNumber() : ""); 055 } 056 setPatronDocument(droolsExchange, patronDocument); 057 } 058 String[] expectedRules = {}; 059 droolsResponse = processRules(droolsExchange, patronDocument, expectedRules); 060 } catch (Exception e) { 061 droolsResponse = new DroolsResponse(); 062 droolsResponse.addErrorMessage(OLEConstants.PTRN_BARCD_NOT_EXT + OLEConstants.PTRN_START_LINK + ConfigContext.getCurrentContextConfig().getProperty("ole.fs.url.base") + OLEConstants.PTRN_END_LINK); 063 droolsResponse.addErrorMessageCode(DroolsConstants.GENERAL_MESSAGE_FLAG); 064 LOG.error("Exception while search patron time", e); 065 return droolsResponse; 066 } 067 068 oleStopWatch.end(); 069 LOG.info("Time taken to look up a patron:" + oleStopWatch.getTotalTime() + " ms"); 070 return droolsResponse; 071 } 072 073 public DroolsResponse processPatronSearchPostProxyHandling(DroolsExchange droolsExchange) { 074 DroolsResponse droolsResponse = null; 075 setProxyCheckDone(droolsExchange, true); 076 077 OlePatronDocument patronDocument = getPatronDocument(droolsExchange); 078 Boolean selfCheckOut = patronDocument.isCheckoutForSelf(); 079 if (!selfCheckOut) { 080 List<OleProxyPatronDocument> oleProxyPatronDocumentList = patronDocument.getOleProxyPatronDocumentList(); 081 if (!CollectionUtils.isEmpty(oleProxyPatronDocumentList)) { 082 OlePatronDocument realOlePatronDocument = identifyOlePatronDocumentForCheckout(oleProxyPatronDocumentList); 083 if (null != realOlePatronDocument) { 084 patronDocument.setSelectedProxyForPatron(realOlePatronDocument); 085 String[] expectedRules = {}; 086 droolsResponse = processRules(droolsExchange,realOlePatronDocument, expectedRules); 087 } 088 } 089 } 090 091 return droolsResponse; 092 } 093 094 /** 095 * 096 * 097 * @param droolsExchange 098 * @param olePatronDocument 099 * @param expectedRules 100 * @return 101 * 102 * OlePatronDocument is passed because it could be either the borrower whose barcode was scanned 103 * or the Proxy Borrower; Hence we cannot retrieve it from the form. 104 */ 105 public DroolsResponse processRules(DroolsExchange droolsExchange, OlePatronDocument olePatronDocument, String[] expectedRules) { 106 DroolsResponse droolsResponse = getOlePatronRecordUtil().fireRules(olePatronDocument, expectedRules); 107 setErrorMessage(droolsExchange, droolsResponse.getErrorMessage()); 108 return droolsResponse; 109 110 } 111 112 private OlePatronDocument identifyOlePatronDocumentForCheckout(List<OleProxyPatronDocument> oleProxyPatronDocuments) { 113 for (Iterator<OleProxyPatronDocument> iterator = oleProxyPatronDocuments.iterator(); iterator.hasNext(); ) { 114 OleProxyPatronDocument proxyPatronDocument = iterator.next(); 115 OlePatronDocument olePatronDocument = proxyPatronDocument.getOlePatronDocument(); 116 if (olePatronDocument.isCheckoutForSelf()) { 117 return olePatronDocument; 118 } 119 } 120 return null; 121 } 122 123 public boolean hasProxyPatrons(DroolsExchange droolsExchange) { 124 return CollectionUtils.isNotEmpty(getPatronDocument(droolsExchange).getOleProxyPatronDocumentList()); 125 } 126 127 //TODO: Do we need to handle lost Patron? 128 private void handleLostPatron(OleLoanForm oleLoanForm) { 129 if (oleLoanForm.getOlePatronDocument().isLostPatron()) { 130 oleLoanForm.setBlockUser(true); 131 } else { 132 oleLoanForm.setBlockUser(false); 133 } 134 } 135 136 public OlePatronRecordUtil getOlePatronRecordUtil() { 137 if (null == olePatronRecordUtil) { 138 olePatronRecordUtil = (OlePatronRecordUtil) SpringContext.getBean("olePatronRecordUtil"); 139 } 140 return olePatronRecordUtil; 141 } 142}