001/*
002 * Copyright 2005 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;
019
020import org.apache.commons.lang.StringUtils;
021import org.apache.ojb.broker.query.Criteria;
022import org.apache.ojb.broker.query.QueryByCriteria;
023import org.apache.ojb.broker.query.QueryFactory;
024import org.kuali.ole.gl.GeneralLedgerConstants;
025import org.kuali.ole.gl.businessobject.ExpenditureTransaction;
026import org.kuali.ole.gl.businessobject.Transaction;
027import org.kuali.ole.gl.dataaccess.ExpenditureTransactionDao;
028import org.kuali.ole.sys.OLEPropertyConstants;
029import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030
031/**
032 * The OJB implmentation of ExpenditureTransactionDao
033 */
034public 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.ole.gl.dataaccess.ExpenditureTransactionDao#getByTransaction(org.kuali.ole.gl.businessobject.Transaction)
051     */
052    public ExpenditureTransaction getByTransaction(Transaction t) {
053        LOG.debug("getByTransaction() started");
054
055        Criteria crit = new Criteria();
056        crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
057        crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
058        crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
059        crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
060        crit.addEqualTo(OLEPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
061        crit.addEqualTo(OLEPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
062        crit.addEqualTo(OLEPropertyConstants.BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
063        crit.addEqualTo(OLEPropertyConstants.OBJECT_TYPE_CODE, t.getFinancialObjectTypeCode());
064        crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_ACCOUNTING_PERIOD, t.getUniversityFiscalPeriodCode());
065        crit.addEqualTo(OLEPropertyConstants.PROJECT_CODE, t.getProjectCode());
066
067        if (StringUtils.isBlank(t.getOrganizationReferenceId())) {
068            crit.addEqualTo(OLEPropertyConstants.ORGANIZATION_REFERENCE_ID, GeneralLedgerConstants.getDashOrganizationReferenceId());
069        }
070        else {
071            crit.addEqualTo(OLEPropertyConstants.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.ole.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.ole.gl.dataaccess.ExpenditureTransactionDao#delete(org.kuali.ole.gl.businessobject.ExpenditureTransaction)
103     */
104    public void delete(ExpenditureTransaction et) {
105        LOG.debug("delete() started");
106
107        getPersistenceBrokerTemplate().delete(et);
108    }
109
110    /**
111     * Since expenditure transactions are temporary, just like flies that live for a mere day, this method removes all of the
112     * currently existing expenditure transactions from the database, all expenditure transactions having run through the poster and
113     * fulfilled their lifecycle
114     * 
115     * @see org.kuali.ole.gl.dataaccess.ExpenditureTransactionDao#deleteAllExpenditureTransactions()
116     */
117    public void deleteAllExpenditureTransactions() {
118        LOG.debug("deleteAllExpenditureTransactions() started");
119        try{
120            Iterator<ExpenditureTransaction> i = getAllExpenditureTransactions();
121            while (i.hasNext()) {
122                ExpenditureTransaction et = i.next();
123                if (LOG.isInfoEnabled()) {
124                    LOG.info("The following ExpenditureTransaction was deleted: " + et.toString());
125                }
126                delete(et);
127            }
128        }
129        catch (Exception e) {
130            throw new RuntimeException(e);
131        }   
132    }
133}