View Javadoc
1   /**
2    * Copyright 2005-2014 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.apache.ojb.broker.core.proxy.ProxyHelper;
20  import org.kuali.rice.krad.bo.ModuleConfiguration;
21  import org.kuali.rice.krad.dao.PersistenceDao;
22  import org.kuali.rice.krad.dao.impl.PersistenceDaoOjb;
23  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24  import org.kuali.rice.krad.service.KualiModuleService;
25  import org.kuali.rice.krad.service.ModuleService;
26  import org.kuali.rice.krad.util.LegacyUtils;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  import org.springframework.transaction.annotation.Transactional;
29  
30  import java.util.HashMap;
31  
32  @Deprecated
33  @Transactional
34  public class PersistenceDaoProxy implements PersistenceDao {
35  	private PersistenceDao persistenceDaoOjb;
36  	private static KualiModuleService kualiModuleService;
37  	private static HashMap<String, PersistenceDao> persistenceDaoValues = new HashMap<String, PersistenceDao>();
38  
39  	public void setPersistenceDaoOjb(PersistenceDao persistenceDaoOjb) {
40  		this.persistenceDaoOjb = persistenceDaoOjb;
41  	}
42  	
43      private PersistenceDao getDao(Class clazz) {
44          ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
45          if (moduleService != null) {
46              ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
47              String dataSourceName = "";
48              if (moduleConfig != null) {
49                  dataSourceName = moduleConfig.getDataSourceName();
50              }
51  
52              if (StringUtils.isNotEmpty(dataSourceName)) {
53                  if (persistenceDaoValues.get(dataSourceName) != null) {
54                      return persistenceDaoValues.get(dataSourceName);
55                  } else {
56                      //using OJB
57                      PersistenceDaoOjb persistDaoOjb = new PersistenceDaoOjb();
58                      persistDaoOjb.setJcdAlias(dataSourceName);
59  
60                      persistenceDaoValues.put(dataSourceName, persistDaoOjb);
61                      return persistDaoOjb;
62                  }
63  
64              }
65          }
66      	return persistenceDaoOjb;
67      }
68  
69  	/**
70       * @see org.kuali.rice.krad.dao.PersistenceDao#clearCache()
71       */
72      public void clearCache() {
73          persistenceDaoOjb.clearCache();
74      }
75  
76      /**
77       * @see org.kuali.rice.krad.dao.PersistenceDao#resolveProxy(java.lang.Object)
78       */
79      public Object resolveProxy(Object o) {
80      	return getDao(ObjectUtils.materializeClassForProxiedObject(o)).resolveProxy(o);
81      }
82  
83      /**
84       * @see org.kuali.rice.krad.dao.PersistenceDao#retrieveAllReferences(java.lang.Object)
85       */
86      public void retrieveAllReferences(Object o) {
87      	getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveAllReferences(o);
88      }
89  
90      /**
91       * @see org.kuali.rice.krad.dao.PersistenceDao#retrieveReference(java.lang.Object, java.lang.String)
92       */
93      public void retrieveReference(Object o, String referenceName) {
94      	getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveReference(o, referenceName);
95      }
96   
97      /**
98  	 * Asks proper DAO implementation if the object is proxied
99  	 * 
100 	 * @see org.kuali.rice.krad.dao.PersistenceDao#isProxied(java.lang.Object)
101 	 */
102 	public boolean isProxied(Object object) {
103 		//if (object instanceof HibernateProxy) return true;
104 		if (ProxyHelper.isProxy(object)) return true;
105 		return false;
106 	}
107 
108 	private static KualiModuleService getKualiModuleService() {
109         if (kualiModuleService == null) {
110             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
111         }
112         return kualiModuleService;
113     }
114 }