View Javadoc

1   /*
2    * Copyright 2006-2011 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  
17  package org.kuali.rice.krad.dao.proxy;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.config.ConfigurationException;
21  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
22  import org.kuali.rice.krad.bo.BusinessObject;
23  import org.kuali.rice.krad.bo.ModuleConfiguration;
24  import org.kuali.rice.krad.bo.PersistableBusinessObject;
25  import org.kuali.rice.krad.dao.BusinessObjectDao;
26  import org.kuali.rice.krad.dao.impl.BusinessObjectDaoJpa;
27  import org.kuali.rice.krad.dao.impl.BusinessObjectDaoOjb;
28  import org.kuali.rice.krad.service.KRADServiceLocator;
29  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
30  import org.kuali.rice.krad.service.KualiModuleService;
31  import org.kuali.rice.krad.service.ModuleService;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  import javax.persistence.EntityManager;
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  @Transactional
42  public class BusinessObjectDaoProxy implements BusinessObjectDao {
43  
44  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class);
45  
46  	private BusinessObjectDao businessObjectDaoJpa;
47  	private BusinessObjectDao businessObjectDaoOjb;
48      private static KualiModuleService kualiModuleService;
49      private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>();
50  
51      private BusinessObjectDao getDao(Class clazz) {
52          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
53          if (moduleService != null) {
54              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
55              String dataSourceName = "";
56              EntityManager entityManager = null;
57              if (moduleConfig != null) {
58                  dataSourceName = moduleConfig.getDataSourceName();
59                  entityManager = moduleConfig.getEntityManager();
60              }
61  
62              if (StringUtils.isNotEmpty(dataSourceName)) {
63                  if (boDaoValues.get(dataSourceName) != null) {
64                      return boDaoValues.get(dataSourceName);
65                  } else {
66                  	if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
67                          //using JPA
68                  		if (entityManager != null) {
69                              BusinessObjectDaoJpa boDaoJpa =
70                              	new BusinessObjectDaoJpa(entityManager, KRADServiceLocator
71                                          .getPersistenceStructureService());
72                              // add to our cache of bo daos
73                  			boDaoValues.put(dataSourceName, boDaoJpa);
74                  			return boDaoJpa;
75                  		} else {
76                  			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).");
77                  		}
78                  	} else {	
79                  	    //using OJB
80                          BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(
81                                  KRADServiceLocator.getPersistenceStructureService());
82                          boDaoOjb.setJcdAlias(dataSourceName);
83                          // add to our cache of bo daos
84                          boDaoValues.put(dataSourceName, boDaoOjb);
85                          return boDaoOjb;
86                      }    
87                  }
88  
89              }
90          }
91          //return businessObjectDaoJpa;
92          return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? businessObjectDaoJpa : businessObjectDaoOjb;
93      }
94  
95  	/**
96  	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map)
97  	 */
98  	public int countMatching(Class clazz, Map<String, ?> fieldValues) {
99  		return getDao(clazz).countMatching(clazz, fieldValues);
100 	}
101 
102 	/**
103 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
104 	 */
105 	public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
106 		return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues);
107 	}
108 
109 	/**
110 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(org.kuali.rice.krad.bo.PersistableBusinessObject)
111 	 */
112 	public void delete(PersistableBusinessObject bo) {
113 		if (bo != null) {
114 			getDao(bo.getClass()).delete(bo);
115 		}
116 	}
117 
118 	/**
119 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(java.util.List)
120 	 */
121 	public void delete(List<? extends PersistableBusinessObject> boList) {
122 		if (!boList.isEmpty()) {
123 			getDao(boList.get(0).getClass()).delete(boList);
124 		}
125 	}
126 
127 	/**
128 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#deleteMatching(java.lang.Class, java.util.Map)
129 	 */
130 	public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
131 		getDao(clazz).deleteMatching(clazz, fieldValues);
132 	}
133 
134 	/**
135 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAll(java.lang.Class)
136 	 */
137 	public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
138 		return getDao(clazz).findAll(clazz);
139 	}
140 
141 	/**
142 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActive(java.lang.Class)
143 	 */
144 	public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz) {
145 		return getDao(clazz).findAllActive(clazz);
146 	}
147 
148 	/**
149 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllInactive(java.lang.Class)
150 	 */
151 	public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) {
152 		return getDao(clazz).findAllInactive(clazz);
153 	}
154 
155 	/**
156 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActiveOrderBy(java.lang.Class, java.lang.String, boolean)
157 	 */
158 	public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
159 		return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending);
160 	}
161 
162 	/**
163 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
164 	 */
165 	public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
166 		return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending);
167 	}
168 
169 	/**
170 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findBySinglePrimaryKey(java.lang.Class, java.lang.Object)
171 	 */
172 	public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
173 		return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey);
174 	}
175 	
176 	/**
177 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKey(java.lang.Class, java.util.Map)
178 	 */
179 	public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) {
180 		return getDao(clazz).findByPrimaryKey(clazz, primaryKeys);
181 	}
182 
183 	/**
184 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatching(java.lang.Class, java.util.Map)
185 	 */
186 	public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) {
187 		return getDao(clazz).findMatching(clazz, fieldValues);
188 	}
189 
190 	/**
191 	 * Has the proxied DAO handle the criteria
192 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatching(org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria)
193 	 */
194 	//public <T extends BusinessObject> Collection<T> findMatching(Criteria criteria) {
195 	//	Class clazz = null;
196 	//	try {
197 	//		clazz = Class.forName(criteria.getEntityName());
198 	//	} catch (ClassNotFoundException cnfe) {
199 	//		throw new RuntimeException("Attempted to run JPA Criteria which uses a non-existent class to query against: "+criteria.getEntityName(), cnfe);
200 	//	}
201 	//	return getDao(clazz).findMatching(criteria);
202 	//}
203 
204 	/**
205 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingActive(java.lang.Class, java.util.Map)
206 	 */
207 	public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues) {
208 		return getDao(clazz).findMatchingActive(clazz, fieldValues);
209 	}
210 
211 	/**
212 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingOrderBy(java.lang.Class, java.util.Map, java.lang.String, boolean)
213 	 */
214 	public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) {
215 		return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending);
216 	}
217 
218 	/**
219 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#retrieve(org.kuali.rice.krad.bo.PersistableBusinessObject)
220 	 */
221 	public PersistableBusinessObject retrieve(PersistableBusinessObject object) {
222 		return getDao(object.getClass()).retrieve(object);
223 	}
224 	
225 	/**
226 
227 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#manageReadOnly(org.kuali.rice.krad.bo.PersistableBusinessObject)
228 	 */
229 	public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
230 		return getDao(bo.getClass()).manageReadOnly(bo);
231 	}
232 
233 	/**
234 	 * Defers to correct DAO for this class
235 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKeyUsingKeyObject(java.lang.Class, java.lang.Object)
236 	 */
237 	public <T extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject) {
238 		return getDao(clazz).findByPrimaryKeyUsingKeyObject(clazz, pkObject);
239 	}
240 
241 	/**
242 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(org.kuali.rice.krad.bo.PersistableBusinessObject)
243 	 */
244 	public PersistableBusinessObject save(PersistableBusinessObject bo) {
245 		PersistableBusinessObject savedBo;
246 		savedBo = getDao(bo.getClass()).save(bo);
247 		return savedBo;
248 	}
249 
250 	/**
251 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(java.util.List)
252 	 */
253 	public List<? extends PersistableBusinessObject> save(List businessObjects) {
254 		if (!businessObjects.isEmpty()) {
255 			return getDao(businessObjects.get(0).getClass()).save(businessObjects);
256 		}
257 		return new ArrayList<PersistableBusinessObject>();
258 	}
259 
260     private static KualiModuleService getKualiModuleService() {
261         if (kualiModuleService == null) {
262             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
263         }
264         return kualiModuleService;
265     }
266 
267 	public void setBusinessObjectDaoJpa(BusinessObjectDao businessObjectDaoJpa) {
268 		this.businessObjectDaoJpa = businessObjectDaoJpa;
269 	}
270 
271 	public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) {
272 		this.businessObjectDaoOjb = businessObjectDaoOjb;
273 	}
274 }