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    
020    import org.apache.commons.lang.StringUtils;
021    import org.apache.ojb.broker.query.Criteria;
022    import org.apache.ojb.broker.query.QueryByCriteria;
023    import org.apache.ojb.broker.query.QueryFactory;
024    import org.kuali.kfs.gl.GeneralLedgerConstants;
025    import org.kuali.kfs.gl.businessobject.ExpenditureTransaction;
026    import org.kuali.kfs.gl.businessobject.Transaction;
027    import org.kuali.kfs.gl.dataaccess.ExpenditureTransactionDao;
028    import org.kuali.kfs.sys.KFSPropertyConstants;
029    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
030    
031    /**
032     * The OJB implmentation of ExpenditureTransactionDao
033     */
034    public class ExpenditureTransactionDaoOjb extends PlatformAwareDaoBaseOjb implements ExpenditureTransactionDao {
035        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExpenditureTransactionDaoOjb.class);
036    
037        /**
038         * Constructs a ExpenditureTransactionDaoOjb instance
039         */
040        public ExpenditureTransactionDaoOjb() {
041            super();
042        }
043    
044        /**
045         * Queries the database to find the expenditure transaction in the database that would be affected if the given transaction is
046         * posted
047         * 
048         * @param t a transaction to find a related expenditure transaction for
049         * @return the expenditure transaction if found, null otherwise
050         * @see org.kuali.kfs.gl.dataaccess.ExpenditureTransactionDao#getByTransaction(org.kuali.kfs.gl.businessobject.Transaction)
051         */
052        public ExpenditureTransaction getByTransaction(Transaction t) {
053            LOG.debug("getByTransaction() started");
054    
055            Criteria crit = new Criteria();
056            crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
057            crit.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
058            crit.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
059            crit.addEqualTo(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
060            crit.addEqualTo(KFSPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
061            crit.addEqualTo(KFSPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
062            crit.addEqualTo(KFSPropertyConstants.BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
063            crit.addEqualTo(KFSPropertyConstants.OBJECT_TYPE_CODE, t.getFinancialObjectTypeCode());
064            crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_ACCOUNTING_PERIOD, t.getUniversityFiscalPeriodCode());
065            crit.addEqualTo(KFSPropertyConstants.PROJECT_CODE, t.getProjectCode());
066    
067            if (StringUtils.isBlank(t.getOrganizationReferenceId())) {
068                crit.addEqualTo(KFSPropertyConstants.ORGANIZATION_REFERENCE_ID, GeneralLedgerConstants.getDashOrganizationReferenceId());
069            }
070            else {
071                crit.addEqualTo(KFSPropertyConstants.ORGANIZATION_REFERENCE_ID, t.getOrganizationReferenceId());
072            }
073    
074            QueryByCriteria qbc = QueryFactory.newQuery(ExpenditureTransaction.class, crit);
075            return (ExpenditureTransaction) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
076        }
077    
078        /**
079         * Fetches all expenditure transactions currently in the database
080         * 
081         * @return an Iterator with all expenditure transactions from the database
082         * @see org.kuali.kfs.gl.dataaccess.ExpenditureTransactionDao#getAllExpenditureTransactions()
083         */
084        public Iterator getAllExpenditureTransactions() {
085            LOG.debug("getAllExpenditureTransactions() started");
086            try {
087                Criteria crit = new Criteria();
088                // We want them all so no criteria is added
089    
090                QueryByCriteria qbc = QueryFactory.newQuery(ExpenditureTransaction.class, crit);
091                return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
092            }
093            catch (Exception e) {
094                throw new RuntimeException(e);
095            }
096        }
097    
098        /**
099         * Deletes the given expenditure transaction
100         * 
101         * @param et the expenditure transaction that will be removed, as such, from the database
102         * @see org.kuali.kfs.gl.dataaccess.ExpenditureTransactionDao#delete(org.kuali.kfs.gl.businessobject.ExpenditureTransaction)
103         */
104        public void delete(ExpenditureTransaction et) {
105            LOG.debug("delete() started");
106    
107            getPersistenceBrokerTemplate().delete(et);
108        }
109    
110        /**
111         * Saves an expenditure transaction
112         * 
113         * @param et the expenditure transaction to save
114         * @see org.kuali.kfs.gl.dataaccess.ExpenditureTransactionDao#save(org.kuali.kfs.gl.businessobject.ExpenditureTransaction)
115         */
116        public void save(ExpenditureTransaction et) {
117            LOG.debug("save() started");
118    
119            getPersistenceBrokerTemplate().store(et);
120        }
121    
122        /**
123         * Since expenditure transactions are temporary, just like flies that live for a mere day, this method removes all of the
124         * currently existing expenditure transactions from the database, all expenditure transactions having run through the poster and
125         * fulfilled their lifecycle
126         * 
127         * @see org.kuali.kfs.gl.dataaccess.ExpenditureTransactionDao#deleteAllExpenditureTransactions()
128         */
129        public void deleteAllExpenditureTransactions() {
130            LOG.debug("deleteAllExpenditureTransactions() started");
131            try{
132                Iterator<ExpenditureTransaction> i = getAllExpenditureTransactions();
133                while (i.hasNext()) {
134                    ExpenditureTransaction et = i.next();
135                    if (LOG.isInfoEnabled()) {
136                        LOG.info("The following ExpenditureTransaction was deleted: " + et.toString());
137                    }
138                    delete(et);
139                }
140            }
141            catch (Exception e) {
142                throw new RuntimeException(e);
143            }   
144        }
145    }