View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.gl.dataaccess.impl;
20  
21  import java.math.BigDecimal;
22  import java.sql.Date;
23  import java.util.Iterator;
24  
25  import org.apache.ojb.broker.metadata.ClassDescriptor;
26  import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
27  import org.apache.ojb.broker.metadata.DescriptorRepository;
28  import org.apache.ojb.broker.metadata.MetadataManager;
29  import org.apache.ojb.broker.query.Criteria;
30  import org.apache.ojb.broker.query.QueryByCriteria;
31  import org.apache.ojb.broker.query.QueryFactory;
32  import org.apache.ojb.broker.query.ReportQueryByCriteria;
33  import org.kuali.kfs.gl.businessobject.CollectorDetail;
34  import org.kuali.kfs.gl.dataaccess.CollectorDetailDao;
35  import org.kuali.kfs.sys.KFSPropertyConstants;
36  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
37  import org.kuali.rice.krad.exception.ClassNotPersistableException;
38  
39  /**
40   * An OJB implementation of the CollectorDetailDao
41   */
42  public class CollectorDetailDaoOjb extends PlatformAwareDaoBaseOjb implements CollectorDetailDao {
43      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CollectorDetailDaoOjb.class);
44  
45      private DescriptorRepository descriptorRepository;
46  
47      public CollectorDetailDaoOjb() {
48          MetadataManager metadataManager = MetadataManager.getInstance();
49          descriptorRepository = metadataManager.getGlobalRepository();
50      }
51  
52      /**
53       * Purge the table by year/chart.  Clears persistence broker template at the end to ensure OJB has to to DB again
54       * to retrieve the post-purged state of the DB. 
55       * 
56       * @see org.kuali.kfs.gl.dataaccess.CollectorDetailDao#purgeYearByChart(java.lang.String, int)
57       */
58      public void purgeYearByChart(String chartOfAccountsCode, int universityFiscalYear) {
59          LOG.debug("purgeYearByChart() started");
60  
61          Criteria criteria = new Criteria();
62          criteria.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
63          criteria.addLessThan(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(universityFiscalYear));
64          
65          getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(CollectorDetail.class, criteria));
66  
67          // This is required because if any deleted items are in the cache, deleteByQuery doesn't
68          // remove them from the cache so a future select will retrieve these deleted account balances from
69          // the cache and return them. Clearing the cache forces OJB to go to the database again.
70          getPersistenceBrokerTemplate().clearCache();
71      }
72  
73      /**
74       * Retrieves the DB table name that's mapped to instances of CollectorDetail by finding the class descriptor name from the
75       * class descriptor respository 
76       * @return the table name where collector details are saved to
77       * @see org.kuali.kfs.gl.dataaccess.CollectorDetailDao#retrieveCollectorDetailTableName()
78       */
79      public String retrieveCollectorDetailTableName() {
80          ClassDescriptor classDescriptor = null;
81          DescriptorRepository globalRepository = descriptorRepository;
82          try {
83              classDescriptor = globalRepository.getDescriptorFor(CollectorDetail.class);
84          }
85          catch (ClassNotPersistenceCapableException e) {
86              throw new ClassNotPersistableException("class '" + CollectorDetail.class.getName() + "' is not persistable", e);
87          }
88  
89          return classDescriptor.getFullTableName();
90      }
91  
92  
93      public Integer getMaxCreateSequence(Date date) {
94          Criteria crit = new Criteria();
95          crit.addEqualTo("CREATE_DT", date);
96  
97          ReportQueryByCriteria q = QueryFactory.newReportQuery(CollectorDetail.class, crit);
98          q.setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
99  
100         Iterator<Object[]> iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
101         if (iter.hasNext()) {
102             Object[] result = iter.next();
103             if (result[0] != null) {
104                 return new Integer(((BigDecimal)result[0]).intValue());
105             }
106         }
107         return null;
108     }
109     
110     
111 }