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