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.FileNotFoundException;
019: import java.io.FileOutputStream;
020: import java.text.DecimalFormat;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.kuali.core.util.KualiDecimal;
027: import org.kuali.module.gl.bo.GlSummary;
028:
029: import com.lowagie.text.Document;
030: import com.lowagie.text.DocumentException;
031: import com.lowagie.text.ExceptionConverter;
032: import com.lowagie.text.Font;
033: import com.lowagie.text.FontFactory;
034: import com.lowagie.text.PageSize;
035: import com.lowagie.text.Phrase;
036: import com.lowagie.text.Rectangle;
037: import com.lowagie.text.pdf.PdfPCell;
038: import com.lowagie.text.pdf.PdfPTable;
039: import com.lowagie.text.pdf.PdfPageEventHelper;
040: import com.lowagie.text.pdf.PdfWriter;
041:
042: /**
043: * This class generates a report for balance encumbrance
044: */
045: public class BalanceEncumbranceReport {
046: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(BalanceEncumbranceReport.class);
048:
049: class PageHelper extends PdfPageEventHelper {
050: public Date runDate;
051: public Font headerFont;
052: public String title;
053: public String type;
054:
055: /**
056: * Adds date, title, and page number on end page.
057: *
058: * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
059: */
060: public void onEndPage(PdfWriter writer, Document document) {
061: try {
062: Rectangle page = document.getPageSize();
063: PdfPTable head = new PdfPTable(3);
064: head.setHeaderRows(2);
065: SimpleDateFormat sdf = new SimpleDateFormat(
066: "MM/dd/yyyy HH:mm:ss");
067: PdfPCell cell = new PdfPCell(new Phrase(sdf
068: .format(runDate), headerFont));
069: cell.setBorder(Rectangle.NO_BORDER);
070: head.addCell(cell);
071:
072: cell = new PdfPCell(new Phrase(title, headerFont));
073: cell.setBorder(Rectangle.NO_BORDER);
074: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
075: head.addCell(cell);
076:
077: cell = new PdfPCell(new Phrase("Page: "
078: + new Integer(writer.getPageNumber()),
079: headerFont));
080: cell.setBorder(Rectangle.NO_BORDER);
081: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
082: head.addCell(cell);
083:
084: cell = new PdfPCell(new Phrase("", headerFont));
085: cell.setBorder(Rectangle.NO_BORDER);
086: head.addCell(cell);
087:
088: cell = new PdfPCell(new Phrase(type, headerFont));
089: cell.setBorder(Rectangle.NO_BORDER);
090: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
091: head.addCell(cell);
092:
093: cell = new PdfPCell(new Phrase("", headerFont));
094: cell.setBorder(Rectangle.NO_BORDER);
095: head.addCell(cell);
096:
097: head.setTotalWidth(page.width() - document.leftMargin()
098: - document.rightMargin());
099: head.writeSelectedRows(0, -1, document.leftMargin(),
100: page.height() - document.topMargin()
101: + head.getTotalHeight(), writer
102: .getDirectContent());
103: } catch (Exception e) {
104: throw new ExceptionConverter(e);
105: }
106: }
107: }
108:
109: /**
110: * Print a balance summary report
111: *
112: * @param runDate
113: * @param fiscalYearName
114: * @param balanceTypeCodes
115: */
116: public void generateReport(Date runDate,
117: List<GlSummary> glBalances, String fiscalYearName,
118: List<String> balanceTypeCodes, String fileprefix,
119: String destinationDirectory) {
120: LOG.debug("generateReport() started");
121:
122: String reportTitle = "GL Summary for Fiscal Year "
123: + fiscalYearName;
124: this .generateReport(glBalances, balanceTypeCodes, runDate,
125: reportTitle, fileprefix, destinationDirectory);
126: }
127:
128: /**
129: * Generates report for balance encumbrance
130: *
131: * @param glBalances balances for G/L entries
132: * @param balanceTypeCodes balance type codes included in report
133: * @param runDate date report was run
134: * @param reportTitle title for report
135: * @param fileprefix file prefix for report
136: * @param destinationDirectory destination of report file
137: */
138: public void generateReport(List<GlSummary> glBalances,
139: List<String> balanceTypeCodes, Date runDate,
140: String reportTitle, String fileprefix,
141: String destinationDirectory) {
142: LOG.debug("generateReport() started");
143:
144: Font headerFont = FontFactory.getFont(FontFactory.COURIER, 8,
145: Font.BOLD);
146: Font textFont = FontFactory.getFont(FontFactory.COURIER, 8,
147: Font.NORMAL);
148:
149: Document document = new Document(PageSize.A4.rotate());
150:
151: PageHelper helper = new PageHelper();
152: helper.runDate = runDate;
153: helper.headerFont = headerFont;
154: helper.title = reportTitle;
155: helper.type = "Balance Type of ";
156:
157: int total = balanceTypeCodes.size();
158: int count = 0;
159: for (Iterator iter = balanceTypeCodes.iterator(); iter
160: .hasNext();) {
161: String element = (String) iter.next();
162: count++;
163: helper.type = helper.type + element;
164: if (count < total) {
165: helper.type = helper.type + "/";
166: }
167: }
168:
169: try {
170: String filename = destinationDirectory + "/" + fileprefix
171: + "_";
172: SimpleDateFormat sdf = new SimpleDateFormat(
173: "yyyyMMdd_HHmmss");
174: filename = filename + sdf.format(runDate);
175: filename = filename + ".pdf";
176: PdfWriter writer = PdfWriter.getInstance(document,
177: new FileOutputStream(filename));
178: writer.setPageEvent(helper);
179:
180: document.open();
181:
182: float[] widths = { 10, 15 };
183: PdfPTable balances = new PdfPTable(widths);
184: balances.setHeaderRows(1);
185: balances.setWidthPercentage(25);
186:
187: // Add headers
188: PdfPCell cell = new PdfPCell(new Phrase("Fund Group",
189: headerFont));
190: balances.addCell(cell);
191: cell = new PdfPCell(new Phrase("YTD Balance", headerFont));
192: balances.addCell(cell);
193:
194: DecimalFormat nf = new DecimalFormat();
195: nf.applyPattern("###,###,###,##0.00");
196:
197: GlSummary totals = new GlSummary();
198: KualiDecimal totalAmount = KualiDecimal.ZERO;
199: for (Iterator iter = glBalances.iterator(); iter.hasNext();) {
200: GlSummary gls = (GlSummary) iter.next();
201: totals.add(gls);
202:
203: cell = new PdfPCell(new Phrase(gls.getFundGroup(),
204: textFont));
205: balances.addCell(cell);
206:
207: totalAmount = gls.getBeginningBalance().add(
208: gls.getAnnualBalance());
209: totalAmount = totalAmount.add(gls
210: .getCgBeginningBalance());
211: cell = new PdfPCell(new Phrase(nf.format(totalAmount
212: .doubleValue()), textFont));
213: balances.addCell(cell);
214: }
215:
216: // Now add the total line
217: cell = new PdfPCell(new Phrase("Total", textFont));
218: balances.addCell(cell);
219:
220: totalAmount = totals.getBeginningBalance().add(
221: totals.getAnnualBalance());
222: totalAmount = totalAmount.add(totals
223: .getCgBeginningBalance());
224: cell = new PdfPCell(new Phrase(nf.format(totalAmount
225: .doubleValue()), textFont));
226: balances.addCell(cell);
227:
228: document.add(balances);
229: } catch (DocumentException de) {
230: LOG.error("generateReport() Error creating PDF report", de);
231: throw new RuntimeException("Report Generation Failed: "
232: + de.getMessage());
233: } catch (FileNotFoundException fnfe) {
234: LOG
235: .error("generateReport() Error writing PDF report",
236: fnfe);
237: throw new RuntimeException(
238: "Report Generation Failed: Error writing to file "
239: + fnfe.getMessage());
240: } finally {
241: if ((document != null) && document.isOpen()) {
242: document.close();
243: }
244: }
245: }
246: }
|