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