1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
36 | |
|
37 | 0 | 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 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); |
44 | 0 | if ( moduleService == null ) |
45 | 0 | throw new ConfigurationException("moduleService is null"); |
46 | |
|
47 | 0 | ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration(); |
48 | 0 | if ( moduleConfig == null ) |
49 | 0 | throw new ConfigurationException("moduleConfiguration is null"); |
50 | |
|
51 | 0 | String dataSourceName = moduleConfig.getDataSourceName(); |
52 | 0 | EntityManager entityManager = moduleConfig.getEntityManager(); |
53 | |
|
54 | 0 | if ( StringUtils.isEmpty(dataSourceName) ) |
55 | 0 | throw new ConfigurationException("dataSourceName is not set"); |
56 | |
|
57 | 0 | if ( OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled() ) { |
58 | 0 | if ( entityManager != null ) |
59 | 0 | return getDbPlatform().getNextValSQL(sequenceName, entityManager); |
60 | |
else |
61 | 0 | throw new ConfigurationException("EntityManager is null"); |
62 | |
} |
63 | |
else { |
64 | 0 | PBKey key = new PBKey(dataSourceName); |
65 | 0 | PersistenceBroker broker = OjbFactoryUtils.getPersistenceBroker(key, false); |
66 | 0 | if ( broker != null ) |
67 | 0 | return getDbPlatform().getNextValSQL(sequenceName, broker); |
68 | |
else |
69 | 0 | throw new ConfigurationException("PersistenceBroker is null"); |
70 | |
} |
71 | |
} |
72 | |
|
73 | |
public Long getNextAvailableSequenceNumber(String sequenceName, |
74 | |
Class<? extends BusinessObject> clazz) { |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
try { |
83 | 0 | return nextAvailableSequenceNumber(sequenceName, clazz); |
84 | |
} |
85 | 0 | catch ( ConfigurationException e ) { |
86 | |
|
87 | 0 | return nextAvailableSequenceNumber(sequenceName, DocumentHeader.class); |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public Long getNextAvailableSequenceNumber(String sequenceName) { |
95 | |
|
96 | 0 | return nextAvailableSequenceNumber(sequenceName, DocumentHeader.class); |
97 | |
} |
98 | |
|
99 | |
private KualiModuleService getKualiModuleService() { |
100 | 0 | if ( kualiModuleService == null ) |
101 | 0 | kualiModuleService = KNSServiceLocator.getKualiModuleService(); |
102 | 0 | return kualiModuleService; |
103 | |
} |
104 | |
} |