View Javadoc

1   /**
2    * Copyright 2005-2013 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  public class DocumentDaoProxy implements DocumentDao {
40  
41      private DocumentDao documentDaoJpa;
42      private DocumentDao documentDaoOjb;
43  
44      private static KualiModuleService kualiModuleService;
45      private static Map<String, DocumentDao> documentDaoValues = new ConcurrentHashMap<String, DocumentDao>();
46  
47      private DocumentDao getDao(Class<? extends Document> clazz) {
48      	ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
49          if (moduleService != null) {
50              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
51              String dataSourceName = "";
52              EntityManager entityManager = null;
53              if (moduleConfig != null) {
54                  dataSourceName = moduleConfig.getDataSourceName();
55                  entityManager = moduleConfig.getEntityManager();
56              }
57  
58              if (StringUtils.isNotEmpty(dataSourceName)) {
59                  if (documentDaoValues.get(dataSourceName) != null) {
60                      return documentDaoValues.get(dataSourceName);
61                  }
62                  if (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.krad"))) {
63                  	//using JPA
64                  	if (entityManager != null) {
65                  		// we set the entity manager directly in the constructor
66                  		DocumentDaoJpa documentDaoJpaInstance =
67                  			new DocumentDaoJpa(entityManager, this.documentDaoJpa.getBusinessObjectDao(),
68                  					this.documentDaoJpa.getDocumentAdHocService());
69  
70                  		documentDaoValues.put(dataSourceName, documentDaoJpaInstance);
71                  		return documentDaoJpaInstance;
72                  	}
73                  	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                  DocumentDaoOjb documentDaoOjbInstance =
77                  	new DocumentDaoOjb(
78                  			this.documentDaoOjb.getBusinessObjectDao(),
79                  			this.documentDaoOjb.getDocumentAdHocService());
80  
81                  // set the data source alias
82                  documentDaoOjbInstance.setJcdAlias(dataSourceName);
83  
84                  documentDaoValues.put(dataSourceName, documentDaoOjbInstance);
85                  return documentDaoOjbInstance;
86  
87                  }
88  
89          }
90          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  		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 		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 }