View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * Created on Sep 6, 2005
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   * Base class to handle pdf for purchase order quote request documents.
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; // A template that will hold the total number of pages.
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       * Overrides the method in PdfPageEventHelper from itext to initialize the template and font for purchase
62       * order quote request pdf documents.
63       *
64       * @param writer   The PdfWriter for this document.
65       * @param document The document.
66       * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
67       */
68      public void onOpenDocument(PdfWriter writer, Document document) {
69          LOG.debug("onOpenDocument() started.");
70          try {
71              // initialization of the template
72              tpl = writer.getDirectContent().createTemplate(100, 100);
73              // initialization of the font
74              helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
75          } catch (Exception e) {
76              throw new ExceptionConverter(e);
77          }
78      }
79  
80      /**
81       * Overrides the method in PdfPageEventHelper from itext to compose the footer and show the
82       * footer.
83       *
84       * @param writer   The PdfWriter for this document.
85       * @param document The document.
86       * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
87       */
88      public void onEndPage(PdfWriter writer, Document document) {
89          LOG.debug("onEndPage() started.");
90          PdfContentByte cb = writer.getDirectContent();
91          cb.saveState();
92          // compose the footer
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          // show the footer
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      * Overrides the method in the PdfPageEventHelper from itext to put the total number of pages into the template.
110      *
111      * @param writer   The PdfWriter for this document.
112      * @param document The document.
113      * @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
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      * Gets a PageEvents object.
126      *
127      * @return a new PageEvents object
128      */
129     public PurchaseOrderPdf getPageEvents() {
130         LOG.debug("getPageEvents() started.");
131         return new PurchaseOrderPdf();
132     }
133 
134     /**
135      * Creates an instance of a new Document and set its margins.
136      *
137      * @return The created Document object.
138      */
139     private Document getDocument() throws DocumentException {
140         LOG.debug("getDocument() started");
141         Document document = new Document(PageSize.A4);
142         // Margins: 36pt = 0.5 inch, 72pt = 1 inch. Left, right, top, bottom.
143         document.setMargins(9, 9, 25, 36);
144         return document;
145     }
146 
147     /**
148      * Generates the purchase order quote request list pdf document based on the data in the given input parameters
149      * by creating a pdf writer using the given byteArrayOutputStream then calls the createPOQuoteRequestsListPdf to
150      * write the pdf document into the writer.
151      *
152      * @param po                    The PurchaseOrderDocument to be used to generate the pdf.
153      * @param byteArrayOutputStream The ByteArrayOutputStream to print the pdf to.
154      * @param institutionName       The purchasing institution name.
155      * @return Collection of errors which are made of the messages from DocumentException.
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      * Invokes the createPOQuoteRequestsListPdf method to create a purchase order quote list request pdf document
177      * and saves it into a file which name and location are specified in the input parameters.
178      *
179      * @param po              The PurchaseOrderDocument to be used to generate the pdf.
180      * @param pdfFileLocation The location to save the pdf file.
181      * @param pdfFilename     The name for the pdf file.
182      * @param institutionName The purchasing institution name.
183      * @return Collection of errors which are made of the messages from DocumentException.
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      * Deletes an already created PDF.
208      *
209      * @param pdfFileLocation The location to save the pdf file.
210      * @param pdfFilename     The name for the pdf file.
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      * Creates the pdf using given input parameters.
223      *
224      * @param po       The PurchaseOrderDocument to be used to create the pdf.
225      * @param document The pdf document whose margins have already been set.
226      * @param writer   The PdfWriter to write the pdf document into.
227      * @param instName The purchasing institution name
228      * @throws DocumentException
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         // These have to be set because they are used by the onOpenDocument() method.
236         this.po = po;
237 
238         // Turn on the page events that handle the header and page numbers.
239         PurchaseOrderPdf events = new PurchaseOrderPdf().getPageEvents();
240         writer.setPageEvent(this); // Passing in "this" lets it know about the po, campusName, etc.
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         // New row
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         // New row
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         // Date format pattern: MM-dd-yyyy
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         // ***** List table *****
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         // The line under the headings.
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     } // End of createQuotePdf()
362 }