001package org.kuali.ole.deliver.service.impl; 002 003import org.apache.log4j.Logger; 004import org.kuali.ole.DataCarrierService; 005import org.kuali.ole.OLEConstants; 006import org.kuali.ole.OLEParameterConstants; 007import org.kuali.ole.deliver.bo.OLEDeliverNotice; 008import org.kuali.ole.deliver.bo.OleLoanDocument; 009import org.kuali.ole.deliver.calendar.service.DateUtil; 010import org.kuali.ole.deliver.processor.LoanProcessor; 011import org.kuali.ole.deliver.service.OLEDeliverNoticeHelperService; 012import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl; 013import org.kuali.ole.deliver.service.OleLoanDocumentDaoOjb; 014import org.kuali.ole.sys.context.SpringContext; 015import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 016import org.kuali.rice.krad.service.BusinessObjectService; 017import org.kuali.rice.krad.service.KRADServiceLocator; 018import org.kuali.rice.krms.api.engine.EngineResults; 019 020import java.math.BigDecimal; 021import java.sql.Timestamp; 022import java.util.*; 023 024/** 025 * Created by vivekb on 9/16/14. 026 */ 027public class OLEDeliverNoticeHelperServiceImpl implements OLEDeliverNoticeHelperService { 028 private static final Logger LOG = Logger.getLogger(OLEDeliverNoticeHelperServiceImpl.class); 029 030 private BusinessObjectService businessObjectService; 031 private LoanProcessor loanProcessor; 032 private OleLoanDocumentDaoOjb oleLoanDocumentDaoOjb; 033 private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService; 034 035 private String overdueNoticeType ; 036 private String courtesyNoticeType ; 037 private String overdueNoticeInterval; 038 private String courtesyNoticeInterval; 039 040 041 /** 042 * 043 * @param businessObjectService 044 */ 045 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 046 this.businessObjectService = businessObjectService; 047 } 048 049 /** 050 * Gets the businessObjectService attribute. 051 * 052 * @return Returns the businessObjectService 053 */ 054 public BusinessObjectService getBusinessObjectService() { 055 if (null == businessObjectService) { 056 businessObjectService = KRADServiceLocator.getBusinessObjectService(); 057 } 058 return businessObjectService; 059 } 060 /** 061 * 062 * @param loanProcessor 063 */ 064 public void setLoanProcessor(LoanProcessor loanProcessor) { 065 this.loanProcessor = loanProcessor; 066 } 067 068 /** 069 * This method initiate LoanProcessor. 070 * 071 * @return LoanProcessor 072 */ 073 private LoanProcessor getLoanProcessor() { 074 if (loanProcessor == null) { 075 loanProcessor = SpringContext.getBean(LoanProcessor.class); 076 } 077 return loanProcessor; 078 } 079 080 /** 081 * 082 * @return overdueNoticeType 083 */ 084 public String getOverdueNoticeType() { 085 if(overdueNoticeType == null){ 086 overdueNoticeType = loanProcessor.getParameter(OLEParameterConstants.OVERDUE_NOTICE_TYPE); 087 } 088 return overdueNoticeType; 089 } 090 091 /** 092 * 093 * @param overdueNoticeType 094 */ 095 public void setOverdueNoticeType(String overdueNoticeType) { 096 this.overdueNoticeType = overdueNoticeType; 097 } 098 099 /** 100 * 101 * @return courtesyNoticeType 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 * @param courtesyNoticeType 113 */ 114 public void setCourtesyNoticeType(String courtesyNoticeType) { 115 this.courtesyNoticeType = courtesyNoticeType; 116 } 117 118 /** 119 * 120 * @return overdueNoticeInterval 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 * @param overdueNoticeInterval 132 */ 133 public void setOverdueNoticeInterval(String overdueNoticeInterval) { 134 this.overdueNoticeInterval = overdueNoticeInterval; 135 } 136 137 /** 138 * 139 * @return courtesyNoticeInterval 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 * @param courtesyNoticeInterval 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 * @param loanId 182 * @return oleDeliverNotices 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 * @param loanId 195 * @return oleDeliverNotices 196 */ 197 @Override 198 public List<OLEDeliverNotice> updateDeliverNotices(String loanId) { 199 List<OLEDeliverNotice> oleDeliverNotices = getDeliverNotices(loanId); 200 for(OLEDeliverNotice deliverNotice : oleDeliverNotices){ 201 //deliverNotice.setNoticeToBeSendDate();//TODO 202 } 203 return oleDeliverNotices; 204 } 205 206 /** 207 * 208 * @param loanId 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/* noticeToBeSentDate.setHours(0); 358 noticeToBeSentDate.setMinutes(0); 359 noticeToBeSentDate.setSeconds(0); 360 noticeToBeSentDate.setNanos(0);*/ 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}