Coverage Report - org.kuali.rice.krad.dao.proxy.BusinessObjectDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
BusinessObjectDaoProxy
0%
0/60
0%
0/26
1.75
 
 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.krad.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.framework.persistence.jpa.OrmUtils;
 22  
 import org.kuali.rice.krad.bo.BusinessObject;
 23  
 import org.kuali.rice.krad.bo.ModuleConfiguration;
 24  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 25  
 import org.kuali.rice.krad.dao.BusinessObjectDao;
 26  
 import org.kuali.rice.krad.dao.impl.BusinessObjectDaoJpa;
 27  
 import org.kuali.rice.krad.dao.impl.BusinessObjectDaoOjb;
 28  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 29  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 30  
 import org.kuali.rice.krad.service.KualiModuleService;
 31  
 import org.kuali.rice.krad.service.ModuleService;
 32  
 import org.springframework.transaction.annotation.Transactional;
 33  
 
 34  
 import javax.persistence.EntityManager;
 35  
 import java.util.ArrayList;
 36  
 import java.util.Collection;
 37  
 import java.util.HashMap;
 38  
 import java.util.List;
 39  
 import java.util.Map;
 40  
 
 41  
 @Transactional
 42  0
 public class BusinessObjectDaoProxy implements BusinessObjectDao {
 43  
 
 44  0
         private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class);
 45  
 
 46  
         private BusinessObjectDao businessObjectDaoJpa;
 47  
         private BusinessObjectDao businessObjectDaoOjb;
 48  
     private static KualiModuleService kualiModuleService;
 49  0
     private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>();
 50  
 
 51  
     private BusinessObjectDao 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 (boDaoValues.get(dataSourceName) != null) {
 64  0
                     return boDaoValues.get(dataSourceName);
 65  
                 } else {
 66  0
                         if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 67  
                         //using JPA
 68  0
                                 if (entityManager != null) {
 69  0
                             BusinessObjectDaoJpa boDaoJpa =
 70  
                                     new BusinessObjectDaoJpa(entityManager, KRADServiceLocator
 71  
                                         .getPersistenceStructureService());
 72  
                             // add to our cache of bo daos
 73  0
                                         boDaoValues.put(dataSourceName, boDaoJpa);
 74  0
                                         return boDaoJpa;
 75  
                                 } else {
 76  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).");
 77  
                                 }
 78  
                         } else {        
 79  
                             //using OJB
 80  0
                         BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(
 81  
                                 KRADServiceLocator.getPersistenceStructureService());
 82  0
                         boDaoOjb.setJcdAlias(dataSourceName);
 83  
                         // add to our cache of bo daos
 84  0
                         boDaoValues.put(dataSourceName, boDaoOjb);
 85  0
                         return boDaoOjb;
 86  
                     }    
 87  
                 }
 88  
 
 89  
             }
 90  
         }
 91  
         //return businessObjectDaoJpa;
 92  0
         return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? businessObjectDaoJpa : businessObjectDaoOjb;
 93  
     }
 94  
 
 95  
         /**
 96  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map)
 97  
          */
 98  
         public int countMatching(Class clazz, Map<String, ?> fieldValues) {
 99  0
                 return getDao(clazz).countMatching(clazz, fieldValues);
 100  
         }
 101  
 
 102  
         /**
 103  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
 104  
          */
 105  
         public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
 106  0
                 return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues);
 107  
         }
 108  
 
 109  
         /**
 110  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(org.kuali.rice.krad.bo.PersistableBusinessObject)
 111  
          */
 112  
         public void delete(PersistableBusinessObject bo) {
 113  0
                 if (bo != null) {
 114  0
                         getDao(bo.getClass()).delete(bo);
 115  
                 }
 116  0
         }
 117  
 
 118  
         /**
 119  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(java.util.List)
 120  
          */
 121  
         public void delete(List<? extends PersistableBusinessObject> boList) {
 122  0
                 if (!boList.isEmpty()) {
 123  0
                         getDao(boList.get(0).getClass()).delete(boList);
 124  
                 }
 125  0
         }
 126  
 
 127  
         /**
 128  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#deleteMatching(java.lang.Class, java.util.Map)
 129  
          */
 130  
         public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
 131  0
                 getDao(clazz).deleteMatching(clazz, fieldValues);
 132  0
         }
 133  
 
 134  
         /**
 135  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAll(java.lang.Class)
 136  
          */
 137  
         public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
 138  0
                 return getDao(clazz).findAll(clazz);
 139  
         }
 140  
 
 141  
         /**
 142  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActive(java.lang.Class)
 143  
          */
 144  
         public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz) {
 145  0
                 return getDao(clazz).findAllActive(clazz);
 146  
         }
 147  
 
 148  
         /**
 149  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllInactive(java.lang.Class)
 150  
          */
 151  
         public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) {
 152  0
                 return getDao(clazz).findAllInactive(clazz);
 153  
         }
 154  
 
 155  
         /**
 156  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActiveOrderBy(java.lang.Class, java.lang.String, boolean)
 157  
          */
 158  
         public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
 159  0
                 return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending);
 160  
         }
 161  
 
 162  
         /**
 163  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
 164  
          */
 165  
         public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
 166  0
                 return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending);
 167  
         }
 168  
 
 169  
         /**
 170  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findBySinglePrimaryKey(java.lang.Class, java.lang.Object)
 171  
          */
 172  
         public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
 173  0
                 return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey);
 174  
         }
 175  
         
 176  
         /**
 177  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKey(java.lang.Class, java.util.Map)
 178  
          */
 179  
         public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) {
 180  0
                 return getDao(clazz).findByPrimaryKey(clazz, primaryKeys);
 181  
         }
 182  
 
 183  
         /**
 184  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatching(java.lang.Class, java.util.Map)
 185  
          */
 186  
         public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) {
 187  0
                 return getDao(clazz).findMatching(clazz, fieldValues);
 188  
         }
 189  
 
 190  
         /**
 191  
          * Has the proxied DAO handle the criteria
 192  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatching(org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria)
 193  
          */
 194  
         //public <T extends BusinessObject> Collection<T> findMatching(Criteria criteria) {
 195  
         //        Class clazz = null;
 196  
         //        try {
 197  
         //                clazz = Class.forName(criteria.getEntityName());
 198  
         //        } catch (ClassNotFoundException cnfe) {
 199  
         //                throw new RuntimeException("Attempted to run JPA Criteria which uses a non-existent class to query against: "+criteria.getEntityName(), cnfe);
 200  
         //        }
 201  
         //        return getDao(clazz).findMatching(criteria);
 202  
         //}
 203  
 
 204  
         /**
 205  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingActive(java.lang.Class, java.util.Map)
 206  
          */
 207  
         public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues) {
 208  0
                 return getDao(clazz).findMatchingActive(clazz, fieldValues);
 209  
         }
 210  
 
 211  
         /**
 212  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingOrderBy(java.lang.Class, java.util.Map, java.lang.String, boolean)
 213  
          */
 214  
         public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) {
 215  0
                 return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending);
 216  
         }
 217  
 
 218  
         /**
 219  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#retrieve(org.kuali.rice.krad.bo.PersistableBusinessObject)
 220  
          */
 221  
         public PersistableBusinessObject retrieve(PersistableBusinessObject object) {
 222  0
                 return getDao(object.getClass()).retrieve(object);
 223  
         }
 224  
         
 225  
         /**
 226  
 
 227  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#manageReadOnly(org.kuali.rice.krad.bo.PersistableBusinessObject)
 228  
          */
 229  
         public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
 230  0
                 return getDao(bo.getClass()).manageReadOnly(bo);
 231  
         }
 232  
 
 233  
         /**
 234  
          * Defers to correct DAO for this class
 235  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKeyUsingKeyObject(java.lang.Class, java.lang.Object)
 236  
          */
 237  
         public <T extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject) {
 238  0
                 return getDao(clazz).findByPrimaryKeyUsingKeyObject(clazz, pkObject);
 239  
         }
 240  
 
 241  
         /**
 242  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(org.kuali.rice.krad.bo.PersistableBusinessObject)
 243  
          */
 244  
         public PersistableBusinessObject save(PersistableBusinessObject bo) {
 245  
                 PersistableBusinessObject savedBo;
 246  0
                 savedBo = getDao(bo.getClass()).save(bo);
 247  0
                 return savedBo;
 248  
         }
 249  
 
 250  
         /**
 251  
          * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(java.util.List)
 252  
          */
 253  
         public List<? extends PersistableBusinessObject> save(List businessObjects) {
 254  0
                 if (!businessObjects.isEmpty()) {
 255  0
                         return getDao(businessObjects.get(0).getClass()).save(businessObjects);
 256  
                 }
 257  0
                 return new ArrayList<PersistableBusinessObject>();
 258  
         }
 259  
 
 260  
     private static KualiModuleService getKualiModuleService() {
 261  0
         if (kualiModuleService == null) {
 262  0
             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
 263  
         }
 264  0
         return kualiModuleService;
 265  
     }
 266  
 
 267  
         public void setBusinessObjectDaoJpa(BusinessObjectDao businessObjectDaoJpa) {
 268  0
                 this.businessObjectDaoJpa = businessObjectDaoJpa;
 269  0
         }
 270  
 
 271  
         public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) {
 272  0
                 this.businessObjectDaoOjb = businessObjectDaoOjb;
 273  0
         }
 274  
 }