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.gl.batch;
17  
18  import java.io.File;
19  import java.text.SimpleDateFormat;
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.List;
23  
24  import org.apache.commons.io.filefilter.RegexFileFilter;
25  import org.kuali.ole.gl.GeneralLedgerConstants;
26  import org.kuali.ole.gl.batch.service.impl.OriginEntryFileIterator;
27  import org.kuali.ole.gl.businessobject.GlSummary;
28  import org.kuali.ole.gl.businessobject.OriginEntryInformation;
29  import org.kuali.ole.gl.businessobject.Reversal;
30  import org.kuali.ole.gl.report.PosterOutputSummaryReport;
31  import org.kuali.ole.gl.service.BalanceService;
32  import org.kuali.ole.gl.service.ReversalService;
33  import org.kuali.ole.sys.FileUtil;
34  import org.kuali.ole.sys.batch.AbstractWrappedBatchStep;
35  import org.kuali.ole.sys.batch.service.WrappedBatchExecutorService.CustomBatchExecutor;
36  import org.kuali.ole.sys.batch.service.WrappingBatchService;
37  import org.kuali.ole.sys.businessobject.SystemOptions;
38  import org.kuali.ole.sys.service.FiscalYearAwareReportWriterService;
39  import org.kuali.ole.sys.service.OptionsService;
40  import org.kuali.ole.sys.service.ReportWriterService;
41  
42  /**
43   * A step to generate summary reports from a recent poster run
44   */
45  public class PosterSummaryReportStep extends AbstractWrappedBatchStep {
46      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PosterSummaryReportStep.class);
47      private static final String DATE_FORMAT = "MMdd";
48      private static final String BUD = "bud";
49      private static final String ACT = "act";
50      private static final String ENC = "enc";
51      private OptionsService optionsService;
52      private ReportWriterService posterOutputSummaryReportWriterService;
53      private FiscalYearAwareReportWriterService posterActualBalanceSummaryReportWriterService;
54      private FiscalYearAwareReportWriterService posterBudgetBalanceSummaryReportWriterService;
55      private FiscalYearAwareReportWriterService posterEncumbranceSummaryReportWriterService;
56      private ReversalService reversalService;
57      private BalanceService balanceService;
58      private String batchFileDirectoryName;
59  
60      /**
61       * @see org.kuali.ole.sys.batch.AbstractWrappedBatchStep#getCustomBatchExecutor()
62       */
63      @Override
64      protected CustomBatchExecutor getCustomBatchExecutor() {
65          return new CustomBatchExecutor() {
66              /**
67               * Runs the process that generates poster summary reports.
68               * 
69               * @return true if the step completed successfully, false if otherwise
70               * @see org.kuali.ole.sys.batch.Step#execute(java.lang.String)
71               */
72              public boolean execute() {
73                  synchronized(this) { // why the synchronization?
74                      final String CURRENT_YEAR_LOWER = getCurrentYearLowerParameter();
75                      final String CURRENT_YEAR_UPPER = getCurrentYearUpperParameter();
76                      final String CURRENT_AND_LAST_YEAR = getCurrentAndLastYearParameter();
77      
78                      SystemOptions currentYear = optionsService.getCurrentYearOptions();
79                      SystemOptions nextYear = optionsService.getOptions(currentYear.getUniversityFiscalYear() + 1);
80                      SystemOptions previousYear = optionsService.getOptions(currentYear.getUniversityFiscalYear() - 1);
81      
82                      Date runDate = getDateTimeService().getCurrentDate();
83                      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
84                      String md = sdf.format(runDate);
85                      generatePosterOutputReport(runDate);
86                      if ((md.compareTo(CURRENT_YEAR_UPPER) > 0) || (md.compareTo(CURRENT_YEAR_LOWER) < 0)) {
87                          // Current year
88                          generateGlSummary(runDate, currentYear, BUD);
89                          generateGlSummary(runDate, currentYear, ACT);
90                          generateGlSummary(runDate, currentYear, ENC);
91                      }
92                      else if ((md.compareTo(CURRENT_AND_LAST_YEAR) > 0)) {
93                          // Current year and Last year
94                          generateGlSummary(runDate, currentYear, BUD);
95                          generateGlSummary(runDate, previousYear, BUD);
96                          generateGlSummary(runDate, currentYear, ACT);
97                          generateGlSummary(runDate, previousYear, ACT);
98                          generateGlSummary(runDate, currentYear, ENC);
99                          generateGlSummary(runDate, previousYear, ENC);
100                     }
101                     else {
102                         // Current year and next year
103                         generateGlSummary(runDate, currentYear, BUD);
104                         generateGlSummary(runDate, nextYear, BUD);
105                         generateGlSummary(runDate, currentYear, ACT);
106                         generateGlSummary(runDate, nextYear, ACT);
107                         generateGlSummary(runDate, currentYear, ENC);
108                         generateGlSummary(runDate, nextYear, ENC);
109                     }
110                 }
111                 return true;
112             }
113         };
114     }
115     
116     /**
117      * Generates reports about the output of a poster run.
118      * 
119      * @param runDate the date the poster was run.
120      */
121     private void generatePosterOutputReport(Date runDate) {
122         PosterOutputSummaryReport posterOutputSummaryReport = new PosterOutputSummaryReport();
123         
124         // summarize all the entries for the main poster
125         File mainPosterFile = FileUtil.getNewestFile(new File(batchFileDirectoryName), new RegexFileFilter((GeneralLedgerConstants.BatchFileSystem.POSTER_INPUT_FILE + "\\.[0-9_\\-]+\\" + GeneralLedgerConstants.BatchFileSystem.EXTENSION)));
126         if (mainPosterFile != null && mainPosterFile.exists()) {
127             OriginEntryFileIterator mainPosterIterator = new OriginEntryFileIterator(mainPosterFile);
128             while (mainPosterIterator.hasNext()) {
129                 final OriginEntryInformation originEntry = mainPosterIterator.next();
130                 posterOutputSummaryReport.summarize(originEntry);
131             }
132         } else {
133             LOG.warn("Could not Main Poster Input file with prefix "+GeneralLedgerConstants.BatchFileSystem.POSTER_INPUT_FILE+" for tabulation in the Poster Output Summary Report");
134         }
135 
136         // summarize today's reversals
137         File reversalPosterFile = FileUtil.getNewestFile(new File(batchFileDirectoryName), new RegexFileFilter((GeneralLedgerConstants.BatchFileSystem.REVERSAL_POSTER_VALID_OUTPUT_FILE + "\\.[0-9_\\-]+\\" + GeneralLedgerConstants.BatchFileSystem.EXTENSION)));
138         if (reversalPosterFile != null && reversalPosterFile.exists()) {
139             OriginEntryFileIterator reversalOriginEntryIterator = new OriginEntryFileIterator(reversalPosterFile);
140             while (reversalOriginEntryIterator.hasNext()) {
141                 final Reversal reversal = new Reversal (reversalOriginEntryIterator.next());
142                 posterOutputSummaryReport.summarize(reversal);
143             }
144         } else {
145             LOG.warn("Could not Reversal Output file with prefix "+GeneralLedgerConstants.BatchFileSystem.REVERSAL_POSTER_VALID_OUTPUT_FILE+" for tabulation in the Poster Output Summary Report");
146         }
147         
148         // summarize the icr poster
149         File icrPosterFile = FileUtil.getNewestFile(new File(batchFileDirectoryName), new RegexFileFilter(GeneralLedgerConstants.BatchFileSystem.ICR_POSTER_INPUT_FILE + "\\.[0-9_\\-]+\\" + GeneralLedgerConstants.BatchFileSystem.EXTENSION));
150         if (icrPosterFile != null && icrPosterFile.exists()) {
151             OriginEntryFileIterator icrIterator = new OriginEntryFileIterator(icrPosterFile);
152             while (icrIterator.hasNext()) {
153                 final OriginEntryInformation originEntry = icrIterator.next();
154                 posterOutputSummaryReport.summarize(originEntry);
155             }
156         } else {
157             LOG.warn("Could not Indirect Cost Recovery Poster Input file with prefix "+GeneralLedgerConstants.BatchFileSystem.ICR_POSTER_INPUT_FILE+" for tabulation in the Poster Output Summary Report");
158         }
159         
160         posterOutputSummaryReport.writeReport(posterOutputSummaryReportWriterService);
161     }
162 
163     /**
164      * Generates the GL Summary report
165      * 
166      * @param runDate the run date of the poster service that should be reported
167      * @param options the options of the fiscal year the poster was run
168      * @param reportType the type of the report that should be generated
169      */
170     protected void generateGlSummary(Date runDate, SystemOptions year, String reportType) {
171         LOG.debug("generateGlSummary() started");
172 
173         FiscalYearAwareReportWriterService fiscalYearAwareReportWriterService;
174         
175         List<String> balanceTypeCodes = new ArrayList<String>();
176         if (ACT.equals(reportType)) {
177             fiscalYearAwareReportWriterService = posterActualBalanceSummaryReportWriterService;
178             balanceTypeCodes.add(year.getActualFinancialBalanceTypeCd());
179         }
180         else if (BUD.equals(reportType)) {
181             fiscalYearAwareReportWriterService = posterBudgetBalanceSummaryReportWriterService;
182             balanceTypeCodes.add(year.getBudgetCheckingBalanceTypeCd());
183             balanceTypeCodes.add(year.getBaseBudgetFinancialBalanceTypeCd());
184             balanceTypeCodes.add(year.getMonthlyBudgetFinancialBalanceTypeCd());
185         } else { // ENC
186             fiscalYearAwareReportWriterService = posterEncumbranceSummaryReportWriterService;
187             balanceTypeCodes.add(year.getExtrnlEncumFinBalanceTypCd());
188             balanceTypeCodes.add(year.getIntrnlEncumFinBalanceTypCd());
189             balanceTypeCodes.add(year.getPreencumbranceFinBalTypeCd());
190             balanceTypeCodes.add(year.getCostShareEncumbranceBalanceTypeCd());
191         }
192 
193         List<GlSummary> balances = balanceService.getGlSummary(year.getUniversityFiscalYear(), balanceTypeCodes);
194         
195         GlSummary totals = new GlSummary();
196         for(GlSummary balance : balances) {
197             totals.add(balance);
198         }        
199         totals.setFundGroup("Total");
200         
201         try {
202             fiscalYearAwareReportWriterService.setFiscalYear(year.getUniversityFiscalYear());
203             ((WrappingBatchService) fiscalYearAwareReportWriterService).initialize();
204             
205             fiscalYearAwareReportWriterService.writeSubTitle("Balance Type of " + balanceTypeCodes + " for Fiscal Year " + year.getUniversityFiscalYearName());
206             fiscalYearAwareReportWriterService.writeNewLines(1);
207             
208             fiscalYearAwareReportWriterService.writeTableRowSeparationLine(totals);
209             fiscalYearAwareReportWriterService.writeTable(balances, true, false);
210             
211             fiscalYearAwareReportWriterService.writeTableRowSeparationLine(totals);
212             fiscalYearAwareReportWriterService.writeTableRow(totals);
213         } finally {
214             ((WrappingBatchService) fiscalYearAwareReportWriterService).destroy();
215         }
216     }
217     
218     /**
219      * @return current year lower parameter for inner class
220      */
221     public String getCurrentYearLowerParameter() {
222         return getParameterService().getParameterValueAsString(getClass(), GeneralLedgerConstants.GlSummaryReport.CURRENT_YEAR_LOWER);
223     }
224     
225     /**
226      * @return current year upper parameter for inner class
227      */
228     public String getCurrentYearUpperParameter() {
229         return getParameterService().getParameterValueAsString(PosterSummaryReportStep.this.getClass(), GeneralLedgerConstants.GlSummaryReport.CURRENT_YEAR_UPPER);
230     }
231     
232     /**
233      * @return current and last year parameter for inner class
234      */
235     public String getCurrentAndLastYearParameter() {
236         return getParameterService().getParameterValueAsString(PosterSummaryReportStep.this.getClass(), GeneralLedgerConstants.GlSummaryReport.CURRENT_AND_LAST_YEAR);
237     }
238 
239     /**
240      * Sets the optionsService attribute, allowing the injection of an implementation of that service
241      * 
242      * @param os the optionsService implementation to set
243      * @see org.kuali.ole.sys.service.OptionsService
244      */
245     public void setOptionsService(OptionsService os) {
246         optionsService = os;
247     }
248 
249     /**
250      * Gets the posterOutputSummaryReportWriterService attribute. 
251      * @return Returns the posterOutputSummaryReportWriterService.
252      */
253     public ReportWriterService getPosterOutputSummaryReportWriterService() {
254         return posterOutputSummaryReportWriterService;
255     }
256 
257     /**
258      * Sets the posterOutputSummaryReportWriterService attribute value.
259      * @param posterOutputSummaryReportWriterService The posterOutputSummaryReportWriterService to set.
260      */
261     public void setPosterOutputSummaryReportWriterService(ReportWriterService posterOutputSummaryReportWriterService) {
262         this.posterOutputSummaryReportWriterService = posterOutputSummaryReportWriterService;
263     }
264 
265     /**
266      * Sets the posterActualBalanceSummaryReportWriterService attribute value.
267      * @param posterActualBalanceSummaryReportWriterService The posterActualBalanceSummaryReportWriterService to set.
268      */
269     public void setPosterActualBalanceSummaryReportWriterService(FiscalYearAwareReportWriterService posterActualBalanceSummaryReportWriterService) {
270         this.posterActualBalanceSummaryReportWriterService = posterActualBalanceSummaryReportWriterService;
271     }
272 
273     /**
274      * Sets the posterBudgetBalanceSummaryReportWriterService attribute value.
275      * @param posterBudgetBalanceSummaryReportWriterService The posterBudgetBalanceSummaryReportWriterService to set.
276      */
277     public void setPosterBudgetBalanceSummaryReportWriterService(FiscalYearAwareReportWriterService posterBudgetBalanceSummaryReportWriterService) {
278         this.posterBudgetBalanceSummaryReportWriterService = posterBudgetBalanceSummaryReportWriterService;
279     }
280 
281     /**
282      * Sets the posterEncumbranceSummaryReportWriterService attribute value.
283      * @param posterEncumbranceSummaryReportWriterService The posterEncumbranceSummaryReportWriterService to set.
284      */
285     public void setPosterEncumbranceSummaryReportWriterService(FiscalYearAwareReportWriterService posterEncumbranceSummaryReportWriterService) {
286         this.posterEncumbranceSummaryReportWriterService = posterEncumbranceSummaryReportWriterService;
287     }
288 
289     /**
290      * Gets the reversalService attribute. 
291      * @return Returns the reversalService.
292      */
293     public ReversalService getReversalService() {
294         return reversalService;
295     }
296 
297     /**
298      * Sets the reversalService attribute value.
299      * @param reversalService The reversalService to set.
300      */
301     public void setReversalService(ReversalService reversalService) {
302         this.reversalService = reversalService;
303     }
304 
305     /**
306      * Sets the balanceService attribute value.
307      * @param balanceService The balanceService to set.
308      */
309     public void setBalanceService(BalanceService balanceService) {
310         this.balanceService = balanceService;
311     }
312 
313     /**
314      * Gets the batchFileDirectoryName attribute. 
315      * @return Returns the batchFileDirectoryName.
316      */
317     public String getBatchFileDirectoryName() {
318         return batchFileDirectoryName;
319     }
320 
321     /**
322      * Sets the batchFileDirectoryName attribute value.
323      * @param batchFileDirectoryName The batchFileDirectoryName to set.
324      */
325     public void setBatchFileDirectoryName(String batchFileDirectoryName) {
326         this.batchFileDirectoryName = batchFileDirectoryName;
327     }
328 }