View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.gl.dataaccess.impl;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  import java.util.Iterator;
21  
22  import org.apache.ojb.broker.metadata.ClassDescriptor;
23  import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
24  import org.apache.ojb.broker.metadata.DescriptorRepository;
25  import org.apache.ojb.broker.metadata.MetadataManager;
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.QueryByCriteria;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.ole.gl.businessobject.CollectorDetail;
31  import org.kuali.ole.gl.dataaccess.CollectorDetailDao;
32  import org.kuali.ole.sys.OLEPropertyConstants;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  import org.kuali.rice.krad.exception.ClassNotPersistableException;
35  
36  /**
37   * An OJB implementation of the CollectorDetailDao
38   */
39  public class CollectorDetailDaoOjb extends PlatformAwareDaoBaseOjb implements CollectorDetailDao {
40      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CollectorDetailDaoOjb.class);
41  
42      private DescriptorRepository descriptorRepository;
43  
44      public CollectorDetailDaoOjb() {
45          MetadataManager metadataManager = MetadataManager.getInstance();
46          descriptorRepository = metadataManager.getGlobalRepository();
47      }
48  
49      /**
50       * Purge the table by year/chart.  Clears persistence broker template at the end to ensure OJB has to to DB again
51       * to retrieve the post-purged state of the DB. 
52       * 
53       * @see org.kuali.ole.gl.dataaccess.CollectorDetailDao#purgeYearByChart(java.lang.String, int)
54       */
55      public void purgeYearByChart(String chartOfAccountsCode, int universityFiscalYear) {
56          LOG.debug("purgeYearByChart() started");
57  
58          Criteria criteria = new Criteria();
59          criteria.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
60          criteria.addLessThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(universityFiscalYear));
61          
62          getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(CollectorDetail.class, criteria));
63  
64          // This is required because if any deleted items are in the cache, deleteByQuery doesn't
65          // remove them from the cache so a future select will retrieve these deleted account balances from
66          // the cache and return them. Clearing the cache forces OJB to go to the database again.
67          getPersistenceBrokerTemplate().clearCache();
68      }
69  
70      /**
71       * Retrieves the DB table name that's mapped to instances of CollectorDetail by finding the class descriptor name from the
72       * class descriptor respository 
73       * @return the table name where collector details are saved to
74       * @see org.kuali.ole.gl.dataaccess.CollectorDetailDao#retrieveCollectorDetailTableName()
75       */
76      public String retrieveCollectorDetailTableName() {
77          ClassDescriptor classDescriptor = null;
78          DescriptorRepository globalRepository = descriptorRepository;
79          try {
80              classDescriptor = globalRepository.getDescriptorFor(CollectorDetail.class);
81          }
82          catch (ClassNotPersistenceCapableException e) {
83              throw new ClassNotPersistableException("class '" + CollectorDetail.class.getName() + "' is not persistable", e);
84          }
85  
86          return classDescriptor.getFullTableName();
87      }
88  
89  
90      public Integer getMaxCreateSequence(Date date) {
91          Criteria crit = new Criteria();
92          crit.addEqualTo("CREATE_DT", date);
93  
94          ReportQueryByCriteria q = QueryFactory.newReportQuery(CollectorDetail.class, crit);
95          q.setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
96  
97          Iterator<Object[]> iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
98          if (iter.hasNext()) {
99              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 }