001 /** 002 * Copyright 2005-2013 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.krad.bo.ModuleConfiguration; 020 import org.kuali.rice.krad.dao.BusinessObjectDao; 021 import org.kuali.rice.krad.dao.DocumentDao; 022 import org.kuali.rice.krad.dao.impl.DocumentDaoOjb; 023 import org.kuali.rice.krad.document.Document; 024 import org.kuali.rice.krad.service.DocumentAdHocService; 025 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 026 import org.kuali.rice.krad.service.KualiModuleService; 027 import org.kuali.rice.krad.service.ModuleService; 028 import org.kuali.rice.krad.util.LegacyUtils; 029 import org.springframework.transaction.annotation.Transactional; 030 031 import java.util.List; 032 import java.util.Map; 033 import java.util.concurrent.ConcurrentHashMap; 034 035 @Transactional 036 public class DocumentDaoProxy implements DocumentDao { 037 038 private DocumentDao documentDaoOjb; 039 040 private static KualiModuleService kualiModuleService; 041 private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>(); 042 043 private DocumentDao getDao(Class<? extends Document> clazz) { 044 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz); 045 if (moduleService != null) { 046 ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration(); 047 String dataSourceName = ""; 048 if (moduleConfig != null) { 049 dataSourceName = moduleConfig.getDataSourceName(); 050 } 051 052 if (StringUtils.isNotEmpty(dataSourceName)) { 053 if (documentDaoValues.get(dataSourceName) != null) { 054 return documentDaoValues.get(dataSourceName); 055 } 056 if (!LegacyUtils.useLegacy(clazz)) { 057 throw new IllegalStateException(this.getClass() + " called with non-legacy class: " + clazz); 058 } 059 //using OJB 060 DocumentDaoOjb documentDaoOjbInstance = 061 new DocumentDaoOjb( 062 this.documentDaoOjb.getBusinessObjectDao(), 063 this.documentDaoOjb.getDocumentAdHocService()); 064 065 // set the data source alias 066 documentDaoOjbInstance.setJcdAlias(dataSourceName); 067 068 documentDaoValues.put(dataSourceName, documentDaoOjbInstance); 069 return documentDaoOjbInstance; 070 071 } 072 073 } 074 075 if (!LegacyUtils.useLegacy(clazz)) { 076 throw new IllegalStateException(this.getClass() + " called with non-legacy class: " + clazz); 077 } 078 079 return documentDaoOjb; 080 } 081 082 /** 083 * @see org.kuali.rice.krad.dao.DocumentDao#save(org.kuali.rice.krad.document.Document) 084 */ 085 @Override 086 public <T extends Document> T save(T document) { 087 return getDao(document.getClass()).save(document); 088 } 089 090 /** 091 * @see org.kuali.rice.krad.dao.DocumentDao#findByDocumentHeaderId(java.lang.Class, java.lang.String) 092 */ 093 @Override 094 public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) { 095 return getDao(clazz).findByDocumentHeaderId(clazz, id); 096 } 097 098 /** 099 * @see org.kuali.rice.krad.dao.DocumentDao#findByDocumentHeaderIds(java.lang.Class, java.util.List) 100 */ 101 @Override 102 public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) { 103 return getDao(clazz).findByDocumentHeaderIds(clazz, idList); 104 } 105 106 /** 107 * @see org.kuali.rice.krad.dao.DocumentDao#getBusinessObjectDao() 108 */ 109 @Override 110 public BusinessObjectDao getBusinessObjectDao() { 111 return documentDaoOjb.getBusinessObjectDao(); 112 } 113 114 /** 115 * @see org.kuali.rice.krad.dao.DocumentDao#getDocumentAdHocService() 116 */ 117 @Override 118 public DocumentAdHocService getDocumentAdHocService() { 119 return documentDaoOjb.getDocumentAdHocService(); 120 } 121 122 public void setDocumentDaoOjb(DocumentDao documentDaoOjb) { 123 this.documentDaoOjb = documentDaoOjb; 124 } 125 126 private synchronized static KualiModuleService getKualiModuleService() { 127 if (kualiModuleService == null) { 128 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService(); 129 } 130 return kualiModuleService; 131 } 132 133 }