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