1 package org.kuali.ole.deliver.service.impl;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.DataCarrierService;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.OLEParameterConstants;
7 import org.kuali.ole.deliver.bo.OLEDeliverNotice;
8 import org.kuali.ole.deliver.bo.OleLoanDocument;
9 import org.kuali.ole.deliver.calendar.service.DateUtil;
10 import org.kuali.ole.deliver.processor.LoanProcessor;
11 import org.kuali.ole.deliver.service.OLEDeliverNoticeHelperService;
12 import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
13 import org.kuali.ole.deliver.service.OleLoanDocumentDaoOjb;
14 import org.kuali.ole.sys.context.SpringContext;
15 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
16 import org.kuali.rice.krad.service.BusinessObjectService;
17 import org.kuali.rice.krad.service.KRADServiceLocator;
18 import org.kuali.rice.krms.api.engine.EngineResults;
19
20 import java.math.BigDecimal;
21 import java.sql.Timestamp;
22 import java.util.*;
23
24
25
26
27 public class OLEDeliverNoticeHelperServiceImpl implements OLEDeliverNoticeHelperService {
28 private static final Logger LOG = Logger.getLogger(OLEDeliverNoticeHelperServiceImpl.class);
29
30 private BusinessObjectService businessObjectService;
31 private LoanProcessor loanProcessor;
32 private OleLoanDocumentDaoOjb oleLoanDocumentDaoOjb;
33 private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService;
34
35 private String overdueNoticeType ;
36 private String courtesyNoticeType ;
37 private String overdueNoticeInterval;
38 private String courtesyNoticeInterval;
39
40
41
42
43
44
45 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
46 this.businessObjectService = businessObjectService;
47 }
48
49
50
51
52
53
54 public BusinessObjectService getBusinessObjectService() {
55 if (null == businessObjectService) {
56 businessObjectService = KRADServiceLocator.getBusinessObjectService();
57 }
58 return businessObjectService;
59 }
60
61
62
63
64 public void setLoanProcessor(LoanProcessor loanProcessor) {
65 this.loanProcessor = loanProcessor;
66 }
67
68
69
70
71
72
73 private LoanProcessor getLoanProcessor() {
74 if (loanProcessor == null) {
75 loanProcessor = SpringContext.getBean(LoanProcessor.class);
76 }
77 return loanProcessor;
78 }
79
80
81
82
83
84 public String getOverdueNoticeType() {
85 if(overdueNoticeType == null){
86 overdueNoticeType = loanProcessor.getParameter(OLEParameterConstants.OVERDUE_NOTICE_TYPE);
87 }
88 return overdueNoticeType;
89 }
90
91
92
93
94
95 public void setOverdueNoticeType(String overdueNoticeType) {
96 this.overdueNoticeType = overdueNoticeType;
97 }
98
99
100
101
102
103 public String getCourtesyNoticeType() {
104 if(courtesyNoticeType == null){
105 courtesyNoticeType =loanProcessor.getParameter(OLEParameterConstants.COURTESY_NOTICE_TYPE);
106 }
107 return courtesyNoticeType;
108 }
109
110
111
112
113
114 public void setCourtesyNoticeType(String courtesyNoticeType) {
115 this.courtesyNoticeType = courtesyNoticeType;
116 }
117
118
119
120
121
122 public String getOverdueNoticeInterval() {
123 if(overdueNoticeInterval == null){
124 overdueNoticeInterval =loanProcessor.getParameter(OLEConstants.OVERDUE_NOTICE_INTER);
125 }
126 return overdueNoticeInterval;
127 }
128
129
130
131
132
133 public void setOverdueNoticeInterval(String overdueNoticeInterval) {
134 this.overdueNoticeInterval = overdueNoticeInterval;
135 }
136
137
138
139
140
141 public String getCourtesyNoticeInterval() {
142 if(courtesyNoticeInterval == null){
143 courtesyNoticeInterval =loanProcessor.getParameter(OLEParameterConstants.COURTESY_NOTICE_INTER);
144 }
145 return courtesyNoticeInterval;
146 }
147
148
149
150
151
152 public void setCourtesyNoticeInterval(String courtesyNoticeInterval) {
153 this.courtesyNoticeInterval = courtesyNoticeInterval;
154 }
155
156 public OleLoanDocumentDaoOjb getOleLoanDocumentDaoOjb() {
157 if(oleLoanDocumentDaoOjb == null){
158 oleLoanDocumentDaoOjb = (OleLoanDocumentDaoOjb) SpringContext.getBean("oleLoanDao");
159 }
160 return oleLoanDocumentDaoOjb;
161 }
162
163 public void setOleLoanDocumentDaoOjb(OleLoanDocumentDaoOjb oleLoanDocumentDaoOjb) {
164 this.oleLoanDocumentDaoOjb = oleLoanDocumentDaoOjb;
165 }
166
167 public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
168 if (oleDeliverRequestDocumentHelperService == null) {
169 oleDeliverRequestDocumentHelperService = SpringContext.getBean(OleDeliverRequestDocumentHelperServiceImpl.class);
170 }
171 return oleDeliverRequestDocumentHelperService;
172 }
173
174
175 public void setOleDeliverRequestDocumentHelperService(OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService) {
176 this.oleDeliverRequestDocumentHelperService = oleDeliverRequestDocumentHelperService;
177 }
178
179
180
181
182
183
184 @Override
185 public List<OLEDeliverNotice> getDeliverNotices(String loanId) {
186 Map noticeMap = new HashMap();
187 noticeMap.put("loanId",loanId);
188 List<OLEDeliverNotice> oleDeliverNotices = (List<OLEDeliverNotice> )getBusinessObjectService().findMatching(OLEDeliverNotice.class,noticeMap);
189 return oleDeliverNotices;
190 }
191
192
193
194
195
196
197 @Override
198 public List<OLEDeliverNotice> updateDeliverNotices(String loanId) {
199 List<OLEDeliverNotice> oleDeliverNotices = getDeliverNotices(loanId);
200 for(OLEDeliverNotice deliverNotice : oleDeliverNotices){
201
202 }
203 return oleDeliverNotices;
204 }
205
206
207
208
209
210 @Override
211 public void deleteDeliverNotices(String loanId) {
212 Map noticeMap = new HashMap();
213 noticeMap.put("loanId",loanId);
214 getBusinessObjectService().deleteMatching(OLEDeliverNotice.class, noticeMap);
215 }
216
217
218
219 public void generateDeliverNotices(OleLoanDocument oleLoanDocument) throws Exception{
220 generateDeliverNotices(oleLoanDocument.getPatronId(), oleLoanDocument.getItemUuid(),
221 oleLoanDocument.getOleCirculationDesk()!=null ? oleLoanDocument.getOleCirculationDesk().getCirculationDeskCode() : null,
222 oleLoanDocument.getBorrowerTypeCode(),oleLoanDocument.getItemTypeName(), oleLoanDocument.getItemStatus(),
223 oleLoanDocument.isClaimsReturnedIndicator() ? OLEConstants.TRUE : OLEConstants.FALSE,
224 oleLoanDocument.getRepaymentFeePatronBillId() != null ? OLEConstants.TRUE : OLEConstants.FALSE,
225 oleLoanDocument.getItemLocation(), oleLoanDocument.getItemCollection(), oleLoanDocument.getItemLibrary(),
226 oleLoanDocument.getItemCampus(), oleLoanDocument.getItemInstitution(), oleLoanDocument.getLoanDueDate(),oleLoanDocument.getLoanId());
227 }
228 public void generateDeliverNotices(String patronId,String itemId, String deskLocation,String borrowerType,
229 String itemType,String itemStatus,String claimsReturned,String replacementBill,
230 String itemShelving,String itemCollection,String itemLibrary,String itemCampus,
231 String itemInstitution,Date itemDueDate,String loanId) throws Exception{
232 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
233
234 String agendaName = OLEConstants.NOTICE_AGENDA_NM;
235
236 dataCarrierService.removeData(patronId+itemId);
237 HashMap<String, Object> termValues = new HashMap<String, Object>();
238 termValues.put(OLEConstants.BORROWER_TYPE, borrowerType);
239 termValues.put(OLEConstants.ITEM_TYPE, itemType);
240
241 termValues.put(OLEConstants.ITEM_STATUS, itemStatus);
242
243 termValues.put(OLEConstants.OleDeliverRequest.CLAIM_RETURNED, claimsReturned);
244 termValues.put(OLEConstants.OleDeliverRequest.REPLACEMENT_FEE_PATRON_BILL, replacementBill);
245
246 termValues.put(OLEConstants.ITEM_SHELVING, itemShelving);
247 termValues.put(OLEConstants.ITEM_COLLECTION, itemCollection);
248 termValues.put(OLEConstants.ITEM_LIBRARY,itemLibrary);
249 termValues.put(OLEConstants.ITEM_CAMPUS, itemCampus);
250 termValues.put(OLEConstants.ITEM_INSTITUTION, itemInstitution);
251
252 termValues.put(OLEConstants.PATRON_ID_POLICY, patronId);
253 termValues.put(OLEConstants.ITEM_ID_POLICY, itemId);
254
255 termValues.put(OLEConstants.ITEMS_DUE_DATE, itemDueDate);
256
257 termValues.put(OLEConstants.DESK_LOCATION, deskLocation);
258
259 if (LOG.isDebugEnabled()) {
260 LOG.debug("termValues.toString()" + termValues.toString());
261 }
262 EngineResults engineResults = getLoanProcessor().getEngineResults(agendaName, termValues);
263 dataCarrierService.removeData(patronId+itemId);
264
265 List<String> errorMessage = (List<String>) engineResults.getAttribute(OLEConstants.ERROR_ACTION);
266 if (errorMessage != null && LOG.isDebugEnabled()) {
267 LOG.debug("errorMessage" + errorMessage.toString());
268 }
269
270 List<OLEDeliverNotice> deliverNotices = (List<OLEDeliverNotice>) engineResults.getAttribute("deliverNotices");
271 if(deliverNotices!=null){
272 for(OLEDeliverNotice deliverNotice : deliverNotices){
273 deliverNotice.setLoanId(loanId);
274 deliverNotice.setPatronId(patronId);
275 }
276 getBusinessObjectService().save(deliverNotices);
277 }
278 }
279
280
281 public void generateDeliverNoticesUsingKRMSValues(List<OLEDeliverNotice> deliverNotices,Timestamp dueDate,
282 String noticeType ,String noticeFormat,
283 String numberOfOverdueToBeSent, String intervalToGenerateNotice,
284 String replacementBill){
285
286 if(deliverNotices==null){
287 deliverNotices = new ArrayList<>();
288 }
289
290 Integer lostCount = 0;
291
292 if(noticeType!= null && noticeType.equalsIgnoreCase(OLEConstants.NOTICE_COURTESY)){
293 OLEDeliverNotice courtesyNotice = new OLEDeliverNotice();
294 courtesyNotice.setNoticeType(noticeType);
295 if(noticeFormat!=null && !noticeFormat.trim().isEmpty()){
296 courtesyNotice.setNoticeSendType(noticeFormat);
297 }else{
298 courtesyNotice.setNoticeSendType(getCourtesyNoticeType());
299 }
300 Timestamp noticeToBeSentDate=null;
301 Integer interval;
302 if(intervalToGenerateNotice!=null && !intervalToGenerateNotice.trim().isEmpty()){
303 interval = intervalToGenerateNotice!=null ? -Integer.parseInt(intervalToGenerateNotice) : null;
304 }else{
305 interval = getCourtesyNoticeInterval()!=null ? -Integer.parseInt(getCourtesyNoticeInterval()) : null;
306 }
307 noticeToBeSentDate=calculateNoticeToBeSentDate(interval,dueDate,1);
308 courtesyNotice.setNoticeToBeSendDate(noticeToBeSentDate);
309 deliverNotices.add(courtesyNotice);
310 }
311 else if(noticeType!= null && noticeType.equalsIgnoreCase(OLEConstants.NOTICE_OVERDUE)){
312 if(numberOfOverdueToBeSent!=null)
313 for(int i = 1; i<= Integer.parseInt(numberOfOverdueToBeSent);i++ ){
314 OLEDeliverNotice overdueNotice = new OLEDeliverNotice();
315 overdueNotice.setNoticeType(noticeType);
316 if(noticeFormat!=null && !noticeFormat.trim().isEmpty()){
317 overdueNotice.setNoticeSendType(noticeFormat);
318 }else{
319 overdueNotice.setNoticeSendType(getOverdueNoticeType());
320 }
321 Timestamp noticeToBeSentDate=null;
322 Integer interval;
323 if(intervalToGenerateNotice!=null && !intervalToGenerateNotice.trim().isEmpty()){
324 interval = intervalToGenerateNotice!=null ? Integer.parseInt(intervalToGenerateNotice) : null;
325 }else{
326 interval = getOverdueNoticeInterval()!=null ? Integer.parseInt(getOverdueNoticeInterval()) : null;
327 }
328 noticeToBeSentDate=calculateNoticeToBeSentDate(interval,dueDate,i);
329 lostCount = i;
330 overdueNotice.setNoticeToBeSendDate(noticeToBeSentDate);
331 deliverNotices.add(overdueNotice);
332 }
333 }
334 if(replacementBill!=null && !replacementBill.trim().isEmpty()){
335 OLEDeliverNotice lostNotice = new OLEDeliverNotice();
336 lostNotice.setNoticeType(OLEConstants.NOTICE_LOST);
337 lostNotice.setReplacementFeeAmount(new BigDecimal(replacementBill));
338 Timestamp noticeToBeSentDate=null;
339 Integer interval;
340 if(intervalToGenerateNotice!=null && !intervalToGenerateNotice.trim().isEmpty()){
341 interval = intervalToGenerateNotice!=null ? Integer.parseInt(intervalToGenerateNotice) : null;
342 }else{
343 interval = getOverdueNoticeInterval()!=null ? Integer.parseInt(getOverdueNoticeInterval()) : null;
344 }
345 noticeToBeSentDate=calculateNoticeToBeSentDate(interval,dueDate,lostCount+1);
346 lostNotice.setNoticeToBeSendDate(noticeToBeSentDate);
347 deliverNotices.add(lostNotice);
348 }
349
350 }
351
352 private Timestamp calculateNoticeToBeSentDate(Integer interval,Timestamp dueDate,Integer count){
353 Timestamp noticeToBeSentDate;
354
355 noticeToBeSentDate=interval!=null && dueDate !=null ?
356 DateUtil.addDays(dueDate,interval * count) : null;
357
358
359
360
361
362 return noticeToBeSentDate;
363 }
364
365 @Override
366 public void updateDeliverNoticeForUnprocessedLoans()throws Exception{
367 Long startTime = System.currentTimeMillis();
368 List<OleLoanDocument> oleLoanDocuments = getOleLoanDocumentDaoOjb().getUnprocessedOverdueLoanDocuments();
369 try {
370 oleLoanDocuments = getOleDeliverRequestDocumentHelperService().getLoanDocumentWithItemInfo(oleLoanDocuments);
371 } catch (Exception e) {
372 LOG.info("Exception occured while setting the item info " + e.getMessage());
373 LOG.error(e,e);
374 throw new Exception();
375 }
376 if(oleLoanDocuments!=null && oleLoanDocuments.size()>0){
377 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
378 oleLoanDocument.setItemTypeName(oleLoanDocument.getItemType());
379 generateDeliverNotices(oleLoanDocument);
380 }
381 }
382 Long endTime = System.currentTimeMillis();
383 Long differenceInTime = endTime-startTime;
384 LOG.info("Time taken for updating the notice table in milliseconds " +differenceInTime);
385 }
386
387 }