Summary Table

Categories Total Count
PII 0
URL 0
DNS 1
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

/**
*
*/
package gov.va.med.ars.configuration.hibernate;

import java.util.HashMap;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* @author
DNS
*
*/

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "gov.va.med.ars.dao.ewv", entityManagerFactoryRef = "ewvEntityManager", transactionManagerRef = "ewvTransactionManager")
public class EWVPersistenceConfig {

@Autowired
DataSource arsDataSource;

@Autowired
private Environment env;

@Value("$(init-db:false)")
private String initDatabase;

@Bean(name = "ewvEntityManager")
public LocalContainerEntityManagerFactoryBean ewvEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(ewvDataSource());
em.setPackagesToScan( "gov.va.med.domain.ewv" );

final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);

return em;
}

@Bean(name = "ewvDataSource")
public DataSource ewvDataSource() {

String ewvUrl = env.getProperty("jdbc.ewv.url");
String arsUrl = env.getProperty("jdbc.ars.url");
String ewvUsername = env.getProperty("jdbc.ewv.username");
String arsUsername = env.getProperty("jdbc.ars.username");

if (StringUtils.isBlank(ewvUrl)) {
ewvUrl = arsUrl;
}
if (StringUtils.isBlank(ewvUsername)) {
ewvUsername = arsUsername;
}
/*
* arsUrl = | arsUsername = | Result
* ewvUrl | ewvUsername |
* -----------------------------------
* True | True | arsDatasource
* True | False | ewvDatasource
* False | True | ewvDatasource
* False | False | ewvDatasource
*
*/
if (arsUrl.equalsIgnoreCase(ewvUrl) && arsUsername.equalsIgnoreCase(ewvUsername)) {

return arsDataSource;

} else {

final DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName(env.getProperty("jdbc.ars.driverClassName"));
dataSource.setUrl(ewvUrl);
dataSource.setUsername(ewvUsername);
if (StringUtils.isNotBlank(env.getProperty("jdbc.ewv.password"))) {

dataSource.setPassword(env.getProperty("jdbc.ewv.password"));

} else {

dataSource.setPassword(env.getProperty("jdbc.ars.password"));

}
return dataSource;

}

}

@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}

@Bean(name = "ewvTransactionManager")
public PlatformTransactionManager ewvTransactionManager() {
EntityManagerFactory factory = ewvEntityManager().getObject();

return new JpaTransactionManager(factory);
}

}