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.kuali.ole.OLEConstants;
6   import org.kuali.ole.OLEParameterConstants;
7   import org.kuali.ole.deliver.batch.OleMailer;
8   import org.kuali.ole.deliver.bo.OLEDeliverNotice;
9   import org.kuali.ole.deliver.bo.OLEDeliverNoticeHistory;
10  import org.kuali.ole.deliver.bo.OleLoanDocument;
11  import org.kuali.ole.deliver.bo.OlePatronDocument;
12  import org.kuali.ole.describe.bo.OleInstanceItemType;
13  import org.kuali.rice.core.api.mail.EmailBody;
14  import org.kuali.rice.core.api.mail.EmailFrom;
15  import org.kuali.rice.core.api.mail.EmailSubject;
16  import org.kuali.rice.core.api.mail.EmailTo;
17  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
18  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
19  import org.kuali.rice.krad.service.BusinessObjectService;
20  import org.kuali.rice.krad.service.KRADServiceLocator;
21  
22  import javax.sql.rowset.serial.SerialBlob;
23  import java.sql.Blob;
24  import java.sql.Timestamp;
25  import java.util.Date;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * Created by pvsubrah on 4/8/15.
32   */
33  public abstract class NoticesExecutor implements Runnable {
34  
35      private static final Logger LOG = Logger.getLogger(NoticesExecutor.class);
36      private OleMailer oleMailer;
37      protected List<OleLoanDocument> loanDocuments;
38      private BusinessObjectService businessObjectService;
39      private CircDeskLocationResolver circDeskLocationResolver;
40      private ParameterValueResolver parameterResolverInstance;
41  
42      public NoticesExecutor(List<OleLoanDocument> loanDocuments) {
43          this.loanDocuments = loanDocuments;
44      }
45  
46      @Override
47      public void run() {
48          LOG.info("NoticesExecutor thread id---->"+Thread.currentThread().getId()+"current thread---->"+Thread.currentThread());
49  
50          //1. Pre process
51          preProcess(loanDocuments);
52          //2. generate email content
53          String mailContent = generateMailContent(loanDocuments);
54          //3. Generate notices
55          List<OLEDeliverNotice> oleDeliverNotices = buildNoticesForDeletion();
56          //4. Save loan document
57          getBusinessObjectService().save(loanDocuments);
58          //5. Delete notices
59          deleteNotices(oleDeliverNotices);
60          //6. update notice history
61          saveOLEDeliverNoticeHistory(oleDeliverNotices, mailContent);
62          //7. send mail
63          sendMail(mailContent);
64          //8 Post process
65          postProcess(loanDocuments);
66      }
67  
68      private void sendMail(String mailContent) {
69          OlePatronDocument olePatron = loanDocuments.get(0).getOlePatron();
70          try {
71              EntityTypeContactInfoBo entityTypeContactInfoBo = olePatron.getEntity()
72                      .getEntityTypeContactInfos().get(0);
73              String emailAddress = getPatronHomeEmailId(entityTypeContactInfoBo) != null ?
74                      getPatronHomeEmailId(entityTypeContactInfoBo) : "";
75  
76              if (loanDocuments.size() == 1) {
77                  sendMailsToPatron(emailAddress, mailContent, loanDocuments.get(0).getItemLocation());
78              } else {
79                  sendMailsToPatron(emailAddress, mailContent, null);
80              }
81  
82          } catch (Exception e) {
83              e.printStackTrace();
84          }
85      }
86  
87      public String getPatronHomeEmailId(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
88          String emailId = "";
89          if (entityTypeContactInfoBo.getEmailAddresses() != null) {
90              for (int j = 0; j < entityTypeContactInfoBo.getEmailAddresses().size(); j++) {
91                  if (entityTypeContactInfoBo.getEmailAddresses().get(j).getDefaultValue()) {
92                      emailId = (entityTypeContactInfoBo.getEmailAddresses().get(j).getEmailAddress());
93                      break;
94                  }
95              }
96          }
97          return emailId;
98      }
99  
100     public String sendMailsToPatron(String emailAddress, String noticeContent, String itemLocation) {
101         String fromAddress = getCircDeskLocationResolver().getReplyToEmail(itemLocation);
102 
103         if (fromAddress == null) {
104             fromAddress = getParameterResolverInstance().getParameter(OLEConstants.APPL_ID, OLEConstants
105                     .DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEParameterConstants
106                     .NOTICE_FROM_MAIL);
107         }
108         try {
109             if (fromAddress != null && (fromAddress.equals("") || fromAddress.trim().isEmpty())) {
110                 fromAddress = OLEConstants.KUALI_MAIL;
111             }
112             if (emailAddress != null && !emailAddress.isEmpty()) {
113                 noticeContent = noticeContent.replace('[', ' ');
114                 noticeContent = noticeContent.replace(']', ' ');
115                 if (!noticeContent.trim().equals("")) {
116                     OleMailer oleMailer = getOleMailer();
117                     oleMailer.sendEmail(new EmailFrom(fromAddress), new EmailTo(emailAddress), new EmailSubject(OLEConstants.NOTICE_MAIL), new EmailBody(noticeContent), true);
118                 }
119             } else {
120             }
121         } catch (Exception e) {
122         }
123 
124         return noticeContent;
125     }
126 
127     private CircDeskLocationResolver getCircDeskLocationResolver() {
128         if (null == circDeskLocationResolver) {
129             circDeskLocationResolver = new CircDeskLocationResolver();
130         }
131         return circDeskLocationResolver;
132     }
133 
134     public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
135         this.circDeskLocationResolver = circDeskLocationResolver;
136     }
137 
138     private OleMailer getOleMailer() {
139         if (null == oleMailer) {
140             oleMailer = GlobalResourceLoader.getService("oleMailer");
141         }
142         return oleMailer;
143     }
144 
145     private void deleteNotices(List<OLEDeliverNotice> oleDeliverNotices) {
146             getBusinessObjectService().delete(oleDeliverNotices);
147     }
148 
149     private void saveOLEDeliverNoticeHistory(List<OLEDeliverNotice> oleDeliverNotices, String mailContent) {
150         for (OLEDeliverNotice oleDeliverNotice : oleDeliverNotices) {
151                 OLEDeliverNoticeHistory oleDeliverNoticeHistory = new OLEDeliverNoticeHistory();
152                 oleDeliverNoticeHistory.setLoanId(oleDeliverNotice.getLoanId());
153                 oleDeliverNoticeHistory.setNoticeType(oleDeliverNotice.getNoticeType());
154                 oleDeliverNoticeHistory.setNoticeSentDate(new Timestamp(new Date().getTime()));
155                 oleDeliverNoticeHistory.setPatronId(oleDeliverNotice.getPatronId());
156                 oleDeliverNoticeHistory.setNoticeSendType(oleDeliverNotice.getNoticeSendType());
157                 oleDeliverNoticeHistory.setNoticeContent(mailContent.getBytes());
158                 getBusinessObjectService().save(oleDeliverNoticeHistory);
159         }
160     }
161 
162     protected BusinessObjectService getBusinessObjectService() {
163         if (null == businessObjectService) {
164             businessObjectService = KRADServiceLocator.getBusinessObjectService();
165         }
166         return businessObjectService;
167     }
168 
169 
170 
171     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
172         this.businessObjectService = businessObjectService;
173     }
174 
175     protected abstract void postProcess(List<OleLoanDocument> loanDocuments);
176     protected abstract void preProcess(List<OleLoanDocument> loanDocuments);
177     public abstract List<OLEDeliverNotice> buildNoticesForDeletion();
178     public abstract String generateMailContent(List<OleLoanDocument> oleLoanDocuments);
179 
180     public ParameterValueResolver getParameterResolverInstance() {
181         if (null == parameterResolverInstance) {
182             parameterResolverInstance = ParameterValueResolver.getInstance();
183         }
184         return parameterResolverInstance;
185     }
186 
187     public String getItemTypeCodeByName(String itemTypeName) {
188         String itemTypeCode = "";
189         List<OleInstanceItemType> instanceItemTypeList = null;
190         Map<String, String> instanceItemTypeMap = new HashMap<String, String>();
191         instanceItemTypeMap.put("instanceItemTypeName", itemTypeName);
192         instanceItemTypeList = (List<OleInstanceItemType>) getBusinessObjectService().findMatching(OleInstanceItemType.class, instanceItemTypeMap);
193         if (instanceItemTypeList != null && instanceItemTypeList.size() > 0) {
194             itemTypeCode = instanceItemTypeList.get(0).getInstanceItemTypeCode();
195         }
196         return itemTypeCode;
197     }
198 
199     protected Timestamp getSendToDate(String noticeToDate) {
200         String lostNoticeToDate;
201         lostNoticeToDate = getParameterResolverInstance().getParameter(OLEConstants
202                 .APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, noticeToDate);
203         Timestamp lostNoticetoSendDate = new Timestamp(System.currentTimeMillis());
204         if (!StringUtils.isEmpty(lostNoticeToDate)) {
205             lostNoticetoSendDate = new Timestamp(new Date(lostNoticeToDate).getTime());
206         }
207         return lostNoticetoSendDate;
208     }
209 
210 }