View Javadoc

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  public class DocumentDaoProxy implements DocumentDao {
42  
43  	private static Logger LOG = Logger.getLogger(DocumentDaoProxy.class);
44  
45      private DocumentDao documentDaoJpa;
46      private DocumentDao documentDaoOjb;
47  
48      private static KualiModuleService kualiModuleService;
49      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      	ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
55          if (moduleService != null) {
56              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
57              String dataSourceName = "";
58              EntityManager entityManager = null;
59              if (moduleConfig != null) {
60                  dataSourceName = moduleConfig.getDataSourceName();
61                  entityManager = moduleConfig.getEntityManager();
62              }
63  
64              if (StringUtils.isNotEmpty(dataSourceName)) {
65                  if (documentDaoValues.get(dataSourceName) != null) {
66                      return documentDaoValues.get(dataSourceName);
67                  } else {
68                      if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
69                          //using JPA
70                          if (entityManager != null) {
71                          	// we set the entity manager directly in the constructor
72                          	DocumentDaoJpa documentDaoJpa =
73                          		new DocumentDaoJpa(entityManager, this.documentDaoJpa.getBusinessObjectDao(),
74                          				this.documentDaoJpa.getDocumentAdHocService());
75  
76                          	documentDaoValues.put(dataSourceName, documentDaoJpa);
77                              return documentDaoJpa;
78                          } else {
79                              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                      	DocumentDaoOjb documentDaoOjb =
85                      		new DocumentDaoOjb(
86                      			this.documentDaoOjb.getBusinessObjectDao(),
87                      			this.documentDaoOjb.getDocumentAdHocService());
88  
89                      	// set the data source alias
90                      	documentDaoOjb.setJcdAlias(dataSourceName);
91  
92                          documentDaoValues.put(dataSourceName, documentDaoOjb);
93                          return documentDaoOjb;
94                      }
95  
96                  }
97  
98              }
99          }
100         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 		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 		return getDao(clazz).findByDocumentHeaderIds(clazz, idList);
115 	}
116 
117 	/**
118 	 * @see org.kuali.rice.kns.dao.DocumentDao#getBusinessObjectDao()
119 	 */
120 	public BusinessObjectDao getBusinessObjectDao() {
121 		if (OrmUtils.isJpaEnabled()) {
122 			return documentDaoJpa.getBusinessObjectDao();
123 		} else {
124 			return documentDaoOjb.getBusinessObjectDao();
125 		}
126 	}
127 
128 	/**
129 	 * @see org.kuali.rice.kns.dao.DocumentDao#getDocumentAdHocService()
130 	 */
131 	public DocumentAdHocService getDocumentAdHocService() {
132 		if (OrmUtils.isJpaEnabled()) {
133 			return documentDaoJpa.getDocumentAdHocService();
134 		} else {
135 			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 		getDao(document.getClass()).save(document);
144 	}
145 
146 	public void setDocumentDaoJpa(DocumentDao documentDaoJpa) {
147 		this.documentDaoJpa = documentDaoJpa;
148 	}
149 
150 	public void setDocumentDaoOjb(DocumentDao documentDaoOjb) {
151 		this.documentDaoOjb = documentDaoOjb;
152 	}
153 
154     private synchronized static KualiModuleService getKualiModuleService() {
155         if (kualiModuleService == null) {
156             kualiModuleService = KNSServiceLocator.getKualiModuleService();
157         }
158         return kualiModuleService;
159     }
160 
161 }