View Javadoc
1   /*
2    * Copyright 2006-2009 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.report;
17  
18  import java.text.MessageFormat;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.kuali.ole.gl.businessobject.OriginEntryInformation;
26  import org.kuali.ole.gl.businessobject.PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal;
27  import org.kuali.ole.gl.businessobject.PosterOutputSummaryBalanceTypeFiscalYearTotal;
28  import org.kuali.ole.gl.businessobject.PosterOutputSummaryBalanceTypeTotal;
29  import org.kuali.ole.gl.businessobject.PosterOutputSummaryEntry;
30  import org.kuali.ole.gl.businessobject.PosterOutputSummaryTotal;
31  import org.kuali.ole.gl.businessobject.Transaction;
32  import org.kuali.ole.gl.service.PosterOutputSummaryService;
33  import org.kuali.ole.sys.OLEKeyConstants;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.ole.sys.service.ReportWriterService;
36  import org.kuali.rice.core.api.config.property.ConfigurationService;
37  
38  /**
39   * A class which builds up the data and then reports the PosterOutputSummary report
40   */
41  public class PosterOutputSummaryReport {
42      private Map<String, PosterOutputSummaryEntry> posterOutputSummaryEntries;
43      private PosterOutputSummaryTotal posterOutputSummaryTotal;
44      private PosterOutputSummaryService posterOutputSummaryService;
45      
46      /**
47       * Constructs a PosterOutputSummaryReport
48       */
49      public PosterOutputSummaryReport() {
50          posterOutputSummaryTotal = new PosterOutputSummaryTotal();
51          posterOutputSummaryEntries = new LinkedHashMap<String, PosterOutputSummaryEntry>();
52      }
53      
54      /**
55       * Summarizes a transaction for this report
56       * @param transaction the transaction to summarize
57       */
58      public void summarize(Transaction transaction) {
59          getPosterOutputSummaryService().summarize(transaction, posterOutputSummaryEntries);
60      }
61      
62      /**
63       * Summarizes an origin entry for this report
64       * @param originEntry the origin entry to summarize
65       */
66      public void summarize(OriginEntryInformation originEntry) {
67          getPosterOutputSummaryService().summarize(originEntry, posterOutputSummaryEntries);
68      }
69      
70      /**
71       * Writes the report to the given reportWriterService
72       * @param reportWriterService the reportWriterService to write the report to
73       */
74      public void writeReport(ReportWriterService reportWriterService) {
75          List<PosterOutputSummaryEntry> entries = new ArrayList<PosterOutputSummaryEntry>(posterOutputSummaryEntries.values());
76          
77          if (entries.size() > 0) {
78              Collections.sort(entries, getPosterOutputSummaryService().getEntryComparator());
79              final ConfigurationService configurationService = SpringContext.getBean(ConfigurationService.class);
80              
81              String currentBalanceTypeCode = entries.get(0).getBalanceTypeCode();
82              PosterOutputSummaryBalanceTypeTotal balanceTypeTotal = new PosterOutputSummaryBalanceTypeTotal(currentBalanceTypeCode);
83              Integer currentFiscalYear = entries.get(0).getUniversityFiscalYear();
84              PosterOutputSummaryBalanceTypeFiscalYearTotal balanceTypeFiscalYearTotal = new PosterOutputSummaryBalanceTypeFiscalYearTotal(currentBalanceTypeCode, currentFiscalYear);
85              String currentFiscalPeriod = entries.get(0).getFiscalPeriodCode();
86              PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal balanceTypeFiscalYearAndPeriodTotal = new PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal(currentBalanceTypeCode, currentFiscalYear, currentFiscalPeriod);
87          
88              final String titleMessage = configurationService.getPropertyValueAsString(OLEKeyConstants.MESSAGE_REPORT_POSTER_OUTPUT_SUMMARY_TITLE_LINE);
89              String formattedTitle = MessageFormat.format(titleMessage, entries.get(0).getUniversityFiscalYear().toString(), entries.get(0).getBalanceTypeCode());
90              
91              reportWriterService.writeFormattedMessageLine(formattedTitle);
92              reportWriterService.writeTableHeader(entries.get(0));
93              
94              for (PosterOutputSummaryEntry entry : entries) {
95                  if (!entry.getBalanceTypeCode().equals(currentBalanceTypeCode)) {
96                      reportWriterService.writeTableRow(balanceTypeFiscalYearAndPeriodTotal);
97                      reportWriterService.writeTableRow(balanceTypeFiscalYearTotal);
98                      reportWriterService.writeTableRow(balanceTypeTotal);
99                      
100                     balanceTypeFiscalYearAndPeriodTotal = new PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal(entry.getBalanceTypeCode(), entry.getUniversityFiscalYear(), entry.getFiscalPeriodCode());
101                     balanceTypeFiscalYearTotal = new PosterOutputSummaryBalanceTypeFiscalYearTotal(entry.getBalanceTypeCode(), entry.getUniversityFiscalYear());
102                     balanceTypeTotal = new PosterOutputSummaryBalanceTypeTotal(entry.getBalanceTypeCode());
103                     currentBalanceTypeCode = entry.getBalanceTypeCode();
104                     currentFiscalYear = entry.getUniversityFiscalYear();
105                     currentFiscalPeriod = entry.getFiscalPeriodCode();
106                     
107                     // new top-level header for balance types
108                     reportWriterService.pageBreak();
109                     formattedTitle = MessageFormat.format(titleMessage, currentFiscalYear.toString(), currentBalanceTypeCode);
110                     reportWriterService.writeFormattedMessageLine(formattedTitle);
111                     reportWriterService.writeTableHeader(entry);
112                 } else if (!entry.getUniversityFiscalYear().equals(currentFiscalYear)) {
113                     reportWriterService.writeTableRow(balanceTypeFiscalYearAndPeriodTotal);
114                     reportWriterService.writeTableRow(balanceTypeFiscalYearTotal);
115                     
116                     balanceTypeFiscalYearAndPeriodTotal = new PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal(entry.getBalanceTypeCode(), entry.getUniversityFiscalYear(), entry.getFiscalPeriodCode());
117                     balanceTypeFiscalYearTotal = new PosterOutputSummaryBalanceTypeFiscalYearTotal(entry.getBalanceTypeCode(), entry.getUniversityFiscalYear());
118                     currentFiscalYear = entry.getUniversityFiscalYear();
119                     currentFiscalPeriod = entry.getFiscalPeriodCode();
120                 } else if (!entry.getFiscalPeriodCode().equals(currentFiscalPeriod)) {
121                     reportWriterService.writeTableRow(balanceTypeFiscalYearAndPeriodTotal);
122                     
123                     balanceTypeFiscalYearAndPeriodTotal = new PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal(entry.getBalanceTypeCode(), entry.getUniversityFiscalYear(), entry.getFiscalPeriodCode());
124                     currentFiscalPeriod = entry.getFiscalPeriodCode();
125                 }
126                 
127                 reportWriterService.writeTableRow(entry);
128                 balanceTypeFiscalYearAndPeriodTotal.addAmount(entry);
129                 balanceTypeFiscalYearTotal.addAmount(entry);
130                 balanceTypeTotal.addAmount(entry);
131                 posterOutputSummaryTotal.addAmount(entry);
132             }
133             
134             reportWriterService.writeTableRow(balanceTypeFiscalYearAndPeriodTotal);
135             reportWriterService.writeTableRow(balanceTypeFiscalYearTotal);
136             reportWriterService.writeTableRow(balanceTypeTotal);
137             reportWriterService.writeNewLines(1);
138             reportWriterService.writeTableRow(posterOutputSummaryTotal);
139         }
140     }
141     
142     /**
143      * @return an implementation of the PosterOutputSummaryService
144      */
145     public PosterOutputSummaryService getPosterOutputSummaryService() {
146         if (posterOutputSummaryService == null) {
147             posterOutputSummaryService = SpringContext.getBean(PosterOutputSummaryService.class);
148         }
149         
150         return posterOutputSummaryService;
151     }
152 }