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:
025: import org.kuali.core.util.KualiDecimal;
026: import org.kuali.kfs.KFSConstants;
027: import org.kuali.module.gl.bo.Transaction;
028:
029: import com.lowagie.text.Document;
030: import com.lowagie.text.DocumentException;
031: import com.lowagie.text.Element;
032: import com.lowagie.text.ExceptionConverter;
033: import com.lowagie.text.Font;
034: import com.lowagie.text.FontFactory;
035: import com.lowagie.text.PageSize;
036: import com.lowagie.text.Phrase;
037: import com.lowagie.text.Rectangle;
038: import com.lowagie.text.pdf.PdfPCell;
039: import com.lowagie.text.pdf.PdfPTable;
040: import com.lowagie.text.pdf.PdfPageEventHelper;
041: import com.lowagie.text.pdf.PdfWriter;
042:
043: /**
044: * This class prints out a transaction listing report. This is different from a transaction report in that this lists all the
045: * transactions and a total amount. The transaction report shows the primary key from transactions and a list of messages for each
046: * one.
047: */
048: public class TransactionListingReport {
049: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
050: .getLogger(TransactionListingReport.class);
051:
052: class PageHelper extends PdfPageEventHelper {
053: public Date runDate;
054: public Font headerFont;
055: public String title;
056:
057: /**
058: * Generates end page for the transaction listing report
059: *
060: * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
061: */
062: public void onEndPage(PdfWriter writer, Document document) {
063: try {
064: Rectangle page = document.getPageSize();
065: PdfPTable head = new PdfPTable(3);
066: SimpleDateFormat sdf = new SimpleDateFormat(
067: "MM/dd/yyyy HH:mm:ss");
068: PdfPCell cell = new PdfPCell(new Phrase(sdf
069: .format(runDate), headerFont));
070: cell.setBorder(Rectangle.NO_BORDER);
071: head.addCell(cell);
072:
073: cell = new PdfPCell(new Phrase(title, headerFont));
074: cell.setBorder(Rectangle.NO_BORDER);
075: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
076: head.addCell(cell);
077:
078: cell = new PdfPCell(new Phrase("Page: "
079: + new Integer(writer.getPageNumber()),
080: headerFont));
081: cell.setBorder(Rectangle.NO_BORDER);
082: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
083: head.addCell(cell);
084:
085: head.setTotalWidth(page.width() - document.leftMargin()
086: - document.rightMargin());
087: head.writeSelectedRows(0, -1, document.leftMargin(),
088: page.height() - document.topMargin()
089: + head.getTotalHeight(), writer
090: .getDirectContent());
091: } catch (Exception e) {
092: throw new ExceptionConverter(e);
093: }
094: }
095: }
096:
097: public TransactionListingReport() {
098: super ();
099: }
100:
101: /**
102: * This will generate a report on the transactions passed to it
103: *
104: * @param transactions Transactions sorted properly
105: * @param runDate date report is run
106: * @param title title of report
107: * @param fileprefix file prefix of file
108: * @param destinationDirectory directory where file resides
109: */
110: public void generateReport(Iterator<Transaction> transactions,
111: Date runDate, String title, String fileprefix,
112: String destinationDirectory) {
113: LOG.debug("generateReport() started");
114:
115: Font headerFont = FontFactory.getFont(FontFactory.COURIER, 8,
116: Font.BOLD);
117: Font textFont = FontFactory.getFont(FontFactory.COURIER, 8,
118: Font.NORMAL);
119:
120: Document document = new Document(PageSize.A4.rotate());
121:
122: PageHelper helper = new PageHelper();
123: helper.runDate = runDate;
124: helper.headerFont = headerFont;
125: helper.title = title;
126:
127: try {
128: String filename = destinationDirectory + "/" + fileprefix
129: + "_";
130: SimpleDateFormat sdf = new SimpleDateFormat(
131: "yyyyMMdd_HHmmss");
132: filename = filename + sdf.format(runDate);
133: filename = filename + ".pdf";
134: PdfWriter writer = PdfWriter.getInstance(document,
135: new FileOutputStream(filename));
136: writer.setPageEvent(helper);
137:
138: document.open();
139:
140: float[] widths = { 5, 9, 5, 5, 6, 5, 6, 5, 7, 25, 10, 10,
141: 10 };
142: PdfPTable transactionList = new PdfPTable(widths);
143: transactionList.setHeaderRows(1);
144: transactionList.setWidthPercentage(100);
145:
146: // Add headers
147: PdfPCell cell = new PdfPCell(new Phrase("Fiscal Year",
148: headerFont));
149: transactionList.addCell(cell);
150: cell = new PdfPCell(
151: new Phrase("Account Number", headerFont));
152: transactionList.addCell(cell);
153: cell = new PdfPCell(new Phrase("Object Code", headerFont));
154: transactionList.addCell(cell);
155: cell = new PdfPCell(new Phrase("Object Type", headerFont));
156: transactionList.addCell(cell);
157: cell = new PdfPCell(new Phrase("Balance Type", headerFont));
158: transactionList.addCell(cell);
159: cell = new PdfPCell(new Phrase("Fiscal Period", headerFont));
160: transactionList.addCell(cell);
161: cell = new PdfPCell(new Phrase("Document Type", headerFont));
162: transactionList.addCell(cell);
163: cell = new PdfPCell(new Phrase("System Origin", headerFont));
164: transactionList.addCell(cell);
165: cell = new PdfPCell(new Phrase("Document Number",
166: headerFont));
167: transactionList.addCell(cell);
168: cell = new PdfPCell(new Phrase("Description", headerFont));
169: transactionList.addCell(cell);
170: cell = new PdfPCell(new Phrase("Debit Amount", headerFont));
171: transactionList.addCell(cell);
172: cell = new PdfPCell(new Phrase("Credit Amount", headerFont));
173: transactionList.addCell(cell);
174: cell = new PdfPCell(new Phrase("Budget Amount", headerFont));
175: transactionList.addCell(cell);
176:
177: int transactionCount = 0;
178: KualiDecimal debitTotal = KualiDecimal.ZERO;
179: KualiDecimal creditTotal = KualiDecimal.ZERO;
180: KualiDecimal budgetTotal = KualiDecimal.ZERO;
181:
182: DecimalFormat nf = new DecimalFormat();
183: nf.applyPattern("###,###,###,##0.00");
184:
185: if (transactions != null) {
186: while (transactions.hasNext()) {
187: Transaction tran = (Transaction) transactions
188: .next();
189:
190: cell = new PdfPCell(
191: new Phrase(
192: tran.getUniversityFiscalYear() == null ? " "
193: : tran
194: .getUniversityFiscalYear()
195: .toString(),
196: textFont));
197: transactionList.addCell(cell);
198: cell = new PdfPCell(new Phrase(tran
199: .getChartOfAccountsCode()
200: + "-" + tran.getAccountNumber(), textFont));
201: transactionList.addCell(cell);
202: cell = new PdfPCell(new Phrase(tran
203: .getFinancialObjectCode(), textFont));
204: transactionList.addCell(cell);
205: cell = new PdfPCell(new Phrase(tran
206: .getFinancialObjectTypeCode(), textFont));
207: transactionList.addCell(cell);
208: cell = new PdfPCell(new Phrase(tran
209: .getFinancialBalanceTypeCode(), textFont));
210: transactionList.addCell(cell);
211: cell = new PdfPCell(new Phrase(tran
212: .getUniversityFiscalPeriodCode(), textFont));
213: transactionList.addCell(cell);
214: cell = new PdfPCell(new Phrase(tran
215: .getFinancialDocumentTypeCode(), textFont));
216: transactionList.addCell(cell);
217: cell = new PdfPCell(new Phrase(tran
218: .getFinancialSystemOriginationCode(),
219: textFont));
220: transactionList.addCell(cell);
221: cell = new PdfPCell(new Phrase(tran
222: .getDocumentNumber(), textFont));
223: transactionList.addCell(cell);
224: cell = new PdfPCell(new Phrase(tran
225: .getTransactionLedgerEntryDescription(),
226: textFont));
227: transactionList.addCell(cell);
228:
229: DecimalFormat decimalFormat = new DecimalFormat();
230:
231: if (KFSConstants.GL_DEBIT_CODE.equals(tran
232: .getTransactionDebitCreditCode())) {
233: cell = new PdfPCell(new Phrase(nf.format(tran
234: .getTransactionLedgerEntryAmount()
235: .doubleValue()), textFont));
236: debitTotal = debitTotal.add(tran
237: .getTransactionLedgerEntryAmount());
238: } else {
239: cell = new PdfPCell(new Phrase(nf.format(0),
240: textFont));
241: }
242: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
243: transactionList.addCell(cell);
244:
245: if (KFSConstants.GL_CREDIT_CODE.equals(tran
246: .getTransactionDebitCreditCode())) {
247: cell = new PdfPCell(new Phrase(nf.format(tran
248: .getTransactionLedgerEntryAmount()
249: .doubleValue()), textFont));
250: creditTotal = creditTotal.add(tran
251: .getTransactionLedgerEntryAmount());
252: } else {
253: cell = new PdfPCell(new Phrase(nf.format(0),
254: textFont));
255: }
256: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
257: transactionList.addCell(cell);
258:
259: if (!KFSConstants.GL_CREDIT_CODE.equals(tran
260: .getTransactionDebitCreditCode())
261: && !KFSConstants.GL_DEBIT_CODE.equals(tran
262: .getTransactionDebitCreditCode())) {
263: cell = new PdfPCell(new Phrase(nf.format(tran
264: .getTransactionLedgerEntryAmount()
265: .doubleValue()), textFont));
266: budgetTotal = budgetTotal.add(tran
267: .getTransactionLedgerEntryAmount());
268: } else {
269: cell = new PdfPCell(new Phrase(nf.format(0),
270: textFont));
271: }
272: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
273: transactionList.addCell(cell);
274:
275: transactionCount++;
276: }
277: }
278:
279: // Now add the total line
280: cell = new PdfPCell(new Phrase("", textFont));
281: transactionList.addCell(cell);
282: cell = new PdfPCell(new Phrase("", textFont));
283: transactionList.addCell(cell);
284:
285: DecimalFormat intf = new DecimalFormat();
286: intf.applyPattern("###,###");
287: cell = new PdfPCell(new Phrase(intf
288: .format(transactionCount), headerFont));
289: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
290: transactionList.addCell(cell);
291: cell = new PdfPCell(new Phrase("", textFont));
292: cell.setColspan(7);
293: transactionList.addCell(cell);
294: cell = new PdfPCell(new Phrase(nf.format(debitTotal
295: .doubleValue()), headerFont));
296: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
297: transactionList.addCell(cell);
298: cell = new PdfPCell(new Phrase(nf.format(creditTotal
299: .doubleValue()), headerFont));
300: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
301: transactionList.addCell(cell);
302:
303: cell = new PdfPCell(new Phrase(nf.format(budgetTotal
304: .doubleValue()), headerFont));
305: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
306: transactionList.addCell(cell);
307:
308: document.add(transactionList);
309: } catch (DocumentException de) {
310: LOG.error("generateReport() Error creating PDF report", de);
311: throw new RuntimeException("Report Generation Failed: "
312: + de.getMessage());
313: } catch (FileNotFoundException fnfe) {
314: LOG
315: .error("generateReport() Error writing PDF report",
316: fnfe);
317: throw new RuntimeException(
318: "Report Generation Failed: Error writing to file "
319: + fnfe.getMessage());
320: } finally {
321: if ((document != null) && document.isOpen()) {
322: document.close();
323: }
324: }
325: }
326: }
|