Coverage Report - org.kuali.rice.kns.dao.proxy.PersistenceDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistenceDaoProxy
0%
0/39
0%
0/26
3
 
 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.apache.ojb.broker.core.proxy.ProxyHelper;
 24  
 import org.hibernate.proxy.HibernateProxy;
 25  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 26  
 import org.kuali.rice.kns.bo.ModuleConfiguration;
 27  
 import org.kuali.rice.kns.dao.PersistenceDao;
 28  
 import org.kuali.rice.kns.dao.impl.PersistenceDaoOjb;
 29  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 30  
 import org.kuali.rice.kns.service.KualiModuleService;
 31  
 import org.kuali.rice.kns.service.ModuleService;
 32  
 import org.kuali.rice.kns.util.ObjectUtils;
 33  
 import org.springframework.transaction.annotation.Transactional;
 34  
 
 35  
 @Transactional
 36  0
 public class PersistenceDaoProxy implements PersistenceDao {
 37  
 
 38  
         private PersistenceDao persistenceDaoJpa;
 39  
         private PersistenceDao persistenceDaoOjb;        
 40  
         private static KualiModuleService kualiModuleService;
 41  0
         private static HashMap<String, PersistenceDao> persistenceDaoValues = new HashMap<String, PersistenceDao>();
 42  
         
 43  
     public void setPersistenceDaoJpa(PersistenceDao persistenceDaoJpa) {
 44  0
                 this.persistenceDaoJpa = persistenceDaoJpa;
 45  0
         }
 46  
 
 47  
         public void setPersistenceDaoOjb(PersistenceDao persistenceDaoOjb) {
 48  0
                 this.persistenceDaoOjb = persistenceDaoOjb;
 49  0
         }
 50  
         
 51  
     private PersistenceDao getDao(Class clazz) {
 52  0
         ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 53  0
         if (moduleService != null) {
 54  0
             ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
 55  0
             String dataSourceName = "";
 56  0
             EntityManager entityManager = null;
 57  0
             if (moduleConfig != null) {
 58  0
                 dataSourceName = moduleConfig.getDataSourceName();
 59  0
                 entityManager = moduleConfig.getEntityManager();
 60  
             }
 61  
 
 62  0
             if (StringUtils.isNotEmpty(dataSourceName)) {
 63  0
                 if (persistenceDaoValues.get(dataSourceName) != null) {
 64  0
                     return persistenceDaoValues.get(dataSourceName);
 65  
                 } else {
 66  0
                         if (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.kns"))) {
 67  
                         //return the default JPA values since PersistenceDaoJPA delegates to the BusinessObjectService which should grab the correct datasource
 68  0
                         persistenceDaoValues.put(dataSourceName, persistenceDaoJpa);
 69  0
                         return persistenceDaoJpa;
 70  
 
 71  
                     } else {
 72  
                         //using OJB
 73  0
                         PersistenceDaoOjb persistDaoOjb = new PersistenceDaoOjb();
 74  0
                         persistDaoOjb.setJcdAlias(dataSourceName);
 75  
                 
 76  0
                         persistenceDaoValues.put(dataSourceName, persistDaoOjb);
 77  0
                         return persistDaoOjb;
 78  
                     }
 79  
 
 80  
                 }
 81  
 
 82  
             }
 83  
         }
 84  0
             return (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.kns"))) ?
 85  
                                                 persistenceDaoJpa : persistenceDaoOjb; 
 86  
     }
 87  
 
 88  
         /**
 89  
      * @see org.kuali.rice.kns.dao.PersistenceDao#clearCache()
 90  
      */
 91  
     public void clearCache() {
 92  
             //if (OrmUtils.isJpaEnabled()) {
 93  0
                     persistenceDaoJpa.clearCache();
 94  
             //} else {
 95  0
                     persistenceDaoOjb.clearCache();
 96  
             //}
 97  0
     }
 98  
 
 99  
     /**
 100  
      * @see org.kuali.rice.kns.dao.PersistenceDao#resolveProxy(java.lang.Object)
 101  
      */
 102  
     public Object resolveProxy(Object o) {
 103  0
             return getDao(ObjectUtils.materializeClassForProxiedObject(o)).resolveProxy(o);
 104  
     }
 105  
 
 106  
     /**
 107  
      * @see org.kuali.rice.kns.dao.PersistenceDao#retrieveAllReferences(java.lang.Object)
 108  
      */
 109  
     public void retrieveAllReferences(Object o) {
 110  0
             getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveAllReferences(o);
 111  0
     }
 112  
 
 113  
     /**
 114  
      * @see org.kuali.rice.kns.dao.PersistenceDao#retrieveReference(java.lang.Object, java.lang.String)
 115  
      */
 116  
     public void retrieveReference(Object o, String referenceName) {
 117  0
             getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveReference(o, referenceName);
 118  0
     }
 119  
  
 120  
     /**
 121  
          * Asks proper DAO implementation if the object is proxied
 122  
          * 
 123  
          * @see org.kuali.rice.kns.dao.PersistenceDao#isProxied(java.lang.Object)
 124  
          */
 125  
         public boolean isProxied(Object object) {
 126  0
                 if (object instanceof HibernateProxy) return true;
 127  0
                 if (ProxyHelper.isProxy(object)) return true;
 128  0
                 return false;
 129  
         }
 130  
 
 131  
         private static KualiModuleService getKualiModuleService() {
 132  0
         if (kualiModuleService == null) {
 133  0
             kualiModuleService = KNSServiceLocatorWeb.getKualiModuleService();
 134  
         }
 135  0
         return kualiModuleService;
 136  
     }
 137  
 }