br.eti.danielcamargo.backend.common.config.context.CoreConfig.java Source code

Java tutorial

Introduction

Here is the source code for br.eti.danielcamargo.backend.common.config.context.CoreConfig.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.eti.danielcamargo.backend.common.config.context;

import br.eti.danielcamargo.backend.hsnpts.core.xml.HsnPersonal;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement
@PropertySource("classpath:META-INF/common/application.properties")
@ComponentScan(basePackages = { "br.eti.danielcamargo.backend.common.core",
        "br.eti.danielcamargo.backend.hsnpts.core" })
public class CoreConfig {

    @Autowired
    private Environment env;

    @Bean
    public Unmarshaller unmarshaller() throws Exception {
        InputStream is = getClass().getResourceAsStream("/META-INF/hsnpts/programas.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(HsnPersonal.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        HsnPersonal hsnPersonal = (HsnPersonal) jaxbUnmarshaller.unmarshal(is);
        hsnPersonal.getProgramas();
        return jaxbUnmarshaller;
    }

    /**
     *
     * @return @throws PropertyVetoException
     */
    @Bean
    public ComboPooledDataSource dataSource() throws PropertyVetoException {

        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(env.getProperty("database.driverClass"));
        dataSource.setJdbcUrl(env.getProperty("database.jdbcUrl"));
        dataSource.setUser(env.getProperty("database.user"));
        dataSource.setPassword(env.getProperty("database.password"));

        //defaults
        dataSource.setMinPoolSize(1);
        dataSource.setMaxPoolSize(2);
        dataSource.setMaxStatements(50);
        dataSource.setIdleConnectionTestPeriod(3000);

        return dataSource;
    }

    @Bean
    public HibernateJpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setDatabase(Database.POSTGRESQL);
        return jpaVendorAdapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws PropertyVetoException {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPackagesToScan("br.com.danielcamargo.backend.hsnpts.core.domain");
        factory.setDataSource(dataSource());
        factory.setJpaVendorAdapter(jpaVendorAdapter());

        Map<String, String> map = new HashMap<>();

        map.put("hibernate.dialect", env.getProperty("database.dialect"));
        map.put("hibernate.hbm2ddl.auto", env.getProperty("database.ddlgen"));
        map.put("hibernate.connection.charSet", "UTF-8");
        map.put("hibernate.show_sql", "true");

        map.put("hibernate.hbm2ddl.import_files", "import.sql");

        factory.setJpaPropertyMap(map);
        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws PropertyVetoException {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    public FreeMarkerConfigurationFactoryBean freemarkerMailConfiguration() throws PropertyVetoException {

        FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
        bean.setTemplateLoaderPaths("classpath:/");
        bean.setDefaultEncoding("UTF-8");

        Properties properties = new Properties();
        properties.put("locale", "pt_BR");
        bean.setFreemarkerSettings(properties);

        return bean;
    }

    @Bean(name = "mailSender")
    public JavaMailSender mailSender() throws PropertyVetoException {
        JavaMailSenderImpl bean = new JavaMailSenderImpl();
        bean.setDefaultEncoding("UTF-8");
        bean.setHost("smtp.gmail.com");
        bean.setPort(587);
        bean.setUsername("danielsudpr@gmail.com ");
        bean.setPassword("***");

        Properties properties = new Properties();
        properties.put("mail.smtp.auth", true);
        properties.put("mail.smtp.starttls.enable", true);

        bean.setJavaMailProperties(properties);

        return bean;
    }

}