Coverage Report - org.kuali.rice.kns.dao.jdbc.SequenceAccessorDaoJdbc
 
Classes in this File Line Coverage Branch Coverage Complexity
SequenceAccessorDaoJdbc
0%
0/27
0%
0/16
5.25
 
 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.rice.kns.dao.jdbc;
 17  
 
 18  
 import javax.persistence.EntityManager;
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.apache.ojb.broker.PBKey;
 22  
 import org.apache.ojb.broker.PersistenceBroker;
 23  
 import org.kuali.rice.core.config.ConfigurationException;
 24  
 import org.kuali.rice.core.util.OrmUtils;
 25  
 import org.kuali.rice.kns.bo.BusinessObject;
 26  
 import org.kuali.rice.kns.bo.DocumentHeader;
 27  
 import org.kuali.rice.kns.bo.ModuleConfiguration;
 28  
 import org.kuali.rice.kns.dao.SequenceAccessorDao;
 29  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 30  
 import org.kuali.rice.kns.service.KualiModuleService;
 31  
 import org.kuali.rice.kns.service.ModuleService;
 32  
 import org.springmodules.orm.ojb.OjbFactoryUtils;
 33  
 
 34  
 /**
 35  
  * This class uses the KualiDBPlatform to get the next number from a given sequence.
 36  
  */
 37  0
 public class SequenceAccessorDaoJdbc extends PlatformAwareDaoBaseJdbc implements SequenceAccessorDao {
 38  
         private KualiModuleService kualiModuleService;
 39  
         
 40  
         private Long nextAvailableSequenceNumber(String sequenceName, 
 41  
                         Class<? extends BusinessObject> clazz) {
 42  
                 
 43  0
         ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 44  0
         if ( moduleService == null )
 45  0
                 throw new ConfigurationException("moduleService is null");
 46  
                                 
 47  0
         ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
 48  0
         if ( moduleConfig == null )
 49  0
                 throw new ConfigurationException("moduleConfiguration is null");
 50  
         
 51  0
             String dataSourceName = moduleConfig.getDataSourceName();
 52  0
             EntityManager entityManager = moduleConfig.getEntityManager();
 53  
 
 54  0
         if ( StringUtils.isEmpty(dataSourceName) ) 
 55  0
                 throw new ConfigurationException("dataSourceName is not set");               
 56  
                 
 57  0
             if ( OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled() ) {
 58  0
             if ( entityManager != null ) 
 59  0
                     return getDbPlatform().getNextValSQL(sequenceName, entityManager);
 60  
             else
 61  0
                     throw new ConfigurationException("EntityManager is null");
 62  
         } 
 63  
             else {
 64  0
                     PBKey key = new PBKey(dataSourceName);
 65  0
                     PersistenceBroker broker = OjbFactoryUtils.getPersistenceBroker(key, false);
 66  0
                     if ( broker != null )
 67  0
                             return getDbPlatform().getNextValSQL(sequenceName, broker);
 68  
                     else
 69  0
                             throw new ConfigurationException("PersistenceBroker is null");                                    
 70  
         }
 71  
         }
 72  
         
 73  
         public Long getNextAvailableSequenceNumber(String sequenceName, 
 74  
                         Class<? extends BusinessObject> clazz) {
 75  
                 
 76  
                 // There are situations where a module hasn't been configured with
 77  
                 // a dataSource.  In these cases, this method would have previously
 78  
                 // thrown an error.  Instead, we've opted to factor out the code,
 79  
                 // catch any configuration-related exceptions, and if one occurs,
 80  
                 // attempt to use the dataSource associated with KNS. -- tbradford
 81  
                 
 82  
                 try {
 83  0
                         return nextAvailableSequenceNumber(sequenceName, clazz);
 84  
                 }
 85  0
                 catch ( ConfigurationException e  ) {
 86  
                     // Use DocumentHeader to get the dataSourceName associated with KNS                        
 87  0
                         return nextAvailableSequenceNumber(sequenceName, DocumentHeader.class);                        
 88  
                 }
 89  
         }
 90  
         
 91  
     /**
 92  
      * @see org.kuali.rice.kns.dao.SequenceAccessorDao#getNextAvailableSequenceNumber(java.lang.String)
 93  
      */
 94  
     public Long getNextAvailableSequenceNumber(String sequenceName) {
 95  
             // Use DocumentHeader to get the dataSourceName associated with KNS
 96  0
             return nextAvailableSequenceNumber(sequenceName, DocumentHeader.class);
 97  
     }
 98  
     
 99  
     private KualiModuleService getKualiModuleService() {
 100  0
         if ( kualiModuleService == null ) 
 101  0
             kualiModuleService = KNSServiceLocator.getKualiModuleService();
 102  0
         return kualiModuleService;
 103  
     }
 104  
 }