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.batch.sufficientFunds.impl;
017:
018: import java.io.FileOutputStream;
019: import java.text.DecimalFormat;
020: import java.text.SimpleDateFormat;
021: import java.util.Collections;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.kuali.core.service.KualiConfigurationService;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.module.gl.batch.sufficientFunds.SufficientFundsReport;
030: import org.kuali.module.gl.bo.SufficientFundRebuild;
031: import org.kuali.module.gl.util.Summary;
032:
033: import com.lowagie.text.Document;
034: import com.lowagie.text.ExceptionConverter;
035: import com.lowagie.text.Font;
036: import com.lowagie.text.FontFactory;
037: import com.lowagie.text.PageSize;
038: import com.lowagie.text.Phrase;
039: import com.lowagie.text.Rectangle;
040: import com.lowagie.text.pdf.PdfPCell;
041: import com.lowagie.text.pdf.PdfPTable;
042: import com.lowagie.text.pdf.PdfPageEventHelper;
043: import com.lowagie.text.pdf.PdfWriter;
044:
045: /**
046: * The default implementation of SufficientFundsReport
047: */
048: public class SufficientFundsReportImpl extends PdfPageEventHelper
049: implements SufficientFundsReport {
050: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
051: .getLogger(SufficientFundsReportImpl.class);
052: private KualiConfigurationService configurationService;
053:
054: /**
055: * Constructs a SufficientFundsReportImpl instance
056: */
057: public SufficientFundsReportImpl() {
058: super ();
059: }
060:
061: /**
062: * Generates the sufficient funds rebuild report and saves it to PDF
063: *
064: * @param reportErrors a Map of errors encountered during the sufficient funds rebuild process
065: * @param reportSummary a List of Strings that summarize the sufficient funds rebuild process
066: * @param runDate the date the sufficient funds rebuild process was run
067: * @param mode the mode the sufficient funds rebuild process was run in
068: * @see org.kuali.module.gl.batch.sufficientFunds.SufficientFundsReport#generateReport(java.util.Map, java.util.List, java.util.Date, int)
069: */
070: public void generateReport(Map reportErrors, List reportSummary,
071: Date runDate, int mode) {
072: LOG.debug("generateReport() started");
073:
074: String destinationDirectory = configurationService
075: .getPropertyString(KFSConstants.REPORTS_DIRECTORY_KEY);
076:
077: String title = "Sufficient Funds Report ";
078: String fileprefix = "sufficientFunds";
079:
080: Font headerFont = FontFactory.getFont(FontFactory.COURIER, 8,
081: Font.BOLD);
082: Font textFont = FontFactory.getFont(FontFactory.COURIER, 8,
083: Font.NORMAL);
084:
085: Document document = new Document(PageSize.A4.rotate());
086:
087: PageHelper helper = new PageHelper();
088: helper.runDate = runDate;
089: helper.headerFont = headerFont;
090: helper.title = title;
091:
092: try {
093: String filename = destinationDirectory + "/" + fileprefix
094: + "_";
095: SimpleDateFormat sdf = new SimpleDateFormat(
096: "yyyyMMdd_HHmmss");
097: filename = filename + sdf.format(runDate);
098: filename = filename + ".pdf";
099: PdfWriter writer = PdfWriter.getInstance(document,
100: new FileOutputStream(filename));
101: writer.setPageEvent(helper);
102:
103: document.open();
104:
105: // Sort what we get
106: Collections.sort(reportSummary);
107:
108: float[] summaryWidths = { 90, 10 };
109: PdfPTable summary = new PdfPTable(summaryWidths);
110: summary.setWidthPercentage(40);
111: PdfPCell cell = new PdfPCell(new Phrase(
112: "S T A T I S T I C S", headerFont));
113: cell.setColspan(2);
114: cell.setBorder(Rectangle.NO_BORDER);
115: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
116: summary.addCell(cell);
117:
118: for (Iterator iter = reportSummary.iterator(); iter
119: .hasNext();) {
120: Summary s = (Summary) iter.next();
121:
122: cell = new PdfPCell(new Phrase(s.getDescription(),
123: textFont));
124: cell.setBorder(Rectangle.NO_BORDER);
125: summary.addCell(cell);
126:
127: if ("".equals(s.getDescription())) {
128: cell = new PdfPCell(new Phrase("", textFont));
129: cell.setBorder(Rectangle.NO_BORDER);
130: summary.addCell(cell);
131: } else {
132: DecimalFormat nf = new DecimalFormat("###,###,##0");
133: cell = new PdfPCell(new Phrase(nf.format(s
134: .getCount()), textFont));
135: cell.setBorder(Rectangle.NO_BORDER);
136: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
137: summary.addCell(cell);
138: }
139: }
140: cell = new PdfPCell(new Phrase(""));
141: cell.setColspan(2);
142: cell.setBorder(Rectangle.NO_BORDER);
143: summary.addCell(cell);
144:
145: document.add(summary);
146:
147: if (reportErrors != null && reportErrors.size() > 0) {
148: float[] warningWidths = { 5, 12, 12, 53 };
149: PdfPTable warnings = new PdfPTable(warningWidths);
150: warnings.setHeaderRows(2);
151: warnings.setWidthPercentage(100);
152: cell = new PdfPCell(new Phrase("W A R N I N G S",
153: headerFont));
154: cell.setColspan(4);
155: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
156: warnings.addCell(cell);
157:
158: // Add headers
159: cell = new PdfPCell(new Phrase("COA", headerFont));
160: warnings.addCell(cell);
161: cell = new PdfPCell(new Phrase("Account/Object Code",
162: headerFont));
163: warnings.addCell(cell);
164: cell = new PdfPCell(new Phrase("Account/Object Type",
165: headerFont));
166: warnings.addCell(cell);
167: cell = new PdfPCell(new Phrase("Warning", headerFont));
168: warnings.addCell(cell);
169:
170: for (Iterator errorIter = reportErrors.keySet()
171: .iterator(); errorIter.hasNext();) {
172: SufficientFundRebuild sfrb = (SufficientFundRebuild) errorIter
173: .next();
174: boolean first = true;
175:
176: List errors = (List) reportErrors.get(sfrb);
177: for (Iterator listIter = errors.iterator(); listIter
178: .hasNext();) {
179: String msg = (String) listIter.next();
180:
181: if (first) {
182: first = false;
183: cell = new PdfPCell(
184: new Phrase(sfrb
185: .getChartOfAccountsCode(),
186: textFont));
187: warnings.addCell(cell);
188: cell = new PdfPCell(
189: new Phrase(
190: sfrb
191: .getAccountNumberFinancialObjectCode(),
192: textFont));
193: warnings.addCell(cell);
194: cell = new PdfPCell(
195: new Phrase(
196: sfrb
197: .getAccountFinancialObjectTypeCode(),
198: textFont));
199: warnings.addCell(cell);
200: } else {
201: cell = new PdfPCell(
202: new Phrase("", textFont));
203: cell.setColspan(3);
204: warnings.addCell(cell);
205: }
206: cell = new PdfPCell(new Phrase(msg, textFont));
207: warnings.addCell(cell);
208: }
209: }
210: document.add(warnings);
211: }
212: } catch (Exception de) {
213: LOG.error("generateReport() Error creating PDF report", de);
214: throw new RuntimeException("Report Generation Failed");
215: }
216:
217: document.close();
218: }
219:
220: /**
221: * An inner class to help set up the PDF that is written
222: */
223: class PageHelper extends PdfPageEventHelper {
224: public Date runDate;
225: public Font headerFont;
226: public String title;
227:
228: /**
229: * Writes the footer on the last page
230: * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
231: */
232: public void onEndPage(PdfWriter writer, Document document) {
233: try {
234: Rectangle page = document.getPageSize();
235: PdfPTable head = new PdfPTable(3);
236: SimpleDateFormat sdf = new SimpleDateFormat(
237: "MM/dd/yyyy HH:mm:ss");
238: PdfPCell cell = new PdfPCell(new Phrase(sdf
239: .format(runDate), headerFont));
240: cell.setBorder(Rectangle.NO_BORDER);
241: head.addCell(cell);
242:
243: cell = new PdfPCell(new Phrase(title, headerFont));
244: cell.setBorder(Rectangle.NO_BORDER);
245: cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
246: head.addCell(cell);
247:
248: cell = new PdfPCell(new Phrase("Page: "
249: + new Integer(writer.getPageNumber()),
250: headerFont));
251: cell.setBorder(Rectangle.NO_BORDER);
252: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
253: head.addCell(cell);
254:
255: head.setTotalWidth(page.width() - document.leftMargin()
256: - document.rightMargin());
257: head.writeSelectedRows(0, -1, document.leftMargin(),
258: page.height() - document.topMargin()
259: + head.getTotalHeight(), writer
260: .getDirectContent());
261: } catch (Exception e) {
262: throw new ExceptionConverter(e);
263: }
264: }
265: }
266:
267: /**
268: * Sets the configurationService attribute value.
269: *
270: * @param configurationService The configurationService to set.
271: */
272: public void setConfigurationService(
273: KualiConfigurationService configurationService) {
274: this.configurationService = configurationService;
275: }
276: }
|