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