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.Collection;
019    
020    import org.apache.ojb.broker.query.Criteria;
021    import org.apache.ojb.broker.query.QueryByCriteria;
022    import org.apache.ojb.broker.query.QueryFactory;
023    import org.kuali.kfs.gl.businessobject.SufficientFundBalances;
024    import org.kuali.kfs.gl.dataaccess.SufficientFundBalancesDao;
025    import org.kuali.kfs.sys.KFSPropertyConstants;
026    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
027    
028    /**
029     * An OJB implementation of the SufficientFundBalancesDao
030     */
031    public class SufficientFundBalancesDaoOjb extends PlatformAwareDaoBaseOjb implements SufficientFundBalancesDao {
032        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SufficientFundBalancesDaoOjb.class);
033    
034        /**
035         * Builds an OJB primary key query from the given parameters and returns the result
036         * 
037         * @param universityFiscalYear the university fiscal year of the sufficient funds balance to return
038         * @param chartOfAccountsCode the chart of accounts code of the sufficient funds balance to return
039         * @param accountNumber the account number of the sufficient funds balance to return
040         * @param financialObjectCode the object code of the sufficient funds balance to return
041         * @return the qualifying sufficient funds balance record, or null no suitable record can be found
042         * @see org.kuali.kfs.gl.dataaccess.SufficientFundBalancesDao#getByPrimaryId(java.lang.Integer, java.lang.String, java.lang.String, java.lang.String)
043         */
044        public SufficientFundBalances getByPrimaryId(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, String financialObjectCode) {
045            LOG.debug("getByPrimaryId() started");
046    
047            Criteria crit = new Criteria();
048            crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, universityFiscalYear);
049            crit.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
050            crit.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
051            crit.addEqualTo(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, financialObjectCode);
052    
053            QueryByCriteria qbc = QueryFactory.newQuery(SufficientFundBalances.class, crit);
054            return (SufficientFundBalances) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
055        }
056    
057        /**
058         * Saves a sufficient fund balance record
059         * @param sfb the sufficient funds balance record to save
060         * @see org.kuali.kfs.gl.dataaccess.SufficientFundBalancesDao#save(org.kuali.kfs.gl.businessobject.SufficientFundBalances)
061         */
062        public void save(SufficientFundBalances sfb) {
063            LOG.debug("save() started");
064    
065            getPersistenceBrokerTemplate().store(sfb);
066        }
067    
068        /**
069         * Builds an OJB query based on the parameter values and returns all the sufficient fund balances that match that record
070         * 
071         * @param universityFiscalYear the university fiscal year of sufficient fund balances to find
072         * @param chartOfAccountsCode the chart of accounts code of sufficient fund balances to find
073         * @param financialObjectCode the object code of sufficient fund balances to find
074         * @return a Collection of sufficient fund balances, qualified by the parameter values
075         * @see org.kuali.kfs.gl.dataaccess.SufficientFundBalancesDao#getByObjectCode(java.lang.Integer, java.lang.String, java.lang.String)
076         */
077        public Collection getByObjectCode(Integer universityFiscalYear, String chartOfAccountsCode, String financialObjectCode) {
078            LOG.debug("getByObjectCode() started");
079    
080            Criteria crit = new Criteria();
081            crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, universityFiscalYear);
082            crit.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
083            crit.addEqualTo(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, financialObjectCode);
084    
085            QueryByCriteria qbc = QueryFactory.newQuery(SufficientFundBalances.class, crit);
086            return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
087        }
088    
089        /**
090         * Deletes sufficient fund balances associated with a given year, chart, and account number
091         * 
092         * @param universityFiscalYear the university fiscal year of sufficient fund balances to delete
093         * @param chartOfAccountsCode the chart code of sufficient fund balances to delete
094         * @param accountNumber the account number of sufficient fund balances to delete
095         * @return the number of records deleted
096         * @see org.kuali.kfs.gl.dataaccess.SufficientFundBalancesDao#deleteByAccountNumber(java.lang.Integer, java.lang.String, java.lang.String)
097         */
098        public int deleteByAccountNumber(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber) {
099            LOG.debug("deleteByAccountNumber() started");
100            
101            Criteria crit = new Criteria();
102            crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, universityFiscalYear);
103            crit.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
104            crit.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
105    
106            QueryByCriteria qbc = QueryFactory.newQuery(SufficientFundBalances.class, crit);
107            int count = getPersistenceBrokerTemplate().getCount(qbc);
108            getPersistenceBrokerTemplate().deleteByQuery(qbc);
109    
110            // This has to be done because deleteByQuery deletes the rows from the table,
111            // but it doesn't delete them from the cache. If the cache isn't cleared,
112            // later on, you could get an Optimistic Lock Exception because OJB thinks rows
113            // exist when they really don't.
114            getPersistenceBrokerTemplate().clearCache();
115            
116            return count;
117        }
118    
119        /**
120         * This method should only be used in unit tests. It loads all the gl_sf_balances_t rows in memory into a collection. This won't
121         * sace for production.
122         * 
123         * @return a Collection of all sufficient funds records in the database
124         */
125        public Collection testingGetAllEntries() {
126            LOG.debug("testingGetAllEntries() started");
127    
128            Criteria criteria = new Criteria();
129            QueryByCriteria qbc = QueryFactory.newQuery(SufficientFundBalances.class, criteria);
130            qbc.addOrderBy(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, true);
131            qbc.addOrderBy(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, true);
132            qbc.addOrderBy(KFSPropertyConstants.ACCOUNT_NUMBER, true);
133            qbc.addOrderBy(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, true);
134    
135            return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
136        }
137    }