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
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
45 stringBuffer.append(getHeaderContent(title));
46
47 stringBuffer.append(getPatronInfo(oleLoanDocuments.get(0).getOlePatron(),title,body));
48 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
49
50 String generateCustomHTML = generateCustomHTML(oleLoanDocument);
51 if (null != generateCustomHTML) {
52 stringBuffer.append(generateCustomHTML);
53 }
54
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> </TD><TD> </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> </TD><TD> </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 = new String(" ");
139 String copyNumber = oleLoanDocument.getItemCopyNumber() != null && !oleLoanDocument.getItemCopyNumber().equals("") ? oleLoanDocument.getItemCopyNumber() : "";
140 String volumeNumber = volume + "/" + issue + "/" + copyNumber;
141 stringBuffer.append("<TR><TD>Volume/Issue/Copy Number :</TD><TD>" + (volumeNumber != null ? volumeNumber : "") + "</TD></TR>");
142 stringBuffer.append("<TR><TD>Item was due :</TD><TD>" + (oleLoanDocument.getLoanDueDate() != null ? sdf.format(oleLoanDocument.getLoanDueDate()).toString() : "") + "</TD></TR>");
143 stringBuffer.append("<TR><TD>Library shelving location :</TD><TD>" + (itemShelvingLocationName(oleLoanDocument.getItemLocation()) != null ? itemShelvingLocationName(oleLoanDocument.getItemLocation()) : "") + "</TD></TR>");
144 try {
145 String callNumber = "";
146 if (oleLoanDocument.getItemCallNumber() != null && !oleLoanDocument.getItemCallNumber().equals("")) {
147 callNumber = oleLoanDocument.getItemCallNumber();
148 }
149 stringBuffer.append("<TR><TD>Call Number :</TD><TD>" + (callNumber != null ? callNumber : "") + "</TD></TR>");
150 } catch (Exception e) {
151 e.printStackTrace();
152 }
153 stringBuffer.append("<TR><TD>Item Barcode :</TD><TD>" + (oleLoanDocument.getItemId() != null ? oleLoanDocument.getItemId() : "") + "</TD></TR>");
154 stringBuffer.append("<TR><TD> </TD><TD> </TD></TR>");
155 stringBuffer.append("</table>");
156 } catch (Exception e) {
157 LOG.error("Error---->While generating HTML overdue content ");
158 if (oleLoanDocument != null) {
159 LOG.error("Error---->Item Barcode " + oleLoanDocument.getItemId());
160 }
161 LOG.error(e.getMessage() + e);
162 }
163 return stringBuffer.toString();
164 }
165
166 protected SimpleDateFormat getSimpleDateFormat() {
167 return new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE + " " + RiceConstants.SIMPLE_DATE_FORMAT_FOR_TIME);
168 }
169
170 private String itemShelvingLocationName(String code) {
171 Map<String, String> criteria = new HashMap<String, String>();
172 criteria.put("locationCode", code);
173 List<OleLocation> oleLocation = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, criteria);
174
175 return oleLocation.size() == 1 ? oleLocation.get(0).getLocationName() : "";
176 }
177
178 public BusinessObjectService getBusinessObjectService() {
179 if (null == businessObjectService) {
180 businessObjectService = KRADServiceLocator.getBusinessObjectService();
181 }
182 return businessObjectService;
183 }
184
185 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
186 this.businessObjectService = businessObjectService;
187 }
188 }