View Javadoc

1   /**
2    * Copyright 2005-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  package org.kuali.rice.krad.dao.proxy;
17  
18  import java.util.HashMap;
19  
20  import javax.persistence.EntityManager;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.core.proxy.ProxyHelper;
24  import org.hibernate.proxy.HibernateProxy;
25  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
26  import org.kuali.rice.krad.bo.ModuleConfiguration;
27  import org.kuali.rice.krad.dao.PersistenceDao;
28  import org.kuali.rice.krad.dao.impl.PersistenceDaoOjb;
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.kuali.rice.krad.util.ObjectUtils;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  @Transactional
36  public class PersistenceDaoProxy implements PersistenceDao {
37  
38  	private PersistenceDao persistenceDaoJpa;
39  	private PersistenceDao persistenceDaoOjb;	
40  	private static KualiModuleService kualiModuleService;
41  	private static HashMap<String, PersistenceDao> persistenceDaoValues = new HashMap<String, PersistenceDao>();
42  	
43      public void setPersistenceDaoJpa(PersistenceDao persistenceDaoJpa) {
44  		this.persistenceDaoJpa = persistenceDaoJpa;
45  	}
46  
47  	public void setPersistenceDaoOjb(PersistenceDao persistenceDaoOjb) {
48  		this.persistenceDaoOjb = persistenceDaoOjb;
49  	}
50  	
51      private PersistenceDao 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 (persistenceDaoValues.get(dataSourceName) != null) {
64                      return persistenceDaoValues.get(dataSourceName);
65                  } else {
66                  	if (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.krad"))) {
67                          //return the default JPA values since PersistenceDaoJPA delegates to the BusinessObjectService which should grab the correct datasource
68                          persistenceDaoValues.put(dataSourceName, persistenceDaoJpa);
69                          return persistenceDaoJpa;
70  
71                      } else {
72                          //using OJB
73                          PersistenceDaoOjb persistDaoOjb = new PersistenceDaoOjb();
74                          persistDaoOjb.setJcdAlias(dataSourceName);
75                  
76                          persistenceDaoValues.put(dataSourceName, persistDaoOjb);
77                          return persistDaoOjb;
78                      }
79  
80                  }
81  
82              }
83          }
84      	return (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.krad"))) ?
85  						persistenceDaoJpa : persistenceDaoOjb; 
86      }
87  
88  	/**
89       * @see org.kuali.rice.krad.dao.PersistenceDao#clearCache()
90       */
91      public void clearCache() {
92      	//if (OrmUtils.isJpaEnabled()) {
93      		persistenceDaoJpa.clearCache();
94      	//} else {
95      		persistenceDaoOjb.clearCache();
96      	//}
97      }
98  
99      /**
100      * @see org.kuali.rice.krad.dao.PersistenceDao#resolveProxy(java.lang.Object)
101      */
102     public Object resolveProxy(Object o) {
103     	return getDao(ObjectUtils.materializeClassForProxiedObject(o)).resolveProxy(o);
104     }
105 
106     /**
107      * @see org.kuali.rice.krad.dao.PersistenceDao#retrieveAllReferences(java.lang.Object)
108      */
109     public void retrieveAllReferences(Object o) {
110     	getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveAllReferences(o);
111     }
112 
113     /**
114      * @see org.kuali.rice.krad.dao.PersistenceDao#retrieveReference(java.lang.Object, java.lang.String)
115      */
116     public void retrieveReference(Object o, String referenceName) {
117     	getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveReference(o, referenceName);
118     }
119  
120     /**
121 	 * Asks proper DAO implementation if the object is proxied
122 	 * 
123 	 * @see org.kuali.rice.krad.dao.PersistenceDao#isProxied(java.lang.Object)
124 	 */
125 	public boolean isProxied(Object object) {
126 		if (object instanceof HibernateProxy) return true;
127 		if (ProxyHelper.isProxy(object)) return true;
128 		return false;
129 	}
130 
131 	private static KualiModuleService getKualiModuleService() {
132         if (kualiModuleService == null) {
133             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
134         }
135         return kualiModuleService;
136     }
137 }