001/*
002 * Copyright 2007 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.math.BigDecimal;
019import java.sql.Date;
020import java.util.Iterator;
021
022import org.apache.ojb.broker.metadata.ClassDescriptor;
023import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
024import org.apache.ojb.broker.metadata.DescriptorRepository;
025import org.apache.ojb.broker.metadata.MetadataManager;
026import org.apache.ojb.broker.query.Criteria;
027import org.apache.ojb.broker.query.QueryByCriteria;
028import org.apache.ojb.broker.query.QueryFactory;
029import org.apache.ojb.broker.query.ReportQueryByCriteria;
030import org.kuali.ole.gl.businessobject.CollectorDetail;
031import org.kuali.ole.gl.dataaccess.CollectorDetailDao;
032import org.kuali.ole.sys.OLEPropertyConstants;
033import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
034import org.kuali.rice.krad.exception.ClassNotPersistableException;
035
036/**
037 * An OJB implementation of the CollectorDetailDao
038 */
039public class CollectorDetailDaoOjb extends PlatformAwareDaoBaseOjb implements CollectorDetailDao {
040    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CollectorDetailDaoOjb.class);
041
042    private DescriptorRepository descriptorRepository;
043
044    public CollectorDetailDaoOjb() {
045        MetadataManager metadataManager = MetadataManager.getInstance();
046        descriptorRepository = metadataManager.getGlobalRepository();
047    }
048
049    /**
050     * Purge the table by year/chart.  Clears persistence broker template at the end to ensure OJB has to to DB again
051     * to retrieve the post-purged state of the DB. 
052     * 
053     * @see org.kuali.ole.gl.dataaccess.CollectorDetailDao#purgeYearByChart(java.lang.String, int)
054     */
055    public void purgeYearByChart(String chartOfAccountsCode, int universityFiscalYear) {
056        LOG.debug("purgeYearByChart() started");
057
058        Criteria criteria = new Criteria();
059        criteria.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
060        criteria.addLessThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(universityFiscalYear));
061        
062        getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(CollectorDetail.class, criteria));
063
064        // This is required because if any deleted items are in the cache, deleteByQuery doesn't
065        // remove them from the cache so a future select will retrieve these deleted account balances from
066        // the cache and return them. Clearing the cache forces OJB to go to the database again.
067        getPersistenceBrokerTemplate().clearCache();
068    }
069
070    /**
071     * Retrieves the DB table name that's mapped to instances of CollectorDetail by finding the class descriptor name from the
072     * class descriptor respository 
073     * @return the table name where collector details are saved to
074     * @see org.kuali.ole.gl.dataaccess.CollectorDetailDao#retrieveCollectorDetailTableName()
075     */
076    public String retrieveCollectorDetailTableName() {
077        ClassDescriptor classDescriptor = null;
078        DescriptorRepository globalRepository = descriptorRepository;
079        try {
080            classDescriptor = globalRepository.getDescriptorFor(CollectorDetail.class);
081        }
082        catch (ClassNotPersistenceCapableException e) {
083            throw new ClassNotPersistableException("class '" + CollectorDetail.class.getName() + "' is not persistable", e);
084        }
085
086        return classDescriptor.getFullTableName();
087    }
088
089
090    public Integer getMaxCreateSequence(Date date) {
091        Criteria crit = new Criteria();
092        crit.addEqualTo("CREATE_DT", date);
093
094        ReportQueryByCriteria q = QueryFactory.newReportQuery(CollectorDetail.class, crit);
095        q.setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
096
097        Iterator<Object[]> iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
098        if (iter.hasNext()) {
099            Object[] result = iter.next();
100            if (result[0] != null) {
101                return new Integer(((BigDecimal)result[0]).intValue());
102            }
103        }
104        return null;
105    }
106    
107    
108}