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