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

Java tutorial

Introduction

Here is the source code for com.dominion.salud.pedicom.configuration.PEDICOMJpaConfigurationCentral.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.Modulos;
import com.dominion.salud.pedicom.negocio.configuration.XmlUnmarshaler;
import com.dominion.salud.pedicom.negocio.entities.Datos;
import com.dominion.salud.pedicom.negocio.entities.Datasources;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
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
@Conditional(Modulos.class)
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.dominion.salud.pedicom.negocio.repositoriesCentral", entityManagerFactoryRef = "CentralEM", transactionManagerRef = "CentralTr")
public class PEDICOMJpaConfigurationCentral {

    @Autowired
    private Environment environment;

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(PEDICOMJpaConfigurationCentral.class);

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

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

    @Bean
    public DataSource crearData() throws Exception {

        Datos datos = allDataSources();
        for (Datasources dat : datos.getDatasources()) {
            if (dat.getNombreDatasource().equals("Central")) {
                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());
                return dat.crearData();
            }
        }
        return null;
    }

    @Bean(name = "CentralEM")
    @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.central")));
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(crearData());
        factory.setJpaVendorAdapter(adapter);
        factory.setPackagesToScan("com.dominion.salud.pedicom.negocio.entitiesCentral");
        factory.setPersistenceUnitName("CentralEM");
        factory.afterPropertiesSet();
        factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        return factory.getObject();
    }
}