001: /*
002: * Copyright 2006-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: package org.kuali.module.gl.util;
017:
018: import java.io.FileOutputStream;
019: import java.text.SimpleDateFormat;
020: import java.util.Date;
021:
022: import com.lowagie.text.Document;
023: import com.lowagie.text.Element;
024: import com.lowagie.text.Font;
025: import com.lowagie.text.FontFactory;
026: import com.lowagie.text.PageSize;
027: import com.lowagie.text.pdf.PdfWriter;
028:
029: /**
030: * This class is used to generate PDF reports for G/L related information
031: */
032: public abstract class AbstractPdfReportGenerator {
033: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
034: .getLogger(AbstractPdfReportGenerator.class);
035: public static final String PDF_FILE_EXTENSION = ".pdf";
036: private static String REPORT_FILE_DATE_FORMAT = "yyyyMMdd_HHmmss";
037:
038: // generate the PDF report with the given information
039: /**
040: * This method generates a PDF report
041: *
042: * @param reportingDate date report supposed to be for
043: * @param title title of report
044: * @param reportFileName file name used for saving report
045: */
046: public void generatePdfReport(Date reportingDate, String title,
047: String reportFileName) {
048: Document document = this .getDocument();
049:
050: PDFPageHelper pageHelper = new PDFPageHelper();
051: pageHelper.setRunDate(reportingDate);
052: pageHelper.setHeaderFont(this .getHeaderFont());
053: pageHelper.setTitle(title);
054:
055: try {
056: PdfWriter pdfWriter = PdfWriter.getInstance(document,
057: new FileOutputStream(reportFileName));
058: pdfWriter.setPageEvent(pageHelper);
059:
060: document.open();
061: document.add(this .getReportContents());
062: } catch (Exception de) {
063: LOG.error("generateReport() Error creating PDF report", de);
064: throw new RuntimeException("Report Generation Failed");
065: } finally {
066: this .closeDocument(document);
067: }
068: }
069:
070: /**
071: * This method returns an Element object which is used to display the actual content of a PDF report
072: *
073: * @return Element represents report contents
074: */
075: public abstract Element getReportContents();
076:
077: /**
078: * This method returns a reports file name
079: *
080: * @param reportNamePrefix prefix of file name
081: * @param destinationDirectory destination directory of file
082: * @param reportingDate date report is for
083: * @return
084: */
085: protected String generateReportFileName(String reportNamePrefix,
086: String destinationDirectory, Date reportingDate) {
087: String reportFilename = destinationDirectory + "/"
088: + reportNamePrefix + "_";
089: SimpleDateFormat dateFormat = new SimpleDateFormat(
090: REPORT_FILE_DATE_FORMAT);
091: reportFilename = reportFilename
092: + dateFormat.format(reportingDate);
093: reportFilename = reportFilename + PDF_FILE_EXTENSION;
094:
095: return reportFilename;
096: }
097:
098: /**
099: * Returns a new document
100: *
101: * @return Document PDF document
102: */
103: protected Document getDocument() {
104: return new Document(PageSize.A4.rotate());
105: }
106:
107: /**
108: * Returns font for header
109: *
110: * @return Font font for header
111: */
112: protected Font getHeaderFont() {
113: return FontFactory.getFont(FontFactory.COURIER, 8, Font.BOLD);
114: }
115:
116: /**
117: * Return font for content
118: *
119: * @return
120: */
121: protected Font getTextFont() {
122: return FontFactory.getFont(FontFactory.COURIER, 8, Font.BOLD);
123: }
124:
125: /**
126: * Close access to document
127: *
128: * @param document document being closed
129: */
130: private void closeDocument(Document document) {
131: try {
132: if ((document != null) && document.isOpen()) {
133: document.close();
134: }
135: } catch (Throwable t) {
136: LOG.error("generateReport() Exception closing report", t);
137: }
138: }
139: }
|