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.dataaccess.impl;
017
018import java.util.Iterator;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.ojb.broker.query.Criteria;
023import org.apache.ojb.broker.query.QueryByCriteria;
024import org.apache.ojb.broker.query.QueryFactory;
025import org.apache.ojb.broker.query.ReportQueryByCriteria;
026import org.kuali.ole.gl.OJBUtility;
027import org.kuali.ole.gl.businessobject.AccountBalance;
028import org.kuali.ole.gl.businessobject.Transaction;
029import org.kuali.ole.gl.dataaccess.AccountBalanceConsolidationDao;
030import org.kuali.ole.gl.dataaccess.AccountBalanceDao;
031import org.kuali.ole.gl.dataaccess.AccountBalanceLevelDao;
032import org.kuali.ole.gl.dataaccess.AccountBalanceObjectDao;
033import org.kuali.ole.sys.OLEPropertyConstants;
034import org.kuali.ole.sys.businessobject.SystemOptions;
035import org.kuali.ole.sys.businessobject.UniversityDate;
036import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
037
038/**
039 * An OJB implmentation of the AccountBalanceDao
040 */
041public class AccountBalanceDaoOjb extends PlatformAwareDaoBaseOjb implements AccountBalanceDao {
042    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountBalanceDaoOjb.class);
043
044    private AccountBalanceConsolidationDao accountBalanceConsolidationDao;
045    private AccountBalanceLevelDao accountBalanceLevelDao;
046    private AccountBalanceObjectDao accountBalanceObjectDao;
047
048    static final private String OBJ_TYP_CD = "financialObject.financialObjectTypeCode";
049
050    /**
051     * Given a transaction, finds a matching account balance in the database
052     * 
053     * @param t a transaction to find an appropriate related account balance for
054     * @return an appropriate account balance
055     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#getByTransaction(org.kuali.ole.gl.businessobject.Transaction)
056     */
057    public AccountBalance getByTransaction(Transaction t) {
058        LOG.debug("getByTransaction() started");
059
060        Criteria crit = new Criteria();
061        crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
062        crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
063        crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
064        crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
065        crit.addEqualTo(OLEPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
066        crit.addEqualTo(OLEPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
067
068        QueryByCriteria qbc = QueryFactory.newQuery(AccountBalance.class, crit);
069        return (AccountBalance) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
070    }
071
072    /**
073     * This method finds the available account balances according to input fields and values
074     * 
075     * @param fieldValues the input fields and values
076     * @return the summary records of account balance entries
077     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAvailableAccountBalance(java.util.Map, boolean)
078     */
079    public Iterator findConsolidatedAvailableAccountBalance(Map fieldValues) {
080        LOG.debug("findConsolidatedAvailableAccountBalance() started");
081
082        Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new AccountBalance());
083        ReportQueryByCriteria query = QueryFactory.newReportQuery(AccountBalance.class, criteria);
084
085        String[] attributes = new String[] { OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, OLEPropertyConstants.ACCOUNT_NUMBER, OLEPropertyConstants.OBJECT_CODE, OBJ_TYP_CD, "sum(currentBudgetLineBalanceAmount)", "sum(accountLineActualsBalanceAmount)", "sum(accountLineEncumbranceBalanceAmount)" };
086
087        String[] groupBy = new String[] { OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, OLEPropertyConstants.ACCOUNT_NUMBER, OLEPropertyConstants.OBJECT_CODE, OBJ_TYP_CD };
088
089        query.setAttributes(attributes);
090        query.addGroupBy(groupBy);
091        OJBUtility.limitResultSize(query);
092
093        return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
094    }
095
096    /**
097     * This method finds the available account balances according to input fields and values
098     * 
099     * @param fieldValues the input fields and values
100     * @return account balance entries
101     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAvailableAccountBalance(java.util.Map)
102     */
103    public Iterator findAvailableAccountBalance(Map fieldValues) {
104        LOG.debug("findAvailableAccountBalance(Map) started");
105
106        Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new AccountBalance());
107        QueryByCriteria query = QueryFactory.newReportQuery(AccountBalance.class, criteria);
108        OJBUtility.limitResultSize(query);
109
110        return getPersistenceBrokerTemplate().getIteratorByQuery(query);
111    }
112
113    /**
114     * Get available balances by consolidation for specific object types
115     * 
116     * @param objectTypes the object types that reported account balances must have
117     * @param universityFiscalYear the university fiscal year of account balances to find
118     * @param chartOfAccountsCode the chart of accounts of account balances to find
119     * @param accountNumber the account number of account balances to find
120     * @param isExcludeCostShare whether cost share entries should be excluded from this inquiry
121     * @param isConsolidated whether the results of this should be consolidated or not
122     * @param pendingEntriesCode whether to include no pending entries, approved pending entries, or all pending entries
123     * @return a List of Maps with the appropriate query results
124     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAccountBalanceByConsolidationByObjectTypes(java.lang.String[],
125     *      java.lang.Integer, java.lang.String, java.lang.String, boolean, boolean, int)
126     */
127    public List findAccountBalanceByConsolidationByObjectTypes(String[] objectTypes, Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, boolean isExcludeCostShare, boolean isConsolidated, int pendingEntriesCode, SystemOptions options, UniversityDate today) {
128        LOG.debug("findAccountBalanceByConsolidationByObjectTypes() started");
129
130        // This is in a new object just to make each class smaller and easier to read
131        try {
132            return accountBalanceConsolidationDao.findAccountBalanceByConsolidationObjectTypes(objectTypes, universityFiscalYear, chartOfAccountsCode, accountNumber, isExcludeCostShare, isConsolidated, pendingEntriesCode, options, today);
133        }
134        catch (Exception e) {
135            LOG.error("findAccountBalanceByConsolidation() " + e.getMessage(), e);
136            throw new RuntimeException(e.getMessage(), e);
137        }
138    }
139
140    /**
141     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAccountBalanceByLevel(java.lang.Integer, java.lang.String,
142     *      java.lang.String, java.lang.String, boolean, boolean, int, org.kuali.ole.sys.businessobject.UniversityDate)
143     */
144    public List findAccountBalanceByLevel(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, String financialConsolidationObjectCode, boolean isCostShareExcluded, boolean isConsolidated, int pendingEntriesCode, UniversityDate today, SystemOptions options) {
145        LOG.debug("findAccountBalanceByLevel() started");
146
147        // This is in a new object just to make each class smaller and easier to read
148        try {
149            return accountBalanceLevelDao.findAccountBalanceByLevel(universityFiscalYear, chartOfAccountsCode, accountNumber, financialConsolidationObjectCode, isCostShareExcluded, isConsolidated, pendingEntriesCode, today, options);
150        }
151        catch (Exception ex) {
152            LOG.error("findAccountBalanceByLevel() " + ex.getMessage(), ex);
153            throw new RuntimeException("error executing findAccountBalanceByLevel()", ex);
154        }
155    }
156
157    /**
158     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAccountBalanceByObject(java.lang.Integer, java.lang.String,
159     *      java.lang.String, java.lang.String, java.lang.String, boolean, boolean, int,
160     *      org.kuali.ole.sys.businessobject.UniversityDate)
161     */
162    public List findAccountBalanceByObject(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, String financialObjectLevelCode, String financialReportingSortCode, boolean isCostShareExcluded, boolean isConsolidated, int pendingEntriesCode, UniversityDate today, SystemOptions options) {
163        LOG.debug("findAccountBalanceByObject() started");
164
165        // This is in a new object just to make each class smaller and easier to read
166        try {
167            return accountBalanceObjectDao.findAccountBalanceByObject(universityFiscalYear, chartOfAccountsCode, accountNumber, financialObjectLevelCode, financialReportingSortCode, isCostShareExcluded, isConsolidated, pendingEntriesCode, today, options);
168        }
169        catch (Exception ex) {
170            LOG.error("findAccountBalanceByObject() " + ex.getMessage(), ex);
171            throw new RuntimeException(ex.getMessage(), ex);
172        }
173    }
174
175    /**
176     * Purge an entire fiscal year for a single chart.
177     * 
178     * @param chartOfAccountsCode the chart of accounts code of account balances to purge
179     * @param year the fiscal year of account balances to purge
180     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#purgeYearByChart(java.lang.String, int)
181     */
182    public void purgeYearByChart(String chartOfAccountsCode, int year) {
183        LOG.debug("purgeYearByChart() started");
184
185        Criteria criteria = new Criteria();
186        criteria.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
187        criteria.addLessThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(year));
188
189        getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(AccountBalance.class, criteria));
190
191        // This is required because if any deleted account balances are in the cache, deleteByQuery doesn't
192        // remove them from the cache so a future select will retrieve these deleted account balances from
193        // the cache and return them. Clearing the cache forces OJB to go to the database again.
194        getPersistenceBrokerTemplate().clearCache();
195    }
196
197    /**
198     * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findCountGreaterOrEqualThan(java.lang.Integer)
199     */
200    public Integer findCountGreaterOrEqualThan(Integer year) {
201        Criteria criteria = new Criteria();
202        criteria.addGreaterOrEqualThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
203
204        ReportQueryByCriteria query = QueryFactory.newReportQuery(AccountBalance.class, criteria);
205
206        return getPersistenceBrokerTemplate().getCount(query);
207    }
208
209    public AccountBalanceConsolidationDao getAccountBalanceConsolidationDao() {
210        return accountBalanceConsolidationDao;
211    }
212
213    public void setAccountBalanceConsolidationDao(AccountBalanceConsolidationDao accountBalanceConsolidationDao) {
214        this.accountBalanceConsolidationDao = accountBalanceConsolidationDao;
215    }
216
217    public AccountBalanceLevelDao getAccountBalanceLevelDao() {
218        return accountBalanceLevelDao;
219    }
220
221    public void setAccountBalanceLevelDao(AccountBalanceLevelDao accountBalanceLevelDao) {
222        this.accountBalanceLevelDao = accountBalanceLevelDao;
223    }
224
225    public AccountBalanceObjectDao getAccountBalanceObjectDao() {
226        return accountBalanceObjectDao;
227    }
228
229    public void setAccountBalanceObjectDao(AccountBalanceObjectDao accountBalanceObjectDao) {
230        this.accountBalanceObjectDao = accountBalanceObjectDao;
231    }
232}