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  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   * Base class to handle pdf for purchase order quote request documents.
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; // A template that will hold the total number of pages.
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       * Overrides the method in PdfPageEventHelper from itext to initialize the template and font for purchase
64       * order quote request pdf documents.
65       *
66       * @param writer   The PdfWriter for this document.
67       * @param document The document.
68       * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
69       */
70      public void onOpenDocument(PdfWriter writer, Document document) {
71          LOG.debug("onOpenDocument() started.");
72          try {
73              // initialization of the template
74              tpl = writer.getDirectContent().createTemplate(100, 100);
75              // initialization of the font
76              helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
77          } catch (Exception e) {
78              throw new ExceptionConverter(e);
79          }
80      }
81  
82      /**
83       * Overrides the method in PdfPageEventHelper from itext to compose the footer and show the
84       * footer.
85       *
86       * @param writer   The PdfWriter for this document.
87       * @param document The document.
88       * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
89       */
90      public void onEndPage(PdfWriter writer, Document document) {
91          LOG.debug("onEndPage() started.");
92          PdfContentByte cb = writer.getDirectContent();
93          cb.saveState();
94          // compose the footer
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         // show the footer
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      * Overrides the method in the PdfPageEventHelper from itext to put the total number of pages into the template.
112      *
113      * @param writer   The PdfWriter for this document.
114      * @param document The document.
115      * @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
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      * Gets a PageEvents object.
128      *
129      * @return a new PageEvents object
130      */
131     public PurchaseOrderPdf getPageEvents() {
132         LOG.debug("getPageEvents() started.");
133         return new PurchaseOrderPdf();
134     }
135 
136     /**
137      * Creates an instance of a new Document and set its margins.
138      *
139      * @return The created Document object.
140      */
141     private Document getDocument() throws DocumentException {
142         LOG.debug("getDocument() started");
143         Document document = new Document(PageSize.A4);
144         // Margins: 36pt = 0.5 inch, 72pt = 1 inch. Left, right, top, bottom.
145         document.setMargins(9, 9, 25, 36);
146         return document;
147     }
148 
149     /**
150      * Generates the purchase order quote request list pdf document based on the data in the given input parameters
151      * by creating a pdf writer using the given byteArrayOutputStream then calls the createPOQuoteRequestsListPdf to
152      * write the pdf document into the writer.
153      *
154      * @param po                    The PurchaseOrderDocument to be used to generate the pdf.
155      * @param byteArrayOutputStream The ByteArrayOutputStream to print the pdf to.
156      * @param institutionName       The purchasing institution name.
157      * @return Collection of errors which are made of the messages from DocumentException.
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      * Invokes the createPOQuoteRequestsListPdf method to create a purchase order quote list request pdf document
179      * and saves it into a file which name and location are specified in the input parameters.
180      *
181      * @param po              The PurchaseOrderDocument to be used to generate the pdf.
182      * @param pdfFileLocation The location to save the pdf file.
183      * @param pdfFilename     The name for the pdf file.
184      * @param institutionName The purchasing institution name.
185      * @return Collection of errors which are made of the messages from DocumentException.
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      * Deletes an already created PDF.
210      *
211      * @param pdfFileLocation The location to save the pdf file.
212      * @param pdfFilename     The name for the pdf file.
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      * Creates the pdf using given input parameters.
225      *
226      * @param po       The PurchaseOrderDocument to be used to create the pdf.
227      * @param document The pdf document whose margins have already been set.
228      * @param writer   The PdfWriter to write the pdf document into.
229      * @param instName The purchasing institution name
230      * @throws DocumentException
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         // These have to be set because they are used by the onOpenDocument() method.
238         this.po = po;
239 
240         // Turn on the page events that handle the header and page numbers.
241         PurchaseOrderPdf events = new PurchaseOrderPdf().getPageEvents();
242         writer.setPageEvent(this); // Passing in "this" lets it know about the po, campusName, etc.
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         // New row
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         // New row
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         // Date format pattern: MM-dd-yyyy
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         // ***** List table *****
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         // The line under the headings.
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     } // End of createQuotePdf()
364 }