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