1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.kuali.ole.module.purap.pdf;
21
22 import com.lowagie.text.*;
23 import com.lowagie.text.pdf.*;
24 import org.kuali.ole.module.purap.PurapConstants;
25 import org.kuali.ole.module.purap.businessobject.PurchaseOrderVendorQuote;
26 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
27 import org.kuali.ole.module.purap.util.PurApDateFormatUtils;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.rice.core.api.datetime.DateTimeService;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.File;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.sql.Date;
36 import java.text.SimpleDateFormat;
37 import java.util.ArrayList;
38 import java.util.Collection;
39
40
41
42
43 public class PurchaseOrderQuoteRequestsPdf extends PdfPageEventHelper {
44 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderQuoteRequestsPdf.class);
45
46 public PdfTemplate tpl;
47 public PdfContentByte cb;
48 public PdfPTable headerTable;
49 public PurchaseOrderDocument po;
50 public BaseFont helv;
51
52 Font titleFont = FontFactory.getFont("ARIAL", 14, 0);
53 Font cellTitleFont = FontFactory.getFont("ARIAL", 8, 0);
54 Font cellTextFont = FontFactory.getFont("ARIAL", 12, 0);
55
56 public PurchaseOrderQuoteRequestsPdf() {
57 super();
58 }
59
60
61
62
63
64
65
66
67
68 public void onOpenDocument(PdfWriter writer, Document document) {
69 LOG.debug("onOpenDocument() started.");
70 try {
71
72 tpl = writer.getDirectContent().createTemplate(100, 100);
73
74 helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
75 } catch (Exception e) {
76 throw new ExceptionConverter(e);
77 }
78 }
79
80
81
82
83
84
85
86
87
88 public void onEndPage(PdfWriter writer, Document document) {
89 LOG.debug("onEndPage() started.");
90 PdfContentByte cb = writer.getDirectContent();
91 cb.saveState();
92
93 String text = "Page " + writer.getPageNumber() + " of ";
94 float textSize = helv.getWidthPoint(text, 12);
95 float textBase = document.bottom() - 20;
96 cb.beginText();
97 cb.setFontAndSize(helv, 12);
98
99 float adjust = helv.getWidthPoint("0", 12);
100 cb.setTextMatrix(document.right() - textSize - adjust, textBase);
101 cb.showText(text);
102 cb.endText();
103 cb.addTemplate(tpl, document.right() - adjust, textBase);
104 cb.saveState();
105 }
106
107
108
109
110
111
112
113
114
115 public void onCloseDocument(PdfWriter writer, Document document) {
116 LOG.debug("onCloseDocument() started.");
117 tpl.beginText();
118 tpl.setFontAndSize(helv, 12);
119 tpl.setTextMatrix(0, 0);
120 tpl.showText("" + (writer.getPageNumber() - 1));
121 tpl.endText();
122 }
123
124
125
126
127
128
129 public PurchaseOrderPdf getPageEvents() {
130 LOG.debug("getPageEvents() started.");
131 return new PurchaseOrderPdf();
132 }
133
134
135
136
137
138
139 private Document getDocument() throws DocumentException {
140 LOG.debug("getDocument() started");
141 Document document = new Document(PageSize.A4);
142
143 document.setMargins(9, 9, 25, 36);
144 return document;
145 }
146
147
148
149
150
151
152
153
154
155
156
157 public Collection generatePOQuoteRequestsListPdf(PurchaseOrderDocument po, ByteArrayOutputStream byteArrayOutputStream, String institutionName) {
158 if (LOG.isDebugEnabled()) {
159 LOG.debug("generatePOQuoteRequestsListPDF() started for po number " + po.getPurapDocumentIdentifier());
160 }
161
162 Collection errors = new ArrayList();
163
164 try {
165 Document doc = this.getDocument();
166 PdfWriter writer = PdfWriter.getInstance(doc, byteArrayOutputStream);
167 this.createPOQuoteRequestsListPdf(po, doc, writer, institutionName);
168 } catch (DocumentException de) {
169 LOG.error(de.getMessage(), de);
170 errors.add(de.getMessage());
171 }
172 return errors;
173 }
174
175
176
177
178
179
180
181
182
183
184
185 public Collection savePOQuoteRequestsListPdf(PurchaseOrderDocument po, String pdfFileLocation, String pdfFilename, String institutionName) {
186 if (LOG.isDebugEnabled()) {
187 LOG.debug("savePOQuoteRequestsListPDF() started for po number " + po.getPurapDocumentIdentifier());
188 }
189
190 Collection errors = new ArrayList();
191
192 try {
193 Document doc = this.getDocument();
194 PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(pdfFileLocation + pdfFilename));
195 this.createPOQuoteRequestsListPdf(po, doc, writer, institutionName);
196 } catch (DocumentException de) {
197 LOG.error(de.getMessage(), de);
198 errors.add(de.getMessage());
199 } catch (FileNotFoundException f) {
200 LOG.error(f.getMessage(), f);
201 errors.add(f.getMessage());
202 }
203 return errors;
204 }
205
206
207
208
209
210
211
212 public void deletePdf(String pdfFileLocation, String pdfFilename) {
213 if (LOG.isDebugEnabled()) {
214 LOG.debug("deletePdf() started for po pdf file: " + pdfFilename);
215 }
216
217 File f = new File(pdfFileLocation + pdfFilename);
218 f.delete();
219 }
220
221
222
223
224
225
226
227
228
229
230 private void createPOQuoteRequestsListPdf(PurchaseOrderDocument po, Document document, PdfWriter writer, String instName) throws DocumentException {
231 if (LOG.isDebugEnabled()) {
232 LOG.debug("createPOQuoteRequestsListPdf() started for po number " + po.getPurapDocumentIdentifier());
233 }
234
235
236 this.po = po;
237
238
239 PurchaseOrderPdf events = new PurchaseOrderPdf().getPageEvents();
240 writer.setPageEvent(this);
241
242 document.open();
243
244 PdfPCell cell;
245 Paragraph p = new Paragraph();
246
247 float[] headerWidths = {0.25f, 0.25f, 0.25f, 0.25f};
248 headerTable = new PdfPTable(headerWidths);
249 headerTable.setWidthPercentage(100);
250 headerTable.setHorizontalAlignment(Element.ALIGN_CENTER);
251
252 headerTable.getDefaultCell().setBorderWidth(0);
253 headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
254 headerTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
255
256
257 cell = new PdfPCell(new Paragraph(instName + "\nRequest for Quotation Vendor List\n\n", titleFont));
258 cell.setBorderWidth(0);
259 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
260 cell.setColspan(4);
261 headerTable.addCell(cell);
262
263
264 cell = new PdfPCell(new Paragraph("PO Number: " + po.getPurapDocumentIdentifier(), cellTextFont));
265 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
266 cell.setBorderWidth(0);
267 headerTable.addCell(cell);
268
269 cell = new PdfPCell(new Paragraph("Req. Number: " + po.getRequisitionIdentifier(), cellTextFont));
270 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
271 cell.setBorderWidth(0);
272 headerTable.addCell(cell);
273
274
275 SimpleDateFormat sdf = PurApDateFormatUtils.getSimpleDateFormat(PurapConstants.NamedDateFormats.KUALI_SIMPLE_DATE_FORMAT_2);
276 Date today = SpringContext.getBean(DateTimeService.class).getCurrentSqlDate();
277 cell = new PdfPCell(new Paragraph("Printed: " + sdf.format(today), cellTextFont));
278 cell.setBorderWidth(0);
279 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
280 headerTable.addCell(cell);
281
282 if (po.getPurchaseOrderQuoteDueDate() != null) {
283 Date dueDate = po.getPurchaseOrderQuoteDueDate();
284 cell = new PdfPCell(new Paragraph("Due: " + sdf.format(dueDate) + "\n\n", cellTextFont));
285 } else {
286 cell = new PdfPCell(new Paragraph("Due: N/A\n\n", cellTextFont));
287 }
288 cell.setBorderWidth(0);
289 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
290 headerTable.addCell(cell);
291
292 document.add(headerTable);
293
294
295 LOG.debug("createPOQuoteRequestsListPdf() list table started.");
296 float[] listWidths = {0.20f, 0.20f, 0.20f, 0.20f, 0.20f};
297 PdfPTable listTable = new PdfPTable(listWidths);
298 listTable.setWidthPercentage(100);
299 listTable.setHorizontalAlignment(Element.ALIGN_CENTER);
300
301 cell = new PdfPCell(new Paragraph("Vendor Name", cellTextFont));
302 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
303 cell.setBorderWidth(0);
304 listTable.addCell(cell);
305 cell = new PdfPCell(new Paragraph("City", cellTextFont));
306 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
307 cell.setBorderWidth(0);
308 listTable.addCell(cell);
309 cell = new PdfPCell(new Paragraph("Attention", cellTextFont));
310 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
311 cell.setBorderWidth(0);
312 listTable.addCell(cell);
313 cell = new PdfPCell(new Paragraph("Fax #", cellTextFont));
314 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
315 cell.setBorderWidth(0);
316 listTable.addCell(cell);
317 cell = new PdfPCell(new Paragraph("Received", cellTextFont));
318 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
319 cell.setBorderWidth(0);
320 listTable.addCell(cell);
321
322
323 cell = new PdfPCell(new Paragraph(" ", cellTitleFont));
324 cell.setFixedHeight(1);
325 cell.setColspan(5);
326 listTable.addCell(cell);
327
328 for (PurchaseOrderVendorQuote poqv : po.getPurchaseOrderVendorQuotes()) {
329 cell = new PdfPCell(new Paragraph(poqv.getVendorName(), cellTextFont));
330 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
331 cell.setBorderWidth(0);
332 listTable.addCell(cell);
333 if (poqv.getVendorStateCode() != null) {
334 cell = new PdfPCell(new Paragraph(poqv.getVendorCityName() + ", " + poqv.getVendorStateCode(), cellTextFont));
335 } else if (poqv.getVendorCountryCode() != null) {
336 cell = new PdfPCell(new Paragraph(poqv.getVendorCityName() + ", " + poqv.getVendorCountryCode(), cellTextFont));
337 } else {
338 cell = new PdfPCell(new Paragraph(poqv.getVendorCityName(), cellTextFont));
339 }
340 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
341 cell.setBorderWidth(0);
342 listTable.addCell(cell);
343 cell = new PdfPCell(new Paragraph(poqv.getVendorAttentionName(), cellTextFont));
344 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
345 cell.setBorderWidth(0);
346 listTable.addCell(cell);
347 cell = new PdfPCell(new Paragraph(poqv.getVendorFaxNumber(), cellTextFont));
348 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
349 cell.setBorderWidth(0);
350 listTable.addCell(cell);
351 cell = new PdfPCell(new Paragraph("__________", cellTextFont));
352 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
353 cell.setBorderWidth(0);
354 listTable.addCell(cell);
355 }
356
357 document.add(listTable);
358
359 document.close();
360 LOG.debug("createPOQuoteRequestsListPdf()pdf document closed.");
361 }
362 }