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.ArrayList;
023: import java.util.Collections;
024: import java.util.Date;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.kuali.module.gl.bo.Transaction;
030:
031: import com.lowagie.text.Document;
032: import com.lowagie.text.DocumentException;
033: import com.lowagie.text.ExceptionConverter;
034: import com.lowagie.text.Font;
035: import com.lowagie.text.FontFactory;
036: import com.lowagie.text.PageSize;
037: import com.lowagie.text.Phrase;
038: import com.lowagie.text.Rectangle;
039: import com.lowagie.text.pdf.PdfPCell;
040: import com.lowagie.text.pdf.PdfPTable;
041: import com.lowagie.text.pdf.PdfPageEventHelper;
042: import com.lowagie.text.pdf.PdfWriter;
043:
044: /**
045: * This class represents the functionality related to the generating the Transaction Report. The transaction report
046: * shows the primary key from transactions and a list of messages for each one.
047: *
048: */
049: public class TransactionReport {
050: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
051: .getLogger(TransactionReport.class);
052:
053: static public class PageHelper extends PdfPageEventHelper {
054: public Date runDate;
055: public Font headerFont;
056: public String title;
057:
058: /**
059: * Generates the end page for this transaction report
060: *
061: * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
062: */
063: public void onEndPage(PdfWriter writer, Document document) {
064: try {
065: Rectangle page = document.getPageSize();
066: PdfPTable head = new PdfPTable(3);
067: SimpleDateFormat sdf = new SimpleDateFormat(
068: "MM/dd/yyyy HH:mm:ss");
069: PdfPCell cell = new PdfPCell(new Phrase(sdf
070: .format(runDate), headerFont));
071: cell.setBorder(Rectangle.NO_BORDER);
072: head.addCell(cell);
073:
074: cell = new PdfPCell(new Phrase(title, headerFont));
075: cell.setBorder(Rectangle.NO_BORDER);
076: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
077: head.addCell(cell);
078:
079: cell = new PdfPCell(new Phrase("Page: "
080: + new Integer(writer.getPageNumber()),
081: headerFont));
082: cell.setBorder(Rectangle.NO_BORDER);
083: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
084: head.addCell(cell);
085:
086: head.setTotalWidth(page.width() - document.leftMargin()
087: - document.rightMargin());
088: head.writeSelectedRows(0, -1, document.leftMargin(),
089: page.height() - document.topMargin()
090: + head.getTotalHeight(), writer
091: .getDirectContent());
092: } catch (Exception e) {
093: throw new ExceptionConverter(e);
094: }
095: }
096: }
097:
098: public TransactionReport() {
099: super ();
100: }
101:
102: /**
103: * Generates transaction report
104: *
105: * @param reportErrors map containing transactions and the errors associated with each transaction
106: * @param reportSummary list of summary objects
107: * @param runDate date report is run
108: * @param title title of report
109: * @param fileprefix file prefix of report file
110: * @param destinationDirectory destination of where report file will reside
111: */
112: public void generateReport(
113: Map<Transaction, List<Message>> reportErrors,
114: List<Summary> reportSummary, Date runDate, String title,
115: String fileprefix, String destinationDirectory) {
116: LOG.debug("generateReport() started");
117:
118: List transactions = new ArrayList();
119: if (reportErrors != null) {
120: transactions.addAll(reportErrors.keySet());
121: }
122: generateReport(transactions, reportErrors, reportSummary,
123: runDate, title, fileprefix, destinationDirectory);
124: }
125:
126: /**
127: * Generates transaction report
128: *
129: * @param errorSortedList list of error'd transactions
130: * @param reportErrors map containing transactions and the errors associated with each transaction
131: * @param reportSummary list of summary objects
132: * @param runDate date report is run
133: * @param title title of report
134: * @param fileprefix file prefix of report file
135: * @param destinationDirectory destination of where report file will reside
136: */
137: public void generateReport(List<Transaction> errorSortedList,
138: Map<Transaction, List<Message>> reportErrors,
139: List<Summary> reportSummary, Date runDate, String title,
140: String fileprefix, String destinationDirectory) {
141: LOG.debug("generateReport() started");
142:
143: Font headerFont = FontFactory.getFont(FontFactory.COURIER, 8,
144: Font.BOLD);
145: Font textFont = FontFactory.getFont(FontFactory.COURIER, 8,
146: Font.NORMAL);
147:
148: Document document = new Document(PageSize.A4.rotate());
149:
150: PageHelper helper = new PageHelper();
151: helper.runDate = runDate;
152: helper.headerFont = headerFont;
153: helper.title = title;
154:
155: try {
156: String filename = destinationDirectory + "/" + fileprefix
157: + "_";
158: SimpleDateFormat sdf = new SimpleDateFormat(
159: "yyyyMMdd_HHmmss");
160: filename = filename + sdf.format(runDate);
161: filename = filename + ".pdf";
162: PdfWriter writer = PdfWriter.getInstance(document,
163: new FileOutputStream(filename));
164: writer.setPageEvent(helper);
165:
166: document.open();
167: appendReport(document, headerFont, textFont,
168: errorSortedList, reportErrors, reportSummary,
169: runDate);
170: } catch (DocumentException de) {
171: LOG.error("generateReport() Error creating PDF report", de);
172: throw new RuntimeException("Report Generation Failed: "
173: + de.getMessage());
174: } catch (FileNotFoundException fnfe) {
175: LOG
176: .error("generateReport() Error writing PDF report",
177: fnfe);
178: throw new RuntimeException(
179: "Report Generation Failed: Error writing to file "
180: + fnfe.getMessage());
181: } finally {
182: if ((document != null) && document.isOpen()) {
183: document.close();
184: }
185: }
186: }
187:
188: /**
189: * Appends the scrubber totals/statistics and error report to the given (iText) document object.
190: *
191: * @param document the PDF document
192: * @param headerFont font for header
193: * @param textFont font for report text
194: * @param errorSortedList list of error'd transactions
195: * @param reportErrors map containing transactions and the errors associated with each transaction
196: * @param reportSummary list of summary objects
197: * @param runDate date report was run
198: * @throws DocumentException
199: */
200: public void appendReport(Document document, Font headerFont,
201: Font textFont, List<Transaction> errorSortedList,
202: Map<Transaction, List<Message>> reportErrors,
203: List<Summary> reportSummary, Date runDate)
204: throws DocumentException {
205: // Sort what we get
206: Collections.sort(reportSummary);
207:
208: float[] summaryWidths = { 80, 20 };
209: PdfPTable summary = new PdfPTable(summaryWidths);
210: summary.setWidthPercentage(40);
211: PdfPCell cell = new PdfPCell(new Phrase("S T A T I S T I C S",
212: headerFont));
213: cell.setColspan(2);
214: cell.setBorder(Rectangle.NO_BORDER);
215: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
216: summary.addCell(cell);
217:
218: for (Iterator iter = reportSummary.iterator(); iter.hasNext();) {
219: Summary s = (Summary) iter.next();
220:
221: cell = new PdfPCell(
222: new Phrase(s.getDescription(), textFont));
223: cell.setBorder(Rectangle.NO_BORDER);
224: summary.addCell(cell);
225:
226: if ("".equals(s.getDescription())) {
227: cell = new PdfPCell(new Phrase("", textFont));
228: cell.setBorder(Rectangle.NO_BORDER);
229: summary.addCell(cell);
230: } else {
231: DecimalFormat nf = new DecimalFormat("###,###,###,##0");
232: cell = new PdfPCell(new Phrase(nf.format(s.getCount()),
233: textFont));
234: cell.setBorder(Rectangle.NO_BORDER);
235: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
236: summary.addCell(cell);
237: }
238: }
239: cell = new PdfPCell(new Phrase(""));
240: cell.setColspan(2);
241: cell.setBorder(Rectangle.NO_BORDER);
242: summary.addCell(cell);
243:
244: document.add(summary);
245:
246: if (reportErrors != null && reportErrors.size() > 0) {
247: float[] warningWidths = { 4, 3, 6, 5, 5, 4, 5, 5, 4, 5, 5,
248: 9, 4, 36 };
249: PdfPTable warnings = new PdfPTable(warningWidths);
250: warnings.setHeaderRows(2);
251: warnings.setWidthPercentage(100);
252: cell = new PdfPCell(new Phrase("W A R N I N G S",
253: headerFont));
254: cell.setColspan(14);
255: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
256: warnings.addCell(cell);
257:
258: // Add headers
259: cell = new PdfPCell(new Phrase("Year", headerFont));
260: warnings.addCell(cell);
261: cell = new PdfPCell(new Phrase("COA", headerFont));
262: warnings.addCell(cell);
263: cell = new PdfPCell(new Phrase("Account", headerFont));
264: warnings.addCell(cell);
265: cell = new PdfPCell(new Phrase("Sacct", headerFont));
266: warnings.addCell(cell);
267: cell = new PdfPCell(new Phrase("Obj", headerFont));
268: warnings.addCell(cell);
269: cell = new PdfPCell(new Phrase("SObj", headerFont));
270: warnings.addCell(cell);
271: cell = new PdfPCell(new Phrase("BalTyp", headerFont));
272: warnings.addCell(cell);
273: cell = new PdfPCell(new Phrase("ObjTyp", headerFont));
274: warnings.addCell(cell);
275: cell = new PdfPCell(new Phrase("Prd", headerFont));
276: warnings.addCell(cell);
277: cell = new PdfPCell(new Phrase("DocType", headerFont));
278: warnings.addCell(cell);
279: cell = new PdfPCell(new Phrase("Origin", headerFont));
280: warnings.addCell(cell);
281: cell = new PdfPCell(new Phrase("DocNbr", headerFont));
282: warnings.addCell(cell);
283: cell = new PdfPCell(new Phrase("Seq", headerFont));
284: warnings.addCell(cell);
285: cell = new PdfPCell(new Phrase("Warning", headerFont));
286: warnings.addCell(cell);
287:
288: for (Iterator errorIter = errorSortedList.iterator(); errorIter
289: .hasNext();) {
290: Transaction tran = (Transaction) errorIter.next();
291: boolean first = true;
292:
293: List errors = (List) reportErrors.get(tran);
294: for (Iterator listIter = errors.iterator(); listIter
295: .hasNext();) {
296: String msg = null;
297: Object m = listIter.next();
298: if (m instanceof Message) {
299: Message mm = (Message) m;
300: msg = mm.getMessage();
301: } else {
302: if (m == null) {
303: msg = "";
304: } else {
305: msg = m.toString();
306: }
307: }
308:
309: if (first) {
310: first = false;
311:
312: if (tran.getUniversityFiscalYear() == null) {
313: cell = new PdfPCell(new Phrase("NULL",
314: textFont));
315: } else {
316: cell = new PdfPCell(new Phrase(tran
317: .getUniversityFiscalYear()
318: .toString(), textFont));
319: }
320: warnings.addCell(cell);
321: cell = new PdfPCell(new Phrase(tran
322: .getChartOfAccountsCode(), textFont));
323: warnings.addCell(cell);
324: cell = new PdfPCell(new Phrase(tran
325: .getAccountNumber(), textFont));
326: warnings.addCell(cell);
327: cell = new PdfPCell(new Phrase(tran
328: .getSubAccountNumber(), textFont));
329: warnings.addCell(cell);
330: cell = new PdfPCell(new Phrase(tran
331: .getFinancialObjectCode(), textFont));
332: warnings.addCell(cell);
333: cell = new PdfPCell(new Phrase(tran
334: .getFinancialSubObjectCode(), textFont));
335: warnings.addCell(cell);
336: cell = new PdfPCell(new Phrase(tran
337: .getFinancialBalanceTypeCode(),
338: textFont));
339: warnings.addCell(cell);
340: cell = new PdfPCell(
341: new Phrase(tran
342: .getFinancialObjectTypeCode(),
343: textFont));
344: warnings.addCell(cell);
345: cell = new PdfPCell(new Phrase(tran
346: .getUniversityFiscalPeriodCode(),
347: textFont));
348: warnings.addCell(cell);
349: cell = new PdfPCell(new Phrase(tran
350: .getFinancialDocumentTypeCode(),
351: textFont));
352: warnings.addCell(cell);
353: cell = new PdfPCell(new Phrase(tran
354: .getFinancialSystemOriginationCode(),
355: textFont));
356: warnings.addCell(cell);
357: cell = new PdfPCell(new Phrase(tran
358: .getDocumentNumber(), textFont));
359: warnings.addCell(cell);
360: if (tran
361: .getTransactionLedgerEntrySequenceNumber() == null) {
362: cell = new PdfPCell(new Phrase("NULL",
363: textFont));
364: } else {
365: cell = new PdfPCell(
366: new Phrase(
367: tran
368: .getTransactionLedgerEntrySequenceNumber()
369: .toString(),
370: textFont));
371: }
372: warnings.addCell(cell);
373: } else {
374: cell = new PdfPCell(new Phrase("", textFont));
375: cell.setColspan(13);
376: warnings.addCell(cell);
377: }
378: cell = new PdfPCell(new Phrase(msg, textFont));
379: warnings.addCell(cell);
380: }
381: }
382: document.add(warnings);
383: }
384: }
385: }
|