Coverage Report - org.kuali.rice.kns.dao.proxy.DocumentDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentDaoProxy
0%
0/40
0%
0/26
3.111
 
 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.kns.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.kns.bo.ModuleConfiguration;
 23  
 import org.kuali.rice.kns.dao.BusinessObjectDao;
 24  
 import org.kuali.rice.kns.dao.DocumentDao;
 25  
 import org.kuali.rice.kns.dao.impl.DocumentDaoJpa;
 26  
 import org.kuali.rice.kns.dao.impl.DocumentDaoOjb;
 27  
 import org.kuali.rice.kns.document.Document;
 28  
 import org.kuali.rice.kns.service.DocumentAdHocService;
 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.springframework.transaction.annotation.Transactional;
 33  
 
 34  
 import javax.persistence.EntityManager;
 35  
 import java.util.List;
 36  
 import java.util.Map;
 37  
 import java.util.concurrent.ConcurrentHashMap;
 38  
 
 39  
 @Transactional
 40  0
 public class DocumentDaoProxy implements DocumentDao {
 41  
 
 42  
     private DocumentDao documentDaoJpa;
 43  
     private DocumentDao documentDaoOjb;
 44  
 
 45  
     private static KualiModuleService kualiModuleService;
 46  0
     private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>();
 47  
 
 48  
     private DocumentDao getDao(Class<? extends Document> 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 (documentDaoValues.get(dataSourceName) != null) {
 61  0
                     return documentDaoValues.get(dataSourceName);
 62  
                 }
 63  0
                 if (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.kns"))) {
 64  
                         //using JPA
 65  0
                         if (entityManager != null) {
 66  
                                 // we set the entity manager directly in the constructor
 67  0
                                 DocumentDaoJpa documentDaoJpaInstance =
 68  
                                         new DocumentDaoJpa(entityManager, this.documentDaoJpa.getBusinessObjectDao(),
 69  
                                                         this.documentDaoJpa.getDocumentAdHocService());
 70  
 
 71  0
                                 documentDaoValues.put(dataSourceName, documentDaoJpaInstance);
 72  0
                                 return documentDaoJpaInstance;
 73  
                         }
 74  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).");
 75  
                 }
 76  
                 //using OJB
 77  0
                 DocumentDaoOjb documentDaoOjbInstance =
 78  
                         new DocumentDaoOjb(
 79  
                                         this.documentDaoOjb.getBusinessObjectDao(),
 80  
                                         this.documentDaoOjb.getDocumentAdHocService());
 81  
 
 82  
                 // set the data source alias
 83  0
                 documentDaoOjbInstance.setJcdAlias(dataSourceName);
 84  
 
 85  0
                 documentDaoValues.put(dataSourceName, documentDaoOjbInstance);
 86  0
                 return documentDaoOjbInstance;
 87  
 
 88  
                 }
 89  
 
 90  
         }
 91  0
         return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? documentDaoJpa : documentDaoOjb;
 92  
     }
 93  
     
 94  
     /**
 95  
          * @see org.kuali.rice.kns.dao.DocumentDao#save(org.kuali.rice.kns.document.Document)
 96  
          */
 97  
     @Override
 98  
         public <T extends Document> T save(T document) {
 99  0
                 return getDao(document.getClass()).save(document);
 100  
         }
 101  
     
 102  
         /**
 103  
          * @see org.kuali.rice.kns.dao.DocumentDao#findByDocumentHeaderId(java.lang.Class, java.lang.String)
 104  
          */
 105  
     @Override
 106  
         public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
 107  0
                 return getDao(clazz).findByDocumentHeaderId(clazz, id);
 108  
         }
 109  
 
 110  
         /**
 111  
          * @see org.kuali.rice.kns.dao.DocumentDao#findByDocumentHeaderIds(java.lang.Class, java.util.List)
 112  
          */
 113  
     @Override
 114  
         public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
 115  0
                 return getDao(clazz).findByDocumentHeaderIds(clazz, idList);
 116  
         }
 117  
 
 118  
         /**
 119  
          * @see org.kuali.rice.kns.dao.DocumentDao#getBusinessObjectDao()
 120  
          */
 121  
     @Override
 122  
         public BusinessObjectDao getBusinessObjectDao() {
 123  0
                 if (OrmUtils.isJpaEnabled()) {
 124  0
                         return documentDaoJpa.getBusinessObjectDao();
 125  
                 }
 126  0
                 return documentDaoOjb.getBusinessObjectDao();
 127  
         }
 128  
 
 129  
         /**
 130  
          * @see org.kuali.rice.kns.dao.DocumentDao#getDocumentAdHocService()
 131  
          */
 132  
     @Override
 133  
         public DocumentAdHocService getDocumentAdHocService() {
 134  0
                 if (OrmUtils.isJpaEnabled()) {
 135  0
                         return documentDaoJpa.getDocumentAdHocService();
 136  
                 }
 137  0
                 return documentDaoOjb.getDocumentAdHocService();
 138  
     }
 139  
         
 140  
         public void setDocumentDaoJpa(DocumentDao documentDaoJpa) {
 141  0
                 this.documentDaoJpa = documentDaoJpa;
 142  0
         }
 143  
 
 144  
         public void setDocumentDaoOjb(DocumentDao documentDaoOjb) {
 145  0
                 this.documentDaoOjb = documentDaoOjb;
 146  0
         }
 147  
 
 148  
     private synchronized static KualiModuleService getKualiModuleService() {
 149  0
         if (kualiModuleService == null) {
 150  0
             kualiModuleService = KNSServiceLocatorWeb.getKualiModuleService();
 151  
         }
 152  0
         return kualiModuleService;
 153  
     }
 154  
 
 155  
 }