1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.dao.proxy; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
|
20 | |
import javax.persistence.EntityManager; |
21 | |
|
22 | |
import org.apache.commons.lang.StringUtils; |
23 | |
import org.kuali.rice.core.config.ConfigurationException; |
24 | |
import org.kuali.rice.core.util.OrmUtils; |
25 | |
import org.kuali.rice.kns.bo.ModuleConfiguration; |
26 | |
import org.kuali.rice.kns.dao.PersistenceDao; |
27 | |
import org.kuali.rice.kns.dao.impl.PersistenceDaoJpa; |
28 | |
import org.kuali.rice.kns.dao.impl.PersistenceDaoOjb; |
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.springframework.transaction.annotation.Transactional; |
33 | |
|
34 | |
@Transactional |
35 | 0 | public class PersistenceDaoProxy implements PersistenceDao { |
36 | |
|
37 | |
private PersistenceDao persistenceDaoJpa; |
38 | |
private PersistenceDao persistenceDaoOjb; |
39 | |
private static KualiModuleService kualiModuleService; |
40 | 0 | private static HashMap<String, PersistenceDao> persistenceDaoValues = new HashMap<String, PersistenceDao>(); |
41 | |
|
42 | |
public void setPersistenceDaoJpa(PersistenceDao persistenceDaoJpa) { |
43 | 0 | this.persistenceDaoJpa = persistenceDaoJpa; |
44 | 0 | } |
45 | |
|
46 | |
public void setPersistenceDaoOjb(PersistenceDao persistenceDaoOjb) { |
47 | 0 | this.persistenceDaoOjb = persistenceDaoOjb; |
48 | 0 | } |
49 | |
|
50 | |
private PersistenceDao getDao(Class clazz) { |
51 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); |
52 | 0 | if (moduleService != null) { |
53 | 0 | ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration(); |
54 | 0 | String dataSourceName = ""; |
55 | 0 | EntityManager entityManager = null; |
56 | 0 | if (moduleConfig != null) { |
57 | 0 | dataSourceName = moduleConfig.getDataSourceName(); |
58 | 0 | entityManager = moduleConfig.getEntityManager(); |
59 | |
} |
60 | |
|
61 | 0 | if (StringUtils.isNotEmpty(dataSourceName)) { |
62 | 0 | if (persistenceDaoValues.get(dataSourceName) != null) { |
63 | 0 | return persistenceDaoValues.get(dataSourceName); |
64 | |
} else { |
65 | 0 | if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) { |
66 | |
|
67 | 0 | persistenceDaoValues.put(dataSourceName, persistenceDaoJpa); |
68 | 0 | return persistenceDaoJpa; |
69 | |
|
70 | |
} else { |
71 | |
|
72 | 0 | PersistenceDaoOjb persistDaoOjb = new PersistenceDaoOjb(); |
73 | 0 | persistDaoOjb.setJcdAlias(dataSourceName); |
74 | |
|
75 | 0 | persistenceDaoValues.put(dataSourceName, persistDaoOjb); |
76 | 0 | return persistDaoOjb; |
77 | |
} |
78 | |
|
79 | |
} |
80 | |
|
81 | |
} |
82 | |
} |
83 | 0 | return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? persistenceDaoJpa : persistenceDaoOjb; |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void clearCache() { |
90 | 0 | if (OrmUtils.isJpaEnabled()) { |
91 | 0 | persistenceDaoJpa.clearCache(); |
92 | |
} else { |
93 | 0 | persistenceDaoOjb.clearCache(); |
94 | |
} |
95 | 0 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public Object resolveProxy(Object o) { |
101 | 0 | return getDao(o.getClass()).resolveProxy(o); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void retrieveAllReferences(Object o) { |
108 | 0 | getDao(o.getClass()).retrieveAllReferences(o); |
109 | 0 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public void retrieveReference(Object o, String referenceName) { |
115 | 0 | getDao(o.getClass()).retrieveReference(o, referenceName); |
116 | 0 | } |
117 | |
|
118 | |
private static KualiModuleService getKualiModuleService() { |
119 | 0 | if (kualiModuleService == null) { |
120 | 0 | kualiModuleService = KNSServiceLocator.getKualiModuleService(); |
121 | |
} |
122 | 0 | return kualiModuleService; |
123 | |
} |
124 | |
} |