001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.dao.proxy;
017
018import java.util.HashMap;
019
020import javax.persistence.EntityManager;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.ojb.broker.core.proxy.ProxyHelper;
024import org.hibernate.proxy.HibernateProxy;
025import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
026import org.kuali.rice.krad.bo.ModuleConfiguration;
027import org.kuali.rice.krad.dao.PersistenceDao;
028import org.kuali.rice.krad.dao.impl.PersistenceDaoOjb;
029import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
030import org.kuali.rice.krad.service.KualiModuleService;
031import org.kuali.rice.krad.service.ModuleService;
032import org.kuali.rice.krad.util.ObjectUtils;
033import org.springframework.transaction.annotation.Transactional;
034
035@Transactional
036public class PersistenceDaoProxy implements PersistenceDao {
037
038        private PersistenceDao persistenceDaoJpa;
039        private PersistenceDao persistenceDaoOjb;       
040        private static KualiModuleService kualiModuleService;
041        private static HashMap<String, PersistenceDao> persistenceDaoValues = new HashMap<String, PersistenceDao>();
042        
043    public void setPersistenceDaoJpa(PersistenceDao persistenceDaoJpa) {
044                this.persistenceDaoJpa = persistenceDaoJpa;
045        }
046
047        public void setPersistenceDaoOjb(PersistenceDao persistenceDaoOjb) {
048                this.persistenceDaoOjb = persistenceDaoOjb;
049        }
050        
051    private PersistenceDao getDao(Class clazz) {
052        ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
053        if (moduleService != null) {
054            ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
055            String dataSourceName = "";
056            EntityManager entityManager = null;
057            if (moduleConfig != null) {
058                dataSourceName = moduleConfig.getDataSourceName();
059                entityManager = moduleConfig.getEntityManager();
060            }
061
062            if (StringUtils.isNotEmpty(dataSourceName)) {
063                if (persistenceDaoValues.get(dataSourceName) != null) {
064                    return persistenceDaoValues.get(dataSourceName);
065                } else {
066                        if (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.krad"))) {
067                        //return the default JPA values since PersistenceDaoJPA delegates to the BusinessObjectService which should grab the correct datasource
068                        persistenceDaoValues.put(dataSourceName, persistenceDaoJpa);
069                        return persistenceDaoJpa;
070
071                    } else {
072                        //using OJB
073                        PersistenceDaoOjb persistDaoOjb = new PersistenceDaoOjb();
074                        persistDaoOjb.setJcdAlias(dataSourceName);
075                
076                        persistenceDaoValues.put(dataSourceName, persistDaoOjb);
077                        return persistDaoOjb;
078                    }
079
080                }
081
082            }
083        }
084        return (OrmUtils.isJpaAnnotated(clazz) && (OrmUtils.isJpaEnabled() || OrmUtils.isJpaEnabled("rice.krad"))) ?
085                                                persistenceDaoJpa : persistenceDaoOjb; 
086    }
087
088        /**
089     * @see org.kuali.rice.krad.dao.PersistenceDao#clearCache()
090     */
091    public void clearCache() {
092        //if (OrmUtils.isJpaEnabled()) {
093                persistenceDaoJpa.clearCache();
094        //} else {
095                persistenceDaoOjb.clearCache();
096        //}
097    }
098
099    /**
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}