View Javadoc
1   package org.kuali.ole.deliver.util;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.OLEParameterConstants;
6   import org.kuali.ole.deliver.batch.OleMailer;
7   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
8   import org.kuali.ole.deliver.bo.OlePatronDocument;
9   import org.kuali.ole.deliver.service.CircDeskLocationResolver;
10  import org.kuali.ole.deliver.service.ParameterValueResolver;
11  import org.kuali.ole.service.OlePatronHelperService;
12  import org.kuali.ole.service.OlePatronHelperServiceImpl;
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  
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * Created by sheiksalahudeenm on 7/29/15.
26   */
27  public class OnHoldCourtesyNoticeUtil {
28      private static final Logger LOG = Logger.getLogger(OnHoldCourtesyNoticeUtil.class);
29      private OlePatronHelperService olePatronHelperService;
30      private OleMailer oleMailer;
31      private CircDeskLocationResolver circDeskLocationResolver;
32      private ParameterValueResolver parameterResolverInstance;
33  
34      public OlePatronHelperService getOlePatronHelperService(){
35          if(olePatronHelperService==null)
36              olePatronHelperService=new OlePatronHelperServiceImpl();
37          return olePatronHelperService;
38      }
39  
40      public CircDeskLocationResolver getCircDeskLocationResolver() {
41          if (null == circDeskLocationResolver) {
42              circDeskLocationResolver = new CircDeskLocationResolver();
43          }
44          return circDeskLocationResolver;
45      }
46  
47      public OleMailer getOleMailer() {
48          if (null == oleMailer) {
49              oleMailer = GlobalResourceLoader.getService("oleMailer");
50          }
51          return oleMailer;
52      }
53  
54      public ParameterValueResolver getParameterResolverInstance() {
55          if (null == parameterResolverInstance) {
56              parameterResolverInstance = ParameterValueResolver.getInstance();
57          }
58          return parameterResolverInstance;
59      }
60  
61      public void setOlePatronHelperService(OlePatronHelperService olePatronHelperService) {
62          this.olePatronHelperService = olePatronHelperService;
63      }
64  
65      public void setOleMailer(OleMailer oleMailer) {
66          this.oleMailer = oleMailer;
67      }
68  
69      public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
70          this.circDeskLocationResolver = circDeskLocationResolver;
71      }
72  
73      public void setParameterResolverInstance(ParameterValueResolver parameterResolverInstance) {
74          this.parameterResolverInstance = parameterResolverInstance;
75      }
76  
77      public String getHeaderContent(String title){
78          StringBuffer stringBuffer = new StringBuffer();
79          stringBuffer.append("<HTML>");
80          stringBuffer.append("<TITLE>" + title + "</TITLE>");
81          stringBuffer.append("<HEAD></HEAD>");
82          stringBuffer.append("<BODY>");
83  
84          return stringBuffer.toString();
85  
86      }
87  
88      public String getPatronInfo(OlePatronDocument olePatronDocument,Map<String,String> fieldLabelMap) {
89          StringBuffer stringBuffer = new StringBuffer();
90          try {
91              EntityTypeContactInfoBo entityTypeContactInfoBo = getEntityTypeContactInfo(olePatronDocument);
92              stringBuffer.append("<HTML>");
93              stringBuffer.append("<TITLE>" + fieldLabelMap.get("noticeTitle") + "</TITLE>");
94              stringBuffer.append("<HEAD></HEAD>");
95              stringBuffer.append("<BODY>");
96              try {
97                  stringBuffer.append("<TABLE></BR></BR><TR><TD>");
98                  stringBuffer.append("Patron Name </TD><TD>:</TD><TD>" + olePatronDocument.getPatronName()  + "</TD></TR><TR><TD>");
99                  String patronPreferredAddress = getOlePatronHelperService().getPatronPreferredAddress(entityTypeContactInfoBo);
100                 stringBuffer.append("Patron Address </TD><TD>:</TD><TD>" + (patronPreferredAddress != null ? patronPreferredAddress : "") + "</TD></TR><TR><TD>");
101                 String patronHomeEmailId = getOlePatronHelperService().getPatronHomeEmailId(entityTypeContactInfoBo);
102                 stringBuffer.append("Patron Email </TD><TD>:</TD><TD>" + (patronHomeEmailId != null ? patronHomeEmailId : "") + "</TD></TR><TR><TD>");
103                 String patronHomePhoneNumber = getOlePatronHelperService().getPatronHomePhoneNumber(entityTypeContactInfoBo);
104                 stringBuffer.append("Patron Phone Number </TD><TD>:</TD><TD>" + (patronHomePhoneNumber != null ? patronHomePhoneNumber : "") + "</TD></TR>");
105             } catch (Exception e) {
106                 e.printStackTrace();
107             }
108             stringBuffer.append("</TABLE>");
109             stringBuffer.append("<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>");
110             stringBuffer.append("<TABLE width=\"100%\">");
111             stringBuffer.append("<TR><TD><CENTER>" + fieldLabelMap.get("noticeTitle") + "</CENTER></TD></TR>");
112             stringBuffer.append("<TR><TD><p>" + fieldLabelMap.get("noticeBody") + "</p></TD></TR>");
113             stringBuffer.append("<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR></TABLE>");
114         } catch (Exception e) {
115             LOG.error("Error---->While generating overdue content for email(Patron Information) ");
116         }
117         System.out.println(stringBuffer.toString());
118         return stringBuffer.toString();
119     }
120 
121     public EntityTypeContactInfoBo getEntityTypeContactInfo(OlePatronDocument olePatronDocument) {
122         if(olePatronDocument!=null && olePatronDocument.getEntity()!=null && olePatronDocument.getEntity().getEntityTypeContactInfos()!=null){
123             return olePatronDocument.getEntity().getEntityTypeContactInfos().get(0);
124         }
125         return null;
126     }
127 
128     public void setItemContent(OleDeliverRequestBo oleDeliverRequestBo,StringBuffer stringBuffer,Map<String,String> fieldLabelMap){
129         stringBuffer.append("<table>");
130         stringBuffer.append(generateItemInfoHTML(oleDeliverRequestBo,fieldLabelMap));
131         if(null != getCustomItemFooterInfo(oleDeliverRequestBo,fieldLabelMap)){
132             stringBuffer.append(getCustomItemFooterInfo(oleDeliverRequestBo,fieldLabelMap));
133         }
134         stringBuffer.append("</table>");
135 
136     }
137 
138     public String getCustomItemFooterInfo(OleDeliverRequestBo oleDeliverRequestBo,Map<String,String> fieldLabelMap) {
139         StringBuffer stringBuffer = new StringBuffer();
140         stringBuffer.append("<TR><TD>");
141         stringBuffer.append(fieldLabelMap.get("Hold Expiration Date")!=null ? fieldLabelMap.get("Hold Expiration Date") : "Hold Expiration Date"+"</TD><TD>:</TD><TD>" + (oleDeliverRequestBo.getHoldExpirationDate() != null ? oleDeliverRequestBo.getHoldExpirationDate() : "") + "</TD></TR>");
142         return stringBuffer.toString();
143     }
144 
145 
146     public String generateRequestMailContentForPatron(OleDeliverRequestBo oleDeliverRequestBo,Map<String,String> fieldLabelMap){
147         StringBuffer stringBuffer = new StringBuffer();
148         stringBuffer.append(getHeaderContent(fieldLabelMap.get("noticeTitle")));
149         stringBuffer.append(getPatronInfo(oleDeliverRequestBo.getOlePatron(), fieldLabelMap));
150         setItemContent(oleDeliverRequestBo, stringBuffer, fieldLabelMap);
151         return stringBuffer.toString();
152 
153     }
154 
155     public String generateItemInfoHTML(OleDeliverRequestBo oleDeliverRequestBo,Map<String,String> fieldLabelMap) {
156         StringBuffer stringBuffer = new StringBuffer();
157         try {
158             stringBuffer.append("<TR><TD>");
159             stringBuffer.append("Circulation Location/Library Name </TD><TD>:</TD><TD>" + (oleDeliverRequestBo.getOlePickUpLocation() != null ? oleDeliverRequestBo.getOlePickUpLocation().getCirculationDeskPublicName() : "") + "</TD></TR><TR><TD>");
160             stringBuffer.append("Circulation Reply-To Email </TD><TD>:</TD><TD>" + (oleDeliverRequestBo.getOlePickUpLocation() != null ? oleDeliverRequestBo.getOlePickUpLocation().getReplyToEmail()!=null ? oleDeliverRequestBo.getOlePickUpLocation().getReplyToEmail() : "" : "") + "</TD></TR><TR><TD>");
161             stringBuffer.append("Title </TD><TD>:</TD><TD>" + (oleDeliverRequestBo.getTitle() != null ? oleDeliverRequestBo.getTitle() : "") + "</TD></TR><TR><TD>");
162             stringBuffer.append("Author </TD><TD>:</TD><TD>" + (oleDeliverRequestBo.getAuthor() != null ? oleDeliverRequestBo.getAuthor() : "") + "</TD></TR><TR><TD>");
163             String volume = oleDeliverRequestBo.getEnumeration() != null && !oleDeliverRequestBo.getEnumeration().equals("") ? oleDeliverRequestBo.getEnumeration() : "";
164             String issue = new String(" ");
165             String copyNumber = oleDeliverRequestBo.getCopyNumber() != null && !oleDeliverRequestBo.getCopyNumber().equals("") ? oleDeliverRequestBo.getCopyNumber() : "";
166             String volumeNumber = volume + "/" + issue + "/" + copyNumber;
167             stringBuffer.append("Volume/Issue/Copy # </TD><TD>:</TD><TD>" + (volumeNumber != null ? volumeNumber : "") + "</TD></TR><TR><TD>");
168             stringBuffer.append("Library shelving location </TD><TD>:</TD><TD>" + ( oleDeliverRequestBo.getShelvingLocation()!= null ? oleDeliverRequestBo.getShelvingLocation() : "") + "</TD></TR><TR><TD>");
169             try {
170                 String callNumber = "";
171                 if (oleDeliverRequestBo.getCallNumber() != null && !oleDeliverRequestBo.getCallNumber().equals("")) {
172                     callNumber = oleDeliverRequestBo.getCallNumber();
173                 }
174                 stringBuffer.append("Call # </TD><TD>:</TD><TD>" + (callNumber != null ? callNumber : "") + "</TD></TR><TR><TD>");
175             } catch (Exception e) {
176                 e.printStackTrace();
177             }
178             stringBuffer.append("Item Barcode </TD><TD>:</TD><TD>" + (oleDeliverRequestBo.getItemId() != null ? oleDeliverRequestBo.getItemId() : "") + "</TD></TR><TR><TD>");
179             stringBuffer.append("<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>");
180 
181         } catch (Exception e) {
182             LOG.error("Error---->While generating HTML overdue content  ");
183             if (oleDeliverRequestBo != null) {
184                 LOG.error("Error---->Item Barcode " + oleDeliverRequestBo.getItemId());
185             }
186             LOG.error(e.getMessage() + e);
187         }
188         System.out.println(stringBuffer.toString());
189         return stringBuffer.toString();
190     }
191 
192     public void sendMail(OleDeliverRequestBo oleDeliverRequestBo, String mailContent) {
193         OlePatronDocument olePatron = oleDeliverRequestBo.getOlePatron();
194         try {
195             EntityTypeContactInfoBo entityTypeContactInfoBo = olePatron.getEntity()
196                     .getEntityTypeContactInfos().get(0);
197             String emailAddress = getPatronHomeEmailId(entityTypeContactInfoBo) != null ?
198                     getPatronHomeEmailId(entityTypeContactInfoBo) : "";
199 
200 
201             sendMailsToPatron(emailAddress, mailContent, oleDeliverRequestBo.getItemLocation());
202         } catch (Exception e) {
203             e.printStackTrace();
204         }
205     }
206 
207     public String getPatronHomeEmailId(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
208         String emailId = "";
209         if (entityTypeContactInfoBo.getEmailAddresses() != null) {
210             for (int j = 0; j < entityTypeContactInfoBo.getEmailAddresses().size(); j++) {
211                 if (entityTypeContactInfoBo.getEmailAddresses().get(j).getDefaultValue()) {
212                     emailId = (entityTypeContactInfoBo.getEmailAddresses().get(j).getEmailAddress());
213                     break;
214                 }
215             }
216         }
217         return emailId;
218     }
219 
220     public String sendMailsToPatron(String emailAddress, String noticeContent, String itemLocation) {
221         String fromAddress = getCircDeskLocationResolver().getReplyToEmail(itemLocation);
222 
223         if (fromAddress == null) {
224             fromAddress = getParameterResolverInstance().getParameter(OLEConstants.APPL_ID, OLEConstants
225                     .DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEParameterConstants
226                     .NOTICE_FROM_MAIL);
227         }
228         try {
229             if (fromAddress != null && (fromAddress.equals("") || fromAddress.trim().isEmpty())) {
230                 fromAddress = OLEConstants.KUALI_MAIL;
231             }
232             if (emailAddress != null && !emailAddress.isEmpty()) {
233                 noticeContent = noticeContent.replace('[', ' ');
234                 noticeContent = noticeContent.replace(']', ' ');
235                 if (!noticeContent.trim().equals("")) {
236                     OleMailer oleMailer = getOleMailer();
237                     oleMailer.sendEmail(new EmailFrom(fromAddress), new EmailTo(emailAddress), new EmailSubject(OLEConstants.NOTICE_MAIL), new EmailBody(noticeContent), true);
238                 }
239             } else {
240             }
241         } catch (Exception e) {
242         }
243 
244         return noticeContent;
245     }
246 
247     public void sendOnHoldNotice(OleDeliverRequestBo oleDeliverRequestBo){
248         Map<String,String> fieldLabelMap  = new HashMap<>();
249         fieldLabelMap.put("noticeTitle","OnHoldNotice");
250         fieldLabelMap.put("noticeBody"," The following requested item(s) is ready for pick-up and will be held until the expiration date at the location shown below.");
251         String mailContent = generateRequestMailContentForPatron(oleDeliverRequestBo,fieldLabelMap);
252         sendMail(oleDeliverRequestBo,mailContent);
253     }
254 }