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.math.BigDecimal;
019    import java.sql.Date;
020    import java.util.Iterator;
021    
022    import org.apache.ojb.broker.metadata.ClassDescriptor;
023    import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
024    import org.apache.ojb.broker.metadata.DescriptorRepository;
025    import org.apache.ojb.broker.metadata.MetadataManager;
026    import org.apache.ojb.broker.query.Criteria;
027    import org.apache.ojb.broker.query.QueryByCriteria;
028    import org.apache.ojb.broker.query.QueryFactory;
029    import org.apache.ojb.broker.query.ReportQueryByCriteria;
030    import org.kuali.kfs.gl.businessobject.CollectorDetail;
031    import org.kuali.kfs.gl.dataaccess.CollectorDetailDao;
032    import org.kuali.kfs.sys.KFSPropertyConstants;
033    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
034    import org.kuali.rice.kns.exception.ClassNotPersistableException;
035    
036    /**
037     * An OJB implementation of the CollectorDetailDao
038     */
039    public 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.kfs.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(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
060            criteria.addLessThan(KFSPropertyConstants.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         * Save specific collector detail
072         * @param detail the CollectorDetail to save
073         * @see org.kuali.kfs.gl.dataaccess.CollectorDetailDao#save(org.kuali.kfs.gl.businessobject.CollectorDetail)
074         */
075        public void save(CollectorDetail detail) {
076            LOG.debug("saveOriginEntry() started");
077    
078            getPersistenceBrokerTemplate().store(detail);
079        }
080    
081        /**
082         * Retrieves the DB table name that's mapped to instances of CollectorDetail by finding the class descriptor name from the
083         * class descriptor respository 
084         * @return the table name where collector details are saved to
085         * @see org.kuali.kfs.gl.dataaccess.CollectorDetailDao#retrieveCollectorDetailTableName()
086         */
087        public String retrieveCollectorDetailTableName() {
088            ClassDescriptor classDescriptor = null;
089            DescriptorRepository globalRepository = descriptorRepository;
090            try {
091                classDescriptor = globalRepository.getDescriptorFor(CollectorDetail.class);
092            }
093            catch (ClassNotPersistenceCapableException e) {
094                throw new ClassNotPersistableException("class '" + CollectorDetail.class.getName() + "' is not persistable", e);
095            }
096    
097            return classDescriptor.getFullTableName();
098        }
099    
100    
101        public Integer getMaxCreateSequence(Date date) {
102            Criteria crit = new Criteria();
103            crit.addEqualTo("CREATE_DT", date);
104    
105            ReportQueryByCriteria q = QueryFactory.newReportQuery(CollectorDetail.class, crit);
106            q.setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
107    
108            Iterator<Object[]> iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
109            if (iter.hasNext()) {
110                Object[] result = iter.next();
111                if (result[0] != null) {
112                    return new Integer(((BigDecimal)result[0]).intValue());
113                }
114            }
115            return null;
116        }
117        
118        
119    }