1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
84
85
86
87
88
89 try {
90 return nextAvailableSequenceNumber(sequenceName, clazz);
91 }
92 catch ( ConfigurationException e ) {
93
94 return nextAvailableSequenceNumber(sequenceName);
95 }
96 }
97
98
99
100
101 public Long getNextAvailableSequenceNumber(String sequenceName) {
102
103 return nextAvailableSequenceNumber(sequenceName);
104 }
105
106 private KualiModuleService getKualiModuleService() {
107 if ( kualiModuleService == null )
108 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
109 return kualiModuleService;
110 }
111 }