001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.dao.proxy; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.rice.core.api.config.ConfigurationException; 020 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; 021 import org.kuali.rice.krad.bo.ModuleConfiguration; 022 import org.kuali.rice.krad.dao.BusinessObjectDao; 023 import org.kuali.rice.krad.dao.DocumentDao; 024 import org.kuali.rice.krad.dao.impl.DocumentDaoJpa; 025 import org.kuali.rice.krad.dao.impl.DocumentDaoOjb; 026 import org.kuali.rice.krad.document.Document; 027 import org.kuali.rice.krad.service.DocumentAdHocService; 028 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 029 import org.kuali.rice.krad.service.KualiModuleService; 030 import org.kuali.rice.krad.service.ModuleService; 031 import org.springframework.transaction.annotation.Transactional; 032 033 import javax.persistence.EntityManager; 034 import java.util.List; 035 import java.util.Map; 036 import java.util.concurrent.ConcurrentHashMap; 037 038 @Transactional 039 public class DocumentDaoProxy implements DocumentDao { 040 041 private DocumentDao documentDaoJpa; 042 private DocumentDao documentDaoOjb; 043 044 private static KualiModuleService kualiModuleService; 045 private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>(); 046 047 private DocumentDao getDao(Class<? extends Document> clazz) { 048 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); 049 if (moduleService != null) { 050 ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration(); 051 String dataSourceName = ""; 052 EntityManager entityManager = null; 053 if (moduleConfig != null) { 054 dataSourceName = moduleConfig.getDataSourceName(); 055 entityManager = moduleConfig.getEntityManager(); 056 } 057 058 if (StringUtils.isNotEmpty(dataSourceName)) { 059 if (documentDaoValues.get(dataSourceName) != null) { 060 return documentDaoValues.get(dataSourceName); 061 } 062 if (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.krad"))) { 063 //using JPA 064 if (entityManager != null) { 065 // we set the entity manager directly in the constructor 066 DocumentDaoJpa documentDaoJpaInstance = 067 new DocumentDaoJpa(entityManager, this.documentDaoJpa.getBusinessObjectDao(), 068 this.documentDaoJpa.getDocumentAdHocService()); 069 070 documentDaoValues.put(dataSourceName, documentDaoJpaInstance); 071 return documentDaoJpaInstance; 072 } 073 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)."); 074 } 075 //using OJB 076 DocumentDaoOjb documentDaoOjbInstance = 077 new DocumentDaoOjb( 078 this.documentDaoOjb.getBusinessObjectDao(), 079 this.documentDaoOjb.getDocumentAdHocService()); 080 081 // set the data source alias 082 documentDaoOjbInstance.setJcdAlias(dataSourceName); 083 084 documentDaoValues.put(dataSourceName, documentDaoOjbInstance); 085 return documentDaoOjbInstance; 086 087 } 088 089 } 090 return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? documentDaoJpa : documentDaoOjb; 091 } 092 093 /** 094 * @see org.kuali.rice.krad.dao.DocumentDao#save(org.kuali.rice.krad.document.Document) 095 */ 096 @Override 097 public <T extends Document> T save(T document) { 098 return getDao(document.getClass()).save(document); 099 } 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 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 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 if (OrmUtils.isJpaEnabled()) { 123 return documentDaoJpa.getBusinessObjectDao(); 124 } 125 return documentDaoOjb.getBusinessObjectDao(); 126 } 127 128 /** 129 * @see org.kuali.rice.krad.dao.DocumentDao#getDocumentAdHocService() 130 */ 131 @Override 132 public DocumentAdHocService getDocumentAdHocService() { 133 if (OrmUtils.isJpaEnabled()) { 134 return documentDaoJpa.getDocumentAdHocService(); 135 } 136 return documentDaoOjb.getDocumentAdHocService(); 137 } 138 139 public void setDocumentDaoJpa(DocumentDao documentDaoJpa) { 140 this.documentDaoJpa = documentDaoJpa; 141 } 142 143 public void setDocumentDaoOjb(DocumentDao documentDaoOjb) { 144 this.documentDaoOjb = documentDaoOjb; 145 } 146 147 private synchronized static KualiModuleService getKualiModuleService() { 148 if (kualiModuleService == null) { 149 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService(); 150 } 151 return kualiModuleService; 152 } 153 154 }