001/*
002 * Copyright 2006 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.businessobject;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.commons.lang.StringUtils;
022
023/**
024 * A collection of many LedgerEntry records, which appropriately groups the records
025 */
026public class LedgerEntryHolder {
027    private Map ledgerEntries;
028    private Map subtotals;
029    private LedgerEntryForReporting 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 LedgerEntryForReporting(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(LedgerEntryForReporting newLedgerEntry, boolean calculateTotal) {
051        Integer fiscalYear = newLedgerEntry.getFiscalYear();
052        String periodCode = newLedgerEntry.getPeriod();
053
054        String balanceType = newLedgerEntry.getBalanceType();
055        String originCode = newLedgerEntry.getOriginCode();
056
057        String keyOfLedgerEntry = balanceType + "-" + originCode + "-" + fiscalYear + "-" + periodCode;
058
059        if (!ledgerEntries.containsKey(keyOfLedgerEntry)) {
060            ledgerEntries.put(keyOfLedgerEntry, newLedgerEntry);
061        }
062        else {
063            LedgerEntryForReporting ledgerEntry = (LedgerEntryForReporting) ledgerEntries.get(keyOfLedgerEntry);
064            ledgerEntry.add(newLedgerEntry);
065        }
066
067        // calculate the subtotals and grand total
068        if (calculateTotal) {
069            updateSubtotal(newLedgerEntry);
070            updateGrandTotal(newLedgerEntry);
071        }
072    }
073
074    /**
075     * update the subtotal using the given ledger entry
076     * 
077     * @param newLedgerEntry a new ledger entry to add to the holder
078     */
079    private void updateSubtotal(LedgerEntryForReporting newLedgerEntry) {
080        String groupingKey = newLedgerEntry.getBalanceType();
081
082        if (StringUtils.isBlank(groupingKey)) {
083            return;
084        }
085
086        LedgerEntryForReporting ledgerEntry = null;
087        if (!subtotals.containsKey(groupingKey)) {
088            ledgerEntry = new LedgerEntryForReporting(null, "", newLedgerEntry.getBalanceType(), SUB_TOTAL);
089            subtotals.put(groupingKey, ledgerEntry);
090        }
091        else {
092            ledgerEntry = (LedgerEntryForReporting) subtotals.get(groupingKey);
093        }
094        ledgerEntry.add(newLedgerEntry);
095    }
096
097    /**
098     * update the grand total with the given ledger entry
099     * 
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}