View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.pdp.batch.service.impl;
17  
18  import java.io.FileOutputStream;
19  import java.text.DecimalFormat;
20  import java.text.MessageFormat;
21  import java.text.SimpleDateFormat;
22  import java.util.Collection;
23  import java.util.Date;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.kuali.ole.gl.report.TransactionReport.PageHelper;
28  import org.kuali.ole.pdp.PdpKeyConstants;
29  import org.kuali.ole.pdp.batch.service.DailyReportService;
30  import org.kuali.ole.pdp.businessobject.DailyReport;
31  import org.kuali.ole.pdp.dataaccess.PaymentDetailDao;
32  import org.kuali.ole.pdp.service.PaymentGroupService;
33  import org.kuali.rice.core.api.config.property.ConfigurationService;
34  import org.kuali.rice.core.api.datetime.DateTimeService;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  import com.lowagie.text.Document;
38  import com.lowagie.text.DocumentException;
39  import com.lowagie.text.Element;
40  import com.lowagie.text.Font;
41  import com.lowagie.text.FontFactory;
42  import com.lowagie.text.PageSize;
43  import com.lowagie.text.Phrase;
44  import com.lowagie.text.Rectangle;
45  import com.lowagie.text.pdf.PdfPCell;
46  import com.lowagie.text.pdf.PdfPTable;
47  import com.lowagie.text.pdf.PdfWriter;
48  
49  @Transactional
50  public class DailyReportServiceImpl implements DailyReportService {
51      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DailyReportServiceImpl.class);
52  
53      private PaymentDetailDao paymentDetailDao;
54      private DateTimeService dateTimeService;
55      private String directoryName;
56      private PaymentGroupService paymentGroupService;
57      private ConfigurationService kualiConfigurationService;
58  
59      private Font headerFont;
60      private Font textFont;
61  
62      public DailyReportServiceImpl() {
63          headerFont = FontFactory.getFont(FontFactory.COURIER, 8, Font.BOLD);
64          textFont = FontFactory.getFont(FontFactory.COURIER, 8, Font.NORMAL);
65      }
66  
67      protected List<DailyReport> getData() {
68          LOG.debug("getData() started");
69  
70          return paymentDetailDao.getDailyReportData(dateTimeService.getCurrentSqlDate());
71      }
72  
73      public void runReport() {
74          LOG.debug("runReport() started");
75  
76          Collection<DailyReport> data = getData();
77          Date today = dateTimeService.getCurrentDate();
78          SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
79  
80          String reportFilePrefix = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_FILE_PREFIX);
81          reportFilePrefix = MessageFormat.format(reportFilePrefix, new Object[] { null });
82  
83          String reportTitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_REPORT_TITLE);
84          reportTitle = MessageFormat.format(reportTitle, new Object[] { sdf.format(today) });
85  
86          Document document = openPdfWriter(directoryName, reportFilePrefix, dateTimeService.getCurrentDate(), reportTitle);
87  
88          try {
89              float[] summaryWidths = { 20, 20, 20, 20, 20 };
90              PdfPTable dataTable = new PdfPTable(summaryWidths);
91              dataTable.setWidthPercentage(100);
92              dataTable.setHeaderRows(1);
93              addHeader(dataTable);
94  
95              boolean rows = false;
96              DailyReport sortTotal = new DailyReport();
97              DailyReport total = new DailyReport();
98              DailyReport dr = new DailyReport();
99  
100             String totalForSubtitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_TOTAL_FOR_SUBTITLE);
101 
102             String totalSubtitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_TOTAL_SUBTITLE);
103             totalSubtitle = MessageFormat.format(totalSubtitle, new Object[] { null });
104 
105 
106             for (Iterator iter = data.iterator(); iter.hasNext();) {
107                 dr = (DailyReport) iter.next();
108 
109                 if (!rows) {
110                     rows = true;
111                     sortTotal = new DailyReport(dr);
112                     sortTotal.addRow(dr);
113                     addRow(dataTable, dr, false, this.paymentGroupService.getSortGroupName(this.paymentGroupService.getSortGroupId(dr.getPaymentGroup())));
114                 }
115                 else if (this.paymentGroupService.getSortGroupId(sortTotal.getPaymentGroup()) != (this.paymentGroupService.getSortGroupId(dr.getPaymentGroup()))) {
116                     String newTotalForSubtitle = MessageFormat.format(totalForSubtitle, new Object[] { this.paymentGroupService.getSortGroupName(this.paymentGroupService.getSortGroupId(sortTotal.getPaymentGroup())) });
117 
118                     addRow(dataTable, sortTotal, true, newTotalForSubtitle);
119                     sortTotal = new DailyReport(dr);
120                     sortTotal.addRow(dr);
121                     addRow(dataTable, dr, false, this.paymentGroupService.getSortGroupName(this.paymentGroupService.getSortGroupId(dr.getPaymentGroup())));
122                 }
123                 else {
124                     sortTotal.addRow(dr);
125                     addRow(dataTable, dr, false, "");
126                 }
127 
128                 total.addRow(dr);
129             }
130 
131             if (rows) {
132                 String newTotalForSubtitle = MessageFormat.format(totalForSubtitle, new Object[] { this.paymentGroupService.getSortGroupName(this.paymentGroupService.getSortGroupId(sortTotal.getPaymentGroup())) });
133 
134                 addRow(dataTable, sortTotal, true, newTotalForSubtitle);
135             }
136             addRow(dataTable, total, true, totalSubtitle);
137 
138             document.add(dataTable);
139         }
140         catch (DocumentException d) {
141             throw new RuntimeException(d);
142         }
143         document.close();
144     }
145 
146     protected void addHeader(PdfPTable dataTable) {
147         String sortOrderSubtitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_SORT_ORDER_SUBTITLE);
148         sortOrderSubtitle = MessageFormat.format(sortOrderSubtitle, new Object[] { null });
149 
150         PdfPCell cell = new PdfPCell(new Phrase(sortOrderSubtitle, headerFont));
151         dataTable.addCell(cell);
152 
153         String customerSubtitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_CUSTOMER_SUBTITLE);
154         customerSubtitle = MessageFormat.format(customerSubtitle, new Object[] { null });
155 
156         cell = new PdfPCell(new Phrase(customerSubtitle, headerFont));
157         dataTable.addCell(cell);
158 
159         String amountOfPaymentsSubtitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_AMOUNT_OF_PAYMENTS_SUBTITLE);
160         amountOfPaymentsSubtitle = MessageFormat.format(amountOfPaymentsSubtitle, new Object[] { null });
161 
162         cell = new PdfPCell(new Phrase(amountOfPaymentsSubtitle, headerFont));
163         cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
164         dataTable.addCell(cell);
165 
166         String numberOfPaymentRecordsSubtitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_NUMBER_OF_PAYMENT_RECORDS_SUBTITLE);
167         numberOfPaymentRecordsSubtitle = MessageFormat.format(numberOfPaymentRecordsSubtitle, new Object[] { null });
168 
169         cell = new PdfPCell(new Phrase(numberOfPaymentRecordsSubtitle, headerFont));
170         cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
171         dataTable.addCell(cell);
172 
173         String numberOfPayeesSubtitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_NUMBER_OF_PAYEES_SUBTITLE);
174         numberOfPayeesSubtitle = MessageFormat.format(numberOfPayeesSubtitle, new Object[] { null });
175 
176         cell = new PdfPCell(new Phrase(numberOfPayeesSubtitle, headerFont));
177         cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
178         dataTable.addCell(cell);
179     }
180 
181     protected void addRow(PdfPTable dataTable, DailyReport dr, boolean bold) {
182         addRow(dataTable, dr, bold, this.paymentGroupService.getSortGroupName(this.paymentGroupService.getSortGroupId(dr.getPaymentGroup())));
183     }
184 
185     protected void addRow(PdfPTable dataTable, DailyReport dr, boolean bold, String name) {
186         DecimalFormat af = new DecimalFormat("###,###,##0.00");
187         DecimalFormat nf = new DecimalFormat("###,##0");
188 
189         Font f = null;
190         if (bold) {
191             f = headerFont;
192 
193             for (int i = 0; i < 5; i++) {
194                 PdfPCell cell = new PdfPCell(new Phrase(" ", f));
195                 cell.setBorder(Rectangle.NO_BORDER);
196                 dataTable.addCell(cell);
197             }
198         }
199         else {
200             f = textFont;
201         }
202 
203         PdfPCell cell = new PdfPCell(new Phrase(name, f));
204         cell.setBorder(Rectangle.NO_BORDER);
205         dataTable.addCell(cell);
206 
207         if (!bold) {
208             cell = new PdfPCell(new Phrase(dr.getCustomer(), f));
209         }
210         else {
211             cell = new PdfPCell(new Phrase("", f));
212         }
213         cell.setBorder(Rectangle.NO_BORDER);
214         dataTable.addCell(cell);
215 
216         cell = new PdfPCell(new Phrase(af.format(dr.getAmount()), f));
217         cell.setBorder(Rectangle.NO_BORDER);
218         cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
219         dataTable.addCell(cell);
220 
221         cell = new PdfPCell(new Phrase(nf.format(dr.getPayments()), f));
222         cell.setBorder(Rectangle.NO_BORDER);
223         cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
224         dataTable.addCell(cell);
225 
226         cell = new PdfPCell(new Phrase(nf.format(dr.getPayees()), f));
227         cell.setBorder(Rectangle.NO_BORDER);
228         cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
229         dataTable.addCell(cell);
230 
231         if (bold) {
232             for (int i = 0; i < 5; i++) {
233                 PdfPCell cell2 = new PdfPCell(new Phrase(" ", f));
234                 cell2.setBorder(Rectangle.NO_BORDER);
235                 dataTable.addCell(cell2);
236             }
237         }
238     }
239 
240     protected Document openPdfWriter(String destinationDirectory, String fileprefix, Date runDate, String title) {
241         try {
242             Document document = new Document(PageSize.A4.rotate());
243 
244             PageHelper helper = new PageHelper();
245             helper.runDate = runDate;
246             helper.headerFont = headerFont;
247             helper.title = title;
248 
249             String filename = destinationDirectory + "/" + fileprefix + "_";
250             SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
251             filename = filename + sdf.format(runDate);
252             filename = filename + ".pdf";
253             PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
254             writer.setPageEvent(helper);
255 
256             document.open();
257 
258             return document;
259         }
260         catch (Exception e) {
261             LOG.error("openPdfWriter() Exception caught trying to create new PDF document", e);
262             if (e instanceof RuntimeException) {
263                 throw (RuntimeException) e;
264             }
265             else {
266                 throw new RuntimeException(e);
267             }
268         }
269     }
270 
271     public void setDirectoryName(String d) {
272         directoryName = d;
273     }
274 
275     public void setDateTimeService(DateTimeService dts) {
276         dateTimeService = dts;
277     }
278 
279     public void setPaymentDetailDao(PaymentDetailDao pdd) {
280         paymentDetailDao = pdd;
281     }
282 
283     /**
284      * @see org.kuali.ole.pdp.batch.service.DailyReportService#setPaymentGroupService(org.kuali.ole.pdp.service.PaymentGroupService)
285      */
286     public void setPaymentGroupService(PaymentGroupService paymentGroupService) {
287         this.paymentGroupService = paymentGroupService;
288     }
289 
290     public void setConfigurationService(ConfigurationService kualiConfigurationService) {
291         this.kualiConfigurationService = kualiConfigurationService;
292     }
293 }