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 2006-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  
 
 17  
 package org.kuali.rice.kns.dao.proxy;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.api.config.ConfigurationException;
 21  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 22  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 23  
 import org.kuali.rice.kns.bo.ModuleConfiguration;
 24  
 import org.kuali.rice.kns.dao.LookupDao;
 25  
 import org.kuali.rice.kns.dao.impl.LookupDaoJpa;
 26  
 import org.kuali.rice.kns.dao.impl.LookupDaoOjb;
 27  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 28  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 29  
 import org.kuali.rice.kns.service.KualiModuleService;
 30  
 import org.kuali.rice.kns.service.ModuleService;
 31  
 import org.springframework.transaction.annotation.Transactional;
 32  
 
 33  
 import javax.persistence.EntityManager;
 34  
 import java.util.Collection;
 35  
 import java.util.Collections;
 36  
 import java.util.HashMap;
 37  
 import java.util.Map;
 38  
 
 39  
 @Transactional
 40  0
 public class LookupDaoProxy implements LookupDao {
 41  
     
 42  
         private LookupDao lookupDaoJpa;
 43  
         private LookupDao lookupDaoOjb;
 44  
     private static KualiModuleService kualiModuleService;
 45  0
     private static Map<String, LookupDao> lookupDaoValues = Collections.synchronizedMap(new HashMap<String, LookupDao>());
 46  
         
 47  
     public void setLookupDaoJpa(LookupDao lookupDaoJpa) {
 48  0
                 this.lookupDaoJpa = lookupDaoJpa;
 49  0
         }
 50  
         
 51  
         public void setLookupDaoOjb(LookupDao lookupDaoOjb) {
 52  0
                 this.lookupDaoOjb = lookupDaoOjb;
 53  0
         }
 54  
         
 55  
     private LookupDao getDao(Class clazz) {
 56  0
         ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 57  0
         if (moduleService != null) {
 58  0
             ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
 59  0
             String dataSourceName = "";
 60  0
             EntityManager entityManager = null;
 61  0
             if (moduleConfig != null) {
 62  0
                 dataSourceName = moduleConfig.getDataSourceName();
 63  0
                 entityManager = moduleConfig.getEntityManager();
 64  
             }
 65  
 
 66  0
             if (StringUtils.isNotEmpty(dataSourceName)) {
 67  0
                 if (lookupDaoValues.get(dataSourceName) != null) {
 68  0
                     return lookupDaoValues.get(dataSourceName);
 69  
                 } else {         
 70  0
                     if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 71  
                         //using JPA               
 72  0
                             LookupDaoJpa classSpecificLookupDaoJpa = new LookupDaoJpa();
 73  0
                                 if (entityManager != null) {
 74  0
                                         classSpecificLookupDaoJpa.setEntityManager(entityManager);
 75  0
                                         classSpecificLookupDaoJpa.setPersistenceStructureService(KNSServiceLocator.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(KNSServiceLocator.getPersistenceStructureService());
 86  0
                         classSpecificLookupDaoOjb.setDateTimeService(CoreApiServiceLocator.getDateTimeService());
 87  0
                         classSpecificLookupDaoOjb.setBusinessObjectDictionaryService(KNSServiceLocatorWeb.getBusinessObjectDictionaryService());
 88  0
                         lookupDaoValues.put(dataSourceName, classSpecificLookupDaoOjb);
 89  0
                         return classSpecificLookupDaoOjb;
 90  
                     }
 91  
                 }
 92  
 
 93  
             }
 94  
         }
 95  
         //return lookupDaoJpa;
 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 = KNSServiceLocatorWeb.getKualiModuleService();
 144  
         }
 145  0
         return kualiModuleService;
 146  
     }
 147  
 }