Coverage Report - org.kuali.rice.kns.dao.proxy.BusinessObjectDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
BusinessObjectDaoProxy
0%
0/58
0%
0/26
1.773
 
 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.HashMap;
 20  
 import java.util.List;
 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.bo.PersistableBusinessObject;
 30  
 import org.kuali.rice.kns.dao.BusinessObjectDao;
 31  
 import org.kuali.rice.kns.dao.impl.BusinessObjectDaoJpa;
 32  
 import org.kuali.rice.kns.dao.impl.BusinessObjectDaoOjb;
 33  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 34  
 import org.kuali.rice.kns.service.KualiModuleService;
 35  
 import org.kuali.rice.kns.service.ModuleService;
 36  
 import org.springframework.transaction.annotation.Transactional;
 37  
 
 38  
 @Transactional
 39  0
 public class BusinessObjectDaoProxy implements BusinessObjectDao {
 40  
 
 41  0
         private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class);
 42  
 
 43  
         private BusinessObjectDao businessObjectDaoJpa;
 44  
         private BusinessObjectDao businessObjectDaoOjb;
 45  
     private static KualiModuleService kualiModuleService;
 46  0
     private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>();
 47  
 
 48  
     private BusinessObjectDao getDao(Class clazz) {
 49  0
         ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 50  0
         if (moduleService != null) {
 51  0
             ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
 52  0
             String dataSourceName = "";
 53  0
             EntityManager entityManager = null;
 54  0
             if (moduleConfig != null) {
 55  0
                 dataSourceName = moduleConfig.getDataSourceName();
 56  0
                 entityManager = moduleConfig.getEntityManager();
 57  
             }
 58  
 
 59  0
             if (StringUtils.isNotEmpty(dataSourceName)) {
 60  0
                 if (boDaoValues.get(dataSourceName) != null) {
 61  0
                     return boDaoValues.get(dataSourceName);
 62  
                 } else {
 63  0
                     if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 64  
                         //using JPA
 65  0
                         if (entityManager != null) {
 66  0
                             BusinessObjectDaoJpa boDaoJpa =
 67  
                                     new BusinessObjectDaoJpa(entityManager, KNSServiceLocator.getPersistenceStructureService());
 68  
                             // add to our cache of bo daos
 69  0
                             boDaoValues.put(dataSourceName, boDaoJpa);
 70  
 
 71  0
                             return boDaoJpa;
 72  
                         } else {
 73  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).");
 74  
                         }
 75  
 
 76  
                     } else {
 77  
                         //using OJB
 78  0
                         BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(KNSServiceLocator.getPersistenceStructureService());
 79  0
                         boDaoOjb.setJcdAlias(dataSourceName);
 80  
                         // add to our cache of bo daos
 81  0
                         boDaoValues.put(dataSourceName, boDaoOjb);
 82  
 
 83  0
                         return boDaoOjb;
 84  
                     }
 85  
 
 86  
                 }
 87  
 
 88  
             }
 89  
         }
 90  0
         return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? businessObjectDaoJpa : businessObjectDaoOjb;
 91  
     }
 92  
 
 93  
         /**
 94  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map)
 95  
          */
 96  
         public int countMatching(Class clazz, Map fieldValues) {
 97  0
                 return getDao(clazz).countMatching(clazz, fieldValues);
 98  
         }
 99  
 
 100  
         /**
 101  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
 102  
          */
 103  
         public int countMatching(Class clazz, Map positiveFieldValues, Map negativeFieldValues) {
 104  0
                 return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues);
 105  
         }
 106  
 
 107  
         /**
 108  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#delete(org.kuali.rice.kns.bo.PersistableBusinessObject)
 109  
          */
 110  
         public void delete(PersistableBusinessObject bo) {
 111  0
                 if (bo != null) {
 112  0
                         getDao(bo.getClass()).delete(bo);
 113  
                 }
 114  0
         }
 115  
 
 116  
         /**
 117  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#delete(java.util.List)
 118  
          */
 119  
         public void delete(List<? extends PersistableBusinessObject> boList) {
 120  0
                 if (!boList.isEmpty()) {
 121  0
                         getDao(boList.get(0).getClass()).delete(boList);
 122  
                 }
 123  0
         }
 124  
 
 125  
         /**
 126  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#deleteMatching(java.lang.Class, java.util.Map)
 127  
          */
 128  
         public void deleteMatching(Class clazz, Map fieldValues) {
 129  0
                 getDao(clazz).deleteMatching(clazz, fieldValues);
 130  0
         }
 131  
 
 132  
         /**
 133  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAll(java.lang.Class)
 134  
          */
 135  
         public Collection findAll(Class clazz) {
 136  0
                 return getDao(clazz).findAll(clazz);
 137  
         }
 138  
 
 139  
         /**
 140  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllActive(java.lang.Class)
 141  
          */
 142  
         public Collection findAllActive(Class clazz) {
 143  0
                 return getDao(clazz).findAllActive(clazz);
 144  
         }
 145  
 
 146  
         /**
 147  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllInactive(java.lang.Class)
 148  
          */
 149  
         public Collection findAllInactive(Class clazz) {
 150  0
                 return getDao(clazz).findAllInactive(clazz);
 151  
         }
 152  
 
 153  
         /**
 154  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllActiveOrderBy(java.lang.Class, java.lang.String, boolean)
 155  
          */
 156  
         public Collection findAllActiveOrderBy(Class clazz, String sortField, boolean sortAscending) {
 157  0
                 return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending);
 158  
         }
 159  
 
 160  
         /**
 161  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
 162  
          */
 163  
         public Collection findAllOrderBy(Class clazz, String sortField, boolean sortAscending) {
 164  0
                 return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending);
 165  
         }
 166  
 
 167  
         /**
 168  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findBySinglePrimaryKey(java.lang.Class, java.lang.Object)
 169  
          */
 170  
         public PersistableBusinessObject findBySinglePrimaryKey(Class clazz, Object primaryKey) {
 171  0
                 return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey);
 172  
         }
 173  
 
 174  
         /**
 175  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findByPrimaryKey(java.lang.Class, java.util.Map)
 176  
          */
 177  
         public PersistableBusinessObject findByPrimaryKey(Class clazz, Map primaryKeys) {
 178  0
                 return getDao(clazz).findByPrimaryKey(clazz, primaryKeys);
 179  
         }
 180  
 
 181  
         /**
 182  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findMatching(java.lang.Class, java.util.Map)
 183  
          */
 184  
         public Collection findMatching(Class clazz, Map fieldValues) {
 185  0
                 return getDao(clazz).findMatching(clazz, fieldValues);
 186  
         }
 187  
 
 188  
         /**
 189  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findMatchingActive(java.lang.Class, java.util.Map)
 190  
          */
 191  
         public Collection findMatchingActive(Class clazz, Map fieldValues) {
 192  0
                 return getDao(clazz).findMatchingActive(clazz, fieldValues);
 193  
         }
 194  
 
 195  
         /**
 196  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findMatchingOrderBy(java.lang.Class, java.util.Map, java.lang.String, boolean)
 197  
          */
 198  
         public Collection findMatchingOrderBy(Class clazz, Map fieldValues, String sortField, boolean sortAscending) {
 199  0
                 return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending);
 200  
         }
 201  
 
 202  
         /**
 203  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#retrieve(org.kuali.rice.kns.bo.PersistableBusinessObject)
 204  
          */
 205  
         public PersistableBusinessObject retrieve(PersistableBusinessObject object) {
 206  0
                 return getDao(object.getClass()).retrieve(object);
 207  
         }
 208  
 
 209  
         /**
 210  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#save(org.kuali.rice.kns.bo.PersistableBusinessObject)
 211  
          */
 212  
         public void save(PersistableBusinessObject bo) {
 213  0
                 getDao(bo.getClass()).save(bo);
 214  0
         }
 215  
 
 216  
         /**
 217  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#save(java.util.List)
 218  
          */
 219  
         public void save(List businessObjects) {
 220  0
                 if (!businessObjects.isEmpty()) {
 221  0
                         getDao(businessObjects.get(0).getClass()).save(businessObjects);
 222  
                 }
 223  0
         }
 224  
 
 225  
     private static KualiModuleService getKualiModuleService() {
 226  0
         if (kualiModuleService == null) {
 227  0
             kualiModuleService = KNSServiceLocator.getKualiModuleService();
 228  
         }
 229  0
         return kualiModuleService;
 230  
     }
 231  
 
 232  
         public void setBusinessObjectDaoJpa(BusinessObjectDao businessObjectDaoJpa) {
 233  0
                 this.businessObjectDaoJpa = businessObjectDaoJpa;
 234  0
         }
 235  
 
 236  
         public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) {
 237  0
                 this.businessObjectDaoOjb = businessObjectDaoOjb;
 238  0
         }
 239  
 
 240  
 }