View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.pdp.batch.service.impl;
20  
21  import java.text.MessageFormat;
22  import java.util.Collection;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.kfs.pdp.PdpKeyConstants;
28  import org.kuali.kfs.pdp.batch.service.DailyReportService;
29  import org.kuali.kfs.pdp.businessobject.DailyReport;
30  import org.kuali.kfs.pdp.dataaccess.PaymentDetailDao;
31  import org.kuali.kfs.pdp.service.PaymentGroupService;
32  import org.kuali.kfs.sys.service.ReportWriterService;
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  @Transactional
38  public class DailyReportServiceImpl implements DailyReportService {
39      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DailyReportServiceImpl.class);
40  
41      protected PaymentDetailDao paymentDetailDao;
42      protected DateTimeService dateTimeService;
43      protected PaymentGroupService paymentGroupService;
44      protected ConfigurationService kualiConfigurationService;
45      protected ReportWriterService dailyReportReportWriterService;
46  
47      protected List<DailyReport> getData() {
48          LOG.debug("getData() started");
49  
50          return paymentDetailDao.getDailyReportData(dateTimeService.getCurrentSqlDate());
51      }
52  
53      @Override
54      public void runReport() {
55          LOG.debug("runReport() started");
56  
57          Collection<DailyReport> data = getData();
58          Date today = dateTimeService.getCurrentDate();
59  
60          // Title and table header
61          dailyReportReportWriterService.writeSubTitle(dateTimeService.toDateTimeStringForFilename(today));
62          dailyReportReportWriterService.writeNewLines(1);
63          dailyReportReportWriterService.writeTableHeader(DailyReport.class);
64  
65          // Objects for sort total and total
66          DailyReport dailyReportSortTotal = new DailyReport();
67          DailyReport dailyReportTotal = new DailyReport();
68          dailyReportTotal.setSortOrder(this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_TOTAL_SUBTITLE));
69  
70          final String DAILY_REPORT_SORT_TOTAL_PROPERTY_STRING = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.DAILY_REPORT_SERVICE_TOTAL_FOR_SUBTITLE);
71  
72          // first is the first entry (ever) we are writing. firstSortTotal is the first we are writing after sortTotal  was written
73          boolean first = true;
74          boolean firstSortTotal = true;
75  
76          for (DailyReport dailyReport : data) {
77              if (!first
78                      && this.paymentGroupService.getSortGroupId(dailyReportSortTotal.getPaymentGroup()) != this.paymentGroupService.getSortGroupId(dailyReport.getPaymentGroup())) {
79                  // Clause is for if the sort group changed which means we print a sortTotal. Unless this is the first element we print, then this doesn't apply
80  
81                  firstSortTotal = true;
82  
83                  // write sortTotal row (sub title)
84                  dailyReportReportWriterService.writeTableRow(dailyReportSortTotal);
85                  dailyReportReportWriterService.writeNewLines(1);
86  
87                  // reset subtotal
88                  dailyReportSortTotal = new DailyReport();
89              } else if (first) {
90                  first = false;
91              }
92  
93              if (firstSortTotal) {
94                  // Set the sortOrder (aka "Title" of the row) since this is the firstSortTotal
95                  dailyReport.setSortOrder(this.paymentGroupService.getSortGroupName(this.paymentGroupService.getSortGroupId(dailyReport.getPaymentGroup())));
96                  firstSortTotal = false;
97              }
98  
99              // We always write the row
100             dailyReportReportWriterService.writeTableRow(dailyReport);
101 
102             // Do the summation
103             dailyReportSortTotal.addRow(dailyReport);
104             dailyReportTotal.addRow(dailyReport);
105 
106             // Set the sortTotal title if it hasn't been set yet. There are two scenarios this happens for: Only 1 element in list or we just reset dailyReportSortTotal
107             if (StringUtils.isEmpty(dailyReportSortTotal.getSortOrder())) {
108                 String newTotalForSubtitle = MessageFormat.format(DAILY_REPORT_SORT_TOTAL_PROPERTY_STRING, new Object[] { this.paymentGroupService.getSortGroupName(this.paymentGroupService.getSortGroupId(dailyReportSortTotal.getPaymentGroup())) });
109                 dailyReportSortTotal.setSortOrder(newTotalForSubtitle);
110             }
111         }
112 
113         // At the end we still have one sortTotal to write. Unless there was no data
114         if (!first) {
115             // Final subtotal to write if there was one
116             dailyReportReportWriterService.writeTableRow(dailyReportSortTotal);
117             dailyReportReportWriterService.writeNewLines(1);
118         }
119 
120         // Write the final total
121         dailyReportReportWriterService.writeTableRowSeparationLine(dailyReportTotal);
122         dailyReportReportWriterService.writeTableRow(dailyReportTotal);
123     }
124 
125     public void setDateTimeService(DateTimeService dts) {
126         dateTimeService = dts;
127     }
128 
129     public void setPaymentDetailDao(PaymentDetailDao pdd) {
130         paymentDetailDao = pdd;
131     }
132 
133     /**
134      * @see org.kuali.kfs.pdp.batch.service.DailyReportService#setPaymentGroupService(org.kuali.kfs.pdp.service.PaymentGroupService)
135      */
136     @Override
137     public void setPaymentGroupService(PaymentGroupService paymentGroupService) {
138         this.paymentGroupService = paymentGroupService;
139     }
140 
141     public void setConfigurationService(ConfigurationService kualiConfigurationService) {
142         this.kualiConfigurationService = kualiConfigurationService;
143     }
144 
145     public ReportWriterService getDailyReportReportWriterService() {
146         return dailyReportReportWriterService;
147     }
148 
149     public void setDailyReportReportWriterService(ReportWriterService dailyReportReportWriterService) {
150         this.dailyReportReportWriterService = dailyReportReportWriterService;
151     }
152 }