View Javadoc
1   /**
2    * Copyright 2005-2016 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.springframework.transaction.annotation.Transactional;
29  
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * @deprecated use new KRAD Data framework {@link org.kuali.rice.krad.data.DataObjectService}
38   */
39  @Deprecated
40  @Transactional
41  public class BusinessObjectDaoProxy implements BusinessObjectDao {
42  
43  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class);
44  
45  	private BusinessObjectDao businessObjectDaoOjb;
46      private static KualiModuleService kualiModuleService;
47      private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>();
48  
49      private BusinessObjectDao getDao(Class clazz) {
50          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
51          if (moduleService != null) {
52              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
53              String dataSourceName = "";
54              if (moduleConfig != null) {
55                  dataSourceName = moduleConfig.getDataSourceName();
56              }
57  
58              if (StringUtils.isNotEmpty(dataSourceName)) {
59                  if (boDaoValues.get(dataSourceName) != null) {
60                      return boDaoValues.get(dataSourceName);
61                  } else {
62                      //using OJB
63                      BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(
64                              KNSServiceLocator.getPersistenceStructureService());
65                      boDaoOjb.setJcdAlias(dataSourceName);
66                      // add to our cache of bo daos
67                      boDaoValues.put(dataSourceName, boDaoOjb);
68                      return boDaoOjb;
69                  }
70  
71              }
72          }
73          return businessObjectDaoOjb;
74      }
75  
76  	/**
77  	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map)
78  	 */
79  	public int countMatching(Class clazz, Map<String, ?> fieldValues) {
80  		return getDao(clazz).countMatching(clazz, fieldValues);
81  	}
82  
83  	/**
84  	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
85  	 */
86  	public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
87  		return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues);
88  	}
89  
90  	/**
91  	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(org.kuali.rice.krad.bo.PersistableBusinessObject)
92  	 */
93  	public void delete(Object bo) {
94  		if (bo != null) {
95  			getDao(bo.getClass()).delete(bo);
96  		}
97  	}
98  
99  	/**
100 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#delete(java.util.List)
101 	 */
102 	public void delete(List<? extends PersistableBusinessObject> boList) {
103 		if (!boList.isEmpty()) {
104 			getDao(boList.get(0).getClass()).delete(boList);
105 		}
106 	}
107 
108 	/**
109 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#deleteMatching(java.lang.Class, java.util.Map)
110 	 */
111 	public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
112 		getDao(clazz).deleteMatching(clazz, fieldValues);
113 	}
114 
115 	/**
116 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAll(java.lang.Class)
117 	 */
118 	public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
119 		return getDao(clazz).findAll(clazz);
120 	}
121 
122 	/**
123 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActive(java.lang.Class)
124 	 */
125 	public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz) {
126 		return getDao(clazz).findAllActive(clazz);
127 	}
128 
129 	/**
130 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllInactive(java.lang.Class)
131 	 */
132 	public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) {
133 		return getDao(clazz).findAllInactive(clazz);
134 	}
135 
136 	/**
137 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllActiveOrderBy(java.lang.Class, java.lang.String, boolean)
138 	 */
139 	public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
140 		return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending);
141 	}
142 
143 	/**
144 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
145 	 */
146 	public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
147 		return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending);
148 	}
149 
150 	/**
151 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findBySinglePrimaryKey(java.lang.Class, java.lang.Object)
152 	 */
153 	public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
154 		return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey);
155 	}
156 	
157 	/**
158 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKey(java.lang.Class, java.util.Map)
159 	 */
160 	public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) {
161 		return getDao(clazz).findByPrimaryKey(clazz, primaryKeys);
162 	}
163 
164 	/**
165 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatching(java.lang.Class, java.util.Map)
166 	 */
167 	public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) {
168 		return getDao(clazz).findMatching(clazz, fieldValues);
169 	}
170 
171 	/**
172 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingActive(java.lang.Class, java.util.Map)
173 	 */
174 	public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues) {
175 		return getDao(clazz).findMatchingActive(clazz, fieldValues);
176 	}
177 
178 	/**
179 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findMatchingOrderBy(java.lang.Class, java.util.Map, java.lang.String, boolean)
180 	 */
181 	public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) {
182 		return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending);
183 	}
184 
185 	/**
186 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#retrieve(org.kuali.rice.krad.bo.PersistableBusinessObject)
187 	 */
188 	public Object retrieve(Object object) {
189 		return getDao(object.getClass()).retrieve(object);
190 	}
191 	
192 	/**
193 
194 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#manageReadOnly(org.kuali.rice.krad.bo.PersistableBusinessObject)
195 	 */
196 	public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
197 		return getDao(bo.getClass()).manageReadOnly(bo);
198 	}
199 
200 	/**
201 	 * Defers to correct DAO for this class
202 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findByPrimaryKeyUsingKeyObject(java.lang.Class, java.lang.Object)
203 	 */
204 	public <T extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject) {
205 		return getDao(clazz).findByPrimaryKeyUsingKeyObject(clazz, pkObject);
206 	}
207 
208 	/**
209 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(org.kuali.rice.krad.bo.PersistableBusinessObject)
210 	 */
211 	public PersistableBusinessObject save(PersistableBusinessObject bo) {
212 		PersistableBusinessObject savedBo;
213 		savedBo = getDao(bo.getClass()).save(bo);
214 		return savedBo;
215 	}
216 
217 	/**
218 	 * @see org.kuali.rice.krad.dao.BusinessObjectDao#save(java.util.List)
219 	 */
220 	public List<? extends PersistableBusinessObject> save(List businessObjects) {
221 		if (!businessObjects.isEmpty()) {
222 			return getDao(businessObjects.get(0).getClass()).save(businessObjects);
223 		}
224 		return new ArrayList<PersistableBusinessObject>();
225 	}
226 
227     private static KualiModuleService getKualiModuleService() {
228         if (kualiModuleService == null) {
229             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
230         }
231         return kualiModuleService;
232     }
233 
234 	public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) {
235 		this.businessObjectDaoOjb = businessObjectDaoOjb;
236 	}
237 }