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