Coverage Report - org.kuali.rice.kns.dao.proxy.DocumentDaoProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentDaoProxy
0%
0/42
0%
0/24
3
 
 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.List;
 19  
 import java.util.Map;
 20  
 import java.util.concurrent.ConcurrentHashMap;
 21  
 
 22  
 import javax.persistence.EntityManager;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.apache.log4j.Logger;
 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.dao.BusinessObjectDao;
 30  
 import org.kuali.rice.kns.dao.DocumentDao;
 31  
 import org.kuali.rice.kns.dao.impl.DocumentDaoJpa;
 32  
 import org.kuali.rice.kns.dao.impl.DocumentDaoOjb;
 33  
 import org.kuali.rice.kns.document.Document;
 34  
 import org.kuali.rice.kns.service.DocumentAdHocService;
 35  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 36  
 import org.kuali.rice.kns.service.KualiModuleService;
 37  
 import org.kuali.rice.kns.service.ModuleService;
 38  
 import org.springframework.transaction.annotation.Transactional;
 39  
 
 40  
 @Transactional
 41  0
 public class DocumentDaoProxy implements DocumentDao {
 42  
 
 43  0
         private static Logger LOG = Logger.getLogger(DocumentDaoProxy.class);
 44  
 
 45  
     private DocumentDao documentDaoJpa;
 46  
     private DocumentDao documentDaoOjb;
 47  
 
 48  
     private static KualiModuleService kualiModuleService;
 49  0
     private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>();
 50  
 
 51  
     private static final String DOCUMENT_AD_HOC_SERVICE_NAME = "documentAdHocService";
 52  
 
 53  
     private DocumentDao getDao(Class clazz) {
 54  0
             ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
 55  0
         if (moduleService != null) {
 56  0
             ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
 57  0
             String dataSourceName = "";
 58  0
             EntityManager entityManager = null;
 59  0
             if (moduleConfig != null) {
 60  0
                 dataSourceName = moduleConfig.getDataSourceName();
 61  0
                 entityManager = moduleConfig.getEntityManager();
 62  
             }
 63  
 
 64  0
             if (StringUtils.isNotEmpty(dataSourceName)) {
 65  0
                 if (documentDaoValues.get(dataSourceName) != null) {
 66  0
                     return documentDaoValues.get(dataSourceName);
 67  
                 } else {
 68  0
                     if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
 69  
                         //using JPA
 70  0
                         if (entityManager != null) {
 71  
                                 // we set the entity manager directly in the constructor
 72  0
                                 DocumentDaoJpa documentDaoJpa =
 73  
                                         new DocumentDaoJpa(entityManager, this.documentDaoJpa.getBusinessObjectDao(),
 74  
                                                         this.documentDaoJpa.getDocumentAdHocService());
 75  
 
 76  0
                                 documentDaoValues.put(dataSourceName, documentDaoJpa);
 77  0
                             return documentDaoJpa;
 78  
                         } else {
 79  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).");
 80  
                         }
 81  
 
 82  
                     } else {
 83  
                         //using OJB
 84  0
                             DocumentDaoOjb documentDaoOjb =
 85  
                                     new DocumentDaoOjb(
 86  
                                             this.documentDaoOjb.getBusinessObjectDao(),
 87  
                                             this.documentDaoOjb.getDocumentAdHocService());
 88  
 
 89  
                             // set the data source alias
 90  0
                             documentDaoOjb.setJcdAlias(dataSourceName);
 91  
 
 92  0
                         documentDaoValues.put(dataSourceName, documentDaoOjb);
 93  0
                         return documentDaoOjb;
 94  
                     }
 95  
 
 96  
                 }
 97  
 
 98  
             }
 99  
         }
 100  0
         return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? documentDaoJpa : documentDaoOjb;
 101  
     }
 102  
 
 103  
         /**
 104  
          * @see org.kuali.rice.kns.dao.DocumentDao#findByDocumentHeaderId(java.lang.Class, java.lang.String)
 105  
          */
 106  
         public Document findByDocumentHeaderId(Class 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  
         public List findByDocumentHeaderIds(Class clazz, List idList) {
 114  0
                 return getDao(clazz).findByDocumentHeaderIds(clazz, idList);
 115  
         }
 116  
 
 117  
         /**
 118  
          * @see org.kuali.rice.kns.dao.DocumentDao#getBusinessObjectDao()
 119  
          */
 120  
         public BusinessObjectDao getBusinessObjectDao() {
 121  0
                 if (OrmUtils.isJpaEnabled()) {
 122  0
                         return documentDaoJpa.getBusinessObjectDao();
 123  
                 } else {
 124  0
                         return documentDaoOjb.getBusinessObjectDao();
 125  
                 }
 126  
         }
 127  
 
 128  
         /**
 129  
          * @see org.kuali.rice.kns.dao.DocumentDao#getDocumentAdHocService()
 130  
          */
 131  
         public DocumentAdHocService getDocumentAdHocService() {
 132  0
                 if (OrmUtils.isJpaEnabled()) {
 133  0
                         return documentDaoJpa.getDocumentAdHocService();
 134  
                 } else {
 135  0
                         return documentDaoOjb.getDocumentAdHocService();
 136  
                 }
 137  
         }
 138  
 
 139  
         /**
 140  
          * @see org.kuali.rice.kns.dao.DocumentDao#save(org.kuali.rice.kns.document.Document)
 141  
          */
 142  
         public void save(Document document) {
 143  0
                 getDao(document.getClass()).save(document);
 144  0
         }
 145  
 
 146  
         public void setDocumentDaoJpa(DocumentDao documentDaoJpa) {
 147  0
                 this.documentDaoJpa = documentDaoJpa;
 148  0
         }
 149  
 
 150  
         public void setDocumentDaoOjb(DocumentDao documentDaoOjb) {
 151  0
                 this.documentDaoOjb = documentDaoOjb;
 152  0
         }
 153  
 
 154  
     private synchronized static KualiModuleService getKualiModuleService() {
 155  0
         if (kualiModuleService == null) {
 156  0
             kualiModuleService = KNSServiceLocator.getKualiModuleService();
 157  
         }
 158  0
         return kualiModuleService;
 159  
     }
 160  
 
 161  
 }