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