Coverage Report - org.kuali.rice.krad.dao.proxy.LookupDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupDaoProxy
0%
0/42
0%
0/20
2.556
 
 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.dao.proxy;
 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.framework.persistence.jpa.OrmUtils;
 22  
 import org.kuali.rice.krad.bo.ModuleConfiguration;
 23  
 import org.kuali.rice.krad.dao.LookupDao;
 24  
 import org.kuali.rice.krad.dao.impl.LookupDaoJpa;
 25  
 import org.kuali.rice.krad.dao.impl.LookupDaoOjb;
 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  
 import org.springframework.transaction.annotation.Transactional;
 31  
 
 32  
 import javax.persistence.EntityManager;
 33  
 import java.util.Collection;
 34  
 import java.util.Collections;
 35  
 import java.util.HashMap;
 36  
 import java.util.Map;
 37  
 
 38  
 @Transactional
 39  0
 public class LookupDaoProxy implements LookupDao {
 40  
     
 41  
         private LookupDao lookupDaoJpa;
 42  
         private LookupDao lookupDaoOjb;
 43  
     private static KualiModuleService kualiModuleService;
 44  0
     private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>());
 45  
         
 46  
     public void setLookupDaoJpa(LookupDao lookupDaoJpa) {
 47  0
                 this.lookupDaoJpa = lookupDaoJpa;
 48  0
         }
 49  
         
 50  
         public void setLookupDaoOjb(LookupDao lookupDaoOjb) {
 51  0
                 this.lookupDaoOjb = lookupDaoOjb;
 52  0
         }
 53  
         
 54  
     private LookupDao getDao(Class clazz) {
 55  0
         ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 56  0
         if (moduleService != null) {
 57  0
             ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
 58  0
             String dataSourceName = "";
 59  0
             EntityManager entityManager = null;
 60  0
             if (moduleConfig != null) {
 61  0
                 dataSourceName = moduleConfig.getDataSourceName();
 62  0
                 entityManager = moduleConfig.getEntityManager();
 63  
             }
 64  
 
 65  0
             if (StringUtils.isNotEmpty(dataSourceName)) {
 66  0
                 if (lookupDaoValues.get(dataSourceName) != null) {
 67  0
                     return lookupDaoValues.get(dataSourceName);
 68  
                 } else {         
 69  0
                     if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 70  
                         //using JPA               
 71  0
                             LookupDaoJpa classSpecificLookupDaoJpa = new LookupDaoJpa();
 72  0
                                 if (entityManager != null) {
 73  0
                                         classSpecificLookupDaoJpa.setEntityManager(entityManager);
 74  0
                                         classSpecificLookupDaoJpa.setPersistenceStructureService(
 75  
                                     KRADServiceLocator.getPersistenceStructureService());
 76  0
                                 classSpecificLookupDaoJpa.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
 77  0
                                         lookupDaoValues.put(dataSourceName, classSpecificLookupDaoJpa);
 78  0
                                         return classSpecificLookupDaoJpa;
 79  
                                 } else {
 80  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).");
 81  
                                 }
 82  
                                         } else {
 83  0
                                                 LookupDaoOjb classSpecificLookupDaoOjb = new LookupDaoOjb();
 84  0
                         classSpecificLookupDaoOjb.setJcdAlias(dataSourceName);
 85  0
                         classSpecificLookupDaoOjb.setPersistenceStructureService(
 86  
                                 KRADServiceLocator.getPersistenceStructureService());
 87  0
                         classSpecificLookupDaoOjb.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
 88  0
                         classSpecificLookupDaoOjb.setDataDictionaryService(
 89  
                                 KRADServiceLocatorWeb.getDataDictionaryService());
 90  0
                         lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
 91  0
                         return classSpecificLookupDaoOjb;
 92  
                     }
 93  
                 }
 94  
 
 95  
             }
 96  
         }
 97  
         //return lookupDaoJpa;
 98  0
         return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? lookupDaoJpa : lookupDaoOjb;
 99  
     }
 100  
     
 101  
         /**
 102  
          * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object)
 103  
          */
 104  
         public boolean createCriteria(Object example, String searchValue, String propertyName, Object criteria) {
 105  0
                 return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, criteria);
 106  
         }
 107  
 
 108  
         /**
 109  
          * @see org.kuali.rice.krad.dao.LookupDao#createCriteria(java.lang.Object, java.lang.String, java.lang.String, boolean, java.lang.Object)
 110  
          */
 111  
         public boolean createCriteria(Object example, String searchValue, String propertyName, boolean caseInsensitive, boolean treatWildcardsAndOperatorsAsLiteral, Object criteria) {
 112  0
                 return getDao(example.getClass()).createCriteria(example, searchValue, propertyName, caseInsensitive,
 113  
                 treatWildcardsAndOperatorsAsLiteral, criteria);
 114  
         }
 115  
 
 116  
         /**
 117  
          * @see org.kuali.rice.krad.dao.LookupDao#findCollectionBySearchHelper(java.lang.Class, java.util.Map, boolean, boolean)
 118  
          */
 119  
         public Collection findCollectionBySearchHelper(Class example, Map formProps, boolean unbounded, boolean usePrimaryKeyValuesOnly) {
 120  0
                 return getDao(example).findCollectionBySearchHelper(example, formProps, unbounded, usePrimaryKeyValuesOnly);
 121  
         }
 122  
 
 123  
         /**
 124  
          * @see org.kuali.rice.krad.dao.LookupDao#findCountByMap(java.lang.Object, java.util.Map)
 125  
          */
 126  
         public Long findCountByMap(Object example, Map formProps) {
 127  0
                 return getDao(example.getClass()).findCountByMap(example, formProps);
 128  
         }
 129  
 
 130  
         /**
 131  
          * @see org.kuali.rice.krad.dao.LookupDao#findObjectByMap(java.lang.Object, java.util.Map)
 132  
          */
 133  
         public Object findObjectByMap(Object example, Map formProps) {
 134  0
                 return getDao(example.getClass()).findObjectByMap(example, formProps);
 135  
         }
 136  
 
 137  
         private static KualiModuleService getKualiModuleService() {
 138  0
         if (kualiModuleService == null) {
 139  0
             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
 140  
         }
 141  0
         return kualiModuleService;
 142  
     }
 143  
 }