001/**
002 * Copyright 2005-2018 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 org.apache.commons.lang.StringUtils;
019import org.apache.ojb.broker.core.proxy.ProxyHelper;
020import org.kuali.rice.krad.bo.ModuleConfiguration;
021import org.kuali.rice.krad.dao.PersistenceDao;
022import org.kuali.rice.krad.dao.impl.PersistenceDaoOjb;
023import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
024import org.kuali.rice.krad.service.KualiModuleService;
025import org.kuali.rice.krad.service.ModuleService;
026import org.kuali.rice.krad.util.LegacyUtils;
027import org.kuali.rice.krad.util.ObjectUtils;
028import org.springframework.transaction.annotation.Transactional;
029
030import java.util.HashMap;
031
032@Deprecated
033@Transactional
034public class PersistenceDaoProxy implements PersistenceDao {
035        private PersistenceDao persistenceDaoOjb;
036        private static KualiModuleService kualiModuleService;
037        private static HashMap<String, PersistenceDao> persistenceDaoValues = new HashMap<String, PersistenceDao>();
038
039        public void setPersistenceDaoOjb(PersistenceDao persistenceDaoOjb) {
040                this.persistenceDaoOjb = persistenceDaoOjb;
041        }
042        
043    private PersistenceDao getDao(Class clazz) {
044        ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
045        if (moduleService != null) {
046            ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
047            String dataSourceName = "";
048            if (moduleConfig != null) {
049                dataSourceName = moduleConfig.getDataSourceName();
050            }
051
052            if (StringUtils.isNotEmpty(dataSourceName)) {
053                if (persistenceDaoValues.get(dataSourceName) != null) {
054                    return persistenceDaoValues.get(dataSourceName);
055                } else {
056                    //using OJB
057                    PersistenceDaoOjb persistDaoOjb = new PersistenceDaoOjb();
058                    persistDaoOjb.setJcdAlias(dataSourceName);
059
060                    persistenceDaoValues.put(dataSourceName, persistDaoOjb);
061                    return persistDaoOjb;
062                }
063
064            }
065        }
066        return persistenceDaoOjb;
067    }
068
069        /**
070     * @see org.kuali.rice.krad.dao.PersistenceDao#clearCache()
071     */
072    public void clearCache() {
073        persistenceDaoOjb.clearCache();
074    }
075
076    /**
077     * @see org.kuali.rice.krad.dao.PersistenceDao#resolveProxy(java.lang.Object)
078     */
079    public Object resolveProxy(Object o) {
080        return getDao(ObjectUtils.materializeClassForProxiedObject(o)).resolveProxy(o);
081    }
082
083    /**
084     * @see org.kuali.rice.krad.dao.PersistenceDao#retrieveAllReferences(java.lang.Object)
085     */
086    public void retrieveAllReferences(Object o) {
087        getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveAllReferences(o);
088    }
089
090    /**
091     * @see org.kuali.rice.krad.dao.PersistenceDao#retrieveReference(java.lang.Object, java.lang.String)
092     */
093    public void retrieveReference(Object o, String referenceName) {
094        getDao(ObjectUtils.materializeClassForProxiedObject(o)).retrieveReference(o, referenceName);
095    }
096 
097    /**
098         * Asks proper DAO implementation if the object is proxied
099         * 
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}