View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.bo.ModuleConfiguration;
20  import org.kuali.rice.krad.dao.BusinessObjectDao;
21  import org.kuali.rice.krad.dao.DocumentDao;
22  import org.kuali.rice.krad.dao.impl.DocumentDaoOjb;
23  import org.kuali.rice.krad.document.Document;
24  import org.kuali.rice.krad.service.DocumentAdHocService;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  import org.kuali.rice.krad.service.KualiModuleService;
27  import org.kuali.rice.krad.service.ModuleService;
28  import org.kuali.rice.krad.util.LegacyUtils;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  import java.util.List;
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  @Transactional
36  public class DocumentDaoProxy implements DocumentDao {
37  
38      private DocumentDao documentDaoOjb;
39  
40      private static KualiModuleService kualiModuleService;
41      private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>();
42  
43      private DocumentDao getDao(Class<? extends Document> clazz) {
44      	ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
45          if (moduleService != null) {
46              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
47              String dataSourceName = "";
48              if (moduleConfig != null) {
49                  dataSourceName = moduleConfig.getDataSourceName();
50              }
51  
52              if (StringUtils.isNotEmpty(dataSourceName)) {
53                  if (documentDaoValues.get(dataSourceName) != null) {
54                      return documentDaoValues.get(dataSourceName);
55                  }
56                  //using OJB
57                  DocumentDaoOjb documentDaoOjbInstance =
58                  	new DocumentDaoOjb(
59                  			this.documentDaoOjb.getBusinessObjectDao(),
60                  			this.documentDaoOjb.getDocumentAdHocService());
61  
62                  // set the data source alias
63                  documentDaoOjbInstance.setJcdAlias(dataSourceName);
64  
65                  documentDaoValues.put(dataSourceName, documentDaoOjbInstance);
66                  return documentDaoOjbInstance;
67  
68                  }
69  
70          }
71  
72          return documentDaoOjb;
73      }
74      
75      /**
76  	 * @see org.kuali.rice.krad.dao.DocumentDao#save(org.kuali.rice.krad.document.Document)
77  	 */
78      @Override
79  	public <T extends Document> T save(T document) {
80  		return getDao(document.getClass()).save(document);
81  	}
82      
83  	/**
84  	 * @see org.kuali.rice.krad.dao.DocumentDao#findByDocumentHeaderId(java.lang.Class, java.lang.String)
85  	 */
86      @Override
87  	public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
88  		return getDao(clazz).findByDocumentHeaderId(clazz, id);
89  	}
90  
91  	/**
92  	 * @see org.kuali.rice.krad.dao.DocumentDao#findByDocumentHeaderIds(java.lang.Class, java.util.List)
93  	 */
94      @Override
95  	public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
96  		return getDao(clazz).findByDocumentHeaderIds(clazz, idList);
97  	}
98  
99  	/**
100 	 * @see org.kuali.rice.krad.dao.DocumentDao#getBusinessObjectDao()
101 	 */
102     @Override
103 	public BusinessObjectDao getBusinessObjectDao() {
104 		return documentDaoOjb.getBusinessObjectDao();
105 	}
106 
107 	/**
108 	 * @see org.kuali.rice.krad.dao.DocumentDao#getDocumentAdHocService()
109 	 */
110     @Override
111 	public DocumentAdHocService getDocumentAdHocService() {
112 		return documentDaoOjb.getDocumentAdHocService();
113     }
114 
115 	public void setDocumentDaoOjb(DocumentDao documentDaoOjb) {
116 		this.documentDaoOjb = documentDaoOjb;
117 	}
118 
119     private synchronized static KualiModuleService getKualiModuleService() {
120         if (kualiModuleService == null) {
121             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
122         }
123         return kualiModuleService;
124     }
125 
126 }