Coverage Report - org.kuali.rice.kns.dao.proxy.PersistenceDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistenceDaoProxy
0%
0/37
0%
0/20
2.625
 
 1  
 /*
 2  
  * Copyright 2007-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.HashMap;
 19  
 
 20  
 import javax.persistence.EntityManager;
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 import org.kuali.rice.core.config.ConfigurationException;
 24  
 import org.kuali.rice.core.util.OrmUtils;
 25  
 import org.kuali.rice.kns.bo.ModuleConfiguration;
 26  
 import org.kuali.rice.kns.dao.PersistenceDao;
 27  
 import org.kuali.rice.kns.dao.impl.PersistenceDaoJpa;
 28  
 import org.kuali.rice.kns.dao.impl.PersistenceDaoOjb;
 29  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 30  
 import org.kuali.rice.kns.service.KualiModuleService;
 31  
 import org.kuali.rice.kns.service.ModuleService;
 32  
 import org.springframework.transaction.annotation.Transactional;
 33  
 
 34  
 @Transactional
 35  0
 public class PersistenceDaoProxy implements PersistenceDao {
 36  
 
 37  
         private PersistenceDao persistenceDaoJpa;
 38  
         private PersistenceDao persistenceDaoOjb;        
 39  
         private static KualiModuleService kualiModuleService;
 40  0
         private static HashMap<String, PersistenceDao> persistenceDaoValues = new HashMap<String, PersistenceDao>();
 41  
         
 42  
     public void setPersistenceDaoJpa(PersistenceDao persistenceDaoJpa) {
 43  0
                 this.persistenceDaoJpa = persistenceDaoJpa;
 44  0
         }
 45  
 
 46  
         public void setPersistenceDaoOjb(PersistenceDao persistenceDaoOjb) {
 47  0
                 this.persistenceDaoOjb = persistenceDaoOjb;
 48  0
         }
 49  
         
 50  
     private PersistenceDao 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 (persistenceDaoValues.get(dataSourceName) != null) {
 63  0
                     return persistenceDaoValues.get(dataSourceName);
 64  
                 } else {
 65  0
                     if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 66  
                         //return the default JPA values since PersistenceDaoJPA delegates to the BusinessObjectService which should grab the correct datasource
 67  0
                         persistenceDaoValues.put(dataSourceName, persistenceDaoJpa);
 68  0
                         return persistenceDaoJpa;
 69  
 
 70  
                     } else {
 71  
                         //using OJB
 72  0
                         PersistenceDaoOjb persistDaoOjb = new PersistenceDaoOjb();
 73  0
                         persistDaoOjb.setJcdAlias(dataSourceName);
 74  
                 
 75  0
                         persistenceDaoValues.put(dataSourceName, persistDaoOjb);
 76  0
                         return persistDaoOjb;
 77  
                     }
 78  
 
 79  
                 }
 80  
 
 81  
             }
 82  
         }
 83  0
             return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? persistenceDaoJpa : persistenceDaoOjb; 
 84  
     }
 85  
 
 86  
         /**
 87  
      * @see org.kuali.rice.kns.dao.PersistenceDao#clearCache()
 88  
      */
 89  
     public void clearCache() {
 90  0
             if (OrmUtils.isJpaEnabled()) {
 91  0
                     persistenceDaoJpa.clearCache();
 92  
             } else {
 93  0
                     persistenceDaoOjb.clearCache();
 94  
             }
 95  0
     }
 96  
 
 97  
     /**
 98  
      * @see org.kuali.rice.kns.dao.PersistenceDao#resolveProxy(java.lang.Object)
 99  
      */
 100  
     public Object resolveProxy(Object o) {
 101  0
             return getDao(o.getClass()).resolveProxy(o);
 102  
     }
 103  
 
 104  
     /**
 105  
      * @see org.kuali.rice.kns.dao.PersistenceDao#retrieveAllReferences(java.lang.Object)
 106  
      */
 107  
     public void retrieveAllReferences(Object o) {
 108  0
             getDao(o.getClass()).retrieveAllReferences(o);
 109  0
     }
 110  
 
 111  
     /**
 112  
      * @see org.kuali.rice.kns.dao.PersistenceDao#retrieveReference(java.lang.Object, java.lang.String)
 113  
      */
 114  
     public void retrieveReference(Object o, String referenceName) {
 115  0
             getDao(o.getClass()).retrieveReference(o, referenceName);
 116  0
     }
 117  
  
 118  
     private static KualiModuleService getKualiModuleService() {
 119  0
         if (kualiModuleService == null) {
 120  0
             kualiModuleService = KNSServiceLocator.getKualiModuleService();
 121  
         }
 122  0
         return kualiModuleService;
 123  
     }
 124  
 }