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.pdp.service.impl;
017:
018: import java.io.FileOutputStream;
019: import java.text.DecimalFormat;
020: import java.text.SimpleDateFormat;
021: import java.util.Collection;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.kuali.core.service.DateTimeService;
027: import org.kuali.module.gl.util.TransactionReport.PageHelper;
028: import org.kuali.module.pdp.bo.DailyReport;
029: import org.kuali.module.pdp.dao.PaymentDetailDao;
030: import org.kuali.module.pdp.service.DailyReportService;
031: import org.springframework.transaction.annotation.Transactional;
032:
033: import com.lowagie.text.Document;
034: import com.lowagie.text.DocumentException;
035: import com.lowagie.text.Element;
036: import com.lowagie.text.Font;
037: import com.lowagie.text.FontFactory;
038: import com.lowagie.text.PageSize;
039: import com.lowagie.text.Phrase;
040: import com.lowagie.text.Rectangle;
041: import com.lowagie.text.pdf.PdfPCell;
042: import com.lowagie.text.pdf.PdfPTable;
043: import com.lowagie.text.pdf.PdfWriter;
044:
045: @Transactional
046: public class DailyReportServiceImpl implements DailyReportService {
047: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
048: .getLogger(DailyReportServiceImpl.class);
049:
050: private PaymentDetailDao paymentDetailDao;
051: private DateTimeService dateTimeService;
052: private String directoryName;
053:
054: private Font headerFont;
055: private Font textFont;
056:
057: public DailyReportServiceImpl() {
058: headerFont = FontFactory.getFont(FontFactory.COURIER, 8,
059: Font.BOLD);
060: textFont = FontFactory.getFont(FontFactory.COURIER, 8,
061: Font.NORMAL);
062: }
063:
064: private List<DailyReport> getData() {
065: LOG.debug("getData() started");
066:
067: return paymentDetailDao.getDailyReportData();
068: }
069:
070: public void runReport() {
071: LOG.debug("runReport() started");
072:
073: Collection<DailyReport> data = getData();
074: Date today = dateTimeService.getCurrentDate();
075: SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
076:
077: Document document = openPdfWriter(directoryName, "pdp_daily",
078: dateTimeService.getCurrentDate(),
079: "Summary of Payments Eligible For Format On "
080: + sdf.format(today));
081:
082: try {
083: float[] summaryWidths = { 20, 20, 20, 20, 20 };
084: PdfPTable dataTable = new PdfPTable(summaryWidths);
085: dataTable.setWidthPercentage(100);
086: dataTable.setHeaderRows(1);
087: addHeader(dataTable);
088:
089: boolean rows = false;
090: DailyReport sortTotal = new DailyReport();
091: DailyReport total = new DailyReport();
092:
093: for (Iterator iter = data.iterator(); iter.hasNext();) {
094: DailyReport dr = (DailyReport) iter.next();
095:
096: if (!rows) {
097: rows = true;
098: sortTotal = new DailyReport(dr);
099: sortTotal.addRow(dr);
100: addRow(dataTable, dr, false, dr.getSortGroupName());
101: } else if (!sortTotal.getSortGroupId().equals(
102: dr.getSortGroupId())) {
103: addRow(dataTable, sortTotal, true, "Total for "
104: + sortTotal.getSortGroupName());
105: sortTotal = new DailyReport(dr);
106: sortTotal.addRow(dr);
107: addRow(dataTable, dr, false, dr.getSortGroupName());
108: } else {
109: sortTotal.addRow(dr);
110: addRow(dataTable, dr, false, "");
111: }
112:
113: total.addRow(dr);
114: }
115:
116: if (rows) {
117: addRow(dataTable, sortTotal, true, "Total for "
118: + sortTotal.getSortGroupName());
119: }
120: addRow(dataTable, total, true, "Total");
121:
122: document.add(dataTable);
123: } catch (DocumentException d) {
124:
125: }
126: document.close();
127: }
128:
129: private void addHeader(PdfPTable dataTable) {
130: PdfPCell cell = new PdfPCell(new Phrase("Sort Order",
131: headerFont));
132: dataTable.addCell(cell);
133:
134: cell = new PdfPCell(new Phrase("Customer", headerFont));
135: dataTable.addCell(cell);
136:
137: cell = new PdfPCell(
138: new Phrase("Amount of Payments", headerFont));
139: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
140: dataTable.addCell(cell);
141:
142: cell = new PdfPCell(new Phrase("# of Payment Records",
143: headerFont));
144: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
145: dataTable.addCell(cell);
146:
147: cell = new PdfPCell(new Phrase("# of Payees", headerFont));
148: cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
149: dataTable.addCell(cell);
150: }
151:
152: private void addRow(PdfPTable dataTable, DailyReport dr,
153: boolean bold) {
154: addRow(dataTable, dr, bold, dr.getSortGroupName());
155: }
156:
157: private void addRow(PdfPTable dataTable, DailyReport dr,
158: boolean bold, String name) {
159: DecimalFormat af = new DecimalFormat("###,###,##0.00");
160: DecimalFormat nf = new DecimalFormat("###,##0");
161:
162: Font f = null;
163: if (bold) {
164: f = headerFont;
165:
166: for (int i = 0; i < 5; i++) {
167: PdfPCell cell = new PdfPCell(new Phrase(" ", f));
168: cell.setBorder(Rectangle.NO_BORDER);
169: dataTable.addCell(cell);
170: }
171: } else {
172: f = textFont;
173: }
174:
175: PdfPCell cell = new PdfPCell(new Phrase(name, f));
176: cell.setBorder(Rectangle.NO_BORDER);
177: dataTable.addCell(cell);
178:
179: if (!bold) {
180: cell = new PdfPCell(new Phrase(dr.getCustomer(), f));
181: } else {
182: cell = new PdfPCell(new Phrase("", f));
183: }
184: cell.setBorder(Rectangle.NO_BORDER);
185: dataTable.addCell(cell);
186:
187: cell = new PdfPCell(new Phrase(af.format(dr.getAmount()), f));
188: cell.setBorder(Rectangle.NO_BORDER);
189: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
190: dataTable.addCell(cell);
191:
192: cell = new PdfPCell(new Phrase(nf.format(dr.getPayments()), f));
193: cell.setBorder(Rectangle.NO_BORDER);
194: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
195: dataTable.addCell(cell);
196:
197: cell = new PdfPCell(new Phrase(nf.format(dr.getPayees()), f));
198: cell.setBorder(Rectangle.NO_BORDER);
199: cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
200: dataTable.addCell(cell);
201:
202: if (bold) {
203: for (int i = 0; i < 5; i++) {
204: PdfPCell cell2 = new PdfPCell(new Phrase(" ", f));
205: cell2.setBorder(Rectangle.NO_BORDER);
206: dataTable.addCell(cell2);
207: }
208: }
209: }
210:
211: protected Document openPdfWriter(String destinationDirectory,
212: String fileprefix, Date runDate, String title) {
213: try {
214: Document document = new Document(PageSize.A4.rotate());
215:
216: PageHelper helper = new PageHelper();
217: helper.runDate = runDate;
218: helper.headerFont = headerFont;
219: helper.title = title;
220:
221: String filename = destinationDirectory + "/" + fileprefix
222: + "_";
223: SimpleDateFormat sdf = new SimpleDateFormat(
224: "yyyyMMdd_HHmmss");
225: filename = filename + sdf.format(runDate);
226: filename = filename + ".pdf";
227: PdfWriter writer = PdfWriter.getInstance(document,
228: new FileOutputStream(filename));
229: writer.setPageEvent(helper);
230:
231: document.open();
232:
233: return document;
234: } catch (Exception e) {
235: LOG
236: .error(
237: "openPdfWriter() Exception caught trying to create new PDF document",
238: e);
239: if (e instanceof RuntimeException) {
240: throw (RuntimeException) e;
241: } else {
242: throw new RuntimeException(e);
243: }
244: }
245: }
246:
247: public void setDirectoryName(String d) {
248: directoryName = d;
249: }
250:
251: public void setDateTimeService(DateTimeService dts) {
252: dateTimeService = dts;
253: }
254:
255: public void setPaymentDetailDao(PaymentDetailDao pdd) {
256: paymentDetailDao = pdd;
257: }
258: }
|