package org.jeasy.jobs;

import org.hibernate.SessionFactory;
import org.jeasy.jobs.execution.JobExecutionRepository;
import org.jeasy.jobs.job.JobRepository;
import org.jeasy.jobs.request.JobRequestRepository;
import org.jeasy.jobs.user.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileReader;
import java.util.Properties;

import static org.jeasy.jobs.DataSourceConfiguration.*;

@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
@ImportResource("classpath:data-source-config.xml")
public class ContextConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContextConfiguration.class);

    @Autowired
    private DataSource dataSource;
    @Autowired
    private String hibernateDialect;

    @Bean
    public LocalSessionFactoryBean localSessionFactoryBean() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.dialect", hibernateDialect);
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource);
        localSessionFactoryBean.setHibernateProperties(hibernateProperties);
        localSessionFactoryBean.setMappingLocations(
                new ClassPathResource("user.hbm.xml"),
                new ClassPathResource("job.hbm.xml"),
                new ClassPathResource("jobRequest.hbm.xml"),
                new ClassPathResource("jobExecution.hbm.xml")
        );
        return localSessionFactoryBean;
    }

    @Bean
    public SessionFactory sessionFactory() {
        return localSessionFactoryBean().getObject();
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        return new HibernateTransactionManager(sessionFactory());
    }

    @Bean
    public UserRepository userRepository() {
        return new UserRepository(sessionFactory());
    }

    @Bean
    public JobRepository jobRepository() {
        return new JobRepository(sessionFactory());
    }

    @Bean
    public JobRequestRepository jobRequestRepository() {
        return new JobRequestRepository(sessionFactory());
    }

    @Bean
    public JobExecutionRepository jobExecutionRepository() {
        return new JobExecutionRepository(sessionFactory());
    }

    @Bean
    public Service service() {
        return new Service(jobExecutionRepository(), jobRequestRepository(), jobRepository());
    }

    @Bean
    public DataSourceConfiguration dataSourceConfiguration() {
        DataSourceConfiguration defaultDataSourceConfiguration = DEFAULT_DATA_SOURCE_CONFIGURATION;
        String configurationPath = System.getProperty(DATA_SOURCE_CONFIGURATION_PROPERTY);
        if (configurationPath == null) {
            LOGGER.info("No data source configuration file specified, using default configuration: " + defaultDataSourceConfiguration);
            return defaultDataSourceConfiguration;
        }
        try {
            Properties properties = new Properties();
            properties.load(new FileReader(new File(configurationPath)));
            DataSourceConfiguration dataSourceConfiguration = new DataSourceConfiguration();
            dataSourceConfiguration.setDatabaseUrl(properties.getProperty(DATA_SOURCE_CONFIGURATION_URL));
            dataSourceConfiguration.setDatabaseUser(properties.getProperty(DATA_SOURCE_CONFIGURATION_USER));
            dataSourceConfiguration.setDatabasePassword(properties.getProperty(DATA_SOURCE_CONFIGURATION_PASSWORD));
            LOGGER.info("Using data source configuration: " + dataSourceConfiguration);
            return dataSourceConfiguration;
        } catch (Exception e) {
            throw new BeanInstantiationException(DataSourceConfiguration.class, "Unable to read data source configuration from file " + configurationPath, e);
        }
    }
}

@@ -22,45 +22,8 @@
 s;%0A%0A
-import org.hibernate.SessionFactory;%0A
 impo
@@ -2095,124 +2095,8 @@
  %7D%0A%0A
-    @Bean%0A    public SessionFactory sessionFactory() %7B%0A        return localSessionFactoryBean().getObject();%0A    %7D%0A%0A
     
@@ -2210,17 +2210,22 @@
 Manager(
-s
+localS
 essionFa
@@ -2221,32 +2221,48 @@
 alSessionFactory
+Bean().getObject
 ());%0A    %7D%0A%0A    
@@ -2338,33 +2338,38 @@
  UserRepository(
-s
+localS
 essionFactory())
@@ -2357,32 +2357,48 @@
 alSessionFactory
+Bean().getObject
 ());%0A    %7D%0A%0A    
@@ -2471,33 +2471,38 @@
 w JobRepository(
-s
+localS
 essionFactory())
@@ -2490,32 +2490,48 @@
 alSessionFactory
+Bean().getObject
 ());%0A    %7D%0A%0A    
@@ -2625,33 +2625,38 @@
 questRepository(
-s
+localS
 essionFactory())
@@ -2644,32 +2644,48 @@
 alSessionFactory
+Bean().getObject
 ());%0A    %7D%0A%0A    
@@ -2793,17 +2793,22 @@
 ository(
-s
+localS
 essionFa
@@ -2808,24 +2808,40 @@
 ssionFactory
+Bean().getObject
 ());%0A    %7D%0A%0A

remove duplicate session factory bean definition