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.module.gl.bo.GlSummary;
027:
028: import com.lowagie.text.Document;
029: import com.lowagie.text.DocumentException;
030: import com.lowagie.text.ExceptionConverter;
031: import com.lowagie.text.Font;
032: import com.lowagie.text.FontFactory;
033: import com.lowagie.text.PageSize;
034: import com.lowagie.text.Phrase;
035: import com.lowagie.text.Rectangle;
036: import com.lowagie.text.pdf.PdfPCell;
037: import com.lowagie.text.pdf.PdfPTable;
038: import com.lowagie.text.pdf.PdfPageEventHelper;
039: import com.lowagie.text.pdf.PdfWriter;
040:
041: public class BalanceReport {
042: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
043: .getLogger(BalanceReport.class);
044:
045: class PageHelper extends PdfPageEventHelper {
046: public Date runDate;
047: public Font headerFont;
048: public String title;
049: public String type;
050:
051: /**
052: * Add date, page number, and title to end page
053: *
054: * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
055: */
056: public void onEndPage(PdfWriter writer, Document document) {
057: try {
058: Rectangle page = document.getPageSize();
059: PdfPTable head = new PdfPTable(3);
060: head.setHeaderRows(2);
061: SimpleDateFormat sdf = new SimpleDateFormat(
062: "MM/dd/yyyy HH:mm:ss");
063: PdfPCell cell = new PdfPCell(new Phrase(sdf
064: .format(runDate), headerFont));
065: cell.setBorder(Rectangle.NO_BORDER);
066: head.addCell(cell);
067:
068: cell = new PdfPCell(new Phrase(title, headerFont));
069: cell.setBorder(Rectangle.NO_BORDER);
070: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
071: head.addCell(cell);
072:
073: cell = new PdfPCell(new Phrase("Page: "
074: + new Integer(writer.getPageNumber()),
075: headerFont));
076: cell.setBorder(Rectangle.NO_BORDER);
077: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
078: head.addCell(cell);
079:
080: cell = new PdfPCell(new Phrase("", headerFont));
081: cell.setBorder(Rectangle.NO_BORDER);
082: head.addCell(cell);
083:
084: cell = new PdfPCell(new Phrase(type, headerFont));
085: cell.setBorder(Rectangle.NO_BORDER);
086: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
087: head.addCell(cell);
088:
089: cell = new PdfPCell(new Phrase("", headerFont));
090: cell.setBorder(Rectangle.NO_BORDER);
091: head.addCell(cell);
092:
093: head.setTotalWidth(page.width() - document.leftMargin()
094: - document.rightMargin());
095: head.writeSelectedRows(0, -1, document.leftMargin(),
096: page.height() - document.topMargin()
097: + head.getTotalHeight(), writer
098: .getDirectContent());
099: } catch (Exception e) {
100: throw new ExceptionConverter(e);
101: }
102: }
103: }
104:
105: /**
106: * Print a balance summary report
107: *
108: * @param runDate date report was run
109: * @param fiscalYearName name of fiscal year
110: * @param balanceTypeCodes list of balance type codes used for report
111: */
112: public void generateReport(Date runDate,
113: List<GlSummary> glBalances, String fiscalYearName,
114: List<String> balanceTypeCodes, String fileprefix,
115: String destinationDirectory) {
116: LOG.debug("generateReport() started");
117:
118: String reportTitle = "GL Summary for Fiscal Year "
119: + fiscalYearName;
120: this .generateReport(glBalances, balanceTypeCodes, runDate,
121: reportTitle, fileprefix, destinationDirectory);
122: }
123:
124: /**
125: * Print a balance summary report
126: *
127: * @param runDate date report was run
128: * @param fiscalYearName name of fiscal year
129: * @param balanceTypeCodes list of balance type codes used for report
130: */
131: public void generateReport(List<GlSummary> glBalances,
132: List<String> balanceTypeCodes, Date runDate,
133: String reportTitle, String fileprefix,
134: String destinationDirectory) {
135: LOG.debug("generateReport() started");
136:
137: Font headerFont = FontFactory.getFont(FontFactory.COURIER, 8,
138: Font.BOLD);
139: Font textFont = FontFactory.getFont(FontFactory.COURIER, 8,
140: Font.NORMAL);
141:
142: Document document = new Document(PageSize.A4.rotate());
143:
144: PageHelper helper = new PageHelper();
145: helper.runDate = runDate;
146: helper.headerFont = headerFont;
147: helper.title = reportTitle;
148: helper.type = "Balance Type of ";
149:
150: int total = balanceTypeCodes.size();
151: int count = 0;
152: for (Iterator iter = balanceTypeCodes.iterator(); iter
153: .hasNext();) {
154: String element = (String) iter.next();
155: count++;
156: helper.type = helper.type + element;
157: if (count < total) {
158: helper.type = helper.type + "/";
159: }
160: }
161:
162: try {
163: String filename = destinationDirectory + "/" + fileprefix
164: + "_";
165: SimpleDateFormat sdf = new SimpleDateFormat(
166: "yyyyMMdd_HHmmss");
167: filename = filename + sdf.format(runDate);
168: filename = filename + ".pdf";
169: PdfWriter writer = PdfWriter.getInstance(document,
170: new FileOutputStream(filename));
171: writer.setPageEvent(helper);
172:
173: document.open();
174:
175: float[] widths = { 10, 15, 15, 15, 15, 15, 15 };
176: PdfPTable balances = new PdfPTable(widths);
177: balances.setHeaderRows(3);
178: balances.setWidthPercentage(100);
179:
180: // Add headers
181: PdfPCell cell = new PdfPCell(new Phrase("Fund Group",
182: headerFont));
183: balances.addCell(cell);
184: cell = new PdfPCell(new Phrase("Beginning Balance",
185: headerFont));
186: balances.addCell(cell);
187: cell = new PdfPCell(
188: new Phrase("Annual Balance", headerFont));
189: balances.addCell(cell);
190: cell = new PdfPCell(new Phrase("Accum Amt for Jul",
191: headerFont));
192: balances.addCell(cell);
193: cell = new PdfPCell(new Phrase("Accum Amt for Aug",
194: headerFont));
195: balances.addCell(cell);
196: cell = new PdfPCell(new Phrase("Accum Amt for Sep",
197: headerFont));
198: balances.addCell(cell);
199: cell = new PdfPCell(new Phrase("Accum Amt for Oct",
200: headerFont));
201: balances.addCell(cell);
202:
203: cell = new PdfPCell(new Phrase("", headerFont));
204: balances.addCell(cell);
205:
206: cell = new PdfPCell(new Phrase("Accum Amt for Nov",
207: headerFont));
208: balances.addCell(cell);
209: cell = new PdfPCell(new Phrase("Accum Amt for Dec",
210: headerFont));
211: balances.addCell(cell);
212: cell = new PdfPCell(new Phrase("Accum Amt for Jan",
213: headerFont));
214: balances.addCell(cell);
215: cell = new PdfPCell(new Phrase("Accum Amt for Feb",
216: headerFont));
217: balances.addCell(cell);
218: cell = new PdfPCell(new Phrase("Accum Amt for Mar",
219: headerFont));
220: balances.addCell(cell);
221: cell = new PdfPCell(new Phrase("Accum Amt for Apr",
222: headerFont));
223: balances.addCell(cell);
224:
225: cell = new PdfPCell(new Phrase("", headerFont));
226: balances.addCell(cell);
227:
228: cell = new PdfPCell(new Phrase("Accum Amt for May",
229: headerFont));
230: balances.addCell(cell);
231: cell = new PdfPCell(new Phrase("Accum Amt for Jun",
232: headerFont));
233: balances.addCell(cell);
234: cell = new PdfPCell(new Phrase("Accum Amt for Close",
235: headerFont));
236: balances.addCell(cell);
237: cell = new PdfPCell(new Phrase("Acctg Period Accum Amt",
238: headerFont));
239: balances.addCell(cell);
240: cell = new PdfPCell(new Phrase("", headerFont));
241: balances.addCell(cell);
242: cell = new PdfPCell(new Phrase("", headerFont));
243: balances.addCell(cell);
244:
245: DecimalFormat nf = new DecimalFormat();
246: nf.applyPattern("###,###,###,##0.00");
247:
248: GlSummary totals = new GlSummary();
249: for (Iterator iter = glBalances.iterator(); iter.hasNext();) {
250: GlSummary gls = (GlSummary) iter.next();
251: totals.add(gls);
252:
253: cell = new PdfPCell(new Phrase(gls.getFundGroup(),
254: textFont));
255: balances.addCell(cell);
256: cell = new PdfPCell(new Phrase(nf.format((gls
257: .getBeginningBalance().add(gls
258: .getCgBeginningBalance())).doubleValue()),
259: textFont));
260: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
261: balances.addCell(cell);
262: cell = new PdfPCell(new Phrase(nf.format(gls
263: .getAnnualBalance().doubleValue()), textFont));
264: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
265: balances.addCell(cell);
266: cell = new PdfPCell(new Phrase(nf.format(gls
267: .getMonth1().doubleValue()), textFont));
268: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
269: balances.addCell(cell);
270: cell = new PdfPCell(new Phrase(nf.format(gls
271: .getMonth2().doubleValue()), textFont));
272: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
273: balances.addCell(cell);
274: cell = new PdfPCell(new Phrase(nf.format(gls
275: .getMonth3().doubleValue()), textFont));
276: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
277: balances.addCell(cell);
278: cell = new PdfPCell(new Phrase(nf.format(gls
279: .getMonth4().doubleValue()), textFont));
280: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
281: balances.addCell(cell);
282:
283: cell = new PdfPCell(new Phrase("", textFont));
284: balances.addCell(cell);
285: cell = new PdfPCell(new Phrase(nf.format(gls
286: .getMonth5().doubleValue()), textFont));
287: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
288: balances.addCell(cell);
289: cell = new PdfPCell(new Phrase(nf.format(gls
290: .getMonth6().doubleValue()), textFont));
291: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
292: balances.addCell(cell);
293: cell = new PdfPCell(new Phrase(nf.format(gls
294: .getMonth7().doubleValue()), textFont));
295: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
296: balances.addCell(cell);
297: cell = new PdfPCell(new Phrase(nf.format(gls
298: .getMonth8().doubleValue()), textFont));
299: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
300: balances.addCell(cell);
301: cell = new PdfPCell(new Phrase(nf.format(gls
302: .getMonth9().doubleValue()), textFont));
303: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
304: balances.addCell(cell);
305: cell = new PdfPCell(new Phrase(nf.format(gls
306: .getMonth10().doubleValue()), textFont));
307: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
308: balances.addCell(cell);
309:
310: cell = new PdfPCell(new Phrase("", textFont));
311: balances.addCell(cell);
312: cell = new PdfPCell(new Phrase(nf.format(gls
313: .getMonth11().doubleValue()), textFont));
314: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
315: balances.addCell(cell);
316: cell = new PdfPCell(new Phrase(nf.format(gls
317: .getMonth12().doubleValue()), textFont));
318: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
319: balances.addCell(cell);
320: cell = new PdfPCell(new Phrase(nf.format(gls
321: .getMonth13().doubleValue()), textFont));
322: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
323: balances.addCell(cell);
324: cell = new PdfPCell(new Phrase(nf.format(gls
325: .getYearBalance().doubleValue()), textFont));
326: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
327: balances.addCell(cell);
328: cell = new PdfPCell(new Phrase("", textFont));
329: balances.addCell(cell);
330: cell = new PdfPCell(new Phrase("", textFont));
331: balances.addCell(cell);
332:
333: cell = new PdfPCell(new Phrase("", textFont));
334: cell.setColspan(7);
335: balances.addCell(cell);
336: }
337:
338: // Now add the total line
339: cell = new PdfPCell(new Phrase("Total", textFont));
340: balances.addCell(cell);
341: cell = new PdfPCell(
342: new Phrase(nf.format((totals.getBeginningBalance()
343: .add(totals.getCgBeginningBalance()))
344: .doubleValue()), textFont));
345: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
346: balances.addCell(cell);
347: cell = new PdfPCell(new Phrase(nf.format(totals
348: .getAnnualBalance().doubleValue()), textFont));
349: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
350: balances.addCell(cell);
351: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth1()
352: .doubleValue()), textFont));
353: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
354: balances.addCell(cell);
355: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth2()
356: .doubleValue()), textFont));
357: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
358: balances.addCell(cell);
359: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth3()
360: .doubleValue()), textFont));
361: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
362: balances.addCell(cell);
363: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth4()
364: .doubleValue()), textFont));
365: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
366: balances.addCell(cell);
367:
368: cell = new PdfPCell(new Phrase("", textFont));
369: balances.addCell(cell);
370: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth5()
371: .doubleValue()), textFont));
372: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
373: balances.addCell(cell);
374: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth6()
375: .doubleValue()), textFont));
376: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
377: balances.addCell(cell);
378: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth7()
379: .doubleValue()), textFont));
380: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
381: balances.addCell(cell);
382: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth8()
383: .doubleValue()), textFont));
384: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
385: balances.addCell(cell);
386: cell = new PdfPCell(new Phrase(nf.format(totals.getMonth9()
387: .doubleValue()), textFont));
388: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
389: balances.addCell(cell);
390: cell = new PdfPCell(new Phrase(nf.format(totals
391: .getMonth10().doubleValue()), textFont));
392: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
393: balances.addCell(cell);
394:
395: cell = new PdfPCell(new Phrase("", textFont));
396: balances.addCell(cell);
397: cell = new PdfPCell(new Phrase(nf.format(totals
398: .getMonth11().doubleValue()), textFont));
399: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
400: balances.addCell(cell);
401: cell = new PdfPCell(new Phrase(nf.format(totals
402: .getMonth12().doubleValue()), textFont));
403: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
404: balances.addCell(cell);
405: cell = new PdfPCell(new Phrase(nf.format(totals
406: .getMonth13().doubleValue()), textFont));
407: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
408: balances.addCell(cell);
409: cell = new PdfPCell(new Phrase(nf.format(totals
410: .getYearBalance().doubleValue()), textFont));
411: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
412: balances.addCell(cell);
413: cell = new PdfPCell(new Phrase("", textFont));
414: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
415: balances.addCell(cell);
416: cell = new PdfPCell(new Phrase("", textFont));
417: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
418: balances.addCell(cell);
419:
420: document.add(balances);
421: } catch (DocumentException de) {
422: LOG.error("generateReport() Error creating PDF report", de);
423: throw new RuntimeException("Report Generation Failed: "
424: + de.getMessage());
425: } catch (FileNotFoundException fnfe) {
426: LOG
427: .error("generateReport() Error writing PDF report",
428: fnfe);
429: throw new RuntimeException(
430: "Report Generation Failed: Error writing to file "
431: + fnfe.getMessage());
432: } finally {
433: if ((document != null) && document.isOpen()) {
434: document.close();
435: }
436: }
437: }
438: }
|