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 java.util.ArrayList;
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.ojb.broker.query.Criteria;
024    import org.apache.ojb.broker.query.QueryFactory;
025    import org.apache.ojb.broker.query.ReportQueryByCriteria;
026    import org.kuali.kfs.gl.Constant;
027    import org.kuali.kfs.gl.OJBUtility;
028    import org.kuali.kfs.module.ld.businessobject.AccountStatusBaseFunds;
029    import org.kuali.kfs.module.ld.dataaccess.LaborBaseFundsDao;
030    import org.kuali.kfs.module.ld.util.ConsolidationUtil;
031    import org.kuali.kfs.sys.KFSConstants;
032    import org.kuali.kfs.sys.KFSPropertyConstants;
033    import org.kuali.kfs.sys.ObjectUtil;
034    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
035    
036    /**
037     * This is the data access object for account status base funds.
038     * 
039     * @see org.kuali.kfs.module.ld.businessobject.AccountStatusBaseFunds
040     */
041    public class LaborBaseFundsDaoOjb extends PlatformAwareDaoBaseOjb implements LaborBaseFundsDao {
042        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborBaseFundsDaoOjb.class);
043    
044        /**
045         * @see org.kuali.kfs.module.ld.dataaccess.LaborBaseFundsDao#findLaborBaseFunds(java.util.Map, boolean)
046         */
047        public List<AccountStatusBaseFunds> findLaborBaseFunds(Map fieldValues, boolean isConsolidated) {
048            LOG.debug("Start findLaborBaseFunds()");
049    
050            Iterator<Object[]> queryResults = this.findLaborBaseFundsRawData(fieldValues, isConsolidated);
051            List<AccountStatusBaseFunds> BaseFundsCollection = new ArrayList<AccountStatusBaseFunds>();
052            while (queryResults != null && queryResults.hasNext()) {
053                BaseFundsCollection.add(this.marshalAccountStatusBaseFunds(queryResults.next()));
054            }
055            return BaseFundsCollection;
056        }
057    
058        // get the labor base funds according to the given criteria
059        protected Iterator<Object[]> findLaborBaseFundsRawData(Map fieldValues, boolean isConsolidated) {
060            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new AccountStatusBaseFunds());
061            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_BASE_BUDGET);
062    
063            criteria.addEqualToField(KFSPropertyConstants.LABOR_OBJECT + "." + KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
064            criteria.addEqualToField(KFSPropertyConstants.LABOR_OBJECT + "." + KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
065            criteria.addEqualToField(KFSPropertyConstants.LABOR_OBJECT + "." + KFSPropertyConstants.FINANCIAL_OBJECT_CODE, KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
066    
067            // this statement is used to force OJB to join LABOR_OBJECT and GL_BALANCE tables
068            criteria.addNotNull(KFSPropertyConstants.LABOR_OBJECT + "." + KFSPropertyConstants.FINANCIAL_OBJECT_FRINGE_OR_SALARY_CODE);
069    
070            ReportQueryByCriteria query = QueryFactory.newReportQuery(AccountStatusBaseFunds.class, criteria);
071    
072            List<String> groupByList = getGroupByList(isConsolidated);
073            String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
074            query.addGroupBy(groupBy);
075    
076            List<String> getAttributeList = getAttributeListForBaseFunds(isConsolidated, false);
077            String[] attributes = (String[]) getAttributeList.toArray(new String[getAttributeList.size()]);
078            query.setAttributes(attributes);
079    
080            return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
081        }
082    
083        // marshal into AccountStatusBaseFunds from the query result
084        protected AccountStatusBaseFunds marshalAccountStatusBaseFunds(Object[] queryResult) {
085            AccountStatusBaseFunds baseFunds = new AccountStatusBaseFunds();
086            List<String> keyFields = this.getAttributeListForBaseFunds(false, true);
087    
088            ObjectUtil.buildObject(baseFunds, queryResult, keyFields);
089            return baseFunds;
090        }
091    
092        // define a list of attributes that are used as the grouping criteria
093        protected List<String> getGroupByList(boolean isConsolidated) {
094            List<String> groupByList = new ArrayList<String>();
095            groupByList.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
096            groupByList.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
097            groupByList.add(KFSPropertyConstants.ACCOUNT_NUMBER);
098            groupByList.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
099    
100            if (!isConsolidated) {
101                groupByList.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
102                groupByList.add(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
103            }
104            return groupByList;
105        }
106    
107        // define the return attribute list
108        protected List<String> getAttributeList(boolean isConsolidated) {
109            List<String> attributeList = getGroupByList(isConsolidated);
110    
111            if (isConsolidated) {
112                attributeList.add("'" + Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER + "'");
113                attributeList.add("'" + Constant.CONSOLIDATED_SUB_OBJECT_CODE + "'");
114            }
115            return attributeList;
116        }
117    
118        // define the return attribute list for base funds query
119        protected List<String> getAttributeListForBaseFunds(boolean isConsolidated, boolean isAttributeNameNeeded) {
120            List<String> attributeList = getAttributeList(isConsolidated);
121    
122            if (!isAttributeNameNeeded) {
123                attributeList.add(ConsolidationUtil.sum(KFSPropertyConstants.ACCOUNTING_LINE_ANNUAL_BALANCE_AMOUNT));
124                attributeList.add(ConsolidationUtil.sum(KFSPropertyConstants.FINANCIAL_BEGINNING_BALANCE_LINE_AMOUNT));
125                attributeList.add(ConsolidationUtil.sum(KFSPropertyConstants.CONTRACTS_GRANTS_BEGINNING_BALANCE_AMOUNT));
126            }
127            else {
128                attributeList.add(KFSPropertyConstants.ACCOUNTING_LINE_ANNUAL_BALANCE_AMOUNT);
129                attributeList.add(KFSPropertyConstants.FINANCIAL_BEGINNING_BALANCE_LINE_AMOUNT);
130                attributeList.add(KFSPropertyConstants.CONTRACTS_GRANTS_BEGINNING_BALANCE_AMOUNT);
131            }
132            return attributeList;
133        }
134    }