1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.criteria;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.config.ConfigurationException;
20 import org.kuali.rice.core.api.criteria.GenericQueryResults;
21 import org.kuali.rice.core.api.criteria.LookupCustomizer;
22 import org.kuali.rice.core.api.criteria.QueryByCriteria;
23 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
24 import org.kuali.rice.krad.bo.ModuleConfiguration;
25 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26 import org.kuali.rice.krad.service.KualiModuleService;
27 import org.kuali.rice.krad.service.ModuleService;
28
29 import javax.persistence.EntityManager;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 public class CriteriaLookupDaoProxy implements CriteriaLookupDao {
35 CriteriaLookupDao criteriaLookupDaoOjb;
36 CriteriaLookupDao criteriaLookupDaoJpa;
37 private static KualiModuleService kualiModuleService;
38 private static Map<String, CriteriaLookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, CriteriaLookupDao>());
39
40 public void setCriteriaLookupDaoJpa(CriteriaLookupDao lookupDaoJpa) {
41 this.criteriaLookupDaoJpa = lookupDaoJpa;
42 }
43
44 public void setCriteriaLookupDaoOjb(CriteriaLookupDao lookupDaoOjb) {
45 this.criteriaLookupDaoOjb = lookupDaoOjb;
46 }
47
48 private CriteriaLookupDao getDao(Class clazz) {
49 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
50 if (moduleService != null) {
51 ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
52 String dataSourceName = "";
53 EntityManager entityManager = null;
54 if (moduleConfig != null) {
55 dataSourceName = moduleConfig.getDataSourceName();
56 entityManager = moduleConfig.getEntityManager();
57 }
58
59 if (StringUtils.isNotEmpty(dataSourceName)) {
60 if (lookupDaoValues.get(dataSourceName) != null) {
61 return lookupDaoValues.get(dataSourceName);
62 } else {
63 if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
64
65 CriteriaLookupDaoJpa classSpecificLookupDaoJpa = new CriteriaLookupDaoJpa();
66 if (entityManager != null) {
67 classSpecificLookupDaoJpa.setEntityManager(entityManager);
68 lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
69 return classSpecificLookupDaoJpa;
70 } else {
71 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).");
72 }
73 } else {
74 CriteriaLookupDaoOjb classSpecificLookupDaoOjb = new CriteriaLookupDaoOjb();
75 classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
76 lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
77 return classSpecificLookupDaoOjb;
78 }
79 }
80
81 }
82 }
83
84 return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? criteriaLookupDaoJpa : criteriaLookupDaoOjb;
85 }
86
87 @Override
88 public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria) {
89 return getDao(queryClass).lookup(queryClass, criteria);
90 }
91
92 @Override
93 public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria,
94 LookupCustomizer<T> customizer) {
95 return getDao(queryClass).lookup(queryClass, criteria, customizer);
96 }
97
98 private static KualiModuleService getKualiModuleService() {
99 if (kualiModuleService == null) {
100 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
101 }
102 return kualiModuleService;
103 }
104 }