View Javadoc

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.krad.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.krad.bo.BusinessObject;
26  import org.kuali.rice.krad.bo.DocumentHeader;
27  import org.kuali.rice.krad.bo.ModuleConfiguration;
28  import org.kuali.rice.krad.dao.SequenceAccessorDao;
29  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
30  import org.kuali.rice.krad.service.KualiModuleService;
31  import org.kuali.rice.krad.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  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          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
46          if ( moduleService == null )
47          	throw new ConfigurationException("moduleService is null");
48          	        	
49          ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
50          if ( moduleConfig == null )
51          	throw new ConfigurationException("moduleConfiguration is null");
52          
53      	if ( OrmUtils.isJpaAnnotated(clazz) && ( OrmUtils.isJpaEnabled() ||	OrmUtils.isJpaEnabled("rice.krad") ) ) {
54      		EntityManager entityManager = moduleConfig.getEntityManager();
55      		
56              if ( entityManager != null ) 
57              	return getDbPlatform().getNextValSQL(sequenceName, entityManager);
58              else
59              	throw new ConfigurationException("EntityManager is null");
60          } 
61      	else {
62      		String dataSourceName = moduleConfig.getDataSourceName();
63      		if ( StringUtils.isEmpty(dataSourceName) ) 
64              	throw new ConfigurationException("dataSourceName is not set");
65      		
66      		PBKey key = new PBKey(dataSourceName);
67      		PersistenceBroker broker = OjbFactoryUtils.getPersistenceBroker(key, false);
68      		if ( broker != null )
69      			return getDbPlatform().getNextValSQL(sequenceName, broker);
70      		else
71      			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  			return nextAvailableSequenceNumber(sequenceName, clazz);
86  		}
87  		catch ( ConfigurationException e  ) {
88  	    	// Use DocumentHeader to get the dataSourceName associated with KNS			
89  			return nextAvailableSequenceNumber(sequenceName, DocumentHeader.class);			
90  		}
91  	}
92  	
93      /**
94       * @see org.kuali.rice.krad.dao.SequenceAccessorDao#getNextAvailableSequenceNumber(java.lang.String)
95       */
96      public Long getNextAvailableSequenceNumber(String sequenceName) {
97      	// Use DocumentHeader to get the dataSourceName associated with KNS
98      	return nextAvailableSequenceNumber(sequenceName, DocumentHeader.class);
99      }
100     
101     private KualiModuleService getKualiModuleService() {
102         if ( kualiModuleService == null ) 
103             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
104         return kualiModuleService;
105     }
106 }