1 package org.kuali.ole.deliver.controller;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.commons.lang3.StringUtils;
5 import org.apache.log4j.Logger;
6 import org.kuali.ole.deliver.bo.OlePatronDocument;
7 import org.kuali.ole.deliver.bo.OleProxyPatronDocument;
8 import org.kuali.ole.deliver.controller.checkout.CircUtilController;
9 import org.kuali.ole.deliver.drools.DroolsConstants;
10 import org.kuali.ole.deliver.form.CircForm;
11 import org.kuali.ole.deliver.form.OleLoanForm;
12 import org.kuali.ole.deliver.util.ErrorMessage;
13 import org.kuali.ole.deliver.util.OlePatronRecordUtil;
14 import org.kuali.ole.sys.context.SpringContext;
15 import org.kuali.ole.utility.OleStopWatch;
16 import org.kuali.rice.krad.web.form.UifFormBase;
17
18 import java.util.Iterator;
19 import java.util.List;
20
21
22
23
24
25 public class PatronLookupCircController extends CircUtilController {
26
27 private static final Logger LOG = Logger.getLogger(PatronLookupCircController.class);
28 private OlePatronRecordUtil olePatronRecordUtil;
29
30 public ErrorMessage searchPatron(UifFormBase form) {
31 ErrorMessage errorMessage = null;
32 OleStopWatch oleStopWatch = new OleStopWatch();
33 oleStopWatch.start();
34 CircForm circForm = (CircForm) form;
35 try {
36 OlePatronDocument patronDocument = circForm.getPatronDocument();
37 if (null != patronDocument && StringUtils.isBlank(patronDocument.getBarcode())) {
38 patronDocument = getOlePatronRecordUtil().getPatronRecordByBarcode(circForm
39 .getPatronBarcode());
40 circForm.setPatronDocument(patronDocument);
41 }
42 String[] expectedRules = {};
43 errorMessage = processRules(circForm, patronDocument, expectedRules);
44 } catch (Exception e) {
45 errorMessage = new ErrorMessage();
46 errorMessage.setErrorMessage("Invalid Patron Barcode. Please try again!");
47 errorMessage.setErrorCode(DroolsConstants.GENERAL_MESSAGE_FLAG);
48 LOG.error("Exception while search patron time", e);
49 return errorMessage;
50 }
51 oleStopWatch.end();
52 LOG.info("Time taken to look up a patron:" + oleStopWatch.getTotalTime() + " ms");
53 return errorMessage;
54 }
55
56 public ErrorMessage processPatronSearchPostProxyHandling(UifFormBase form) {
57 CircForm circForm = (CircForm) form;
58 ErrorMessage errorMessage = null;
59 circForm.setProxyCheckDone(true);
60
61 OlePatronDocument patronDocument = circForm.getPatronDocument();
62 Boolean selfCheckOut = patronDocument.isCheckoutForSelf();
63 if (!selfCheckOut) {
64 List<OleProxyPatronDocument> oleProxyPatronDocumentList = patronDocument.getOleProxyPatronDocumentList();
65 if (!CollectionUtils.isEmpty(oleProxyPatronDocumentList)) {
66 OlePatronDocument realOlePatronDocument = identifyOlePatronDocumentForCheckout(oleProxyPatronDocumentList);
67 if (null != realOlePatronDocument) {
68 patronDocument.setSelectedProxyForPatron(realOlePatronDocument);
69 String[] expectedRules = {};
70 errorMessage = processRules(circForm,realOlePatronDocument, expectedRules);
71 }
72 }
73 }
74
75 return errorMessage;
76 }
77
78
79
80
81
82
83
84
85
86
87
88 public ErrorMessage processRules(CircForm circForm, OlePatronDocument olePatronDocument, String[] expectedRules) {
89 ErrorMessage errorMessage = getOlePatronRecordUtil().fireRules(olePatronDocument, expectedRules);
90 circForm.setErrorMessage(errorMessage);
91 return errorMessage;
92
93 }
94
95 private OlePatronDocument identifyOlePatronDocumentForCheckout(List<OleProxyPatronDocument> oleProxyPatronDocuments) {
96 for (Iterator<OleProxyPatronDocument> iterator = oleProxyPatronDocuments.iterator(); iterator.hasNext(); ) {
97 OleProxyPatronDocument proxyPatronDocument = iterator.next();
98 OlePatronDocument olePatronDocument = proxyPatronDocument.getOlePatronDocument();
99 if (olePatronDocument.isCheckoutForSelf()) {
100 return olePatronDocument;
101 }
102 }
103 return null;
104 }
105
106 public boolean hasProxyPatrons(UifFormBase form) {
107 CircForm circForm = (CircForm) form;
108 return CollectionUtils.isNotEmpty(circForm.getPatronDocument().getOleProxyPatronDocumentList());
109 }
110
111
112 private void handleLostPatron(OleLoanForm oleLoanForm) {
113 if (oleLoanForm.getOlePatronDocument().isLostPatron()) {
114 oleLoanForm.setBlockUser(true);
115 } else {
116 oleLoanForm.setBlockUser(false);
117 }
118 }
119
120 public OlePatronRecordUtil getOlePatronRecordUtil() {
121 if (null == olePatronRecordUtil) {
122 olePatronRecordUtil = (OlePatronRecordUtil) SpringContext.getBean("olePatronRecordUtil");
123 }
124 return olePatronRecordUtil;
125 }
126
127
128 }