View Javadoc
1   package org.kuali.ole.deliver.notice.executors;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.common.util.CollectionUtils;
5   import org.kuali.incubator.SolrRequestReponseHandler;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.bo.OLEDeliverNotice;
8   import org.kuali.ole.deliver.bo.OleLoanDocument;
9   import org.kuali.ole.deliver.bo.OlePatronDocument;
10  import org.kuali.ole.deliver.notice.NoticeSolrInputDocumentGenerator;
11  import org.kuali.ole.deliver.notice.bo.OleNoticeContentConfigurationBo;
12  import org.kuali.ole.deliver.service.NoticesExecutor;
13  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
14  
15  import java.util.*;
16  
17  /**
18   * Created by maheswarang on 7/6/15.
19   */
20  public abstract class LoanNoticesExecutor extends NoticesExecutor {
21  
22      private static final Logger LOG = Logger.getLogger(LoanNoticesExecutor.class);
23      protected List<OleLoanDocument> loanDocuments;
24      protected String noticeContentConfigName;
25      protected Map<String,String> fieldLabelMap = new HashMap<String,String>();
26      protected OleNoticeContentConfigurationBo oleNoticeContentConfigurationBo;
27      private SolrRequestReponseHandler solrRequestReponseHandler;
28      private NoticeSolrInputDocumentGenerator noticeSolrInputDocumentGenerator;
29  
30      public LoanNoticesExecutor(Map loanNoticeMap) {
31          this.loanDocuments = (List<OleLoanDocument>) loanNoticeMap.get(OLEConstants.LOAN_DOCUMENTS);
32          this.noticeContentConfigName = (String) loanNoticeMap.get(OLEConstants.NOTICE_CONTENT_CONFIG_NAME);
33      }
34  
35      @Override
36      public void run() {
37          LOG.info("NoticesExecutor thread id---->"+Thread.currentThread().getId()+"current thread---->"+Thread.currentThread());
38  
39          //1. Pre process
40          preProcess(loanDocuments);
41          //2. Determine the correct NoticeConfigurationBo
42          setOleNoticeContentConfigurationBo();
43          //3. generate email content
44          String mailContent = generateMailContent(loanDocuments);
45          //4. Generate notices
46          List<OLEDeliverNotice> oleDeliverNotices = buildNoticesForDeletion();
47          //5. Save loan document
48          getBusinessObjectService().save(loanDocuments);
49          //6. Delete notices
50          deleteNotices(oleDeliverNotices);
51          //7. update notice history
52          saveOLEDeliverNoticeHistory(oleDeliverNotices, mailContent);
53          //8. send mail
54          sendMail(mailContent);
55          //9. Index the mail content for solr search
56          getSolrRequestReponseHandler().updateSolr(CollectionUtils.singletonList(
57                  getNoticeSolrInputDocumentGenerator().getSolrInputDocument(
58                          buildMapForIndexToSolr(getNoticeType(), mailContent, loanDocuments))));
59          //10. Post process
60          postProcess(loanDocuments);
61      }
62  
63      private NoticeSolrInputDocumentGenerator getNoticeSolrInputDocumentGenerator() {
64          if (null == noticeSolrInputDocumentGenerator) {
65              noticeSolrInputDocumentGenerator = new NoticeSolrInputDocumentGenerator();
66          }
67          return noticeSolrInputDocumentGenerator;
68      }
69  
70      private SolrRequestReponseHandler getSolrRequestReponseHandler() {
71          if (null == solrRequestReponseHandler) {
72              solrRequestReponseHandler = new SolrRequestReponseHandler();
73          }
74          return solrRequestReponseHandler;
75      }
76  
77      protected abstract String getNoticeType();
78  
79      public void sendMail(String mailContent) {
80          OlePatronDocument olePatron = loanDocuments.get(0).getOlePatron();
81          try {
82              EntityTypeContactInfoBo entityTypeContactInfoBo = olePatron.getEntity()
83                      .getEntityTypeContactInfos().get(0);
84              String emailAddress = getPatronHomeEmailId(entityTypeContactInfoBo) != null ?
85                      getPatronHomeEmailId(entityTypeContactInfoBo) : "";
86  
87              if (loanDocuments.size() == 1) {
88                  sendMailsToPatron(emailAddress, mailContent, loanDocuments.get(0).getItemLocation());
89              } else {
90                  sendMailsToPatron(emailAddress, mailContent, null);
91              }
92  
93          } catch (Exception e) {
94              e.printStackTrace();
95          }
96      }
97  
98      private Map buildMapForIndexToSolr(String noticeType, String noticeContent, List<OleLoanDocument> oleLoanDocuments) {
99          Map parameterMap = new HashMap();
100         parameterMap.put("DocType", noticeType);
101         parameterMap.put("DocFormat", "Email");
102         parameterMap.put("noticeType", noticeType);
103         parameterMap.put("noticeContent", noticeContent);
104         String patronBarcode = oleLoanDocuments.get(0).getOlePatron().getBarcode();
105         String patronId = oleLoanDocuments.get(0).getOlePatron().getOlePatronId();
106         parameterMap.put("patronBarcode", patronBarcode);
107         Date dateSent = new Date();
108         parameterMap.put("dateSent", dateSent);
109         parameterMap.put("uniqueId", patronId + dateSent.getTime());
110         List<String> itemBarcodes = new ArrayList<>();
111         for (Iterator<OleLoanDocument> iterator = oleLoanDocuments.iterator(); iterator.hasNext(); ) {
112             OleLoanDocument oleLoanDocument = iterator.next();
113             String itemBarcode = oleLoanDocument.getItemId();
114             itemBarcodes.add(itemBarcode);
115         }
116         parameterMap.put("itemBarcodes",itemBarcodes);
117         return parameterMap;
118     }
119 
120     protected abstract void postProcess(List<OleLoanDocument> loanDocuments);
121     protected abstract void preProcess(List<OleLoanDocument> loanDocuments);
122     public abstract List<OLEDeliverNotice> buildNoticesForDeletion();
123     public abstract String generateMailContent(List<OleLoanDocument> oleLoanDocuments);
124     public abstract void setOleNoticeContentConfigurationBo();
125 
126 }