001: /*
002: * Copyright 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.ExpenditureTransaction;
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 for creating a Expenditure Transaction Report
046: *
047: */
048: public class ExpenditureTransactionReport {
049: // copied from TransactionReport
050: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
051: .getLogger(ExpenditureTransactionReport.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 information for a expenditure 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 ExpenditureTransactionReport() {
099: super ();
100: }
101:
102: /**
103: * Generate expenditure transaction report. Creates a list of errors from expenditure transactions.
104: *
105: * @param reportErrors map of errors associated with each expenditure transaction
106: * @param reportSummary list of summary objects about report
107: * @param runDate date report is run
108: * @param title title of the report
109: * @param fileprefix prefix of report name
110: * @param destinationDirectory directory where report file resides
111: */
112: public void generateReport(
113: Map<ExpenditureTransaction, List<Message>> reportErrors,
114: List<Summary> reportSummary, Date runDate, String title,
115: String fileprefix, String destinationDirectory) {
116: LOG.debug("generateReport() started");
117:
118: List expenditureTransactions = new ArrayList();
119: if (reportErrors != null) {
120: expenditureTransactions.addAll(reportErrors.keySet());
121: }
122: generateReport(expenditureTransactions, reportErrors,
123: reportSummary, runDate, title, fileprefix,
124: destinationDirectory);
125: }
126:
127: /**
128: * Generate expenditure transaction report
129: *
130: * @param reportErrors list of errors associated
131: * @param reportSummary list of summary objects about report
132: * @param runDate date report is run
133: * @param title title of report
134: * @param fileprefix prefix of report name
135: * @param destinationDirectory directory where report file resides
136: */
137: public void generateReport(
138: List<ExpenditureTransaction> errorSortedList,
139: Map<ExpenditureTransaction, List<Message>> reportErrors,
140: List<Summary> reportSummary, Date runDate, String title,
141: String fileprefix, 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 = title;
155:
156: try {
157: String filename = destinationDirectory + "/" + fileprefix
158: + "_";
159: SimpleDateFormat sdf = new SimpleDateFormat(
160: "yyyyMMdd_HHmmss");
161: filename = filename + sdf.format(runDate);
162: filename = filename + ".pdf";
163: PdfWriter writer = PdfWriter.getInstance(document,
164: new FileOutputStream(filename));
165: writer.setPageEvent(helper);
166:
167: document.open();
168: appendReport(document, headerFont, textFont,
169: errorSortedList, reportErrors, reportSummary,
170: runDate);
171: } catch (DocumentException de) {
172: LOG.error("generateReport() Error creating PDF report", de);
173: throw new RuntimeException("Report Generation Failed: "
174: + de.getMessage());
175: } catch (FileNotFoundException fnfe) {
176: LOG
177: .error("generateReport() Error writing PDF report",
178: fnfe);
179: throw new RuntimeException(
180: "Report Generation Failed: Error writing to file "
181: + fnfe.getMessage());
182: } finally {
183: if ((document != null) && document.isOpen()) {
184: document.close();
185: }
186: }
187: }
188:
189: /**
190: * Appends the scrubber totals/statistics and error report to the given (iText) document object.
191: *
192: * @param document document representing report
193: * @param headerFont header font
194: * @param textFont text font
195: * @param errorSortedList list of expenditure transactions
196: * @param reportErrors map containing expenditure transactions/list of messages pairs
197: * @param reportSummary list of summary objects
198: * @param runDate date report is run
199: * @throws DocumentException
200: */
201: public void appendReport(Document document, Font headerFont,
202: Font textFont,
203: List<ExpenditureTransaction> errorSortedList,
204: Map<ExpenditureTransaction, List<Message>> reportErrors,
205: List<Summary> reportSummary, Date runDate)
206: throws DocumentException {
207: // Sort what we get
208: Collections.sort(reportSummary);
209:
210: float[] summaryWidths = { 80, 20 };
211: PdfPTable summary = new PdfPTable(summaryWidths);
212: summary.setWidthPercentage(40);
213: PdfPCell cell = new PdfPCell(new Phrase("S T A T I S T I C S",
214: headerFont));
215: cell.setColspan(2);
216: cell.setBorder(Rectangle.NO_BORDER);
217: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
218: summary.addCell(cell);
219:
220: for (Iterator iter = reportSummary.iterator(); iter.hasNext();) {
221: Summary s = (Summary) iter.next();
222:
223: cell = new PdfPCell(
224: new Phrase(s.getDescription(), textFont));
225: cell.setBorder(Rectangle.NO_BORDER);
226: summary.addCell(cell);
227:
228: if ("".equals(s.getDescription())) {
229: cell = new PdfPCell(new Phrase("", textFont));
230: cell.setBorder(Rectangle.NO_BORDER);
231: summary.addCell(cell);
232: } else {
233: DecimalFormat nf = new DecimalFormat("###,###,###,##0");
234: cell = new PdfPCell(new Phrase(nf.format(s.getCount()),
235: textFont));
236: cell.setBorder(Rectangle.NO_BORDER);
237: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
238: summary.addCell(cell);
239: }
240: }
241: cell = new PdfPCell(new Phrase(""));
242: cell.setColspan(2);
243: cell.setBorder(Rectangle.NO_BORDER);
244: summary.addCell(cell);
245:
246: document.add(summary);
247:
248: if (reportErrors != null && reportErrors.size() > 0) {
249: float[] warningWidths = { 4, 3, 6, 5, 5, 4, 5, 5, 4, 13,
250: 10, 36 };
251: PdfPTable warnings = new PdfPTable(warningWidths);
252: warnings.setHeaderRows(2);
253: warnings.setWidthPercentage(100);
254: cell = new PdfPCell(new Phrase("W A R N I N G S",
255: headerFont));
256: cell.setColspan(12);
257: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
258: warnings.addCell(cell);
259:
260: // Add headers
261: cell = new PdfPCell(new Phrase("Year", headerFont));
262: warnings.addCell(cell);
263: cell = new PdfPCell(new Phrase("COA", headerFont));
264: warnings.addCell(cell);
265: cell = new PdfPCell(new Phrase("Account", headerFont));
266: warnings.addCell(cell);
267: cell = new PdfPCell(new Phrase("Sacct", headerFont));
268: warnings.addCell(cell);
269: cell = new PdfPCell(new Phrase("Obj", headerFont));
270: warnings.addCell(cell);
271: cell = new PdfPCell(new Phrase("SObj", headerFont));
272: warnings.addCell(cell);
273: cell = new PdfPCell(new Phrase("BalTyp", headerFont));
274: warnings.addCell(cell);
275: cell = new PdfPCell(new Phrase("ObjTyp", headerFont));
276: warnings.addCell(cell);
277: cell = new PdfPCell(new Phrase("Prd", headerFont));
278: warnings.addCell(cell);
279: cell = new PdfPCell(new Phrase("PrjCd", headerFont));
280: warnings.addCell(cell);
281: cell = new PdfPCell(new Phrase("OrgRefId", headerFont));
282: warnings.addCell(cell);
283: cell = new PdfPCell(new Phrase("Warning", headerFont));
284: warnings.addCell(cell);
285:
286: for (Iterator errorIter = errorSortedList.iterator(); errorIter
287: .hasNext();) {
288: ExpenditureTransaction et = (ExpenditureTransaction) errorIter
289: .next();
290: boolean first = true;
291:
292: List errors = (List) reportErrors.get(et);
293: for (Iterator listIter = errors.iterator(); listIter
294: .hasNext();) {
295: String msg = null;
296: Object m = listIter.next();
297: if (m instanceof Message) {
298: Message mm = (Message) m;
299: msg = mm.getMessage();
300: } else {
301: if (m == null) {
302: msg = "";
303: } else {
304: msg = m.toString();
305: }
306: }
307:
308: if (first) {
309: first = false;
310:
311: if (et.getUniversityFiscalYear() == null) {
312: cell = new PdfPCell(new Phrase("NULL",
313: textFont));
314: } else {
315: cell = new PdfPCell(new Phrase(et
316: .getUniversityFiscalYear()
317: .toString(), textFont));
318: }
319: warnings.addCell(cell);
320: cell = new PdfPCell(new Phrase(et
321: .getChartOfAccountsCode(), textFont));
322: warnings.addCell(cell);
323: cell = new PdfPCell(new Phrase(et
324: .getAccountNumber(), textFont));
325: warnings.addCell(cell);
326: cell = new PdfPCell(new Phrase(et
327: .getSubAccountNumber(), textFont));
328: warnings.addCell(cell);
329: cell = new PdfPCell(new Phrase(et
330: .getObjectCode(), textFont));
331: warnings.addCell(cell);
332: cell = new PdfPCell(new Phrase(et
333: .getSubObjectCode(), textFont));
334: warnings.addCell(cell);
335: cell = new PdfPCell(new Phrase(et
336: .getBalanceTypeCode(), textFont));
337: warnings.addCell(cell);
338: cell = new PdfPCell(new Phrase(et
339: .getObjectTypeCode(), textFont));
340: warnings.addCell(cell);
341: cell = new PdfPCell(new Phrase(et
342: .getUniversityFiscalAccountingPeriod(),
343: textFont));
344: warnings.addCell(cell);
345: cell = new PdfPCell(new Phrase(et
346: .getProjectCode(), textFont));
347: warnings.addCell(cell);
348: cell = new PdfPCell(
349: new Phrase(et
350: .getOrganizationReferenceId(),
351: textFont));
352: warnings.addCell(cell);
353: } else {
354: cell = new PdfPCell(new Phrase("", textFont));
355: cell.setColspan(11);
356: warnings.addCell(cell);
357: }
358: cell = new PdfPCell(new Phrase(msg, textFont));
359: warnings.addCell(cell);
360: }
361: }
362: document.add(warnings);
363: }
364: }
365: }
|