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