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.dataaccess.impl;
017    
018    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.APRIL;
019    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.AUGUST;
020    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.DECEMBER;
021    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.FEBRUARY;
022    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.JANUARY;
023    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.JULY;
024    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.JUNE;
025    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.MARCH;
026    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.MAY;
027    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.NOVEMBER;
028    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.OCTOBER;
029    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.SEPTEMBER;
030    import static org.kuali.kfs.module.ld.LaborPropertyConstants.AccountingPeriodProperties.YEAR_END;
031    import static org.kuali.kfs.module.ld.util.ConsolidationUtil.buildConsolidatedQuery;
032    import static org.kuali.kfs.module.ld.util.ConsolidationUtil.buildGroupByCollection;
033    import static org.kuali.kfs.module.ld.util.ConsolidationUtil.sum;
034    
035    import java.util.ArrayList;
036    import java.util.Collection;
037    import java.util.HashMap;
038    import java.util.Iterator;
039    import java.util.List;
040    import java.util.Map;
041    import java.util.Set;
042    
043    import org.apache.commons.lang.StringUtils;
044    import org.apache.ojb.broker.query.Criteria;
045    import org.apache.ojb.broker.query.Query;
046    import org.apache.ojb.broker.query.QueryByCriteria;
047    import org.apache.ojb.broker.query.QueryFactory;
048    import org.apache.ojb.broker.query.ReportQueryByCriteria;
049    import org.kuali.kfs.coa.service.BalanceTypeService;
050    import org.kuali.kfs.gl.OJBUtility;
051    import org.kuali.kfs.gl.dataaccess.LedgerBalanceBalancingDao;
052    import org.kuali.kfs.module.ld.LaborConstants;
053    import org.kuali.kfs.module.ld.businessobject.EmployeeFunding;
054    import org.kuali.kfs.module.ld.businessobject.LaborBalanceSummary;
055    import org.kuali.kfs.module.ld.businessobject.LedgerBalance;
056    import org.kuali.kfs.module.ld.businessobject.LedgerBalanceForYearEndBalanceForward;
057    import org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao;
058    import org.kuali.kfs.module.ld.util.ConsolidationUtil;
059    import org.kuali.kfs.sys.KFSConstants;
060    import org.kuali.kfs.sys.KFSPropertyConstants;
061    import org.kuali.kfs.sys.ObjectUtil;
062    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
063    import org.kuali.rice.kns.service.KualiConfigurationService;
064    
065    /**
066     * This is the data access object for ledger balance.
067     * 
068     * @see org.kuali.kfs.module.ld.businessobject.LedgerBalance
069     */
070    public class LaborLedgerBalanceDaoOjb extends PlatformAwareDaoBaseOjb implements LaborLedgerBalanceDao, LedgerBalanceBalancingDao {
071        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborLedgerBalanceDaoOjb.class);
072        private KualiConfigurationService kualiConfigurationService;
073    
074        private BalanceTypeService balanceTypService;
075    
076        /**
077         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findBalancesForFiscalYear(java.lang.Integer)
078         */
079        public Iterator<LedgerBalance> findBalancesForFiscalYear(Integer year) {
080            LOG.debug("findBalancesForFiscalYear() started");
081    
082            Criteria c = new Criteria();
083            c.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
084    
085            QueryByCriteria query = QueryFactory.newQuery(LedgerBalance.class, c);
086            query.addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
087            query.addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
088            query.addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
089            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
090            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
091            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
092            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
093    
094            return getPersistenceBrokerTemplate().getIteratorByQuery(query);
095        }
096    
097        /**
098         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findBalancesForFiscalYear(java.lang.Integer, java.util.Map)
099         */
100        public Iterator<LedgerBalance> findBalancesForFiscalYear(Integer fiscalYear, Map<String, String> fieldValues) {
101    
102            Criteria criteria = buildCriteriaFromMap(fieldValues, new LedgerBalance());
103            criteria.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
104    
105            QueryByCriteria query = QueryFactory.newQuery(LedgerBalance.class, criteria);
106    
107            query.addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
108            query.addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
109            query.addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
110            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
111            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
112            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
113            query.addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
114    
115            return getPersistenceBrokerTemplate().getIteratorByQuery(query);
116        }
117    
118        /**
119         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findBalance(java.util.Map, boolean)
120         */
121        public Iterator<LedgerBalance> findBalance(Map fieldValues, boolean isConsolidated) {
122            LOG.debug("findBalance() started");
123    
124            Query query = this.getBalanceQuery(fieldValues, isConsolidated);
125            OJBUtility.limitResultSize(query);
126    
127            if (isConsolidated) {
128                return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
129            }
130            return getPersistenceBrokerTemplate().getIteratorByQuery(query);
131        }
132    
133        /**
134         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#getConsolidatedBalanceRecordCount(java.util.Map)
135         */
136        public Iterator getConsolidatedBalanceRecordCount(Map fieldValues) {
137            LOG.debug("getBalanceRecordCount() started");
138    
139            ReportQueryByCriteria query = this.getBalanceCountQuery(fieldValues);
140            return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
141        }
142    
143        // build the query for balance search
144        protected Query getBalanceQuery(Map fieldValues, boolean isConsolidated) {
145            LOG.debug("getBalanceQuery(Map, boolean) started");
146            LOG.debug("Building criteria from map fields: " + fieldValues.keySet());
147    
148            Criteria criteria = buildCriteriaFromMap(fieldValues, new LedgerBalance());
149            ReportQueryByCriteria query = QueryFactory.newReportQuery(LedgerBalance.class, criteria);
150    
151            // if consolidated, then ignore subaccount number and balance type code
152            if (isConsolidated) {
153                buildConsolidatedQuery(query, sum(JULY.propertyName), sum(AUGUST.propertyName), sum(SEPTEMBER.propertyName), sum(OCTOBER.propertyName), sum(NOVEMBER.propertyName), sum(DECEMBER.propertyName), sum(JANUARY.propertyName), sum(FEBRUARY.propertyName), sum(MARCH.propertyName), sum(APRIL.propertyName), sum(MAY.propertyName), sum(JUNE.propertyName), sum(YEAR_END.propertyName));
154            }
155    
156            return query;
157        }
158    
159        // build the query for balance search
160        protected ReportQueryByCriteria getBalanceCountQuery(Map fieldValues) {
161            Criteria criteria = buildCriteriaFromMap(fieldValues, new LedgerBalance());
162            ReportQueryByCriteria query = QueryFactory.newReportQuery(LedgerBalance.class, criteria);
163    
164            // set the selection attributes
165            query.setAttributes(new String[] { "count(*)" });
166    
167            Collection<String> groupByList = buildGroupByCollection();
168            groupByList.remove(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
169            groupByList.remove(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
170            groupByList.remove(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
171    
172            // add the group criteria into the selection statement
173            String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
174            query.addGroupBy(groupBy);
175            return query;
176        }
177    
178        /**
179         * This method builds the query criteria based on the input field map
180         * 
181         * @param fieldValues
182         * @param balance
183         * @return a query criteria
184         */
185        protected Criteria buildCriteriaFromMap(Map fieldValues, LedgerBalance balance) {
186            Map localFieldValues = new HashMap();
187            localFieldValues.putAll(fieldValues);
188    
189            Criteria criteria = new Criteria();
190    
191            // handle encumbrance balance type
192            String propertyName = KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE;
193            if (localFieldValues.containsKey(propertyName)) {
194                String propertyValue = (String) localFieldValues.get(propertyName);
195                if (KFSConstants.AGGREGATE_ENCUMBRANCE_BALANCE_TYPE_CODE.equals(propertyValue)) {
196                    localFieldValues.remove(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
197    
198                    // parse the university fiscal year since it's a required field from the lookups
199                    String universityFiscalYearStr = (String) localFieldValues.get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
200                    Integer universityFiscalYear = new Integer(universityFiscalYearStr);
201    
202                    criteria.addIn(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, balanceTypService.getEncumbranceBalanceTypes(universityFiscalYear));
203                }
204            }
205    
206            criteria.addAndCriteria(OJBUtility.buildCriteriaFromMap(localFieldValues, new LedgerBalance()));
207            return criteria;
208        }
209    
210        /**
211         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findCurrentFunds(java.util.Map)
212         */
213        public List<LedgerBalance> findCurrentFunds(Map fieldValues) {
214            LOG.debug("Start findCurrentFunds()");
215    
216            Iterator<Object[]> queryResults = this.findCurrentFundsRawData(fieldValues);
217            List<LedgerBalance> currentFundsCollection = new ArrayList<LedgerBalance>();
218            while (queryResults != null && queryResults.hasNext()) {
219                currentFundsCollection.add(this.marshalFundsAsLedgerBalance(queryResults.next()));
220            }
221            return currentFundsCollection;
222        }
223    
224        /**
225         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findEncumbranceFunds(java.util.Map)
226         */
227        public List<LedgerBalance> findEncumbranceFunds(Map fieldValues) {
228            LOG.debug("Start findEncumbranceFunds()");
229    
230            Iterator<Object[]> queryResults = this.findEncumbranceFundsRawData(fieldValues);
231            List<LedgerBalance> currentFundsCollection = new ArrayList<LedgerBalance>();
232            while (queryResults != null && queryResults.hasNext()) {
233                currentFundsCollection.add(this.marshalFundsAsLedgerBalance(queryResults.next()));
234            }
235            return currentFundsCollection;
236        }
237    
238        /**
239         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findCurrentEmployeeFunds(java.util.Map)
240         */
241        public List<EmployeeFunding> findCurrentEmployeeFunds(Map fieldValues) {
242            LOG.debug("Start findCurrentEmployeeFunds()");
243    
244            Iterator<Object[]> queryResults = this.findCurrentFundsRawData(fieldValues);
245            List<EmployeeFunding> currentFundsCollection = new ArrayList<EmployeeFunding>();
246            while (queryResults != null && queryResults.hasNext()) {
247                currentFundsCollection.add(this.marshalFundsAsEmployeeFunding(queryResults.next()));
248            }
249            return currentFundsCollection;
250        }
251    
252        /**
253         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findEncumbranceEmployeeFunds(java.util.Map)
254         */
255        public List<EmployeeFunding> findEncumbranceEmployeeFunds(Map fieldValues) {
256            LOG.debug("Start findCurrentEmployeeFunds()");
257    
258            Iterator<Object[]> queryResults = this.findEncumbranceFundsRawData(fieldValues);
259            List<EmployeeFunding> currentFundsCollection = new ArrayList<EmployeeFunding>();
260            while (queryResults != null && queryResults.hasNext()) {
261                currentFundsCollection.add(this.marshalFundsAsEmployeeFunding(queryResults.next()));
262            }
263            return currentFundsCollection;
264        }
265    
266        /**
267         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findBalanceSummary(java.lang.Integer, java.util.Collection)
268         */
269        public List<LaborBalanceSummary> findBalanceSummary(Integer fiscalYear, Collection<String> balanceTypes) {
270            LOG.debug("Start findBalanceSummary()");
271    
272            Criteria criteria = new Criteria();
273            criteria.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
274            criteria.addIn(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, balanceTypes);
275    
276            Iterator<Object[]> queryResults = this.findBalanceSummaryRawData(criteria);
277            List<LaborBalanceSummary> balanceSummaryCollection = new ArrayList<LaborBalanceSummary>();
278            while (queryResults != null && queryResults.hasNext()) {
279                balanceSummaryCollection.add(this.marshalFundsAsLaborBalanceSummary(queryResults.next()));
280            }
281            return balanceSummaryCollection;
282        }
283    
284        /**
285         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#save(org.kuali.kfs.module.ld.businessobject.LedgerBalance)
286         */
287        public void save(LedgerBalance ledgerBalance) {
288            getPersistenceBrokerTemplate().store(ledgerBalance);
289        }
290    
291        // get the current funds according to the given criteria
292        protected Iterator<Object[]> findCurrentFundsRawData(Map fieldValues) {
293            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new LedgerBalance());
294            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_ACTUAL);
295    
296            List<String> objectTypeCodes = new ArrayList<String>();
297            objectTypeCodes.add(LaborConstants.BalanceInquiries.EMPLOYEE_FUNDING_EXPENSE_OBJECT_TYPE_CODE);
298            objectTypeCodes.add(LaborConstants.BalanceInquiries.EMPLOYEE_FUNDING_NORMAL_OP_EXPENSE_OBJECT_TYPE_CODE);
299            criteria.addIn(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, objectTypeCodes);
300    
301            return this.findFundsRawData(criteria);
302        }
303    
304        // get the encumbrance funds according to the given criteria
305        protected Iterator<Object[]> findEncumbranceFundsRawData(Map fieldValues) {
306            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new LedgerBalance());
307            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_INTERNAL_ENCUMBRANCE);
308    
309            return this.findFundsRawData(criteria);
310        }
311    
312        // get the funds based on the given criteria
313        protected Iterator<Object[]> findFundsRawData(Criteria criteria) {
314            ReportQueryByCriteria query = QueryFactory.newReportQuery(LedgerBalance.class, criteria);
315    
316            List<String> groupByList = this.getGroupByListForFundingInquiry();
317            String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
318            query.addGroupBy(groupBy);
319    
320            List<String> getAttributeList = getAttributeListForFundingInquiry(false);
321            String[] attributes = (String[]) getAttributeList.toArray(new String[getAttributeList.size()]);
322            query.setAttributes(attributes);
323    
324            OJBUtility.limitResultSize(query);
325            return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
326        }
327    
328        // get the balance summary based on the given criteria
329        protected Iterator<Object[]> findBalanceSummaryRawData(Criteria criteria) {
330            ReportQueryByCriteria query = QueryFactory.newReportQuery(LedgerBalance.class, criteria);
331    
332            List<String> groupByList = this.getGroupByListForBalanceSummary();
333            String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
334            query.addGroupBy(groupBy);
335    
336            List<String> getAttributeList = this.getAttributeListForBalanceSummary(false);
337            String[] attributes = (String[]) getAttributeList.toArray(new String[getAttributeList.size()]);
338            query.setAttributes(attributes);
339    
340            query.addOrderByAscending(groupByList.get(0));
341            return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
342        }
343    
344        // marshal into AccountStatusBaseFunds from the query result
345        protected LedgerBalance marshalFundsAsLedgerBalance(Object[] queryResult) {
346            LedgerBalance ledgerBalance = new LedgerBalance();
347            List<String> keyFields = this.getAttributeListForFundingInquiry(true);
348    
349            ObjectUtil.buildObject(ledgerBalance, queryResult, keyFields);
350            return ledgerBalance;
351        }
352    
353        // marshal into AccountStatusBaseFunds from the query result
354        protected EmployeeFunding marshalFundsAsEmployeeFunding(Object[] queryResult) {
355            EmployeeFunding employeeFunding = new EmployeeFunding();
356            List<String> keyFields = this.getAttributeListForFundingInquiry(true);
357    
358            ObjectUtil.buildObject(employeeFunding, queryResult, keyFields);
359            return employeeFunding;
360        }
361    
362        // marshal into LaborBalanceSummary from the query result
363        protected LaborBalanceSummary marshalFundsAsLaborBalanceSummary(Object[] queryResult) {
364            return new LaborBalanceSummary(queryResult);
365        }
366    
367        // define the attribute list that can be used to group the search results
368        protected List<String> getGroupByListForFundingInquiry() {
369            List<String> groupByList = new ArrayList<String>();
370            groupByList.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
371            groupByList.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
372            groupByList.add(KFSPropertyConstants.ACCOUNT_NUMBER);
373            groupByList.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
374            groupByList.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
375            groupByList.add(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
376            groupByList.add(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
377            groupByList.add(KFSPropertyConstants.POSITION_NUMBER);
378            groupByList.add(KFSPropertyConstants.EMPLID);
379            return groupByList;
380        }
381    
382        // define the return attribute list for funding query
383        protected List<String> getAttributeListForFundingInquiry(boolean isAttributeNameNeeded) {
384            List<String> attributeList = getGroupByListForFundingInquiry();
385            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.ACCOUNTING_LINE_ANNUAL_BALANCE_AMOUNT, isAttributeNameNeeded));
386            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.FINANCIAL_BEGINNING_BALANCE_LINE_AMOUNT, isAttributeNameNeeded));
387            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.CONTRACTS_GRANTS_BEGINNING_BALANCE_AMOUNT, isAttributeNameNeeded));
388            return attributeList;
389        }
390    
391        // define the attribute list that can be used to group the search results
392        protected List<String> getGroupByListForBalanceSummary() {
393            List<String> groupByList = new ArrayList<String>();
394            groupByList.add("account.subFundGroup.fundGroupCode");
395            return groupByList;
396        }
397    
398        // define the return attribute list for balance summary
399        protected List<String> getAttributeListForBalanceSummary(boolean isAttributeNameNeeded) {
400            List<String> attributeList = getGroupByListForBalanceSummary();
401            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.ACCOUNTING_LINE_ANNUAL_BALANCE_AMOUNT, isAttributeNameNeeded));
402            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.FINANCIAL_BEGINNING_BALANCE_LINE_AMOUNT, isAttributeNameNeeded));
403            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.CONTRACTS_GRANTS_BEGINNING_BALANCE_AMOUNT, isAttributeNameNeeded));
404            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH1_AMOUNT, isAttributeNameNeeded));
405            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH2_AMOUNT, isAttributeNameNeeded));
406            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH3_AMOUNT, isAttributeNameNeeded));
407            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH4_AMOUNT, isAttributeNameNeeded));
408            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH5_AMOUNT, isAttributeNameNeeded));
409            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH6_AMOUNT, isAttributeNameNeeded));
410            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH7_AMOUNT, isAttributeNameNeeded));
411            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH8_AMOUNT, isAttributeNameNeeded));
412            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH9_AMOUNT, isAttributeNameNeeded));
413            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH10_AMOUNT, isAttributeNameNeeded));
414            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH11_AMOUNT, isAttributeNameNeeded));
415            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH12_AMOUNT, isAttributeNameNeeded));
416            attributeList.add(ConsolidationUtil.wrapAttributeName(KFSPropertyConstants.MONTH13_AMOUNT, isAttributeNameNeeded));
417            return attributeList;
418        }
419    
420        /**
421         * Sets the kualiConfigurationService attribute value.
422         * 
423         * @param kualiConfigurationService The kualiConfigurationService to set.
424         */
425        public void setKualiConfigurationService(KualiConfigurationService kualiConfigurationService) {
426            this.kualiConfigurationService = kualiConfigurationService;
427        }
428    
429        public void setBalanceTypService(BalanceTypeService balanceTypService) {
430            this.balanceTypService = balanceTypService;
431        }
432    
433        /**
434         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findBalancesForFiscalYear(java.lang.Integer, java.util.Map,
435         *      java.util.List, java.util.List)
436         */
437        public Iterator<LedgerBalanceForYearEndBalanceForward> findBalancesForFiscalYear(Integer fiscalYear, Map<String, String> fieldValues, List<String> subFundGroupCodes, List<String> fundGroupCodes) {
438            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new LedgerBalanceForYearEndBalanceForward());
439            criteria.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
440    
441            String chartAccountsCode = fieldValues.get(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
442            String accountNumber = fieldValues.get(KFSPropertyConstants.ACCOUNT_NUMBER);
443            
444            // add subfund criteria if the account is not provided
445            if(StringUtils.isEmpty(chartAccountsCode) || StringUtils.isEmpty(accountNumber)) {
446                if (subFundGroupCodes != null && !subFundGroupCodes.isEmpty()) {
447                    Criteria criteriaForSubFundGroup = new Criteria();
448                    String subFundGroupFieldName = KFSPropertyConstants.ACCOUNT + "." + KFSPropertyConstants.SUB_FUND_GROUP_CODE;
449                    criteriaForSubFundGroup.addIn(subFundGroupFieldName, subFundGroupCodes);
450        
451                    if (fundGroupCodes != null && !fundGroupCodes.isEmpty()) {
452        
453                        Criteria criteriaForFundGroup = new Criteria();
454                        String fundGroupFieldName = KFSPropertyConstants.ACCOUNT + "." + KFSPropertyConstants.SUB_FUND_GROUP + "." + KFSPropertyConstants.FUND_GROUP_CODE;
455                        criteriaForFundGroup.addIn(fundGroupFieldName, fundGroupCodes);
456        
457                        criteriaForSubFundGroup.addOrCriteria(criteriaForFundGroup);
458                    }
459                    
460                    criteria.addAndCriteria(criteriaForSubFundGroup);
461                }
462            }
463    
464            QueryByCriteria query = QueryFactory.newQuery(LedgerBalanceForYearEndBalanceForward.class, criteria);
465            return getPersistenceBrokerTemplate().getIteratorByQuery(query);
466        }
467    
468        /**
469         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findAccountsInFundGroups(java.lang.Integer, java.util.Map,
470         *      java.util.List, java.util.List)
471         */
472        public List<List<String>> findAccountsInFundGroups(Integer fiscalYear, Map<String, String> fieldValues, List<String> subFundGroupCodes, List<String> fundGroupCodes) {
473            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new LedgerBalanceForYearEndBalanceForward());
474            criteria.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
475    
476            if (subFundGroupCodes != null && !subFundGroupCodes.isEmpty()) {
477                Criteria criteriaForSubFundGroup = new Criteria();
478                String subFundGroupFieldName = KFSPropertyConstants.ACCOUNT + "." + KFSPropertyConstants.SUB_FUND_GROUP_CODE;
479                criteriaForSubFundGroup.addIn(subFundGroupFieldName, subFundGroupCodes);
480    
481                if (fundGroupCodes != null && !fundGroupCodes.isEmpty()) {
482    
483                    Criteria criteriaForFundGroup = new Criteria();
484                    String fundGroupFieldName = KFSPropertyConstants.ACCOUNT + "." + KFSPropertyConstants.SUB_FUND_GROUP + "." + KFSPropertyConstants.FUND_GROUP_CODE;
485                    criteriaForFundGroup.addIn(fundGroupFieldName, fundGroupCodes);
486    
487                    criteriaForSubFundGroup.addOrCriteria(criteriaForFundGroup);
488                }
489                criteria.addAndCriteria(criteriaForSubFundGroup);
490            }
491    
492            ReportQueryByCriteria query = QueryFactory.newReportQuery(LedgerBalanceForYearEndBalanceForward.class, criteria);
493    
494            query.setAttributes(LaborConstants.ACCOUNT_FIELDS);
495            query.setDistinct(true);
496    
497            Iterator<Object[]> accountIterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
498    
499            List<List<String>> accounts = new ArrayList<List<String>>();
500            while (accountIterator != null && accountIterator.hasNext()) {
501                Object[] accountObject = accountIterator.next();
502    
503                List<String> account = new ArrayList<String>();
504                account.add(accountObject[0].toString());
505                account.add(accountObject[1].toString());
506    
507                accounts.add(account);
508            }
509            return accounts;
510        }
511    
512        /**
513         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#findLedgerBalances(java.util.Map, java.util.Map, java.util.Set,
514         *      java.util.List, java.util.List)
515         */
516        public Collection<LedgerBalance> findLedgerBalances(Map<String, List<String>> fieldValues, Map<String, List<String>> excludedFieldValues, Set<Integer> fiscalYears, List<String> balanceTypeList, List<String> positionObjectGroupCodes) {
517            Criteria criteria = new Criteria();
518    
519            for (String fieldName : fieldValues.keySet()) {
520                Criteria criteriaForIncludedFields = new Criteria();
521                criteria.addIn(fieldName, fieldValues.get(fieldName));
522                criteria.addAndCriteria(criteriaForIncludedFields);
523            }        
524            
525            for (String fieldName : excludedFieldValues.keySet()) {
526                Criteria criteriaForExcludedFields = new Criteria();
527                criteria.addNotIn(fieldName, excludedFieldValues.get(fieldName));
528                criteria.addAndCriteria(criteriaForExcludedFields);
529            }
530    
531            if (fiscalYears != null && !fiscalYears.isEmpty()) {
532                Criteria criteriaForFiscalyear = new Criteria();
533                criteriaForFiscalyear.addIn(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYears);
534                criteria.addAndCriteria(criteriaForFiscalyear);
535            }
536    
537            if (balanceTypeList != null && !balanceTypeList.isEmpty()) {
538                Criteria criteriaForBalanceTypes = new Criteria();
539                criteriaForBalanceTypes.addIn(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, balanceTypeList);
540                criteria.addAndCriteria(criteriaForBalanceTypes);
541            }
542    
543            if (positionObjectGroupCodes != null && !positionObjectGroupCodes.isEmpty()) {
544                Criteria criteriaForLaborObjects = new Criteria();
545                criteriaForLaborObjects.addIn(KFSPropertyConstants.LABOR_OBJECT + "." + KFSPropertyConstants.POSITION_OBJECT_GROUP_CODE, positionObjectGroupCodes);
546                criteria.addAndCriteria(criteriaForLaborObjects);
547            }
548    
549            QueryByCriteria query = QueryFactory.newQuery(LedgerBalance.class, criteria);
550            return getPersistenceBrokerTemplate().getCollectionByQuery(query);
551        }
552    
553        /**
554         * @see org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao#deleteLedgerBalancesPriorToYear(java.lang.Integer, java.lang.String)
555         */
556        public void deleteLedgerBalancesPriorToYear(Integer fiscalYear, String chartOfAccountsCode) {
557            LOG.debug("deleteLedgerBalancesPriorToYear() started");
558    
559            Criteria criteria = new Criteria();
560            criteria.addLessThan(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
561            criteria.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
562    
563            QueryByCriteria query = new QueryByCriteria(LedgerBalance.class, criteria);
564            getPersistenceBrokerTemplate().deleteByQuery(query);       
565        }
566        
567        /**
568         * @see org.kuali.kfs.gl.dataaccess.BalancingDao#findCountGreaterOrEqualThan(java.lang.Integer)
569         */
570        public Integer findCountGreaterOrEqualThan(Integer year) {
571            Criteria criteria = new Criteria();
572            criteria.addGreaterOrEqualThan(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
573            
574            ReportQueryByCriteria query = QueryFactory.newReportQuery(LedgerBalance.class, criteria);
575            
576            return getPersistenceBrokerTemplate().getCount(query);
577        }
578    }