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