View Javadoc
1   package org.kuali.ole.deliver.notice.executors;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang3.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.incubator.SolrRequestReponseHandler;
7   import org.kuali.ole.OLEConstants;
8   import org.kuali.ole.deliver.bo.*;
9   import org.kuali.ole.deliver.notice.NoticeSolrInputDocumentGenerator;
10  import org.kuali.ole.deliver.notice.bo.OleNoticeContentConfigurationBo;
11  import org.kuali.ole.deliver.notice.noticeFormatters.RequestEmailContentFormatter;
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 6/24/15.
19   */
20  public abstract class RequestNoticesExecutor extends NoticesExecutor {
21      private static final Logger LOG = Logger.getLogger(RequestNoticesExecutor.class);
22      protected List<OLEDeliverNotice> deliverNotices;
23      protected String noticeContentConfigName;
24      protected List<OLEDeliverNotice> filteredDeliverNotices = new ArrayList<OLEDeliverNotice>();
25      protected RequestEmailContentFormatter requestEmailContentFormatter;
26      protected List<OleDeliverRequestBo> deliverRequestBos = new ArrayList<OleDeliverRequestBo>();
27  
28  
29      protected OleNoticeContentConfigurationBo oleNoticeContentConfigurationBo;
30      private NoticeSolrInputDocumentGenerator noticeSolrInputDocumentGenerator;
31      private SolrRequestReponseHandler solrRequestReponseHandler;
32  
33      public void setRequestEmailContentFormatter(RequestEmailContentFormatter requestEmailContentFormatter) {
34          this.requestEmailContentFormatter = requestEmailContentFormatter;
35      }
36  
37      public RequestNoticesExecutor(Map requestMap) {
38          this.deliverNotices = (List<OLEDeliverNotice>) requestMap.get(OLEConstants.DELIVER_NOTICES);
39          this.noticeContentConfigName = (String) requestMap.get(OLEConstants.NOTICE_CONTENT_CONFIG_NAME);
40      }
41  
42      public abstract RequestEmailContentFormatter getRequestEmailContentFormatter();
43  
44  
45      public List<OLEDeliverNotice> getDeliverNotices() {
46          return deliverNotices;
47      }
48  
49      public void setDeliverNotices(List<OLEDeliverNotice> deliverNotices) {
50          this.deliverNotices = deliverNotices;
51      }
52  
53      public List<OleDeliverRequestBo> getDeliverRequestBos() {
54          return deliverRequestBos;
55      }
56  
57      public void setDeliverRequestBos(List<OleDeliverRequestBo> deliverRequestBos) {
58          this.deliverRequestBos = deliverRequestBos;
59      }
60  
61      public List<OLEDeliverNotice> getFilteredDeliverNotices() {
62          return filteredDeliverNotices;
63      }
64  
65      public void setFilteredDeliverNotices(List<OLEDeliverNotice> filteredDeliverNotices) {
66          this.filteredDeliverNotices = filteredDeliverNotices;
67      }
68  
69  
70      public abstract String getType();
71  
72      public abstract String getTitle();
73  
74      public abstract String getBody();
75  
76      public abstract boolean isValidRequestToSendNotice(OleDeliverRequestBo oleDeliverRequestBo);
77  
78      protected abstract void postProcess();
79  
80      public abstract void setOleNoticeContentConfigurationBo();
81  
82  
83      private void preProcess() {
84          if(deliverNotices !=null && deliverNotices.size()>0){
85              for(OLEDeliverNotice oleDeliverNotice : deliverNotices){
86                  setItemInformations(oleDeliverNotice.getOleDeliverRequestBo());
87                  if(isValidRequestToSendNotice(oleDeliverNotice.getOleDeliverRequestBo())){
88                  deliverRequestBos.add(oleDeliverNotice.getOleDeliverRequestBo());
89                  filteredDeliverNotices.add(oleDeliverNotice);
90                  }
91              }
92          }
93  
94      }
95  
96  
97      public String generateMailContent() {
98          String mailContent = getRequestEmailContentFormatter().generateMailContentForPatron(deliverRequestBos, oleNoticeContentConfigurationBo);
99          System.out.println(mailContent);
100         return mailContent;
101     }
102 
103 
104     public void sendMail(String mailContent) {
105         if (CollectionUtils.isNotEmpty(deliverRequestBos)) {
106             OlePatronDocument olePatron = deliverRequestBos.get(0).getOlePatron();
107             try {
108                 EntityTypeContactInfoBo entityTypeContactInfoBo = olePatron.getEntity()
109                         .getEntityTypeContactInfos().get(0);
110                 String emailAddress = getPatronHomeEmailId(entityTypeContactInfoBo) != null ?
111                         getPatronHomeEmailId(entityTypeContactInfoBo) : "";
112 
113                 if (deliverRequestBos.size() == 1) {
114                     sendMailsToPatron(emailAddress, mailContent, deliverRequestBos.get(0).getItemLocation());
115                 } else {
116                     sendMailsToPatron(emailAddress, mailContent, null);
117                 }
118 
119             } catch (Exception e) {
120                 e.printStackTrace();
121             }
122         }
123     }
124 
125 
126     public NoticeSolrInputDocumentGenerator getNoticeSolrInputDocumentGenerator() {
127         if (null == noticeSolrInputDocumentGenerator) {
128             noticeSolrInputDocumentGenerator = new NoticeSolrInputDocumentGenerator();
129         }
130         return noticeSolrInputDocumentGenerator;
131     }
132 
133     public SolrRequestReponseHandler getSolrRequestReponseHandler() {
134         if (null == solrRequestReponseHandler) {
135             solrRequestReponseHandler = new SolrRequestReponseHandler();
136         }
137         return solrRequestReponseHandler;
138     }
139 
140     @Override
141     public void run() {
142 
143         preProcess();
144 
145         setOleNoticeContentConfigurationBo();
146 
147         String mailContent = generateMailContent();
148 
149         if (StringUtils.isNotBlank(mailContent)) {
150             System.out.println(mailContent);
151 
152             sendMail(mailContent);
153 
154             deleteNotices(filteredDeliverNotices);
155 
156             saveOLEDeliverNoticeHistory(filteredDeliverNotices, mailContent);
157 
158             getSolrRequestReponseHandler().updateSolr(org.kuali.common.util.CollectionUtils.singletonList(
159                     getNoticeSolrInputDocumentGenerator().getSolrInputDocument(
160                             buildMapForIndexToSolr(getType(),mailContent, deliverRequestBos))));
161 
162             postProcess();
163         }
164 
165 
166     }
167 
168     public Map buildMapForIndexToSolr(String noticeType, String noticeContent, List<OleDeliverRequestBo> oleDeliverRequestBos) {
169         Map parameterMap = new HashMap();
170         parameterMap.put("DocType", noticeType);
171         parameterMap.put("DocFormat", "Email");
172         parameterMap.put("noticeType", noticeType);
173         parameterMap.put("noticeContent", noticeContent);
174         String patronBarcode = oleDeliverRequestBos.get(0).getOlePatron().getBarcode();
175         String patronId = oleDeliverRequestBos.get(0).getOlePatron().getOlePatronId();
176         parameterMap.put("patronBarcode", patronBarcode);
177         Date dateSent = new Date();
178         parameterMap.put("dateSent", dateSent);
179         parameterMap.put("uniqueId", patronId+ dateSent.getTime());
180         List<String> itemBarcodes = new ArrayList<>();
181         for (Iterator<OleDeliverRequestBo> iterator = oleDeliverRequestBos.iterator(); iterator.hasNext(); ) {
182             OleDeliverRequestBo oleDeliverRequestBo = iterator.next();
183             String itemBarcode = oleDeliverRequestBo.getItemId();
184             itemBarcodes.add(itemBarcode);
185         }
186         parameterMap.put("itemBarcodes",itemBarcodes);
187         return parameterMap;
188     }
189 
190 
191 }