View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.criteria;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.config.ConfigurationException;
21  import org.kuali.rice.core.api.criteria.GenericQueryResults;
22  import org.kuali.rice.core.api.criteria.LookupCustomizer;
23  import org.kuali.rice.core.api.criteria.QueryByCriteria;
24  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25  import org.kuali.rice.krad.bo.ModuleConfiguration;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28  import org.kuali.rice.krad.service.KualiModuleService;
29  import org.kuali.rice.krad.service.ModuleService;
30  
31  import javax.persistence.EntityManager;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  public class CriteriaLookupDaoProxy implements CriteriaLookupDao {
37      CriteriaLookupDao criteriaLookupDaoOjb;
38      CriteriaLookupDao criteriaLookupDaoJpa;
39      private static KualiModuleService kualiModuleService;
40      private static Map<String, CriteriaLookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, CriteriaLookupDao>());
41  
42      public void setCriteriaLookupDaoJpa(CriteriaLookupDao lookupDaoJpa) {
43  		this.criteriaLookupDaoJpa = lookupDaoJpa;
44  	}
45  
46  	public void setCriteriaLookupDaoOjb(CriteriaLookupDao lookupDaoOjb) {
47  		this.criteriaLookupDaoOjb = lookupDaoOjb;
48  	}
49  
50      private CriteriaLookupDao getDao(Class clazz) {
51          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
52          if (moduleService != null) {
53              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
54              String dataSourceName = "";
55              EntityManager entityManager = null;
56              if (moduleConfig != null) {
57                  dataSourceName = moduleConfig.getDataSourceName();
58                  entityManager = moduleConfig.getEntityManager();
59              }
60  
61              if (StringUtils.isNotEmpty(dataSourceName)) {
62                  if (lookupDaoValues.get(dataSourceName) != null) {
63                      return lookupDaoValues.get(dataSourceName);
64                  } else {
65                      if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
66                          //using JPA
67                  	    CriteriaLookupDaoJpa classSpecificLookupDaoJpa = new CriteriaLookupDaoJpa();
68                  		if (entityManager != null) {
69                  			classSpecificLookupDaoJpa.setEntityManager(entityManager);
70                  			lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
71                  			return classSpecificLookupDaoJpa;
72                  		} else {
73                  			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).");
74                  		}
75  					} else {
76  						CriteriaLookupDaoOjb classSpecificLookupDaoOjb = new CriteriaLookupDaoOjb();
77                          classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
78                          lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
79                          return classSpecificLookupDaoOjb;
80                      }
81                  }
82  
83              }
84          }
85          //return lookupDaoJpa;
86          return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? criteriaLookupDaoJpa : criteriaLookupDaoOjb;
87      }
88  
89      @Override
90      public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria) {
91          return getDao(queryClass).lookup(queryClass, criteria);
92      }
93  
94      @Override
95      public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria,
96              LookupCustomizer<T> customizer) {
97          return getDao(queryClass).lookup(queryClass, criteria, customizer);
98      }
99  
100     private static KualiModuleService getKualiModuleService() {
101         if (kualiModuleService == null) {
102             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
103         }
104         return kualiModuleService;
105     }
106 }