com.dominion.salud.pedicom.configuration.PEDICOMJpaConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for com.dominion.salud.pedicom.configuration.PEDICOMJpaConfiguration.java

Source

/*
 * Copyright (C) 2016 Dominion Global
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.dominion.salud.pedicom.configuration;

import com.dominion.salud.pedicom.negocio.configuration.XmlUnmarshaler;
import com.dominion.salud.pedicom.negocio.entities.Datos;
import com.dominion.salud.pedicom.negocio.configuration.RoutingDataSource;
import com.dominion.salud.pedicom.negocio.entities.Datasources;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityManagerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.dominion.salud.pedicom.negocio.repositories", entityManagerFactoryRef = "MainEM")
public class PEDICOMJpaConfiguration {

    @Autowired
    private Environment environment;

    private static final Logger logger = LoggerFactory.getLogger(PEDICOMJpaConfiguration.class);

    @Bean
    @Primary
    @Autowired
    public JpaTransactionManager transactionManager() throws Exception {
        JpaTransactionManager txManager = new JpaTransactionManager();
        JpaDialect jpaDialect = new HibernateJpaDialect();
        txManager.setEntityManagerFactory(entityManagerFactory());
        txManager.setJpaDialect(jpaDialect);
        txManager.setPersistenceUnitName("MainEM");
        return txManager;
    }

    @Bean
    @Autowired
    public Datos allDataSources() {
        XmlUnmarshaler xml = new XmlUnmarshaler();
        Datos datos = (Datos) xml.unmarshal();
        return datos;
    }

    @Bean
    @Autowired
    public RoutingDataSource routingDataSource() throws Exception {
        RoutingDataSource routingDataSource = new RoutingDataSource();
        Map targetDataSource = new HashMap();
        Datos datos = allDataSources();
        for (Datasources dat : datos.getDatasources()) {
            logger.debug("          Configuracion centro : " + dat.getNombreDatasource());
            logger.debug("              driverClassName: " + dat.getDriverClassName());
            logger.debug("              url: " + dat.getUrl());
            logger.debug("              username: " + dat.getUsername());
            logger.debug("              password: " + dat.getPassword());
            logger.debug("              mail.username: " + dat.getUsernameEmail());
            logger.debug("              mail.password: " + dat.getPasswordEmail());
            logger.debug("              mail.from: " + dat.getFrom());
            logger.debug("              mail.host: " + dat.getHost());
            logger.debug("              mail.port: " + dat.getPort());
            logger.debug("              mail.TLS: " + dat.getTLS());
            logger.debug("              mail.cc: " + dat.getMailCC());
            logger.debug("              mail.ssl: " + dat.getSSL());
            targetDataSource.put(dat.getNombreDatasource(), dat.crearData());
        }
        routingDataSource.setTargetDataSources(targetDataSource);
        routingDataSource.setDefaultTargetDataSource(datos.getDatasources().get(0).crearData());
        routingDataSource.afterPropertiesSet();
        return routingDataSource;
    }

    @Bean(name = "MainEM")
    @Autowired
    public EntityManagerFactory entityManagerFactory() throws Exception {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setGenerateDdl(
                Boolean.getBoolean(StringUtils.trim(environment.getRequiredProperty("hibernate.generate_ddl"))));
        adapter.setShowSql(
                Boolean.getBoolean(StringUtils.trim(environment.getRequiredProperty("hibernate.show_sql"))));
        adapter.setDatabasePlatform(StringUtils.trim(environment.getRequiredProperty("hibernate.dialect")));
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(routingDataSource());
        factory.setJpaVendorAdapter(adapter);
        factory.setPackagesToScan("com.dominion.salud.pedicom.negocio.entities");
        factory.setPersistenceUnitName("MainEM");
        factory.afterPropertiesSet();
        factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        return factory.getObject();
    }
}