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