View Javadoc
1   /*
2    * Copyright 2006 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.businessobject;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  /**
24   * A collection of many LedgerEntry records, which appropriately groups the records
25   */
26  public class LedgerEntryHolder {
27      private Map ledgerEntries;
28      private Map subtotals;
29      private LedgerEntryForReporting grandTotal;
30  
31      private static final String GRAND_TOTAL = "Grand Total";
32      private static final String SUB_TOTAL = "Subtotal";
33  
34      /**
35       * Constructs a LedgerEntryHolder.java.
36       */
37      public LedgerEntryHolder() {
38          ledgerEntries = new HashMap();
39          subtotals = new HashMap();
40          grandTotal = new LedgerEntryForReporting(null, null, null, GRAND_TOTAL);
41      }
42  
43      /**
44       * add a given ledger entry into the holder. If there exists a ledger entry with the same key, then update the amount and count
45       * fields of the ledger entry; otherwise, insert it into the holder.
46       * 
47       * @param newLedgerEntry the given ledger entry
48       * @param calculateTotals indicate if the subtotals and grand total need to be calculated
49       */
50      public void insertLedgerEntry(LedgerEntryForReporting newLedgerEntry, boolean calculateTotal) {
51          Integer fiscalYear = newLedgerEntry.getFiscalYear();
52          String periodCode = newLedgerEntry.getPeriod();
53  
54          String balanceType = newLedgerEntry.getBalanceType();
55          String originCode = newLedgerEntry.getOriginCode();
56  
57          String keyOfLedgerEntry = balanceType + "-" + originCode + "-" + fiscalYear + "-" + periodCode;
58  
59          if (!ledgerEntries.containsKey(keyOfLedgerEntry)) {
60              ledgerEntries.put(keyOfLedgerEntry, newLedgerEntry);
61          }
62          else {
63              LedgerEntryForReporting ledgerEntry = (LedgerEntryForReporting) ledgerEntries.get(keyOfLedgerEntry);
64              ledgerEntry.add(newLedgerEntry);
65          }
66  
67          // calculate the subtotals and grand total
68          if (calculateTotal) {
69              updateSubtotal(newLedgerEntry);
70              updateGrandTotal(newLedgerEntry);
71          }
72      }
73  
74      /**
75       * update the subtotal using the given ledger entry
76       * 
77       * @param newLedgerEntry a new ledger entry to add to the holder
78       */
79      private void updateSubtotal(LedgerEntryForReporting newLedgerEntry) {
80          String groupingKey = newLedgerEntry.getBalanceType();
81  
82          if (StringUtils.isBlank(groupingKey)) {
83              return;
84          }
85  
86          LedgerEntryForReporting ledgerEntry = null;
87          if (!subtotals.containsKey(groupingKey)) {
88              ledgerEntry = new LedgerEntryForReporting(null, "", newLedgerEntry.getBalanceType(), SUB_TOTAL);
89              subtotals.put(groupingKey, ledgerEntry);
90          }
91          else {
92              ledgerEntry = (LedgerEntryForReporting) subtotals.get(groupingKey);
93          }
94          ledgerEntry.add(newLedgerEntry);
95      }
96  
97      /**
98       * update the grand total with the given ledger entry
99       * 
100      * @param newLedgerEntry entry to help update the grand total
101      */
102     private void updateGrandTotal(LedgerEntryForReporting newLedgerEntry) {
103         this.grandTotal.add(newLedgerEntry);
104     }
105 
106     /**
107      * Gets the grandTotal attribute.
108      * 
109      * @return Returns the grandTotal.
110      */
111     public LedgerEntryForReporting getGrandTotal() {
112         return grandTotal;
113     }
114 
115     /**
116      * Gets the ledgerEntries attribute.
117      * 
118      * @return Returns the ledgerEntries.
119      */
120     public Map getLedgerEntries() {
121         return ledgerEntries;
122     }
123 
124     /**
125      * Gets the subtotals attribute.
126      * 
127      * @return Returns the subtotals.
128      */
129     public Map getSubtotals() {
130         return subtotals;
131     }
132 }