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