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.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  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          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
44          if ( moduleService == null )
45          	throw new ConfigurationException("moduleService is null");
46          	        	
47          ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
48          if ( moduleConfig == null )
49          	throw new ConfigurationException("moduleConfiguration is null");
50          
51      	String dataSourceName = moduleConfig.getDataSourceName();
52      	EntityManager entityManager = moduleConfig.getEntityManager();
53  
54          if ( StringUtils.isEmpty(dataSourceName) ) 
55          	throw new ConfigurationException("dataSourceName is not set");       	
56          	
57      	if ( OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled() ) {
58              if ( entityManager != null ) 
59              	return getDbPlatform().getNextValSQL(sequenceName, entityManager);
60              else
61              	throw new ConfigurationException("EntityManager is null");
62          } 
63      	else {
64      		PBKey key = new PBKey(dataSourceName);
65      		PersistenceBroker broker = OjbFactoryUtils.getPersistenceBroker(key, false);
66      		if ( broker != null )
67      			return getDbPlatform().getNextValSQL(sequenceName, broker);
68      		else
69      			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  			return nextAvailableSequenceNumber(sequenceName, clazz);
84  		}
85  		catch ( ConfigurationException e  ) {
86  	    	// Use DocumentHeader to get the dataSourceName associated with KNS			
87  			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      	return nextAvailableSequenceNumber(sequenceName, DocumentHeader.class);
97      }
98      
99      private KualiModuleService getKualiModuleService() {
100         if ( kualiModuleService == null ) 
101             kualiModuleService = KNSServiceLocator.getKualiModuleService();
102         return kualiModuleService;
103     }
104 }