View Javadoc
1   package org.kuali.ole.deliver.service;
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.bo.OleLoanDocument;
7   import org.kuali.ole.deliver.bo.OlePatronDocument;
8   import org.kuali.ole.describe.bo.OleLocation;
9   import org.kuali.ole.service.OlePatronHelperService;
10  import org.kuali.ole.service.OlePatronHelperServiceImpl;
11  import org.kuali.rice.core.api.util.RiceConstants;
12  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
13  import org.kuali.rice.krad.service.BusinessObjectService;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  /**
22   * Created by pvsubrah on 4/6/15.
23   */
24  public abstract class NoticeMailContentFormatter {
25      private static final Logger LOG = Logger.getLogger(NoticeMailContentFormatter.class);
26      private ParameterValueResolver parameterValueResolver;
27      private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService;
28      private BusinessObjectService businessObjectService;
29      private OlePatronHelperServiceImpl olePatronHelperService;
30  
31      public OlePatronHelperService getOlePatronHelperService(){
32          if(olePatronHelperService==null)
33              olePatronHelperService=new OlePatronHelperServiceImpl();
34          return olePatronHelperService;
35      }
36  
37      public void setOlePatronHelperService(OlePatronHelperServiceImpl olePatronHelperService) {
38          this.olePatronHelperService = olePatronHelperService;
39      }
40  
41  
42      public String generateMailContentForPatron(List<OleLoanDocument> oleLoanDocuments,String title,String body){
43          StringBuffer stringBuffer = new StringBuffer();
44          //Add header
45          stringBuffer.append(getHeaderContent(title));
46          //Add patron info
47          stringBuffer.append(getPatronInfo(oleLoanDocuments.get(0).getOlePatron(),title,body));
48          for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
49              //add custom HTML (Overdue or Replacement or Courtsey)
50              String generateCustomHTML = generateCustomHTML(oleLoanDocument);
51              if (null != generateCustomHTML) {
52                  stringBuffer.append(generateCustomHTML);
53              }
54              //add item info
55              stringBuffer.append(generateItemInfoHTML(oleLoanDocument));
56          }
57          return stringBuffer.toString();
58  
59      }
60  
61      protected abstract String generateCustomHTML(OleLoanDocument oleLoanDocument);
62  
63      public String getHeaderContent(String title){
64          StringBuffer stringBuffer = new StringBuffer();
65          stringBuffer.append("<HTML>");
66          stringBuffer.append("<TITLE>" + title + "</TITLE>");
67          stringBuffer.append("<HEAD></HEAD>");
68          stringBuffer.append("<BODY>");
69  
70          return stringBuffer.toString();
71  
72      }
73  
74      private ParameterValueResolver getParameterInstance() {
75          if (null == parameterValueResolver) {
76              parameterValueResolver = ParameterValueResolver.getInstance();
77          }
78          return parameterValueResolver;
79      }
80  
81      public void setParameterValueResolver(ParameterValueResolver parameterValueResolver) {
82          this.parameterValueResolver = parameterValueResolver;
83      }
84  
85      public String getPatronInfo(OlePatronDocument olePatronDocument,String title,String body) {
86          StringBuffer stringBuffer = new StringBuffer();
87          try {
88              OleDeliverRequestDocumentHelperServiceImpl deliverService = getOleDeliverRequestDocumentHelperService();
89              EntityTypeContactInfoBo entityTypeContactInfoBo = olePatronDocument.getEntity().getEntityTypeContactInfos().get(0);
90  
91              stringBuffer.append("<HTML>");
92              stringBuffer.append("<TITLE>" + title + "</TITLE>");
93              stringBuffer.append("<HEAD></HEAD>");
94              stringBuffer.append("<BODY>");
95  
96              try {
97                  stringBuffer.append("<TABLE></BR></BR>");
98                  stringBuffer.append("<TR><TD>Patron Name :</TD><TD>" + olePatronDocument.getEntity().getNames().get(0).getFirstName() + " " + olePatronDocument.getEntity().getNames().get(0).getLastName() + "</TD></TR>");
99                  stringBuffer.append("<TR><TD>Address :</TD><TD>" + (getOlePatronHelperService().getPatronPreferredAddress(entityTypeContactInfoBo) != null ? getOlePatronHelperService().getPatronPreferredAddress(entityTypeContactInfoBo) : "") + "</TD></TR>");
100                 stringBuffer.append("<TR><TD>EMAIL :</TD><TD>" + (getOlePatronHelperService().getPatronHomeEmailId(entityTypeContactInfoBo) != null ? getOlePatronHelperService().getPatronHomeEmailId(entityTypeContactInfoBo) : "") + "</TD></TR>");
101 
102                 stringBuffer.append("<TR><TD>Phone Number :</TD><TD>" + (getOlePatronHelperService().getPatronHomePhoneNumber(entityTypeContactInfoBo) != null ? getOlePatronHelperService().getPatronHomePhoneNumber(entityTypeContactInfoBo) : "") + "</TD></TR>");
103             } catch (Exception e) {
104                 e.printStackTrace();
105             }
106             stringBuffer.append("</TABLE>");
107 
108             stringBuffer.append("<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>");
109             stringBuffer.append("<TABLE width=\"100%\">");
110             stringBuffer.append("<TR><TD><CENTER>" + title + "</CENTER></TD></TR>");
111             stringBuffer.append("<TR><TD><p>" + body + "</p></TD></TR>");
112             stringBuffer.append("<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR></TABLE>");
113         } catch (Exception e) {
114             LOG.error("Error---->While generating overdue content for email(Patron Information) ");
115         }
116         return stringBuffer.toString();
117     }
118 
119     private OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
120         if (null == oleDeliverRequestDocumentHelperService) {
121             oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
122         }
123         return oleDeliverRequestDocumentHelperService;
124     }
125 
126     public void setOleDeliverRequestDocumentHelperService(OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService) {
127         this.oleDeliverRequestDocumentHelperService = oleDeliverRequestDocumentHelperService;
128     }
129 
130     public String generateItemInfoHTML(OleLoanDocument oleLoanDocument) {
131         StringBuffer stringBuffer = new StringBuffer();
132         SimpleDateFormat sdf = getSimpleDateFormat();
133         try {
134             stringBuffer.append("<table>");
135             stringBuffer.append("<TR><TD>Title :</TD><TD>" + (oleLoanDocument.getTitle() != null ? oleLoanDocument.getTitle() : "") + "</TD></TR>");
136             stringBuffer.append("<TR><TD>Author :</TD><TD>" + (oleLoanDocument.getAuthor() != null ? oleLoanDocument.getAuthor() : "") + "</TD></TR>");
137             String volume = oleLoanDocument.getEnumeration() != null && !oleLoanDocument.getEnumeration().equals("") ? oleLoanDocument.getEnumeration() : "";
138             String issue = oleLoanDocument.getChronology()!=null && !oleLoanDocument.getChronology().equals("") ? oleLoanDocument.getChronology() : "";
139             String copyNumber = oleLoanDocument.getItemCopyNumber() != null && !oleLoanDocument.getItemCopyNumber().equals("") ? oleLoanDocument.getItemCopyNumber() : "";
140             String volumeNumber = volume + "/" + issue + "/" + copyNumber;
141             stringBuffer.append("<TR><TD>Copy Number :</TD><TD>" + (oleLoanDocument.getItemCopyNumber() != null && !oleLoanDocument.getItemCopyNumber().equals("") ? oleLoanDocument.getItemCopyNumber() : "") + "</TD></TR>");
142             stringBuffer.append("<TR><TD>Enumeration :</TD><TD>" + (oleLoanDocument.getEnumeration() != null && !oleLoanDocument.getEnumeration().equals("") ? oleLoanDocument.getEnumeration() : "" )+ "</TD></TR>");
143             stringBuffer.append("<TR><TD>Chronology :</TD><TD>" + (oleLoanDocument.getChronology() != null && !oleLoanDocument.getChronology().equals("") ? oleLoanDocument.getChronology() : "" )+ "</TD></TR>");
144             stringBuffer.append("<TR><TD>Item was due :</TD><TD>" + (oleLoanDocument.getLoanDueDate() != null ? sdf.format(oleLoanDocument.getLoanDueDate()).toString() : "") + "</TD></TR>");
145             stringBuffer.append("<TR><TD>Library Location :</TD><TD>" + ( itemShelvingLocationName(oleLoanDocument.getItemLibrary())!= null ?itemShelvingLocationName(oleLoanDocument.getItemLibrary()): "") + "</TD></TR>");
146             stringBuffer.append("<TR><TD>Library shelving location :</TD><TD>" + ( itemShelvingLocationName(oleLoanDocument.getItemLocation())!= null ? itemShelvingLocationName(oleLoanDocument.getItemLocation()): "") + "</TD></TR>");
147             try {
148                 String callNumber = "";
149                 if (oleLoanDocument.getItemCallNumber() != null && !oleLoanDocument.getItemCallNumber().equals("")) {
150                     callNumber = oleLoanDocument.getItemCallNumber();
151                 }
152                 stringBuffer.append("<TR><TD>Call Number :</TD><TD>" + (callNumber != null ? callNumber : "") + "</TD></TR>");
153             } catch (Exception e) {
154                 e.printStackTrace();
155             }
156             stringBuffer.append("<TR><TD>Item Barcode :</TD><TD>" + (oleLoanDocument.getItemId() != null ? oleLoanDocument.getItemId() : "") + "</TD></TR>");
157             stringBuffer.append("<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>");
158             stringBuffer.append("</table>");
159         } catch (Exception e) {
160             LOG.error("Error---->While generating HTML overdue content  ");
161             if (oleLoanDocument != null) {
162                 LOG.error("Error---->Item Barcode " + oleLoanDocument.getItemId());
163             }
164             LOG.error(e.getMessage() + e);
165         }
166         return stringBuffer.toString();
167     }
168 
169     protected SimpleDateFormat getSimpleDateFormat() {
170         return new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE + " " + RiceConstants.SIMPLE_DATE_FORMAT_FOR_TIME);
171     }
172 
173     private String itemShelvingLocationName(String code) {
174         Map<String, String> criteria = new HashMap<String, String>();
175         criteria.put("locationCode", code);
176         List<OleLocation> oleLocation = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, criteria);
177 
178         return oleLocation.size() == 1 ? oleLocation.get(0).getLocationName() : "";
179     }
180 
181     public BusinessObjectService getBusinessObjectService() {
182         if (null == businessObjectService) {
183             businessObjectService = KRADServiceLocator.getBusinessObjectService();
184         }
185         return businessObjectService;
186     }
187 
188     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
189         this.businessObjectService = businessObjectService;
190     }
191 }