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.app.persistence.jpa;
017
018import org.kuali.rice.core.framework.persistence.jpa.NullEntityManagerFactory;
019import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
020import org.springframework.beans.factory.FactoryBean;
021import org.springframework.beans.factory.InitializingBean;
022
023import javax.persistence.EntityManagerFactory;
024import javax.sql.DataSource;
025
026/**
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public class RiceEntityManagerProxyFactoryBean implements FactoryBean, InitializingBean {
030
031        private RiceLocalContainerEntityManagerFactoryBean factoryBean;
032        private String prefix;
033        private DataSource datasource;
034        private String moduleJpaEnabledPropertyPrefix;
035        
036        public RiceEntityManagerProxyFactoryBean(String prefix, DataSource datasource) {
037                this.prefix = prefix;
038                this.datasource = datasource;
039                this.moduleJpaEnabledPropertyPrefix = prefix;
040        }
041                
042        public RiceEntityManagerProxyFactoryBean(String prefix, DataSource datasource, String moduleJpaEnabledPropertyPrefix) {
043                this.prefix = prefix;
044                this.datasource = datasource;
045                this.moduleJpaEnabledPropertyPrefix = moduleJpaEnabledPropertyPrefix;
046        }       
047
048        public void afterPropertiesSet() throws Exception {
049                if (OrmUtils.isJpaEnabled(moduleJpaEnabledPropertyPrefix)) {
050                        factoryBean = new RiceLocalContainerEntityManagerFactoryBean(prefix, datasource);
051                        factoryBean.afterPropertiesSet();
052                }
053        }
054        
055        public Class getObjectType() {
056                return (factoryBean != null ? factoryBean.getObjectType() : EntityManagerFactory.class);
057        }
058
059        public Object getObject() throws Exception {
060                return (factoryBean != null ? factoryBean.getObject() : new NullEntityManagerFactory());
061        }
062
063        public boolean isSingleton() {
064                return true;
065        }
066
067}