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.module.ld.businessobject.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.kfs.gl.Constant;
024    import org.kuali.kfs.gl.OJBUtility;
025    import org.kuali.kfs.module.ld.LaborConstants;
026    import org.kuali.kfs.module.ld.LaborConstants.BenefitExpenseTransfer;
027    import org.kuali.kfs.module.ld.businessobject.LedgerBalance;
028    import org.kuali.kfs.module.ld.util.ConsolidationUtil;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.KFSPropertyConstants;
031    import org.kuali.kfs.sys.businessobject.SystemOptions;
032    import org.kuali.kfs.sys.service.OptionsService;
033    
034    /**
035     * The class is the front-end for the balance inquiry of Ledger Balance For Benefit Expense Transfer processing.
036     */
037    public class LedgerBalanceForBenefitExpenseTransferLookupableHelperServiceImpl extends LedgerBalanceForExpenseTransferLookupableHelperServiceImpl {
038        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LedgerBalanceForBenefitExpenseTransferLookupableHelperServiceImpl.class);
039    
040        private OptionsService optionsService;
041    
042        /**
043         * @see org.kuali.rice.kns.lookup.Lookupable#getSearchResults(java.util.Map)
044         */
045        @Override
046        public List getSearchResults(Map fieldValues) {
047            LOG.info("Start getSearchResults()");
048    
049            setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
050            setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
051    
052            String fiscalYearString = (String) fieldValues.get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
053            SystemOptions options = this.getOptions(fiscalYearString);
054    
055            fieldValues.put(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, options.getFinObjTypeExpenditureexpCd());
056            fieldValues.put(KFSPropertyConstants.LABOR_OBJECT + "." + KFSPropertyConstants.FINANCIAL_OBJECT_FRINGE_OR_SALARY_CODE, BenefitExpenseTransfer.LABOR_LEDGER_BENEFIT_CODE);
057    
058            // get the ledger balances with actual balance type code
059            fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, options.getActualFinancialBalanceTypeCd());
060            Collection actualBalances = buildDetailedBalanceCollection(getBalanceService().findBalance(fieldValues, false), Constant.NO_PENDING_ENTRY);
061    
062            // get the ledger balances with effort balance type code
063            fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_A21);
064            Collection effortBalances = buildDetailedBalanceCollection(getBalanceService().findBalance(fieldValues, false), Constant.NO_PENDING_ENTRY);
065            
066            List<String> consolidationKeyList = getConsolidationKeyList();       
067            Collection<LedgerBalance> consolidatedBalances = ConsolidationUtil.consolidateA2Balances(actualBalances, effortBalances, options.getActualFinancialBalanceTypeCd(), consolidationKeyList);
068            this.resetFieldValues(consolidatedBalances);
069    
070            Integer recordCount = getBalanceService().getBalanceRecordCount(fieldValues, true);
071            Long actualSize = OJBUtility.getResultActualSize(consolidatedBalances, recordCount, fieldValues, new LedgerBalance());
072    
073            return buildSearchResultList(consolidatedBalances, actualSize);
074        }
075    
076        // reset the values for the specified fields
077        private void resetFieldValues(Collection<LedgerBalance> consolidatedBalances) {
078            for(LedgerBalance ledgerBalance : consolidatedBalances) {
079                ledgerBalance.setEmplid(null);
080                ledgerBalance.setPositionNumber(null);
081            }
082        }
083    
084        // get the consolidation key field names
085        private List<String> getConsolidationKeyList() {
086            List<String> consolidationKeyList = new ArrayList<String>();
087            consolidationKeyList.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
088            consolidationKeyList.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
089            consolidationKeyList.add(KFSPropertyConstants.ACCOUNT_NUMBER);
090            consolidationKeyList.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
091            consolidationKeyList.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
092            consolidationKeyList.add(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
093            consolidationKeyList.add(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
094            consolidationKeyList.add(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
095            return consolidationKeyList;
096        }
097    
098        /**
099         * get the Options object for the given fiscal year
100         * 
101         * @param fiscalYearString the given fiscal year
102         * @return the Options object for the given fiscal year
103         */
104        private SystemOptions getOptions(String fiscalYearString) {
105            SystemOptions options;
106            if (fiscalYearString == null) {
107                options = optionsService.getCurrentYearOptions();
108            }
109            else {
110                Integer fiscalYear = Integer.valueOf(fiscalYearString.trim());
111                options = optionsService.getOptions(fiscalYear);
112            }
113            return options;
114        }
115    
116        /**
117         * Sets the optionsService attribute value.
118         * 
119         * @param optionsService The optionsService to set.
120         */
121        public void setOptionsService(OptionsService optionsService) {
122            this.optionsService = optionsService;
123        }
124    }