Coverage Report - org.kuali.rice.krad.criteria.CriteriaLookupDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
CriteriaLookupDaoProxy
0%
0/34
0%
0/20
3.333
 
 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  0
 public class CriteriaLookupDaoProxy implements CriteriaLookupDao {
 37  
     CriteriaLookupDao criteriaLookupDaoOjb;
 38  
     CriteriaLookupDao criteriaLookupDaoJpa;
 39  
     private static KualiModuleService kualiModuleService;
 40  0
     private static Map<String, CriteriaLookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, CriteriaLookupDao>());
 41  
 
 42  
     public void setCriteriaLookupDaoJpa(CriteriaLookupDao lookupDaoJpa) {
 43  0
                 this.criteriaLookupDaoJpa = lookupDaoJpa;
 44  0
         }
 45  
 
 46  
         public void setCriteriaLookupDaoOjb(CriteriaLookupDao lookupDaoOjb) {
 47  0
                 this.criteriaLookupDaoOjb = lookupDaoOjb;
 48  0
         }
 49  
 
 50  
     private CriteriaLookupDao getDao(Class clazz) {
 51  0
         ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 52  0
         if (moduleService != null) {
 53  0
             ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
 54  0
             String dataSourceName = "";
 55  0
             EntityManager entityManager = null;
 56  0
             if (moduleConfig != null) {
 57  0
                 dataSourceName = moduleConfig.getDataSourceName();
 58  0
                 entityManager = moduleConfig.getEntityManager();
 59  
             }
 60  
 
 61  0
             if (StringUtils.isNotEmpty(dataSourceName)) {
 62  0
                 if (lookupDaoValues.get(dataSourceName) != null) {
 63  0
                     return lookupDaoValues.get(dataSourceName);
 64  
                 } else {
 65  0
                     if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 66  
                         //using JPA
 67  0
                             CriteriaLookupDaoJpa classSpecificLookupDaoJpa = new CriteriaLookupDaoJpa();
 68  0
                                 if (entityManager != null) {
 69  0
                                         classSpecificLookupDaoJpa.setEntityManager(entityManager);
 70  0
                                         lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
 71  0
                                         return classSpecificLookupDaoJpa;
 72  
                                 } else {
 73  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).");
 74  
                                 }
 75  
                                         } else {
 76  0
                                                 CriteriaLookupDaoOjb classSpecificLookupDaoOjb = new CriteriaLookupDaoOjb();
 77  0
                         classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
 78  0
                         lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
 79  0
                         return classSpecificLookupDaoOjb;
 80  
                     }
 81  
                 }
 82  
 
 83  
             }
 84  
         }
 85  
         //return lookupDaoJpa;
 86  0
         return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? criteriaLookupDaoJpa : criteriaLookupDaoOjb;
 87  
     }
 88  
 
 89  
     @Override
 90  
     public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria) {
 91  0
         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  0
         return getDao(queryClass).lookup(queryClass, criteria, customizer);
 98  
     }
 99  
 
 100  
     private static KualiModuleService getKualiModuleService() {
 101  0
         if (kualiModuleService == null) {
 102  0
             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
 103  
         }
 104  0
         return kualiModuleService;
 105  
     }
 106  
 }