View Javadoc
1   /**
2    * Copyright 2005-2014 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.api.config.property.ConfigContext;
23  import org.kuali.rice.core.framework.persistence.jdbc.dao.PlatformAwareDaoBaseJdbc;
24  import org.kuali.rice.krad.bo.ModuleConfiguration;
25  import org.kuali.rice.krad.dao.SequenceAccessorDao;
26  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
27  import org.kuali.rice.krad.service.KRADServiceLocator;
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.kuali.rice.krad.util.KRADConstants;
32  import org.kuali.rice.krad.util.LegacyUtils;
33  import org.springmodules.orm.ojb.OjbFactoryUtils;
34  
35  import javax.sql.DataSource;
36  
37  /**
38   * This class uses the KualiDBPlatform to get the next number from a given sequence.
39   */
40  @Deprecated
41  public class SequenceAccessorDaoJdbc extends PlatformAwareDaoBaseJdbc implements SequenceAccessorDao {
42  	private KualiModuleService kualiModuleService;
43  
44  	private Long nextAvailableSequenceNumber(String sequenceName, Class clazz) {
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          String dataSourceName = moduleConfig.getDataSourceName();
54  
55          if (StringUtils.isEmpty(dataSourceName)) {
56              return nextAvailableSequenceNumber(sequenceName);
57          } else {
58              PBKey key = new PBKey(dataSourceName);
59              PersistenceBroker broker = OjbFactoryUtils.getPersistenceBroker(key, false);
60  
61              if (broker != null) {
62                  return getDbPlatform().getNextValSQL(sequenceName, broker);
63              } else {
64                  throw new ConfigurationException("PersistenceBroker is null");
65              }
66          }
67  	}
68  
69      private Long nextAvailableSequenceNumber(String sequenceName) {
70          DataSource dataSource = (DataSource) ConfigContext.getCurrentContextConfig().getObject(KRADConstants.KRAD_APPLICATION_DATASOURCE);
71          if (dataSource == null) {
72              dataSource = KRADServiceLocator.getKradApplicationDataSource();
73          }
74          return Long.valueOf(MaxValueIncrementerFactory.getIncrementer(dataSource, sequenceName).nextLongValue());
75      }
76  	
77  	public Long getNextAvailableSequenceNumber(String sequenceName, Class clazz) {
78          if (!LegacyUtils.useLegacy(clazz)) {
79              throw new ConfigurationException("SequenceAccessorService should not be used with new data framework! Use "
80                      + MaxValueIncrementerFactory.class.getName() + " instead.");
81          }
82  
83  		// There are situations where a module hasn't been configured with
84  		// a dataSource.  In these cases, this method would have previously
85  		// thrown an error.  Instead, we've opted to factor out the code,
86  		// catch any configuration-related exceptions, and if one occurs,
87  		// attempt to use the dataSource associated with KNS. -- tbradford
88  		
89  		try {
90  			return nextAvailableSequenceNumber(sequenceName, clazz);
91  		}
92  		catch ( ConfigurationException e  ) {
93  	    	// Use kradApplication.datasource to get the dataSource associated with KNS
94  			return nextAvailableSequenceNumber(sequenceName);
95  		}
96  	}
97  	
98      /**
99       * @see org.kuali.rice.krad.dao.SequenceAccessorDao#getNextAvailableSequenceNumber(java.lang.String)
100      */
101     public Long getNextAvailableSequenceNumber(String sequenceName) {
102     	// Use kradApplication.datasource to get the dataSource associated with KNS
103     	return nextAvailableSequenceNumber(sequenceName);
104     }
105     
106     private KualiModuleService getKualiModuleService() {
107         if ( kualiModuleService == null ) 
108             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
109         return kualiModuleService;
110     }
111 }