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