<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
          http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="org.bitbucket.kienerj.moleculedatabaseframework, org.bitbucket.kienerj.mdfsimplewebapp" />
    <context:annotation-config />

    <aop:aspectj-autoproxy/>

    <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:Application.properties</value>
            </list>
        </property>
    </bean>

    <!-- spring-data JPA Configuration -->
    <jpa:repositories base-package="org.bitbucket.kienerj.moleculedatabaseframework.repository, org.bitbucket.kienerj.mdfsimplewebapp.repository" />

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="org.bitbucket.kienerj.moleculedatabaseframework.entity, org.bitbucket.kienerj.mdfsimplewebapp" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="false" />
                <property name="databasePlatform" value="org.bitbucket.kienerj.moleculedatabaseframework.sql.BingoPostgreSQLDialect" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.id.new_generator_mappings">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.jdbc.batch_size">100</prop>
            </props>
        </property>
    </bean>

    <!-- DataSource Configuration -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="#{'jdbc:postgresql://${database.host}:${database.port}/${database.name}'}" />
    </bean>

    <!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
<!--    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <ref local="mainDataSource" />
        </property>
    </bean>

    BoneCP configuration
    <bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass" value="org.postgresql.Driver" />
        <property name="jdbcUrl" value="#{'jdbc:postgresql://${database.host}:${database.port}/${database.name}'}" />
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="idleConnectionTestPeriodInMinutes" value="1"/>
        <property name="IdleMaxAgeInMinutes" value="4"/>
        <property name="maxConnectionsPerPartition" value="25"/>
        <property name="minConnectionsPerPartition" value="5"/>
        <property name="partitionCount" value="1"/>
        <property name="acquireIncrement" value="5"/>
        <property name="statementsCacheSize" value="50"/>
         Without this option you will get a Warning when hibernate loads
            "Cannot change transaction read-only property in the middle of a transaction."
            (this is an exception message from PostgreSQL JDBC driver)
            It seems when hibernate loads and does stuff it does not properly end transaction.
            This settings assures that the pool resets all connections before making
            them available again (eg. rolls back all exsiting transactions.

        <property name="resetConnectionOnClose" value="true"/>

            Prevents Severer error:
                The web application appears to have started a thread named
                [com.google.common.base.internal.Finalizer] but has failed to
                stop it. This is very likely to create a memory leak.
            when undeploying application from tomcat.

        <property name="disableConnectionTracking" value="true"/>
    </bean>-->

    <!-- Transaction Configuration -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" order="2000"/>

    <bean id="simpleCompoundClass" class="java.lang.Class" factory-method="forName">
        <constructor-arg value="org.bitbucket.kienerj.mdfsimplewebapp.entity.SimpleCompound"/>
    </bean>
    <bean id="simpleLotClass" class="java.lang.Class" factory-method="forName">
        <constructor-arg value="org.bitbucket.kienerj.mdfsimplewebapp.entity.SimpleLot"/>
    </bean>

    <!-- Chemistry Configuration -->
    <bean id="chemistryFormat" class="org.bitbucket.kienerj.moleculedatabaseframework.chemistry.ChemistryFormat"
          factory-method="valueOf">
        <constructor-arg>
            <value>#{'${chemistry.format}'.toUpperCase()}</value>
        </constructor-arg>
    </bean>

    <!-- Cache Configuration -->
    <cache:annotation-driven cache-manager="cacheManager"/>


    <bean id="cacheManager"
          class="org.springframework.cache.ehcache.EhCacheCacheManager"
          p:cacheManager-ref="ehcache"/>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
          p:configLocation="classpath:ehcache.xml"
          p:cacheManagerName="moleculeDatabaseCacheManager"
          p:shared="true"/>

    <!-- Configures Spring MVC -->
    <import resource="mvc-config.xml" />

</beans>
