View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.jfree.util.Log;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.OLEParameterConstants;
8   import org.kuali.ole.deliver.bo.OLEDeliverNotice;
9   import org.kuali.ole.deliver.bo.OleLoanDocument;
10  import org.kuali.ole.describe.bo.OleInstanceItemType;
11  
12  import java.sql.Timestamp;
13  import java.util.*;
14  
15  /**
16   * Created by premkb on 3/30/15.
17   */
18  public class OverdueNoticesExecutor extends NoticesExecutor {
19      private static final Logger LOG = Logger.getLogger(OverdueNoticesExecutor.class);
20      private NoticeMailContentFormatter noticeMailContentFormatter;
21  
22      public OverdueNoticesExecutor(List<OleLoanDocument> loanDocuments) {
23          super(loanDocuments);
24      }
25  
26      @Override
27      protected void postProcess(List<OleLoanDocument> loanDocuments) {
28  
29      }
30  
31      @Override
32      protected void preProcess(List<OleLoanDocument> loanDocuments) {
33  
34      }
35  
36      public List<OLEDeliverNotice> buildNoticesForDeletion() {
37          List<OLEDeliverNotice> oleDeliverNotices = new ArrayList<>();
38          for (OleLoanDocument loanDocument:loanDocuments) {
39              if (loanDocument.getItemTypeName() != null) {
40                  loanDocument.setItemType(getItemTypeCodeByName(loanDocument.getItemTypeName()));
41              }
42              String overdueNoticeToDate = getParameterResolverInstance().getParameter(OLEConstants
43                      .APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEConstants.OVERDUE_NOTICE_TO_DATE);
44              Timestamp overdueNoticetoSendDate = new Timestamp(System.currentTimeMillis());
45              if (!StringUtils.isEmpty(overdueNoticeToDate)) {
46                  overdueNoticetoSendDate = new Timestamp(new Date(overdueNoticeToDate).getTime());
47              }
48              for (OLEDeliverNotice oleDeliverNotice : loanDocument.getDeliverNotices()) {
49                  LOG.info("OverdueNoticesExecutor thread id---->"+Thread.currentThread().getId()+"current thread---->"+Thread.currentThread()+"Loan id-->"+loanDocument.getLoanId()+"notice id--->"+oleDeliverNotice.getId());
50                  Timestamp toBeSendDate = oleDeliverNotice.getNoticeToBeSendDate();
51                  if (oleDeliverNotice.getNoticeType().equals(OLEConstants.NOTICE_OVERDUE) && toBeSendDate.compareTo(overdueNoticetoSendDate) < 0) {
52                      try {
53                          int noOfOverdueNoticeSent = Integer.parseInt(loanDocument.getNumberOfOverdueNoticesSent() != null ? loanDocument.getNumberOfOverdueNoticesSent() : "0");
54                          noOfOverdueNoticeSent = noOfOverdueNoticeSent + 1;
55                          loanDocument.setNumberOfOverdueNoticesSent(Integer.toString(noOfOverdueNoticeSent));
56                          loanDocument.setOverDueNoticeDate(new java.sql.Date(System.currentTimeMillis()));
57                          //getBusinessObjectService().save(loanDocument);
58                          oleDeliverNotices.add(oleDeliverNotice);
59                      } catch (Exception e) {
60                          Log.info(e.getStackTrace());
61                      }
62  
63                  }
64              }
65          }
66          return oleDeliverNotices;
67      }
68  
69      public String getItemTypeCodeByName(String itemTypeName) {
70          String itemTypeCode = "";
71          List<OleInstanceItemType> instanceItemTypeList = null;
72          Map<String, String> instanceItemTypeMap = new HashMap<String, String>();
73          instanceItemTypeMap.put("instanceItemTypeName", itemTypeName);
74          instanceItemTypeList = (List<OleInstanceItemType>) getBusinessObjectService().findMatching(OleInstanceItemType.class, instanceItemTypeMap);
75          if (instanceItemTypeList != null && instanceItemTypeList.size() > 0) {
76              itemTypeCode = instanceItemTypeList.get(0).getInstanceItemTypeCode();
77          }
78          return itemTypeCode;
79      }
80  
81  
82      public String generateMailContent(List<OleLoanDocument> oleLoanDocuments) {
83          String title = getParameterResolverInstance().getParameter(OLEConstants.APPL_ID, OLEConstants
84                  .DLVR_NMSPC, OLEConstants.DLVR_CMPNT,
85                  OLEParameterConstants
86                          .OVERDUE_TITLE);
87          String body = getParameterResolverInstance().getParameter(OLEConstants.APPL_ID_OLE, OLEConstants
88                  .DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEConstants.OleDeliverRequest.OVERDUE_NOTICE_CONTENT);
89          String mailContent = getNoticeMailContentFormatter().generateMailContentForPatron(oleLoanDocuments,title,body);
90  
91          return mailContent;
92      }
93  
94      private NoticeMailContentFormatter getNoticeMailContentFormatter() {
95          if (null == noticeMailContentFormatter) {
96              noticeMailContentFormatter = new OverdueNoticeEmailContentFormatter();
97          }
98          return noticeMailContentFormatter;
99      }
100 
101     public void setNoticeMailContentFormatter(NoticeMailContentFormatter noticeMailContentFormatter) {
102         this.noticeMailContentFormatter = noticeMailContentFormatter;
103     }
104 }