1 package org.kuali.ole.deliver.bo;
2
3
4
5
6
7
8
9
10
11 import com.lowagie.text.*;
12 import com.lowagie.text.Font;
13 import com.lowagie.text.pdf.PdfPTable;
14 import com.lowagie.text.pdf.PdfPageEventHelper;
15 import com.lowagie.text.pdf.PdfWriter;
16 import org.apache.log4j.Logger;
17 import org.kuali.ole.OLEConstants;
18 import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
19 import org.kuali.rice.krad.service.BusinessObjectService;
20 import org.kuali.rice.krad.service.KRADServiceLocator;
21
22 import javax.servlet.ServletOutputStream;
23 import javax.servlet.http.HttpServletResponse;
24 import java.awt.*;
25 import java.io.ByteArrayOutputStream;
26 import java.io.OutputStream;
27 import java.math.BigDecimal;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.List;
32
33
34
35
36 public class PrintBill extends PdfPageEventHelper {
37
38 private static final Logger LOG = Logger.getLogger(PrintBill.class);
39
40 private Map<String, Font> printFontMap = new HashMap<String, Font>();
41
42 private Map<String, Font> fontMap = new HashMap<String, Font>();
43 private Map<String, Color> colorMap = new HashMap<String, Color>();
44 private Map<String, Color> printColorMap = new HashMap<String, Color>();
45
46
47 public void populateFontMap() {
48 fontMap.put("COURIER", new Font(Font.COURIER));
49 fontMap.put("BOLD", new Font(Font.BOLD));
50 fontMap.put("BOLDITALIC", new Font(Font.BOLDITALIC));
51 fontMap.put("DEFAULTSIZE", new Font(Font.DEFAULTSIZE));
52 fontMap.put("HELVETICA", new Font(Font.HELVETICA));
53 fontMap.put("ITALIC", new Font(Font.ITALIC));
54 fontMap.put("NORMAL", new Font(Font.NORMAL));
55 fontMap.put("STRIKETHRU", new Font(Font.STRIKETHRU));
56 fontMap.put("SYMBOL", new Font(Font.SYMBOL));
57 fontMap.put("TIMES_ROMAN", new Font(Font.TIMES_ROMAN));
58 fontMap.put("UNDEFINED", new Font(Font.UNDEFINED));
59 fontMap.put("UNDERLINE", new Font(Font.UNDERLINE));
60 fontMap.put("ZAPFDINGBATS", new Font(Font.ZAPFDINGBATS));
61
62 }
63
64 public void populateColorMap() {
65 colorMap.put("WHITE", Color.WHITE);
66 colorMap.put("YELLOW", Color.YELLOW);
67 colorMap.put("BLACK", Color.BLACK);
68 colorMap.put("BLUE", Color.BLUE);
69 colorMap.put("CYAN", Color.CYAN);
70 colorMap.put("DARK_GRAY", Color.DARK_GRAY);
71 colorMap.put("GRAY", Color.GRAY);
72 colorMap.put("GREEN", Color.GREEN);
73 colorMap.put("LIGHT_GRAY", Color.LIGHT_GRAY);
74 colorMap.put("MAGENTA", Color.MAGENTA);
75 colorMap.put("ORANGE", Color.ORANGE);
76 colorMap.put("PINK", Color.PINK);
77 colorMap.put("RED", Color.RED);
78
79 colorMap.put("PINK", Color.PINK);
80 }
81
82 public void populatePrintFontMap() {
83 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
84 Map<String, String> criteriaMap = new HashMap<String, String>();
85 criteriaMap.put("namespaceCode", "OLE-PRNT");
86 criteriaMap.put("componentCode", "Patron Bill Font");
87 List<ParameterBo> parametersList = (List<ParameterBo>) businessObjectService.findMatching(ParameterBo.class, criteriaMap);
88 for (int i = 0; i < parametersList.size(); i++) {
89 printFontMap.put(parametersList.get(i).getName(), fontMap.get(parametersList.get(i).getValue()));
90 }
91 }
92
93 public void populatePrintColorMap() {
94 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
95 Map<String, String> criteriaMap = new HashMap<String, String>();
96 criteriaMap.put("namespaceCode", "OLE-PRNT");
97 criteriaMap.put("componentCode", "Patron Bill Color");
98 List<ParameterBo> parametersList = (List<ParameterBo>) businessObjectService.findMatching(ParameterBo.class, criteriaMap);
99 for (int i = 0; i < parametersList.size(); i++) {
100 printColorMap.put(parametersList.get(i).getName(), colorMap.get(parametersList.get(i).getValue()));
101 }
102 }
103
104 public String getTemplate() {
105
106 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
107 Map<String, String> criteriaMap = new HashMap<String, String>();
108 criteriaMap.put("namespaceCode", "OLE-PRNT");
109 criteriaMap.put("componentCode", "Print Template");
110 List<ParameterBo> parametersList = (List<ParameterBo>) businessObjectService.findMatching(ParameterBo.class, criteriaMap);
111 return parametersList.get(0).getValue();
112
113 }
114
115
116
117
118
119
120
121
122 public void generatePdf(String firstName, String lastName, List<PatronBillPayment> patronBillPayments, List<FeeType> feeTypeList, HttpServletResponse response) {
123 String template = getTemplate();
124 if (template.equalsIgnoreCase(OLEConstants.BILL_TEMP_NORMAL)) {
125 createPdf(firstName, lastName, patronBillPayments, feeTypeList, response);
126 } else if (template.equalsIgnoreCase(OLEConstants.BILL_TEMP_TABLE)) {
127 createPdfWithTable(firstName, lastName, patronBillPayments, feeTypeList, response);
128 }
129 }
130
131
132
133
134
135
136
137
138 public void createPdf(String firstName, String lastName, List<PatronBillPayment> patronBillPayments, List<FeeType> feeTypeList, HttpServletResponse response) {
139 LOG.debug("Initialize Normal pdf Template");
140 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
141 BigDecimal feeAmount = BigDecimal.valueOf(0);
142 String billNumber = "";
143 try {
144 populateColorMap();
145 populateFontMap();
146 populatePrintColorMap();
147 populatePrintFontMap();
148 response.setContentType("application/pdf");
149 Document document = this.getDocument(0, 0, 5, 5);
150 document.open();
151 document.newPage();
152
153
154 Paragraph paraGraph = new Paragraph();
155 paraGraph.setAlignment(Element.ALIGN_CENTER);
156 paraGraph.add(new Chunk(OLEConstants.BILL_TITLE));
157 paraGraph.add(Chunk.NEWLINE);
158 paraGraph.add(Chunk.NEWLINE);
159 paraGraph.add(Chunk.NEWLINE);
160 paraGraph.add(new Chunk(OLEConstants.BILL_DT + " : " + patronBillPayments.get(0).getBillDate().toString() + " "));
161 paraGraph.add(Chunk.NEWLINE);
162 paraGraph.add(new Chunk(OLEConstants.FIRST_NAME + " : " + firstName, printFontMap.get("Patron_Name_Font")));
163 paraGraph.add(Chunk.NEWLINE);
164 paraGraph.add(new Chunk(OLEConstants.LAST_NAME + " : " + lastName, printFontMap.get("Item_Title_Font")));
165 paraGraph.add(Chunk.NEWLINE);
166 for (int j = 0; j < feeTypeList.size(); j++) {
167 if (!feeTypeList.get(j).getBillNumber().equals(billNumber)) {
168 paraGraph.add(new Chunk(OLEConstants.BILL_NO + " : " + feeTypeList.get(j).getBillNumber()));
169 paraGraph.add(Chunk.NEWLINE);
170 }
171 paraGraph.add(new Chunk(feeTypeList.get(j).getFeeTypeName() + " " + feeTypeList.get(j).getFeeAmount() + "$"));
172 paraGraph.add(Chunk.NEWLINE);
173
174
175 paraGraph.add(new Chunk(OLEConstants.ITM_BARCODE + " : " + feeTypeList.get(j).getItemBarcode(), printFontMap.get("Patron_Name_Font")));
176 paraGraph.add(Chunk.NEWLINE);
177 paraGraph.add(new Chunk(OLEConstants.ITM_TYP + " : " + feeTypeList.get(j).getItemType(), printFontMap.get("Patron_Name_Font")));
178 paraGraph.add(Chunk.NEWLINE);
179 paraGraph.add(new Chunk(OLEConstants.ITM_TIT + " : " + feeTypeList.get(j).getItemTitle(), printFontMap.get("Patron_Name_Font")));
180 paraGraph.add(Chunk.NEWLINE);
181 paraGraph.add(new Chunk(OLEConstants.ITM_BARCODE + " : " + feeTypeList.get(j).getItemAuthor(), printFontMap.get("Patron_Name_Font")));
182 paraGraph.add(Chunk.NEWLINE);
183 paraGraph.add(new Chunk(OLEConstants.ITM_BARCODE + " : " + feeTypeList.get(j).getItemCallNumber(), printFontMap.get("Patron_Name_Font")));
184 paraGraph.add(Chunk.NEWLINE);
185 paraGraph.add(new Chunk(OLEConstants.ITM_BARCODE + " : " + feeTypeList.get(j).getItemCopyNumber(), printFontMap.get("Patron_Name_Font")));
186 paraGraph.add(Chunk.NEWLINE);
187 paraGraph.add(Chunk.NEWLINE);
188 paraGraph.add(Chunk.NEWLINE);
189 feeAmount = feeAmount.add(feeTypeList.get(j).getFeeAmount());
190 billNumber = feeTypeList.get(j).getBillNumber();
191 }
192 paraGraph.add(Chunk.NEWLINE);
193 paraGraph.add(new Chunk(OLEConstants.TOT_AMT + " : " + feeAmount + "$", printFontMap.get("Total_Font")).setBackground(printColorMap.get("Total_BGColor")));
194 response.setContentType("application/pdf");
195 ServletOutputStream sos = response.getOutputStream();
196 PdfWriter.getInstance(document, sos);
197 document.open();
198 document.add(paraGraph);
199 document.close();
200
201
202 byteArrayOutputStream.flush();
203 byteArrayOutputStream.close();
204 sos.flush();
205 sos.close();
206 } catch (Exception e) {
207 LOG.error(e, e);
208 }
209 }
210
211
212 public Document getDocument(float f1, float f2, float f3, float f4) {
213 Document document = new Document(PageSize.A4);
214 document.setMargins(f1, f2, f3, f4);
215 return document;
216 }
217
218
219
220
221
222
223
224
225 public void createPdfWithTable(String firstName, String lastName, List<PatronBillPayment> patronBillPayments, List<FeeType> feeTypeList, HttpServletResponse response) {
226 LOG.debug("Initialize Table pdf Template");
227 OutputStream out = null;
228 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
229 BigDecimal feeAmount = BigDecimal.valueOf(0);
230 try {
231 populateColorMap();
232 populateFontMap();
233 populatePrintColorMap();
234 populatePrintFontMap();
235 response.setContentType("application/pdf");
236 Document document = this.getDocument(0, 0, 5, 5);
237 document.open();
238 document.newPage();
239 PdfPTable pdfTable = new PdfPTable(9);
240 pdfTable.getDefaultCell().setBorder(0);
241 Table table = new Table(9);
242
243 table.setWidth(100);
244 table.setDefaultVerticalAlignment(Element.ALIGN_TOP);
245 table.setCellsFitPage(true);
246 table.setPadding(2);
247 table.setSpacing(0);
248 Paragraph paraGraph = new Paragraph();
249 paraGraph.setAlignment(Element.ALIGN_CENTER);
250 paraGraph.add(new Chunk(OLEConstants.BILL_TITLE));
251 paraGraph.add(Chunk.NEWLINE);
252 paraGraph.add(Chunk.NEWLINE);
253 paraGraph.add(Chunk.NEWLINE);
254 paraGraph.add(new Chunk(OLEConstants.BILL_DT + " : " + patronBillPayments.get(0).getBillDate().toString() + " "));
255 paraGraph.add(Chunk.NEWLINE);
256 paraGraph.add(new Chunk(OLEConstants.FIRST_NAME + " : " + firstName, printFontMap.get("Patron_Name_Font")));
257 paraGraph.add(Chunk.NEWLINE);
258 paraGraph.add(new Chunk(OLEConstants.LAST_NAME + " : " + lastName, printFontMap.get("Patron_Name_Font")));
259 paraGraph.add(Chunk.NEWLINE);
260 Cell cell01 = new Cell(OLEConstants.BILL_NO);
261 cell01.setBackgroundColor(Color.gray);
262 table.addCell(cell01);
263 Cell cell02 = new Cell(OLEConstants.FEE_TYPE);
264 cell02.setBackgroundColor(Color.gray);
265 table.addCell(cell02);
266 Cell cell03 = new Cell(OLEConstants.FEE_AMT);
267 cell03.setBackgroundColor(Color.gray);
268 table.addCell(cell03);
269 Cell cell04 = new Cell(OLEConstants.ITM_BARCODE);
270 cell04.setBackgroundColor(Color.gray);
271 table.addCell(cell04);
272 Cell cell05 = new Cell(OLEConstants.ITM_TYP);
273 cell05.setBackgroundColor(Color.gray);
274 table.addCell(cell05);
275 Cell cell06 = new Cell(OLEConstants.ITM_TIT);
276 cell06.setBackgroundColor(Color.gray);
277 table.addCell(cell06);
278 Cell cell07 = new Cell(OLEConstants.ITM_AUT);
279 cell07.setBackgroundColor(Color.gray);
280 table.addCell(cell07);
281 Cell cell08 = new Cell(OLEConstants.ITM_CALL);
282 cell08.setBackgroundColor(Color.gray);
283 table.addCell(cell08);
284 Cell cell09 = new Cell(OLEConstants.ITM_COPY);
285 cell09.setBackgroundColor(Color.gray);
286 table.addCell(cell09);
287 for (int i = 0; i < feeTypeList.size(); i++) {
288 Cell cell = new Cell(feeTypeList.get(i).getBillNumber());
289 table.addCell(cell);
290 Cell cell1 = new Cell(feeTypeList.get(i).getFeeTypeName());
291 table.addCell(cell1);
292 Cell cell2 = new Cell(feeTypeList.get(i).getFeeAmount().toString());
293 table.addCell(cell2);
294 Cell cell3 = new Cell(feeTypeList.get(i).getItemBarcode());
295 table.addCell(cell3);
296 Cell cell4 = new Cell(feeTypeList.get(i).getItemType());
297 table.addCell(cell4);
298 Cell cell5 = new Cell(feeTypeList.get(i).getItemTitle());
299 table.addCell(cell5);
300 Cell cell6 = new Cell(feeTypeList.get(i).getItemAuthor());
301 table.addCell(cell6);
302 Cell cell7 = new Cell(feeTypeList.get(i).getItemCallNumber());
303 table.addCell(cell7);
304 Cell cell8 = new Cell(feeTypeList.get(i).getItemCopyNumber());
305 table.addCell(cell8);
306 feeAmount = feeAmount.add(feeTypeList.get(i).getFeeAmount());
307 }
308 paraGraph.add(new Chunk(OLEConstants.TOT_AMT + " : " + feeAmount + "$", printFontMap.get("Patron_Name_Font")));
309 paraGraph.add(Chunk.NEWLINE);
310 response.setContentType("application/pdf");
311 ServletOutputStream sos = response.getOutputStream();
312 PdfWriter.getInstance(document, sos);
313 document.open();
314 document.add(paraGraph);
315 document.add(table);
316 document.close();
317 byteArrayOutputStream.flush();
318 byteArrayOutputStream.close();
319 } catch (Exception e) {
320 LOG.error(e, e);
321 }
322 }
323 }
324
325