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.SalaryExpenseTransfer;
026    import org.kuali.kfs.module.ld.businessobject.LedgerBalance;
027    import org.kuali.kfs.module.ld.util.ConsolidationUtil;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.KFSKeyConstants;
030    import org.kuali.kfs.sys.KFSPropertyConstants;
031    import org.kuali.kfs.sys.businessobject.SystemOptions;
032    import org.kuali.kfs.sys.context.SpringContext;
033    import org.kuali.kfs.sys.service.OptionsService;
034    import org.kuali.rice.kns.lookup.CollectionIncomplete;
035    import org.kuali.rice.kns.service.DataDictionaryService;
036    import org.kuali.rice.kns.util.GlobalVariables;
037    import org.kuali.rice.kns.util.ObjectUtils;
038    
039    /**
040     * Service implementation of LedgerBalanceForSalaryExpenseTransferLookupableHelperService. The class is the front-end for the
041     * balance inquiry of Ledger Balance For Salary Expense Transfer processing.
042     */
043    public class LedgerBalanceForSalaryExpenseTransferLookupableHelperServiceImpl extends LedgerBalanceForExpenseTransferLookupableHelperServiceImpl {
044        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LedgerBalanceForSalaryExpenseTransferLookupableHelperServiceImpl.class);
045    
046        private OptionsService optionsService;
047    
048        /**
049         * @see org.kuali.kfs.module.ld.businessobject.lookup.LedgerBalanceForExpenseTransferLookupableHelperServiceImpl#getSearchResults(java.util.Map)
050         */
051        @Override
052        public List getSearchResults(Map fieldValues) {
053            LOG.info("Start getSearchResults()");
054    
055            setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
056            setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
057    
058            String fiscalYearString = (String) fieldValues.get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
059            SystemOptions options = this.getOptions(fiscalYearString);
060            
061            if (ObjectUtils.isNull(options)) {
062                return new CollectionIncomplete(new ArrayList(), new Long(0));
063            }
064    
065            fieldValues.put(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, options.getFinObjTypeExpenditureexpCd());
066            fieldValues.put(KFSPropertyConstants.LABOR_OBJECT + "." + KFSPropertyConstants.FINANCIAL_OBJECT_FRINGE_OR_SALARY_CODE, SalaryExpenseTransfer.LABOR_LEDGER_SALARY_CODE);
067    
068            // get the ledger balances with actual balance type code
069            fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, options.getActualFinancialBalanceTypeCd());
070            Collection actualBalances = buildDetailedBalanceCollection(getBalanceService().findBalance(fieldValues, false), Constant.NO_PENDING_ENTRY);
071    
072            // get the ledger balances with effort balance type code
073            fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_A21);
074            Collection effortBalances = buildDetailedBalanceCollection(getBalanceService().findBalance(fieldValues, false), Constant.NO_PENDING_ENTRY);
075    
076            List<String> consolidationKeyList = LedgerBalance.getPrimaryKeyList();
077            Collection<LedgerBalance> consolidatedBalances = ConsolidationUtil.consolidateA2Balances(actualBalances, effortBalances, options.getActualFinancialBalanceTypeCd(), consolidationKeyList);
078    
079            Integer recordCount = getBalanceService().getBalanceRecordCount(fieldValues, true);
080            Long actualSize = OJBUtility.getResultActualSize(consolidatedBalances, recordCount, fieldValues, new LedgerBalance());
081    
082            return buildSearchResultList(consolidatedBalances, actualSize);
083        }
084        
085        /**
086         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
087         */
088        @Override
089        public void validateSearchParameters(Map fieldValues) {
090            String fiscalYearString = (String) fieldValues.get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
091            SystemOptions options = this.getOptions(fiscalYearString);
092            
093            if(ObjectUtils.isNull(options)) {
094                DataDictionaryService dictionaryService = SpringContext.getBean(DataDictionaryService.class);
095                String label = dictionaryService.getAttributeLabel(LedgerBalance.class, KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
096                
097                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, KFSKeyConstants.ERROR_EXISTENCE, label);
098            }
099        }
100    
101        /**
102         * get the Options object for the given fiscal year
103         * 
104         * @param fiscalYearString the given fiscal year
105         * @return the Options object for the given fiscal year
106         */
107        private SystemOptions getOptions(String fiscalYearString) {
108            SystemOptions options;
109            if (fiscalYearString == null) {
110                options = optionsService.getCurrentYearOptions();
111            }
112            else {
113                Integer fiscalYear = Integer.valueOf(fiscalYearString.trim());
114                options = optionsService.getOptions(fiscalYear);
115            }
116            return options;
117        }
118    
119        /**
120         * Sets the optionsService attribute value.
121         * 
122         * @param optionsService The optionsService to set.
123         */
124        public void setOptionsService(OptionsService optionsService) {
125            this.optionsService = optionsService;
126        }
127    }