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