001/*
002 * Copyright 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.businessobject;
017
018import java.util.Comparator;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.ole.sys.OLEPropertyConstants;
022
023/**
024 * Holds summary data for the ledger summary report
025 */
026public class LedgerSummaryDetailLine extends LedgerBalanceTypeSummaryTotalLine {
027    private String financialSystemOriginationCode;
028    private Integer universityFiscalYear;
029    private String universityAccountPeriodCode;
030
031    /**
032     * Constructs a LedgerSummaryDetailLine
033     * 
034     * @param balanceTypeCode
035     * @param financialSystemOriginationCode
036     * @param universityFiscalYear
037     * @param universityAccountPeriodCode
038     */
039    public LedgerSummaryDetailLine(String balanceTypeCode, String financialSystemOriginationCode, Integer universityFiscalYear, String universityAccountPeriodCode) {
040        super(balanceTypeCode);
041        this.financialSystemOriginationCode = financialSystemOriginationCode;
042        this.universityFiscalYear = universityFiscalYear;
043        this.universityAccountPeriodCode = universityAccountPeriodCode;
044    }
045
046    /**
047     * Gets the financialSystemOriginationCode attribute.
048     * 
049     * @return Returns the financialSystemOriginationCode.
050     */
051    public String getFinancialSystemOriginationCode() {
052        return financialSystemOriginationCode;
053    }
054
055    /**
056     * Gets the universityFiscalYear attribute.
057     * 
058     * @return Returns the universityFiscalYear.
059     */
060    public Integer getUniversityFiscalYear() {
061        return universityFiscalYear;
062    }
063
064    /**
065     * Gets the universityAccountPeriodCode attribute.
066     * 
067     * @return Returns the universityAccountPeriodCode.
068     */
069    public String getUniversityAccountPeriodCode() {
070        return universityAccountPeriodCode;
071    }
072
073    /**
074     * @return gets a "key" for this summary line - just a convenient key for Maps which might hold these
075     */
076    public String getKey() {
077        return LedgerSummaryDetailLine.makeKey(this.getFinancialBalanceTypeCode(), this.getFinancialSystemOriginationCode(), this.getUniversityFiscalYear(), this.getUniversityAccountPeriodCode());
078    }
079
080    /**
081     * Generates a Map key in a consistent format with the rest of the uses of this class for a given OriginEntryInformation
082     * 
083     * @param entry the entry to build a key for
084     * @return the "key" for a summary line which would include totals from entries like the given origin entry
085     */
086    public static String getKeyString(OriginEntryInformation entry) {
087        return LedgerSummaryDetailLine.makeKey(entry.getFinancialBalanceTypeCode(), entry.getFinancialSystemOriginationCode(), entry.getUniversityFiscalYear(), entry.getUniversityFiscalPeriodCode());
088    }
089
090    /**
091     * Given the various values, puts together a convenient Map key
092     * 
093     * @param balanceTypeCode a balance type code
094     * @param financialSystemOriginationCode an origination code
095     * @param universityFiscalYear a fiscal year, smothered in mustard
096     * @param universityAccountingPeriodCode an accounting period code
097     * @return all of them magically put together, to form a Map key. Like Voltron, but more financially oriented
098     */
099    private static String makeKey(String balanceTypeCode, String financialSystemOriginationCode, Integer universityFiscalYear, String universityAccountingPeriodCode) {
100        return StringUtils.join(new String[] { balanceTypeCode, financialSystemOriginationCode, universityFiscalYear == null ? "" : universityFiscalYear.toString(), universityAccountingPeriodCode }, ':');
101    }
102
103    /**
104     * @return a standard comparator for comparing NightlyOutPendingEntryLedgerSummaryDetailLine objects
105     */
106    public static Comparator<LedgerSummaryDetailLine> getStandardComparator() {
107        return new Comparator<LedgerSummaryDetailLine>() {
108
109            /**
110             * Compares two NightlyOutPendingEntryLedgerSummaryDetailLine objects
111             * 
112             * @param detail1 the first NightlyOutPendingEntryLedgerSummaryDetailLine object
113             * @param detail2 the second NightlyOutPendingEntryLedgerSummaryDetailLine other
114             * @return the standard 0 for equals, greater than 0 for greater than, less than 0 for less than
115             */
116            public int compare(LedgerSummaryDetailLine detail1, LedgerSummaryDetailLine detail2) {
117                int comp = 0;
118                comp = nullSafeCompare(detail1.getFinancialBalanceTypeCode(), detail2.getFinancialBalanceTypeCode());
119
120                if (comp == 0) {
121                    comp = nullSafeCompare(detail1.getFinancialSystemOriginationCode(), detail2.getFinancialSystemOriginationCode());
122                }
123
124                if (comp == 0) {
125                    comp = nullSafeCompare(detail1.getUniversityFiscalYear(), detail2.getUniversityFiscalYear());
126                }
127
128                if (comp == 0) {
129                    comp = nullSafeCompare(detail1.getUniversityAccountPeriodCode(), detail2.getUniversityAccountPeriodCode());
130                }
131
132                return comp;
133            }
134
135            /**
136             * Checks for nulls in the two comparables before calling the compare. If one is null and not the other, the null is
137             * considered less. If both are null they are considered equal.
138             * 
139             * @param o1 object to compare
140             * @param o2 object to compare o1 to
141             * @return -1 for less, 0 for equal, 1 for greater
142             */
143            protected int nullSafeCompare(Comparable o1, Comparable o2) {
144                if (o1 == null && o2 != null) {
145                    return -1;
146                }
147
148                if (o1 != null && o2 == null) {
149                    return 1;
150                }
151
152                if (o1 == null && o2 == null) {
153                    return 0;
154                }
155
156                return o1.compareTo(o2);
157            }
158        };
159    }
160
161    public static String[] keyFields = new String[] { OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, OLEPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, "universityAccountPeriodCode" };
162
163}