1 package org.kuali.ole.ncip.service.impl;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.deliver.bo.OLEDeliverNotice;
7 import org.kuali.ole.deliver.bo.OleLoanDocument;
8 import org.kuali.ole.deliver.bo.OlePatronDocument;
9 import org.kuali.ole.deliver.controller.checkout.CircUtilController;
10 import org.kuali.ole.deliver.util.*;
11 import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
12 import org.kuali.rice.core.api.config.property.ConfigContext;
13 import org.kuali.rice.krad.service.BusinessObjectService;
14 import org.kuali.rice.krad.service.KRADServiceLocator;
15
16 import java.sql.Timestamp;
17 import java.util.*;
18 import java.util.concurrent.Callable;
19
20
21
22
23 public class RenewItemExecutor implements Callable {
24
25 private static final Logger LOG = Logger.getLogger(RenewItemExecutor.class);
26
27 private final String itemBarcode;
28 private final OlePatronDocument olePatronDocument;
29
30 private BusinessObjectService businessObjectService;
31 private CircUtilController circUtilController;
32 private OleItemRecordForCirc oleItemRecordForCirc;
33
34
35 public RenewItemExecutor(OlePatronDocument olePatronDocument, String itemBarcode) {
36 this.olePatronDocument = olePatronDocument;
37 this.itemBarcode = itemBarcode;
38 }
39
40 @Override
41 public Object call() throws Exception {
42 long startTime = System.currentTimeMillis();
43 DroolsResponse droolsResponse = renew();
44 long endTime = System.currentTimeMillis();
45 LOG.info("Time taken to process renew item for barcode: " + itemBarcode + " is: " + (endTime - startTime) + "ms");
46 return droolsResponse;
47 }
48
49 public DroolsResponse renew() {
50 DroolsResponse finalDroolResponse = null;
51 List<OleLoanDocument> loanDocuments = getLoanDocuments(itemBarcode, olePatronDocument);
52
53 if (loanDocuments.size() > 0) {
54 OleLoanDocument oleLoanDocument = loanDocuments.get(0);
55 boolean isIndefiniteDueDate = IsIndefiniteDueDate(oleLoanDocument);
56 if (!isIndefiniteDueDate) {
57 Timestamp loanDueDate = oleLoanDocument.getLoanDueDate();
58 Date pastDueDate = oleLoanDocument.getPastDueDate();
59
60 oleLoanDocument.setPastDueDate(oleLoanDocument.getLoanDueDate());
61
62 ItemRecord itemRecord = getCircUtilController().getItemRecordByBarcode(itemBarcode);
63 OleItemRecordForCirc oleItemRecordForCirc = getOleItemRecordForCirc(itemRecord);
64 NoticeInfo noticeInfo = new NoticeInfo();
65
66 finalDroolResponse = fireRules(olePatronDocument, oleLoanDocument, oleItemRecordForCirc, noticeInfo);
67
68 if (finalDroolResponse.isRuleMatched()) {
69 if (StringUtils.isBlank(finalDroolResponse.retrieveErrorMessage())) {
70 boolean pastAndRenewDueDateSame = false;
71 try {
72 pastAndRenewDueDateSame = isPastAndRenewDueDateSame(oleLoanDocument);
73 if (!pastAndRenewDueDateSame) {
74 Integer numRenewals = IncrementRenewalCount(oleLoanDocument);
75 oleLoanDocument.setNumberOfRenewals(numRenewals.toString());
76 oleLoanDocument.getDeliverNotices().clear();
77 List<OLEDeliverNotice> oleDeliverNotices = getCircUtilController().processNotices(oleLoanDocument, oleItemRecordForCirc.getItemRecord());
78 oleLoanDocument.setDeliverNotices(oleDeliverNotices);
79 if (null != oleLoanDocument.getLoanId()) {
80 finalDroolResponse.setSucessMessage("Successfully Renewed");
81 finalDroolResponse.getDroolsExchange().addToContext(oleLoanDocument.getItemUuid(), oleLoanDocument);
82 }
83 } else {
84 oleLoanDocument.setLoanDueDate(loanDueDate);
85 oleLoanDocument.setPastDueDate(pastDueDate);
86 oleLoanDocument.setErrorMessage("The past and the current due data are same and hence item wasn't renewed.");
87 finalDroolResponse.getDroolsExchange().addToContext(itemBarcode, oleLoanDocument);
88 }
89 } catch (Exception e) {
90 oleLoanDocument.setLoanDueDate(loanDueDate);
91 oleLoanDocument.setPastDueDate(pastDueDate);
92 if (e.getMessage().equalsIgnoreCase("No Fixed Due Date found for the renewal policy")) {
93 oleLoanDocument.setErrorMessage("Item wasn't renewed. Invalid Fixed Due Date Mapping.");
94 finalDroolResponse.getDroolsExchange().addToContext(itemBarcode, oleLoanDocument);
95 } else {
96 oleLoanDocument.setErrorMessage("Item wasn't renewed. " + e.getMessage());
97 finalDroolResponse.getDroolsExchange().addToContext(itemBarcode, oleLoanDocument);
98 }
99 }
100 } else {
101 oleLoanDocument.setLoanDueDate(loanDueDate);
102 oleLoanDocument.setPastDueDate(pastDueDate);
103 oleLoanDocument.setErrorMessage(finalDroolResponse.retrieveErrorMessage());
104 finalDroolResponse.getDroolsExchange().addToContext(itemBarcode, oleLoanDocument);
105 }
106 } else {
107 oleLoanDocument.setLoanDueDate(loanDueDate);
108 oleLoanDocument.setPastDueDate(pastDueDate);
109 oleLoanDocument.setErrorMessage("Item wasn't renewed as no circulation policy was found!");
110 finalDroolResponse.getDroolsExchange().addToContext(itemBarcode, oleLoanDocument);
111 }
112 }else{
113 finalDroolResponse = new DroolsResponse();
114 oleLoanDocument.setErrorMessage("Item wasn't renewed. Items on indefinite loan do not need to be renewed.");
115 finalDroolResponse.getDroolsExchange().addToContext(itemBarcode, oleLoanDocument);
116 }
117
118 } else {
119 finalDroolResponse = new DroolsResponse();
120 OleLoanDocument oleLoanDocument = new OleLoanDocument();
121 oleLoanDocument.setErrorMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITM_NT_LOAN) +" - Item Barcode("+itemBarcode+")");
122 oleLoanDocument.setItemId(itemBarcode);
123 finalDroolResponse.getDroolsExchange().addToContext(itemBarcode, oleLoanDocument);
124 }
125
126 return finalDroolResponse;
127 }
128
129 private boolean IsIndefiniteDueDate(OleLoanDocument oleLoanDocument) {
130 return oleLoanDocument.getLoanDueDate() == null ? true : false ;
131 }
132
133 private boolean isPastAndRenewDueDateSame(OleLoanDocument oleLoanDocument) throws Exception {
134 Timestamp pastLoanDueDate = oleLoanDocument.getLoanDueDate();
135 Date newLoanDueDate = oleLoanDocument.getPastDueDate();
136 if (pastLoanDueDate != null) {
137 return (new Date(pastLoanDueDate.getTime()).compareTo(newLoanDueDate) == 0 ? true : false);
138 } else {
139 throw new Exception("No Fixed Due Date found for the renewal policy");
140 }
141 }
142
143 private List<OleLoanDocument> getLoanDocuments(String itemBarcode, OlePatronDocument olePatronDocument) {
144 HashMap<String, Object> criteriaMap = new HashMap<>();
145 criteriaMap.put("patronId", olePatronDocument.getOlePatronId());
146 criteriaMap.put("itemId", itemBarcode);
147 List<OleLoanDocument> loanDocuments =
148 (List<OleLoanDocument>) getBusinessObjectService().findMatching(OleLoanDocument.class, criteriaMap);
149 return loanDocuments;
150 }
151
152 private Integer IncrementRenewalCount(OleLoanDocument oleLoanDocument) {
153 Integer numRenewals = 0;
154 String numberOfRenewalsPreviously = oleLoanDocument.getNumberOfRenewals();
155 if (StringUtils.isNotBlank(numberOfRenewalsPreviously)) {
156 numRenewals = new Integer(numberOfRenewalsPreviously) + 1;
157 }
158 return numRenewals;
159 }
160
161 private DroolsResponse fireRules(OlePatronDocument olePatronDocument, OleLoanDocument oleLoanDocument, OleItemRecordForCirc oleItemRecordForCirc, NoticeInfo noticeInfo) {
162 DroolsResponse droolsResponse = new DroolsResponse();
163 List<Object> facts = new ArrayList<>();
164 facts.add(oleLoanDocument);
165 facts.add(olePatronDocument);
166 facts.add(oleItemRecordForCirc);
167 facts.add(droolsResponse);
168 facts.add(noticeInfo);
169 getCircUtilController().fireRules(facts, null, "renewal validation");
170 return droolsResponse;
171 }
172
173 private OleItemRecordForCirc getOleItemRecordForCirc(ItemRecord itemRecord) {
174 if (null == oleItemRecordForCirc) {
175 oleItemRecordForCirc = ItemInfoUtil.getInstance().getOleItemRecordForCirc(itemRecord, null);
176 }
177 return oleItemRecordForCirc;
178 }
179
180 private CircUtilController getCircUtilController() {
181 if (null == circUtilController) {
182 circUtilController = new CircUtilController();
183 }
184 return circUtilController;
185 }
186
187
188 public BusinessObjectService getBusinessObjectService() {
189 if (null == businessObjectService) {
190 businessObjectService = KRADServiceLocator.getBusinessObjectService();
191 }
192 return businessObjectService;
193 }
194
195 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
196 this.businessObjectService = businessObjectService;
197 }
198
199
200 public void setCircUtilController(CircUtilController circUtilController) {
201 this.circUtilController = circUtilController;
202 }
203
204 public void setOleItemRecordForCirc(OleItemRecordForCirc oleItemRecordForCirc) {
205 this.oleItemRecordForCirc = oleItemRecordForCirc;
206 }
207 }