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