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.service.impl;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.collections.IteratorUtils;
027import org.kuali.ole.coa.businessobject.Account;
028import org.kuali.ole.coa.businessobject.OrganizationReversion;
029import org.kuali.ole.coa.service.BalanceTypeService;
030import org.kuali.ole.coa.service.ObjectTypeService;
031import org.kuali.ole.coa.service.SubFundGroupService;
032import org.kuali.ole.gl.GeneralLedgerConstants;
033import org.kuali.ole.gl.OJBUtility;
034import org.kuali.ole.gl.batch.BalanceForwardStep;
035import org.kuali.ole.gl.batch.service.FilteringBalanceIterator;
036import org.kuali.ole.gl.businessobject.Balance;
037import org.kuali.ole.gl.businessobject.GlSummary;
038import org.kuali.ole.gl.dataaccess.BalanceDao;
039import org.kuali.ole.gl.service.BalanceService;
040import org.kuali.ole.sys.OLEConstants;
041import org.kuali.ole.sys.OLEPropertyConstants;
042import org.kuali.ole.sys.businessobject.SystemOptions;
043import org.kuali.ole.sys.context.SpringContext;
044import org.kuali.ole.sys.service.OptionsService;
045import org.kuali.ole.sys.service.UniversityDateService;
046import org.kuali.rice.core.api.parameter.ParameterEvaluator;
047import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
048import org.kuali.rice.core.api.util.type.KualiDecimal;
049import org.kuali.rice.coreservice.framework.parameter.ParameterService;
050import org.springframework.transaction.annotation.Transactional;
051
052/**
053 * This class is the OJB implementation of the Balance Service
054 */
055@Transactional
056public class BalanceServiceImpl implements BalanceService {
057    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BalanceServiceImpl.class);
058
059    protected static final String PARAMETER_PREFIX = "SELECTION_";
060
061    protected BalanceDao balanceDao;
062    protected OptionsService optionsService;
063    protected ObjectTypeService objectTypeService;
064    protected SubFundGroupService subFundGroupService;
065    protected ParameterService parameterService;
066    protected BalanceTypeService balanceTypService;
067    // must have no asset, liability or fund balance balances other than object code 9899
068
069    Collection<String> assetLiabilityFundBalanceObjectTypeCodes = null;
070    Collection<String> encumbranceBaseBudgetBalanceTypeCodes = null;
071    Collection<String> actualBalanceCodes = null;
072    Collection<String> incomeObjectTypeCodes = null;
073    Collection<String> expenseObjectTypeCodes = null;
074
075    /**
076     * @param universityFiscalYear the fiscal year to find balances for
077     * @param balanceTypeCodes the balance types to summarize
078     * @return a list of summarized GL balances
079     * @see org.kuali.ole.gl.service.BalanceService#getGlSummary(int, java.util.List)
080     */
081    public List<GlSummary> getGlSummary(int universityFiscalYear, List<String> balanceTypeCodes) {
082        LOG.debug("getGlSummary() started");
083
084        List<GlSummary> sum = new ArrayList<GlSummary>();
085
086        Iterator<Object[]> i = balanceDao.getGlSummary(universityFiscalYear, balanceTypeCodes);
087        while (i.hasNext()) {
088            Object[] data = i.next();
089            sum.add(new GlSummary(data));
090        }
091        return sum;
092    }
093
094    /**
095     * Defers to the DAO to find all balances in the fiscal year.
096     * 
097     * @param fiscalYear the fiscal year to find balances for
098     * @return an Iterator full of balances from the given fiscal year
099     * @see org.kuali.ole.gl.service.BalanceService#findBalancesForFiscalYear(java.lang.Integer)
100     */
101    public Iterator<Balance> findBalancesForFiscalYear(Integer fiscalYear) {
102        return (Iterator<Balance>) balanceDao.findBalancesForFiscalYear(fiscalYear);
103    }
104
105    /**
106     * Checks the given account to see if there are any non zero asset fund liability fund balances for them
107     * 
108     * @param account an account to find balances for
109     * @return true if there are non zero asset liability fund balances, false if otherwise
110     * @see org.kuali.ole.gl.service.BalanceService#hasAssetLiabilityFundBalanceBalances(org.kuali.ole.coa.businessobject.Account)
111     */
112    public boolean hasAssetLiabilityFundBalanceBalances(Account account) {
113
114        /*
115         * Here is an excerpt from the original Oracle trigger: SELECT fin_object_cd FROM gl_balance_t WHERE univ_fiscal_yr =
116         * p_univ_fiscal_yr AND fin_coa_cd = p_fin_coa_cd AND account_nbr = p_account_nbr AND fin_object_cd != '9899' AND
117         * fin_obj_typ_cd IN ('AS', 'LI', 'FB') AND fin_balance_typ_cd = 'AC' GROUP BY fin_object_cd HAVING
118         * ABS(SUM(fin_beg_bal_ln_amt + acln_annl_bal_amt)) > 0); added absolute value function to sum--prevents the case of 2
119         * entries (1 pos and 1 neg) from canceling each other out and allowing the acct to be closed when it shouldn't be.
120         */
121
122        // FIXME! - date service should be injected
123        Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
124        ArrayList fundBalanceObjectCodes = new ArrayList();
125        fundBalanceObjectCodes.add(null == account.getChartOfAccounts() ? null : account.getChartOfAccounts().getFundBalanceObjectCode());
126        Iterator balances = balanceDao.findBalances(account, fiscalYear, null, fundBalanceObjectCodes, getAssetLiabilityFundBalanceBalanceTypeCodes(), getActualBalanceCodes());
127
128        KualiDecimal begin;
129        KualiDecimal annual;
130
131        // TODO KULCOA-335 - is absolute value necessary to prevent obscure sets of values
132        // from masking accounts that should remain open?
133
134        Map groups = new HashMap();
135
136        while (balances.hasNext()) {
137            Balance balance = (Balance) balances.next();
138            begin = balance.getBeginningBalanceLineAmount();
139            annual = balance.getAccountLineAnnualBalanceAmount();
140
141            String objectCode = balance.getObjectCode();
142
143            KualiDecimal runningTotal = (KualiDecimal) groups.get(objectCode);
144
145            if (runningTotal == null) {
146                runningTotal = KualiDecimal.ZERO;
147            }
148
149            runningTotal = runningTotal.add(begin);
150            runningTotal = runningTotal.add(annual);
151
152            groups.put(objectCode, runningTotal);
153
154
155        }
156
157        boolean success = false;
158
159        Iterator iter = groups.keySet().iterator();
160        while (iter.hasNext()) {
161            success |= ((KualiDecimal) groups.get(iter.next())).isNonZero();
162        }
163
164        return success;
165
166    }
167
168    /**
169     * Given an iterator of balances, this returns the sum of each balance's beginning balance line amount + annual account linge
170     * balance amount
171     * 
172     * @param balances an Iterator of balances to sum
173     * @return the sum of all of those balances
174     */
175    protected KualiDecimal sumBalances(Iterator balances) {
176        KualiDecimal runningTotal = KualiDecimal.ZERO;
177
178        KualiDecimal begin;
179        KualiDecimal annual;
180
181        while (balances.hasNext()) {
182            Balance balance = (Balance) balances.next();
183            begin = balance.getBeginningBalanceLineAmount();
184            annual = balance.getAccountLineAnnualBalanceAmount();
185
186            runningTotal = runningTotal.add(begin);
187            runningTotal = runningTotal.add(annual);
188        }
189
190        return runningTotal;
191
192    }
193
194    /**
195     * Returns the sum of balances considered as income for the given account
196     * 
197     * @param account the account to find income balances for
198     * @return the sum of income balances
199     */
200    protected KualiDecimal incomeBalances(Account account) {
201
202        /*
203         * SELECT SUM(fin_beg_bal_ln_amt + acln_annl_bal_amt) INTO v_y FROM gl_balance_t WHERE univ_fiscal_yr = p_univ_fiscal_yr AND
204         * fin_coa_cd = p_fin_coa_cd AND account_nbr = p_account_nbr AND (fin_object_cd = '9899' OR fin_obj_typ_cd IN ('CH', 'IC',
205         * 'IN', 'TI')) AND fin_balance_typ_cd = 'AC';
206         * @return
207         */
208
209        // FIXME! - date service should be injected
210        Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
211
212        ArrayList fundBalanceObjectCodes = new ArrayList();
213        fundBalanceObjectCodes.add(account.getChartOfAccounts().getFundBalanceObjectCode());
214        Iterator balances = balanceDao.findBalances(account, fiscalYear, fundBalanceObjectCodes, null, getIncomeObjectTypeCodes(), getActualBalanceCodes());
215
216        return sumBalances(balances);
217
218    }
219
220    /**
221     * Sums all the balances associated with a given account that would be considered "expense" balances
222     * 
223     * @param account an account to find expense balances for
224     * @return the sum of those balances
225     */
226    protected KualiDecimal expenseBalances(Account account) {
227        /*
228         * Here is an excerpt from the original Oracle Trigger: SELECT SUM(fin_beg_bal_ln_amt || acln_annl_bal_amt) INTO v_x FROM
229         * gl_balance_t WHERE univ_fiscal_yr = p_univ_fiscal_yr AND fin_coa_cd = p_fin_coa_cd AND account_nbr = p_account_nbr AND
230         * fin_obj_typ_cd IN ('EE', 'ES', 'EX', 'TE') AND fin_balance_typ_cd = 'AC'; This method...
231         */
232
233        // FIXME! - date service should be injected
234        Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
235        Iterator balances = balanceDao.findBalances(account, fiscalYear, null, null, getExpenseObjectTypeCodes(), getActualBalanceCodes());
236
237        return sumBalances(balances);
238
239    }
240
241    /**
242     * Checks to see if the total income balances for the given account equal the total expense balances for the given account
243     * 
244     * @param an account to find balances for
245     * @return true if income balances equal expense balances, false otherwise
246     * @see org.kuali.ole.gl.service.BalanceService#fundBalanceWillNetToZero(org.kuali.ole.coa.businessobject.Account)
247     */
248    public boolean fundBalanceWillNetToZero(Account account) {
249        KualiDecimal income = incomeBalances(account);
250        KualiDecimal expense = expenseBalances(account);
251
252        return income.equals(expense);
253    }
254
255    /**
256     * Finds all of the encumbrance balances for the given account, and figures out if those encumbrances will have a net impact on
257     * the budget
258     * 
259     * @param account an account to find balances for
260     * @return true if summed encumbrances for the account are not zero (meaning encumbrances will have a net impact on the budget),
261     *         false if otherwise
262     * @see org.kuali.ole.gl.service.BalanceService#hasEncumbrancesOrBaseBudgets(org.kuali.ole.coa.businessobject.Account)
263     */
264    public boolean hasEncumbrancesOrBaseBudgets(Account account) {
265
266        /*
267         * check for Encumbrances and base budgets Here is an excerpt from the original Oracle Trigger: SELECT
268         * SUM(fin_beg_bal_ln_amt + acln_annl_bal_amt) INTO v_y FROM gl_balance_t WHERE univ_fiscal_yr = p_univ_fiscal_yr AND
269         * fin_coa_cd = p_fin_coa_cd AND account_nbr = p_account_nbr AND fin_balance_typ_cd IN ('EX', 'IE', 'PE', 'BB'); v_rowcnt :=
270         * SQL%ROWCOUNT;
271         */
272
273        // FIXME! - date service should be injected
274        Integer fiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
275        Iterator balances = balanceDao.findBalances(account, fiscalYear, null, null, null, getEncumbranceBaseBudgetBalanceTypeCodes());
276
277        return sumBalances(balances).isNonZero();
278    }
279
280    /**
281     * Returns whether or not the beginning budget is loaded for the given account. Of course, it doesn't really check the
282     * account...just the options for the current year to see if all the beginning balances have been loaded
283     * 
284     * @param an account to check whether the beginning balance is loaded for
285     * @return true if the beginning balance is loaded, false otherwise
286     * @see org.kuali.ole.gl.service.BalanceService#beginningBalanceLoaded(org.kuali.ole.coa.businessobject.Account)
287     */
288    public boolean beginningBalanceLoaded(Account account) {
289        return optionsService.getCurrentYearOptions().isFinancialBeginBalanceLoadInd();
290    }
291
292    /**
293     * Determines if the account has asset/liability balances associated with it that will have a net impact
294     * 
295     * @param account an account to check balances for
296     * @return true if the account has an asset liability balance, false otherwise
297     * @see org.kuali.ole.gl.service.BalanceService#hasAssetLiabilityOrFundBalance(org.kuali.ole.coa.businessobject.Account)
298     */
299    public boolean hasAssetLiabilityOrFundBalance(Account account) {
300        return hasAssetLiabilityFundBalanceBalances(account) || !fundBalanceWillNetToZero(account) || hasEncumbrancesOrBaseBudgets(account);
301    }
302
303    public void setBalanceDao(BalanceDao balanceDao) {
304        this.balanceDao = balanceDao;
305    }
306
307    public void setOptionsService(OptionsService optionsService) {
308        this.optionsService = optionsService;
309    }
310
311    /**
312     * This method finds the summary records of balance entries according to input fields an values, using the DAO. The results will
313     * be limited to the system lookup results limit.
314     * 
315     * @param fieldValues the input fields an values
316     * @param isConsolidated consolidation option is applied or not
317     * @return the summary records of balance entries
318     * @see org.kuali.ole.gl.service.BalanceService#lookupCashBalance(java.util.Map, boolean)
319     */
320    public Iterator lookupCashBalance(Map fieldValues, boolean isConsolidated) {
321        LOG.debug("findCashBalance() started");
322
323        return balanceDao.lookupCashBalance(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues));
324    }
325
326    /**
327     * This method gets the size of cash balance entries according to input fields and values
328     * 
329     * @param fieldValues the input fields and values
330     * @param isConsolidated consolidation option is applied or not
331     * @return the count of cash balance entries
332     * @see org.kuali.ole.gl.service.BalanceService#getCashBalanceRecordCount(java.util.Map, boolean)
333     */
334    public Integer getCashBalanceRecordCount(Map fieldValues, boolean isConsolidated) {
335        LOG.debug("getCashBalanceRecordCount() started");
336
337        Integer recordCount = new Integer(0);
338        if (!isConsolidated) {
339            recordCount = balanceDao.getDetailedCashBalanceRecordCount(fieldValues, getEncumbranceBalanceTypes(fieldValues));
340        }
341        else {
342            recordCount = balanceDao.getConsolidatedCashBalanceRecordCount(fieldValues, getEncumbranceBalanceTypes(fieldValues));
343        }
344        return recordCount;
345    }
346
347    /**
348     * This method gets the size of balance entries according to input fields and values
349     * 
350     * @param fieldValues the input fields and values
351     * @param isConsolidated consolidation option is applied or not
352     * @return the size of balance entries
353     * @see org.kuali.ole.gl.service.BalanceService#findBalance(java.util.Map, boolean)
354     */
355    public Iterator findBalance(Map fieldValues, boolean isConsolidated) {
356        LOG.debug("findBalance() started");
357        return balanceDao.findBalance(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues));
358    }
359
360    /**
361     * This method finds the summary records of balance entries according to input fields and values
362     * 
363     * @param fieldValues the input fields and values
364     * @param isConsolidated consolidation option is applied or not
365     * @return the summary records of balance entries
366     * @see org.kuali.ole.gl.service.BalanceService#getBalanceRecordCount(java.util.Map, boolean)
367     */
368    public Integer getBalanceRecordCount(Map fieldValues, boolean isConsolidated) {
369        LOG.debug("getBalanceRecordCount() started");
370
371        Integer recordCount = null;
372        if (!isConsolidated) {
373            recordCount = OJBUtility.getResultSizeFromMap(fieldValues, new Balance()).intValue();
374        }
375        else {
376            Iterator recordCountIterator = balanceDao.getConsolidatedBalanceRecordCount(fieldValues, getEncumbranceBalanceTypes(fieldValues));
377            // TODO: WL: why build a list and waste time/memory when we can just iterate through the iterator and do a count?
378            List recordCountList = IteratorUtils.toList(recordCountIterator);
379            recordCount = recordCountList.size();
380        }
381        return recordCount;
382    }
383
384    /**
385     * Purge the balance table by year/chart
386     * 
387     * @param chart the chart of balances to purge
388     * @param year the year of balances to purge
389     */
390    public void purgeYearByChart(String chart, int year) {
391        LOG.debug("purgeYearByChart() started");
392
393        balanceDao.purgeYearByChart(chart, year);
394    }
395
396    /**
397     * Private method to load the values from the system options service and store them locally for later use.
398     */
399    protected void loadConstantsFromOptions() {
400        LOG.debug("loadConstantsFromOptions() started");
401        SystemOptions options = optionsService.getCurrentYearOptions();
402        // String[] actualBalanceCodes = new String[] { "AC" };
403        actualBalanceCodes = Arrays.asList( options.getActualFinancialBalanceTypeCd() ); // AC
404        // String[] incomeObjectTypeCodes = new String[] { "CH", "IC", "IN", "TI" };
405        incomeObjectTypeCodes = Arrays.asList( options.getFinObjTypeIncomeNotCashCd(), // IC
406                options.getFinObjectTypeIncomecashCode(), // IN
407                options.getFinObjTypeCshNotIncomeCd(), // CH
408                options.getFinancialObjectTypeTransferIncomeCd() // TI
409        );
410        // String[] expenseObjectTypeCodes = new String[] { "EE", "ES", "EX", "TE" };
411        expenseObjectTypeCodes = Arrays.asList( options.getFinObjTypeExpendNotExpCode(), // EE?
412                options.getFinObjTypeExpenditureexpCd(), // ES
413                options.getFinObjTypeExpNotExpendCode(), // EX?
414                options.getFinancialObjectTypeTransferExpenseCd() // TE
415        );
416        // String[] assetLiabilityFundBalanceBalanceTypeCodes = new String[] { "AS", "LI", "FB" };
417        assetLiabilityFundBalanceObjectTypeCodes = Arrays.asList( options.getFinancialObjectTypeAssetsCd(), // AS
418                options.getFinObjectTypeLiabilitiesCode(), // LI
419                options.getFinObjectTypeFundBalanceCd() // FB
420        );
421        // String[] encumbranceBaseBudgetBalanceTypeCodes = new String[] { "EX", "IE", "PE", "BB" };
422        encumbranceBaseBudgetBalanceTypeCodes = Arrays.asList( options.getExtrnlEncumFinBalanceTypCd(), // EX
423                options.getIntrnlEncumFinBalanceTypCd(), // IE
424                options.getPreencumbranceFinBalTypeCd(), // PE
425                options.getBaseBudgetFinancialBalanceTypeCd() // BB
426        );
427    }
428
429    /**
430     * Use the options table to get a list of all the balance type codes associated with actual balances
431     * 
432     * @return an array of balance type codes for actual balances
433     */
434    protected Collection<String> getActualBalanceCodes() {
435        if (actualBalanceCodes == null) {
436            loadConstantsFromOptions();
437        }
438        return actualBalanceCodes;
439    }
440
441    /**
442     * Uses the options table to find all the balance type codes associated with income
443     * 
444     * @return an array of income balance type codes
445     */
446    protected Collection<String> getIncomeObjectTypeCodes() {
447        if (incomeObjectTypeCodes == null) {
448            loadConstantsFromOptions();
449        }
450        return incomeObjectTypeCodes;
451    }
452
453    /**
454     * Uses the options table to find all the balance type codes associated with expenses
455     * 
456     * @return an array of expense option type codes
457     */
458    protected Collection<String> getExpenseObjectTypeCodes() {
459        if (expenseObjectTypeCodes == null) {
460            loadConstantsFromOptions();
461        }
462        return expenseObjectTypeCodes;
463    }
464
465    /**
466     * Uses the options table to find all the balance type codes associated with asset/liability
467     * 
468     * @return an array of asset/liability balance type codes
469     */
470    protected Collection<String> getAssetLiabilityFundBalanceBalanceTypeCodes() {
471        if (assetLiabilityFundBalanceObjectTypeCodes == null) {
472            loadConstantsFromOptions();
473        }
474        return assetLiabilityFundBalanceObjectTypeCodes;
475    }
476
477    /**
478     * Uses the options table to create a list of all the balance type codes associated with encumbrances
479     * 
480     * @return an array of encumbrance balance type codes
481     */
482    protected Collection<String> getEncumbranceBaseBudgetBalanceTypeCodes() {
483        if (encumbranceBaseBudgetBalanceTypeCodes == null) {
484            loadConstantsFromOptions();
485        }
486        return encumbranceBaseBudgetBalanceTypeCodes;
487    }
488
489    /**
490     * Uses the DAO to count the number of balances associated with the given fiscal year
491     * 
492     * @param fiscal year a fiscal year to count balances for
493     * @return an integer with the number of balances
494     * @see org.kuali.ole.gl.service.BalanceService#countBalancesForFiscalYear(java.lang.Integer)
495     */
496    public int countBalancesForFiscalYear(Integer year) {
497        return balanceDao.countBalancesForFiscalYear(year);
498    }
499
500    /**
501     * This method returns all of the balances specifically for the nominal activity closing job
502     * 
503     * @param year year to find balances for
504     * @return an Iterator of nominal activity balances
505     * @see org.kuali.ole.gl.service.BalanceService#findNominalActivityBalancesForFiscalYear(java.lang.Integer)
506     */
507    public Iterator<Balance> findNominalActivityBalancesForFiscalYear(Integer year) {
508        // generate List of nominal activity object type codes
509        List<String> nominalActivityObjectTypeCodes = objectTypeService.getNominalActivityClosingAllowedObjectTypes(year);
510        SystemOptions currentYearOptions = optionsService.getCurrentYearOptions();
511        return balanceDao.findNominalActivityBalancesForFiscalYear(year, nominalActivityObjectTypeCodes, currentYearOptions);
512    }
513
514    /**
515     * Returns all the balances to be forwarded for the "cumulative" rule
516     * 
517     * @param year the fiscal year to find balances for
518     * @return an Iterator of balances to process for the cumulative/active balance forward process
519     * @see org.kuali.ole.gl.service.BalanceService#findCumulativeBalancesToForwardForFiscalYear(java.lang.Integer)
520     */
521    public Iterator<Balance> findCumulativeBalancesToForwardForFiscalYear(Integer year) {
522        List<String> cumulativeForwardBalanceObjectTypes = objectTypeService.getCumulativeForwardBalanceObjectTypes(year);
523        Collection<String> contractsAndGrantsDenotingValues = subFundGroupService.getContractsAndGrantsDenotingValues();
524        Collection<String> subFundGroupsForCumulativeBalanceForwardingArray = parameterService.getParameterValuesAsString(BalanceForwardStep.class, GeneralLedgerConstants.BalanceForwardRule.SUB_FUND_GROUPS_FOR_INCEPTION_TO_DATE_REPORTING);
525        Collection<String> cumulativeBalanceForwardBalanceTypesArray = parameterService.getParameterValuesAsString(BalanceForwardStep.class, GeneralLedgerConstants.BalanceForwardRule.BALANCE_TYPES_TO_ROLL_FORWARD_FOR_INCOME_EXPENSE);
526        boolean fundGroupDenotesCGInd = parameterService.getParameterValueAsBoolean(Account.class, OLEConstants.ChartApcParms.ACCOUNT_FUND_GROUP_DENOTES_CG);
527        Iterator<Balance> balances = balanceDao.findCumulativeBalancesToForwardForFiscalYear(year, cumulativeForwardBalanceObjectTypes, contractsAndGrantsDenotingValues, subFundGroupsForCumulativeBalanceForwardingArray, cumulativeBalanceForwardBalanceTypesArray, fundGroupDenotesCGInd);
528
529        FilteringBalanceIterator filteredBalances = SpringContext.getBean(FilteringBalanceIterator.class, "glBalanceAnnualAndCGTotalNotZeroIterator");
530        filteredBalances.setBalancesSource(balances);
531
532        return filteredBalances;
533    }
534
535    /**
536     * Returns all the balances specifically to be processed by the balance forwards job for the "general" rule
537     * 
538     * @param year the fiscal year to find balances for
539     * @return an Iterator of balances to process for the general balance forward process
540     * @see org.kuali.ole.gl.service.BalanceService#findGeneralBalancesToForwardForFiscalYear(java.lang.Integer)
541     */
542    public Iterator<Balance> findGeneralBalancesToForwardForFiscalYear(Integer year) {
543        List<String> generalForwardBalanceObjectTypes = objectTypeService.getGeneralForwardBalanceObjectTypes(year);
544        Collection<String> generalBalanceForwardBalanceTypesArray = parameterService.getParameterValuesAsString(BalanceForwardStep.class, GeneralLedgerConstants.BalanceForwardRule.BALANCE_TYPES_TO_ROLL_FORWARD_FOR_BALANCE_SHEET);
545        Iterator<Balance> balances = balanceDao.findGeneralBalancesToForwardForFiscalYear(year, generalForwardBalanceObjectTypes, generalBalanceForwardBalanceTypesArray);
546
547        Map<String, FilteringBalanceIterator> balanceIterators = SpringContext.getBeansOfType(FilteringBalanceIterator.class);
548        FilteringBalanceIterator filteredBalances = balanceIterators.get("glBalanceTotalNotZeroIterator");
549        filteredBalances.setBalancesSource(balances);
550
551        return filteredBalances;
552    }
553
554    /**
555     * Returns all of the balances to be forwarded for the organization reversion process
556     * 
557     * @param year the year of balances to find
558     * @param endOfYear whether the organization reversion process is running end of year (before the fiscal year change over) or
559     *        beginning of year (after the fiscal year change over)
560     * @return an iterator of balances to put through the strenuous organization reversion process
561     * @see org.kuali.ole.gl.service.BalanceService#findOrganizationReversionBalancesForFiscalYear(java.lang.Integer, boolean)
562     */
563    public Iterator<Balance> findOrganizationReversionBalancesForFiscalYear(Integer year, boolean endOfYear) {
564        SystemOptions options = SpringContext.getBean(OptionsService.class).getOptions(year);
565        List<ParameterEvaluator> parameterEvaluators = new ArrayList<ParameterEvaluator>();
566
567        int i = 1;
568        boolean moreParams = true;
569        while (moreParams) {
570            if (parameterService.parameterExists(OrganizationReversion.class, PARAMETER_PREFIX + i)) {
571                ParameterEvaluator parameterEvaluator = /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(OrganizationReversion.class, PARAMETER_PREFIX + i);
572                parameterEvaluators.add(parameterEvaluator);
573            }
574            else {
575                moreParams = false;
576            }
577            i++;
578        }
579        return balanceDao.findOrganizationReversionBalancesForFiscalYear(year, endOfYear, options, parameterEvaluators);
580    }
581
582    /**
583     * Gets the encumbrance balance types.
584     * 
585     * @param fieldValues
586     * @return a list with the encumbrance balance types
587     */
588    protected List<String> getEncumbranceBalanceTypes(Map fieldValues) {
589
590        List<String> encumbranceBalanceTypes = null;
591        if (fieldValues.containsKey(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
592            // the year should be part of the results for both the cash balance and regular balance lookupables
593            String universityFiscalYearStr = (String) fieldValues.get(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
594            Integer universityFiscalYear = new Integer(universityFiscalYearStr);
595            encumbranceBalanceTypes = balanceTypService.getEncumbranceBalanceTypes(universityFiscalYear);
596        }
597        return encumbranceBalanceTypes;
598    }
599
600    /**
601     * Sets the objectTypeService.
602     * 
603     * @param objectTypeService
604     */
605    public void setObjectTypeService(ObjectTypeService objectTypeService) {
606        this.objectTypeService = objectTypeService;
607    }
608
609    /**
610     * Sets the subFundGroupService.
611     * 
612     * @param subFundGroupService
613     */
614    public void setSubFundGroupService(SubFundGroupService subFundGroupService) {
615        this.subFundGroupService = subFundGroupService;
616    }
617
618    /**
619     * Sets the parameterService.
620     * 
621     * @param parameterService
622     */
623    public void setParameterService(ParameterService parameterService) {
624        this.parameterService = parameterService;
625    }
626
627    /**
628     * Sets the balanceTypService.
629     * 
630     * @param balanceTypService
631     */
632    public void setBalanceTypService(BalanceTypeService balanceTypService) {
633        this.balanceTypService = balanceTypService;
634    }
635
636}