001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.module.gl.util;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.apache.commons.lang.StringUtils;
022:
023: /**
024: * A collection of many LedgerEntry records, which appropriately groups the records
025: */
026: public class LedgerEntryHolder {
027: private Map ledgerEntries;
028: private Map subtotals;
029: private LedgerEntry grandTotal;
030:
031: private static final String GRAND_TOTAL = "Grand Total";
032: private static final String SUB_TOTAL = "Subtotal";
033:
034: /**
035: * Constructs a LedgerEntryHolder.java.
036: */
037: public LedgerEntryHolder() {
038: ledgerEntries = new HashMap();
039: subtotals = new HashMap();
040: grandTotal = new LedgerEntry(null, null, null, GRAND_TOTAL);
041: }
042:
043: /**
044: * add a given ledger entry into the holder. If there exists a ledger entry with the same key, then update the amount and count
045: * fields of the ledger entry; otherwise, insert it into the holder.
046: *
047: * @param newLedgerEntry the given ledger entry
048: * @param calculateTotals indicate if the subtotals and grand total need to be calculated
049: */
050: public void insertLedgerEntry(LedgerEntry newLedgerEntry,
051: boolean calculateTotal) {
052: Integer fiscalYear = newLedgerEntry.getFiscalYear();
053: String periodCode = newLedgerEntry.getPeriod();
054:
055: String balanceType = newLedgerEntry.getBalanceType();
056: String originCode = newLedgerEntry.getOriginCode();
057:
058: String keyOfLedgerEntry = balanceType + "-" + originCode + "-"
059: + fiscalYear + "-" + periodCode;
060:
061: if (!ledgerEntries.containsKey(keyOfLedgerEntry)) {
062: ledgerEntries.put(keyOfLedgerEntry, newLedgerEntry);
063: } else {
064: LedgerEntry ledgerEntry = (LedgerEntry) ledgerEntries
065: .get(keyOfLedgerEntry);
066: ledgerEntry.add(newLedgerEntry);
067: }
068:
069: // calculate the subtotals and grand total
070: if (calculateTotal) {
071: updateSubtotal(newLedgerEntry);
072: updateGrandTotal(newLedgerEntry);
073: }
074: }
075:
076: /**
077: * update the subtotal using the given ledger entry
078: *
079: * @param newLedgerEntry a new ledger entry to add to the holder
080: */
081: private void updateSubtotal(LedgerEntry newLedgerEntry) {
082: String groupingKey = newLedgerEntry.getBalanceType();
083:
084: if (StringUtils.isBlank(groupingKey)) {
085: return;
086: }
087:
088: LedgerEntry ledgerEntry = null;
089: if (!subtotals.containsKey(groupingKey)) {
090: ledgerEntry = new LedgerEntry(null, "", newLedgerEntry
091: .getBalanceType(), SUB_TOTAL);
092: subtotals.put(groupingKey, ledgerEntry);
093: } else {
094: ledgerEntry = (LedgerEntry) subtotals.get(groupingKey);
095: }
096: ledgerEntry.add(newLedgerEntry);
097: }
098:
099: /**
100: * update the grand total with the given ledger entry
101: *
102: * @param newLedgerEntry entry to help update the grand total
103: */
104: private void updateGrandTotal(LedgerEntry newLedgerEntry) {
105: this .grandTotal.add(newLedgerEntry);
106: }
107:
108: /**
109: * Gets the grandTotal attribute.
110: *
111: * @return Returns the grandTotal.
112: */
113: public LedgerEntry getGrandTotal() {
114: return grandTotal;
115: }
116:
117: /**
118: * Gets the ledgerEntries attribute.
119: *
120: * @return Returns the ledgerEntries.
121: */
122: public Map getLedgerEntries() {
123: return ledgerEntries;
124: }
125:
126: /**
127: * Gets the subtotals attribute.
128: *
129: * @return Returns the subtotals.
130: */
131: public Map getSubtotals() {
132: return subtotals;
133: }
134: }
|