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