View Javadoc
1   /*
2    * Copyright 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.util.ArrayList;
19  import java.util.LinkedHashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.gl.businessobject.LedgerBalanceTypeSummaryTotalLine;
25  import org.kuali.ole.gl.businessobject.LedgerSummaryDetailLine;
26  import org.kuali.ole.gl.businessobject.LedgerSummaryTotalLine;
27  import org.kuali.ole.gl.businessobject.OriginEntryInformation;
28  import org.kuali.ole.sys.DynamicCollectionComparator;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.service.ReportWriterService;
31  
32  /**
33   * Helper class which can summarize entries by balance type and then print out a ledger summary report
34   */
35  public class LedgerSummaryReport {
36      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LedgerSummaryReport.class);
37      
38      private LedgerSummaryTotalLine ledgerTotalLine;
39      private Map<String, LedgerBalanceTypeSummaryTotalLine> balanceTypeTotals;
40      Map<String, LedgerSummaryDetailLine> details;
41      
42      /**
43       * Constructs a LedgerSummaryReport
44       */
45      public LedgerSummaryReport() {
46          ledgerTotalLine = new LedgerSummaryTotalLine();
47          balanceTypeTotals = new LinkedHashMap<String, LedgerBalanceTypeSummaryTotalLine>();
48          details = new LinkedHashMap<String, LedgerSummaryDetailLine>();
49      }
50      
51      /**
52       * Summarizes an entry into the various totals which this report is keeping
53       * @param entry an entry to summarize
54       */
55      public void summarizeEntry(OriginEntryInformation entry) {
56          LedgerBalanceTypeSummaryTotalLine balanceTypeTotal = getBalanceTypeSummaryTotalLine(entry, balanceTypeTotals);
57          LedgerSummaryDetailLine detailLine = getDetailLine(entry, details);
58          addEntryToLedgerSummaries(entry, ledgerTotalLine, balanceTypeTotal, detailLine);
59      }
60      
61      /**
62       * Retrieves the proper balance type summarizer from the given map, or creates a new summarizer and puts it in the Map if it doesn't already exist
63       * @param entry the origin entry to find a balance type summarizer for
64       * @param balanceTypeTotals the Map of balance type summarizers
65       * @return the proper balance type summarizer
66       */
67      protected LedgerBalanceTypeSummaryTotalLine getBalanceTypeSummaryTotalLine(OriginEntryInformation entry, Map<String, LedgerBalanceTypeSummaryTotalLine> balanceTypeTotals) {
68          final String balanceTypeCode = (StringUtils.isBlank(entry.getFinancialBalanceTypeCode())) ? " " : entry.getFinancialBalanceTypeCode();
69          LedgerBalanceTypeSummaryTotalLine balanceTypeTotal = balanceTypeTotals.get(balanceTypeCode);
70          if (balanceTypeTotal == null) {
71              balanceTypeTotal = new LedgerBalanceTypeSummaryTotalLine(balanceTypeCode);
72              balanceTypeTotals.put(balanceTypeCode, balanceTypeTotal);
73          }
74          return balanceTypeTotal;
75      }
76      
77      /**
78       * Retrieves the proper detail line summarizer from the given map, or creates a new summarizer and adds it to the map if needed
79       * @param entry the origin entry to find a detail line summarizer for
80       * @param detailLines a Map of detail line summarizers
81       * @return the proper detail line summarizer
82       */
83      protected LedgerSummaryDetailLine getDetailLine(OriginEntryInformation entry, Map<String, LedgerSummaryDetailLine> detailLines) {
84          final String key = LedgerSummaryDetailLine.getKeyString(entry);
85          LedgerSummaryDetailLine detailLine = detailLines.get(key);
86          if (detailLine == null) {
87              detailLine = new LedgerSummaryDetailLine(entry.getFinancialBalanceTypeCode(), entry.getFinancialSystemOriginationCode(), entry.getUniversityFiscalYear(), entry.getUniversityFiscalPeriodCode());
88              detailLines.put(detailLine.getKey(), detailLine);
89          }
90          return detailLine;
91      }
92      
93      /**
94       * Adds the amount of the origin entry into the appropriate total - debit, credit, or budget - on the various ledger summarizers
95       * @param originEntry the origin entry to add the total from
96       * @param totalLine a complete total to add the amount to
97       * @param balanceTypeTotal the total for the entries with the same balance type as the origin entry to add the amount to
98       * @param detailLine the proper detail amount to add the amoun tto
99       */
100     protected void addEntryToLedgerSummaries(OriginEntryInformation originEntry, LedgerSummaryTotalLine totalLine, LedgerBalanceTypeSummaryTotalLine balanceTypeTotal, LedgerSummaryDetailLine detailLine) {
101         if (originEntry.getTransactionDebitCreditCode() != null && originEntry.getTransactionDebitCreditCode().equals(OLEConstants.GL_DEBIT_CODE)) {
102             totalLine.addDebitAmount(originEntry.getTransactionLedgerEntryAmount());
103             balanceTypeTotal.addDebitAmount(originEntry.getTransactionLedgerEntryAmount());
104             detailLine.addDebitAmount(originEntry.getTransactionLedgerEntryAmount());
105         } else if (originEntry.getTransactionDebitCreditCode() != null && originEntry.getTransactionDebitCreditCode().equals(OLEConstants.GL_CREDIT_CODE)) {
106             totalLine.addCreditAmount(originEntry.getTransactionLedgerEntryAmount());
107             balanceTypeTotal.addCreditAmount(originEntry.getTransactionLedgerEntryAmount());
108             detailLine.addCreditAmount(originEntry.getTransactionLedgerEntryAmount());
109         } else{
110             totalLine.addBudgetAmount(originEntry.getTransactionLedgerEntryAmount());
111             balanceTypeTotal.addBudgetAmount(originEntry.getTransactionLedgerEntryAmount());
112             detailLine.addBudgetAmount(originEntry.getTransactionLedgerEntryAmount());
113         }
114     }
115     
116     /**
117      * Writes the report of totals to the given reportWriterService
118      * @param reportWriterService a report writer service to write the ledger summary report to
119      */
120     public void writeReport(ReportWriterService reportWriterService) {
121         if (details.size() > 0) {
122             List<LedgerSummaryDetailLine> detailList = new ArrayList<LedgerSummaryDetailLine>(details.values());
123             DynamicCollectionComparator.sort(detailList, LedgerSummaryDetailLine.keyFields);
124         
125             reportWriterService.writeTableHeader(detailList.get(0));
126             String currentBalanceType = detailList.get(0).getFinancialBalanceTypeCode();
127             currentBalanceType = StringUtils.isBlank(currentBalanceType) ? " " : currentBalanceType;
128             
129             for (LedgerSummaryDetailLine detailLine : detailList) {
130                 String detailLineBalanceType = (StringUtils.isBlank(detailLine.getFinancialBalanceTypeCode())) ? " " : detailLine.getFinancialBalanceTypeCode();
131                 if (!detailLineBalanceType.equals(currentBalanceType)) {
132                     LedgerBalanceTypeSummaryTotalLine subTitleLine = balanceTypeTotals.get(currentBalanceType);
133                     
134                     if (subTitleLine != null) {
135                         reportWriterService.writeTableRow(subTitleLine);
136                         reportWriterService.writeTableRowSeparationLine(subTitleLine);
137                     }
138                     currentBalanceType = detailLineBalanceType;
139                 }
140 
141                 reportWriterService.writeTableRow(detailLine);
142             }
143             final LedgerBalanceTypeSummaryTotalLine lastLine = balanceTypeTotals.get(detailList.get(detailList.size()-1).getFinancialBalanceTypeCode());
144             if (lastLine != null) {
145                 reportWriterService.writeTableRow(lastLine);
146             }
147             
148             reportWriterService.writeTableRowSeparationLine(ledgerTotalLine);
149             reportWriterService.writeTableRow(ledgerTotalLine);
150         }
151     }
152     
153 }