001/*
002 * Copyright 2006-2009 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.ole.gl.report;
017
018import java.text.MessageFormat;
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.LinkedHashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.kuali.ole.gl.businessobject.OriginEntryInformation;
026import org.kuali.ole.gl.businessobject.PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal;
027import org.kuali.ole.gl.businessobject.PosterOutputSummaryBalanceTypeFiscalYearTotal;
028import org.kuali.ole.gl.businessobject.PosterOutputSummaryBalanceTypeTotal;
029import org.kuali.ole.gl.businessobject.PosterOutputSummaryEntry;
030import org.kuali.ole.gl.businessobject.PosterOutputSummaryTotal;
031import org.kuali.ole.gl.businessobject.Transaction;
032import org.kuali.ole.gl.service.PosterOutputSummaryService;
033import org.kuali.ole.sys.OLEKeyConstants;
034import org.kuali.ole.sys.context.SpringContext;
035import org.kuali.ole.sys.service.ReportWriterService;
036import org.kuali.rice.core.api.config.property.ConfigurationService;
037
038/**
039 * A class which builds up the data and then reports the PosterOutputSummary report
040 */
041public class PosterOutputSummaryReport {
042    private Map<String, PosterOutputSummaryEntry> posterOutputSummaryEntries;
043    private PosterOutputSummaryTotal posterOutputSummaryTotal;
044    private PosterOutputSummaryService posterOutputSummaryService;
045    
046    /**
047     * Constructs a PosterOutputSummaryReport
048     */
049    public PosterOutputSummaryReport() {
050        posterOutputSummaryTotal = new PosterOutputSummaryTotal();
051        posterOutputSummaryEntries = new LinkedHashMap<String, PosterOutputSummaryEntry>();
052    }
053    
054    /**
055     * Summarizes a transaction for this report
056     * @param transaction the transaction to summarize
057     */
058    public void summarize(Transaction transaction) {
059        getPosterOutputSummaryService().summarize(transaction, posterOutputSummaryEntries);
060    }
061    
062    /**
063     * Summarizes an origin entry for this report
064     * @param originEntry the origin entry to summarize
065     */
066    public void summarize(OriginEntryInformation originEntry) {
067        getPosterOutputSummaryService().summarize(originEntry, posterOutputSummaryEntries);
068    }
069    
070    /**
071     * Writes the report to the given reportWriterService
072     * @param reportWriterService the reportWriterService to write the report to
073     */
074    public void writeReport(ReportWriterService reportWriterService) {
075        List<PosterOutputSummaryEntry> entries = new ArrayList<PosterOutputSummaryEntry>(posterOutputSummaryEntries.values());
076        
077        if (entries.size() > 0) {
078            Collections.sort(entries, getPosterOutputSummaryService().getEntryComparator());
079            final ConfigurationService configurationService = SpringContext.getBean(ConfigurationService.class);
080            
081            String currentBalanceTypeCode = entries.get(0).getBalanceTypeCode();
082            PosterOutputSummaryBalanceTypeTotal balanceTypeTotal = new PosterOutputSummaryBalanceTypeTotal(currentBalanceTypeCode);
083            Integer currentFiscalYear = entries.get(0).getUniversityFiscalYear();
084            PosterOutputSummaryBalanceTypeFiscalYearTotal balanceTypeFiscalYearTotal = new PosterOutputSummaryBalanceTypeFiscalYearTotal(currentBalanceTypeCode, currentFiscalYear);
085            String currentFiscalPeriod = entries.get(0).getFiscalPeriodCode();
086            PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal balanceTypeFiscalYearAndPeriodTotal = new PosterOutputSummaryBalanceTypeFiscalYearAndPeriodTotal(currentBalanceTypeCode, currentFiscalYear, currentFiscalPeriod);
087        
088            final String titleMessage = configurationService.getPropertyValueAsString(OLEKeyConstants.MESSAGE_REPORT_POSTER_OUTPUT_SUMMARY_TITLE_LINE);
089            String formattedTitle = MessageFormat.format(titleMessage, entries.get(0).getUniversityFiscalYear().toString(), entries.get(0).getBalanceTypeCode());
090            
091            reportWriterService.writeFormattedMessageLine(formattedTitle);
092            reportWriterService.writeTableHeader(entries.get(0));
093            
094            for (PosterOutputSummaryEntry entry : entries) {
095                if (!entry.getBalanceTypeCode().equals(currentBalanceTypeCode)) {
096                    reportWriterService.writeTableRow(balanceTypeFiscalYearAndPeriodTotal);
097                    reportWriterService.writeTableRow(balanceTypeFiscalYearTotal);
098                    reportWriterService.writeTableRow(balanceTypeTotal);
099                    
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}