001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * Created on Sep 6, 2005
018: *
019: */
020: package org.kuali.module.purap.pdf;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024: import java.io.FileNotFoundException;
025: import java.io.FileOutputStream;
026: import java.util.ArrayList;
027: import java.util.Calendar;
028: import java.util.Collection;
029:
030: import org.kuali.module.purap.bo.PurchaseOrderVendorQuote;
031: import org.kuali.module.purap.document.PurchaseOrderDocument;
032:
033: import com.lowagie.text.Document;
034: import com.lowagie.text.DocumentException;
035: import com.lowagie.text.Element;
036: import com.lowagie.text.ExceptionConverter;
037: import com.lowagie.text.Font;
038: import com.lowagie.text.FontFactory;
039: import com.lowagie.text.PageSize;
040: import com.lowagie.text.Paragraph;
041: import com.lowagie.text.pdf.BaseFont;
042: import com.lowagie.text.pdf.PdfContentByte;
043: import com.lowagie.text.pdf.PdfPCell;
044: import com.lowagie.text.pdf.PdfPTable;
045: import com.lowagie.text.pdf.PdfPageEventHelper;
046: import com.lowagie.text.pdf.PdfTemplate;
047: import com.lowagie.text.pdf.PdfWriter;
048:
049: /**
050: * Base class to handle pdf for purchase order quote request documents.
051: *
052: */
053: public class PurchaseOrderQuoteRequestsPdf extends PdfPageEventHelper {
054: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
055: .getLogger(PurchaseOrderQuoteRequestsPdf.class);
056:
057: public PdfTemplate tpl; // A template that will hold the total number of pages.
058: public PdfContentByte cb;
059: public PdfPTable headerTable;
060: public PurchaseOrderDocument po;
061: public BaseFont helv;
062:
063: Font titleFont = FontFactory.getFont("ARIAL", 14, 0);
064: Font cellTitleFont = FontFactory.getFont("ARIAL", 8, 0);
065: Font cellTextFont = FontFactory.getFont("ARIAL", 12, 0);
066:
067: public PurchaseOrderQuoteRequestsPdf() {
068: super ();
069: }
070:
071: /**
072: * Overrides the method in PdfPageEventHelper from itext to initialize the template and font for purchase
073: * order quote request pdf documents.
074: *
075: * @param writer The PdfWriter for this document.
076: * @param document The document.
077: * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
078: */
079: public void onOpenDocument(PdfWriter writer, Document document) {
080: LOG.debug("onOpenDocument() started.");
081: try {
082: // initialization of the template
083: tpl = writer.getDirectContent().createTemplate(100, 100);
084: // initialization of the font
085: helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI,
086: false);
087: } catch (Exception e) {
088: throw new ExceptionConverter(e);
089: }
090: }
091:
092: /**
093: * Overrides the method in PdfPageEventHelper from itext to compose the footer and show the
094: * footer.
095: *
096: * @param writer The PdfWriter for this document.
097: * @param document The document.
098: * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
099: */
100: public void onEndPage(PdfWriter writer, Document document) {
101: LOG.debug("onEndPage() started.");
102: PdfContentByte cb = writer.getDirectContent();
103: cb.saveState();
104: // compose the footer
105: String text = "Page " + writer.getPageNumber() + " of ";
106: float textSize = helv.getWidthPoint(text, 12);
107: float textBase = document.bottom() - 20;
108: cb.beginText();
109: cb.setFontAndSize(helv, 12);
110: // show the footer
111: float adjust = helv.getWidthPoint("0", 12);
112: cb
113: .setTextMatrix(document.right() - textSize - adjust,
114: textBase);
115: cb.showText(text);
116: cb.endText();
117: cb.addTemplate(tpl, document.right() - adjust, textBase);
118: cb.saveState();
119: }
120:
121: /**
122: * Overrides the method in the PdfPageEventHelper from itext to put the total number of pages into the template.
123: *
124: * @param writer The PdfWriter for this document.
125: * @param document The document.
126: * @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
127: */
128: public void onCloseDocument(PdfWriter writer, Document document) {
129: LOG.debug("onCloseDocument() started.");
130: tpl.beginText();
131: tpl.setFontAndSize(helv, 12);
132: tpl.setTextMatrix(0, 0);
133: tpl.showText("" + (writer.getPageNumber() - 1));
134: tpl.endText();
135: }
136:
137: /**
138: * Gets a PageEvents object.
139: *
140: * @return a new PageEvents object
141: */
142: public PurchaseOrderPdf getPageEvents() {
143: LOG.debug("getPageEvents() started.");
144: return new PurchaseOrderPdf();
145: }
146:
147: /**
148: * Creates an instance of a new Document and set its margins.
149: *
150: * @return The created Document object.
151: */
152: private Document getDocument() throws DocumentException {
153: LOG.debug("getDocument() started");
154: Document document = new Document(PageSize.A4);
155: // Margins: 36pt = 0.5 inch, 72pt = 1 inch. Left, right, top, bottom.
156: document.setMargins(9, 9, 25, 36);
157: return document;
158: }
159:
160: /**
161: * Generates the purchase order quote request list pdf document based on the data in the given input parameters
162: * by creating a pdf writer using the given byteArrayOutputStream then calls the createPOQuoteRequestsListPdf to
163: * write the pdf document into the writer.
164: *
165: * @param po The PurchaseOrderDocument to be used to generate the pdf.
166: * @param byteArrayOutputStream The ByteArrayOutputStream to print the pdf to.
167: * @return Collection of errors which are made of the messages from DocumentException.
168: */
169: public Collection generatePOQuoteRequestsListPdf(
170: PurchaseOrderDocument po,
171: ByteArrayOutputStream byteArrayOutputStream) {
172: LOG
173: .debug("generatePOQuoteRequestsListPDF() started for po number "
174: + po.getPurapDocumentIdentifier());
175:
176: Collection errors = new ArrayList();
177:
178: try {
179: Document doc = this .getDocument();
180: PdfWriter writer = PdfWriter.getInstance(doc,
181: byteArrayOutputStream);
182: this .createPOQuoteRequestsListPdf(po, doc, writer);
183: } catch (DocumentException de) {
184: LOG.error(de.getMessage(), de);
185: errors.add(de.getMessage());
186: }
187: return errors;
188: }
189:
190: /**
191: * Invokes the createPOQuoteRequestsListPdf method to create a purchase order quote list request pdf document
192: * and saves it into a file which name and location are specified in the input parameters.
193: *
194: * @param po The PurchaseOrderDocument to be used to generate the pdf.
195: * @param pdfFileLocation The location to save the pdf file.
196: * @param pdfFilename The name for the pdf file.
197: * @return Collection of errors which are made of the messages from DocumentException.
198: */
199: public Collection savePOQuoteRequestsListPdf(
200: PurchaseOrderDocument po, String pdfFileLocation,
201: String pdfFilename) {
202: LOG.debug("savePOQuoteRequestsListPDF() started for po number "
203: + po.getPurapDocumentIdentifier());
204:
205: Collection errors = new ArrayList();
206:
207: try {
208: Document doc = this .getDocument();
209: PdfWriter writer = PdfWriter
210: .getInstance(doc, new FileOutputStream(
211: pdfFileLocation + pdfFilename));
212: this .createPOQuoteRequestsListPdf(po, doc, writer);
213: } catch (DocumentException de) {
214: LOG.error(de.getMessage(), de);
215: errors.add(de.getMessage());
216: } catch (FileNotFoundException f) {
217: LOG.error(f.getMessage(), f);
218: errors.add(f.getMessage());
219: }
220: return errors;
221: }
222:
223: /**
224: * Deletes an already created PDF.
225: *
226: * @param pdfFileLocation The location to save the pdf file.
227: * @param pdfFilename The name for the pdf file.
228: */
229: public void deletePdf(String pdfFileLocation, String pdfFilename) {
230: LOG
231: .debug("deletePdf() started for po pdf file: "
232: + pdfFilename);
233:
234: File f = new File(pdfFileLocation + pdfFilename);
235: f.delete();
236: }
237:
238: /**
239: * Creates the pdf using given input parameters.
240: *
241: * @param po The PurchaseOrderDocument to be used to create the pdf.
242: * @param document The pdf document whose margins have already been set.
243: * @param writer The PdfWriter to write the pdf document into.
244: * @throws DocumentException
245: */
246: private void createPOQuoteRequestsListPdf(PurchaseOrderDocument po,
247: Document document, PdfWriter writer)
248: throws DocumentException {
249: LOG
250: .debug("createPOQuoteRequestsListPdf() started for po number "
251: + po.getPurapDocumentIdentifier());
252:
253: // These have to be set because they are used by the onOpenDocument() method.
254: this .po = po;
255:
256: // Turn on the page events that handle the header and page numbers.
257: PurchaseOrderPdf events = new PurchaseOrderPdf()
258: .getPageEvents();
259: writer.setPageEvent(this ); // Passing in "this" lets it know about the po, campusName, etc.
260:
261: document.open();
262:
263: PdfPCell cell;
264: Paragraph p = new Paragraph();
265:
266: float[] headerWidths = { 0.25f, 0.25f, 0.25f, 0.25f };
267: headerTable = new PdfPTable(headerWidths);
268: headerTable.setWidthPercentage(100);
269: headerTable.setHorizontalAlignment(Element.ALIGN_CENTER);
270:
271: headerTable.getDefaultCell().setBorderWidth(0);
272: headerTable.getDefaultCell().setHorizontalAlignment(
273: Element.ALIGN_CENTER);
274: headerTable.getDefaultCell().setVerticalAlignment(
275: Element.ALIGN_CENTER);
276:
277: // New row
278: cell = new PdfPCell(
279: new Paragraph(
280: "Indiana University\nRequest for Quotation Vendor List\n\n",
281: titleFont));
282: cell.setBorderWidth(0);
283: cell.setHorizontalAlignment(Element.ALIGN_CENTER);
284: cell.setColspan(4);
285: headerTable.addCell(cell);
286:
287: // New row
288: cell = new PdfPCell(new Paragraph("PO Number: "
289: + po.getPurapDocumentIdentifier(), cellTextFont));
290: cell.setHorizontalAlignment(Element.ALIGN_CENTER);
291: cell.setBorderWidth(0);
292: headerTable.addCell(cell);
293:
294: cell = new PdfPCell(new Paragraph("Req. Number: "
295: + po.getRequisitionIdentifier(), cellTextFont));
296: cell.setHorizontalAlignment(Element.ALIGN_CENTER);
297: cell.setBorderWidth(0);
298: headerTable.addCell(cell);
299:
300: Calendar today = Calendar.getInstance();
301: // MONTH starts w January = 0.
302: cell = new PdfPCell(new Paragraph("Printed: "
303: + (today.get(Calendar.YEAR) + "-"
304: + today.get(Calendar.MONTH) + 1 + "-" + today
305: .get(Calendar.DATE)), cellTextFont));
306: cell.setBorderWidth(0);
307: cell.setHorizontalAlignment(Element.ALIGN_CENTER);
308: headerTable.addCell(cell);
309:
310: if (po.getPurchaseOrderQuoteDueDate() != null) {
311: String dueDate = po.getPurchaseOrderQuoteDueDate()
312: .toString();
313: cell = new PdfPCell(new Paragraph("Due: " + dueDate
314: + "\n\n", cellTextFont));
315: } else {
316: cell = new PdfPCell(new Paragraph("Due: N/A\n\n",
317: cellTextFont));
318: }
319: cell.setBorderWidth(0);
320: cell.setHorizontalAlignment(Element.ALIGN_CENTER);
321: headerTable.addCell(cell);
322:
323: document.add(headerTable);
324:
325: // ***** List table *****
326: LOG.debug("createPOQuoteRequestsListPdf() list table started.");
327: float[] listWidths = { 0.20f, 0.20f, 0.20f, 0.20f, 0.20f };
328: PdfPTable listTable = new PdfPTable(listWidths);
329: listTable.setWidthPercentage(100);
330: listTable.setHorizontalAlignment(Element.ALIGN_CENTER);
331:
332: cell = new PdfPCell(new Paragraph("Vendor Name", cellTextFont));
333: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
334: cell.setBorderWidth(0);
335: listTable.addCell(cell);
336: cell = new PdfPCell(new Paragraph("City", cellTextFont));
337: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
338: cell.setBorderWidth(0);
339: listTable.addCell(cell);
340: cell = new PdfPCell(new Paragraph("Attention", cellTextFont));
341: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
342: cell.setBorderWidth(0);
343: listTable.addCell(cell);
344: cell = new PdfPCell(new Paragraph("Fax #", cellTextFont));
345: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
346: cell.setBorderWidth(0);
347: listTable.addCell(cell);
348: cell = new PdfPCell(new Paragraph("Received", cellTextFont));
349: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
350: cell.setBorderWidth(0);
351: listTable.addCell(cell);
352:
353: // The line under the headings.
354: cell = new PdfPCell(new Paragraph(" ", cellTitleFont));
355: cell.setFixedHeight(1);
356: cell.setColspan(5);
357: listTable.addCell(cell);
358:
359: for (PurchaseOrderVendorQuote poqv : po
360: .getPurchaseOrderVendorQuotes()) {
361: cell = new PdfPCell(new Paragraph(poqv.getVendorName(),
362: cellTextFont));
363: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
364: cell.setBorderWidth(0);
365: listTable.addCell(cell);
366: if (poqv.getVendorStateCode() != null) {
367: cell = new PdfPCell(new Paragraph(poqv
368: .getVendorCityName()
369: + ", " + poqv.getVendorStateCode(),
370: cellTextFont));
371: } else if (poqv.getVendorCountryCode() != null) {
372: cell = new PdfPCell(new Paragraph(poqv
373: .getVendorCityName()
374: + ", " + poqv.getVendorCountryCode(),
375: cellTextFont));
376: } else {
377: cell = new PdfPCell(new Paragraph(poqv
378: .getVendorCityName(), cellTextFont));
379: }
380: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
381: cell.setBorderWidth(0);
382: listTable.addCell(cell);
383: cell = new PdfPCell(new Paragraph(poqv
384: .getVendorAttentionName(), cellTextFont));
385: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
386: cell.setBorderWidth(0);
387: listTable.addCell(cell);
388: cell = new PdfPCell(new Paragraph(
389: poqv.getVendorFaxNumber(), cellTextFont));
390: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
391: cell.setBorderWidth(0);
392: listTable.addCell(cell);
393: cell = new PdfPCell(new Paragraph("__________",
394: cellTextFont));
395: cell.setHorizontalAlignment(Element.ALIGN_LEFT);
396: cell.setBorderWidth(0);
397: listTable.addCell(cell);
398: }
399:
400: document.add(listTable);
401:
402: document.close();
403: LOG.debug("createPOQuoteRequestsListPdf()pdf document closed.");
404: } // End of createQuotePdf()
405: }
|