1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kns.dao.proxy; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
21 | |
import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; |
22 | |
import org.kuali.rice.kns.bo.BusinessObject; |
23 | |
import org.kuali.rice.kns.bo.ModuleConfiguration; |
24 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
25 | |
import org.kuali.rice.kns.dao.BusinessObjectDao; |
26 | |
import org.kuali.rice.kns.dao.impl.BusinessObjectDaoJpa; |
27 | |
import org.kuali.rice.kns.dao.impl.BusinessObjectDaoOjb; |
28 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
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.springframework.transaction.annotation.Transactional; |
33 | |
|
34 | |
import javax.persistence.EntityManager; |
35 | |
import java.util.ArrayList; |
36 | |
import java.util.Collection; |
37 | |
import java.util.HashMap; |
38 | |
import java.util.List; |
39 | |
import java.util.Map; |
40 | |
|
41 | |
@Transactional |
42 | 0 | public class BusinessObjectDaoProxy implements BusinessObjectDao { |
43 | |
|
44 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class); |
45 | |
|
46 | |
private BusinessObjectDao businessObjectDaoJpa; |
47 | |
private BusinessObjectDao businessObjectDaoOjb; |
48 | |
private static KualiModuleService kualiModuleService; |
49 | 0 | private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>(); |
50 | |
|
51 | |
private BusinessObjectDao 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 (boDaoValues.get(dataSourceName) != null) { |
64 | 0 | return boDaoValues.get(dataSourceName); |
65 | |
} else { |
66 | 0 | if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) { |
67 | |
|
68 | 0 | if (entityManager != null) { |
69 | 0 | BusinessObjectDaoJpa boDaoJpa = |
70 | |
new BusinessObjectDaoJpa(entityManager, KNSServiceLocator.getPersistenceStructureService()); |
71 | |
|
72 | 0 | boDaoValues.put(dataSourceName, boDaoJpa); |
73 | 0 | return boDaoJpa; |
74 | |
} else { |
75 | 0 | throw new ConfigurationException("EntityManager is null. EntityManager must be set in the Module Configuration bean in the appropriate spring beans xml. (see nested exception for details)."); |
76 | |
} |
77 | |
} else { |
78 | |
|
79 | 0 | BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(KNSServiceLocator.getPersistenceStructureService()); |
80 | 0 | boDaoOjb.setJcdAlias(dataSourceName); |
81 | |
|
82 | 0 | boDaoValues.put(dataSourceName, boDaoOjb); |
83 | 0 | return boDaoOjb; |
84 | |
} |
85 | |
} |
86 | |
|
87 | |
} |
88 | |
} |
89 | |
|
90 | 0 | return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? businessObjectDaoJpa : businessObjectDaoOjb; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public int countMatching(Class clazz, Map<String, ?> fieldValues) { |
97 | 0 | return getDao(clazz).countMatching(clazz, fieldValues); |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) { |
104 | 0 | return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues); |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public void delete(PersistableBusinessObject bo) { |
111 | 0 | if (bo != null) { |
112 | 0 | getDao(bo.getClass()).delete(bo); |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void delete(List<? extends PersistableBusinessObject> boList) { |
120 | 0 | if (!boList.isEmpty()) { |
121 | 0 | getDao(boList.get(0).getClass()).delete(boList); |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void deleteMatching(Class clazz, Map<String, ?> fieldValues) { |
129 | 0 | getDao(clazz).deleteMatching(clazz, fieldValues); |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) { |
136 | 0 | return getDao(clazz).findAll(clazz); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz) { |
143 | 0 | return getDao(clazz).findAllActive(clazz); |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) { |
150 | 0 | return getDao(clazz).findAllInactive(clazz); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending) { |
157 | 0 | return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending); |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) { |
164 | 0 | return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending); |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) { |
171 | 0 | return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey); |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) { |
178 | 0 | return getDao(clazz).findByPrimaryKey(clazz, primaryKeys); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) { |
185 | 0 | return getDao(clazz).findMatching(clazz, fieldValues); |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues) { |
206 | 0 | return getDao(clazz).findMatchingActive(clazz, fieldValues); |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) { |
213 | 0 | return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending); |
214 | |
} |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public PersistableBusinessObject retrieve(PersistableBusinessObject object) { |
220 | 0 | return getDao(object.getClass()).retrieve(object); |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) { |
228 | 0 | return getDao(bo.getClass()).manageReadOnly(bo); |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
public <T extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject) { |
236 | 0 | return getDao(clazz).findByPrimaryKeyUsingKeyObject(clazz, pkObject); |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public PersistableBusinessObject save(PersistableBusinessObject bo) { |
243 | |
PersistableBusinessObject savedBo; |
244 | 0 | savedBo = getDao(bo.getClass()).save(bo); |
245 | 0 | return savedBo; |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public List<? extends PersistableBusinessObject> save(List businessObjects) { |
252 | 0 | if (!businessObjects.isEmpty()) { |
253 | 0 | return getDao(businessObjects.get(0).getClass()).save(businessObjects); |
254 | |
} |
255 | 0 | return new ArrayList<PersistableBusinessObject>(); |
256 | |
} |
257 | |
|
258 | |
private static KualiModuleService getKualiModuleService() { |
259 | 0 | if (kualiModuleService == null) { |
260 | 0 | kualiModuleService = KNSServiceLocatorWeb.getKualiModuleService(); |
261 | |
} |
262 | 0 | return kualiModuleService; |
263 | |
} |
264 | |
|
265 | |
public void setBusinessObjectDaoJpa(BusinessObjectDao businessObjectDaoJpa) { |
266 | 0 | this.businessObjectDaoJpa = businessObjectDaoJpa; |
267 | 0 | } |
268 | |
|
269 | |
public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) { |
270 | 0 | this.businessObjectDaoOjb = businessObjectDaoOjb; |
271 | 0 | } |
272 | |
} |