/*
 * =============================================================================
 * Copyright 1998-2012, IAESTE Internet Development Team. All rights reserved.
 * -----------------------------------------------------------------------------
 * Project: IntraWeb Services (iws-client) - net.iaeste.iws.client.spring.Config
 * -----------------------------------------------------------------------------
 * This software is provided by the members of the IAESTE Internet Development
 * Team (IDT) to IAESTE A.s.b.l. It is for internal use only and may not be
 * redistributed. IAESTE A.s.b.l. is not permitted to sell this software.
 *
 * This software is provided "as is"; the IDT or individuals within the IDT
 * cannot be held legally responsible for any problems the software may cause.
 * =============================================================================
 */
package net.iaeste.iws.client.spring;

import net.iaeste.iws.client.Configuration;
import org.postgresql.ds.PGSimpleDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author  Kim Jensen / last $Author:$
 * @version $Revision:$ / $Date:$
 * @since   1.7
 */
@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
public class Config {

    private final Configuration configuration = Configuration.getInstance();

    @Bean
    public DataSource dataSource() {
        final Boolean usePG = configuration.getUsePostgreSQLDatabase();
        return usePG ? preparePostgreSQLDataSource() : prepareHSQLDataSource();
    }

    private DataSource prepareHSQLDataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.HSQL)
                .addScript("net/iaeste/iws/persistence/hsqldb/init_tables.sql")
                .addScript("net/iaeste/iws/persistence/hsqldb/init_views.sql")
                .addScript("net/iaeste/iws/persistence/hsqldb/init_data.sql")
                .addScript("net/iaeste/iws/persistence/hsqldb/offers-init.sql")
//                .addScript("classpath:/hsqldb/exchange-init.sql")
//                .addScript("classpath:/hsqldb/exchange-views.sql")
//                .addScript("classpath:/hsqldb/exchange-data.sql")
                .build();
    }

    private DataSource preparePostgreSQLDataSource() {
        final PGSimpleDataSource dataSource = new PGSimpleDataSource();

        dataSource.setDatabaseName(configuration.getPostgresqlDatabaseName());
        dataSource.setUser(configuration.getPostgresqlUsername());
        dataSource.setPassword(configuration.getPostgresqlPassword());
        dataSource.setServerName(configuration.getPostgresqlServerName());
        dataSource.setPortNumber(configuration.getPostgresqlPort());

        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
        final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        factoryBean.setPackagesToScan("net.iaeste.iws.persistence");
        factoryBean.setDataSource(dataSource());
        factoryBean.setJpaVendorAdapter(vendorAdapter);

        return factoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());

        return transactionManager;
    }
}

@@ -1649,17 +1649,16 @@
 @author 
- 
 Kim Jens
@@ -1721,18 +1721,16 @@
  @since 
-  
 1.7%0A */%0A
@@ -2574,34 +2574,32 @@
 fers-init.sql%22)%0A
-//
                 
@@ -2606,34 +2606,50 @@
 .addScript(%22
-classpath:
+net/iaeste/iws/persistence
 /hsqldb/exch
@@ -2660,24 +2660,108 @@
 -init.sql%22)%0A
+                .addScript(%22net/iaeste/iws/persistence/hsqldb/offers-triggers.sql%22)%0A
 //          
@@ -2805,21 +2805,20 @@
 xchange-
-views
+data
 .sql%22)%0A/
@@ -2869,28 +2869,29 @@
 db/exchange-
-data
+views
 .sql%22)%0A     

add missing sql scripts to Config (refs #55)