<?xml version="1.0" encoding="UTF-8"?>
<!--
    Repository and Service layers
-->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- ========================= RESOURCE DEFINITIONS ========================= -->

    <!-- import the dataSource definition -->
    <import resource="datasource-config.xml"/>

    <context:component-scan
        base-package="org.springframework.samples.petclinic.service"/>

    <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
    <!-- (in this case, JDBC-related settings for the JPA EntityManager definition below) -->
    <context:property-placeholder location="classpath:spring/data-access.properties" system-properties-mode="OVERRIDE"/>

    <!-- enables scanning for @Transactional annotations -->
    <tx:annotation-driven/>


    <!-- ==================		 3 Profiles to choose from 			===================
                                    - jdbc (uses Spring" JdbcTemplate)
                                    - jpa
                                    - spring-data-jpa
          =============================================================================-->

    <beans profile="jpa,spring-data-jpa">
        <!-- JPA EntityManagerFactory -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
              p:dataSource-ref="dataSource">
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                      p:database="${jpa.database}" p:showSql="${jpa.showSql}"/>
                <!-- the 'database' parameter refers to the database dialect being used.
                	By default, Hibernate will use a 'HSQL' dialect because 'jpa.database' has been set to 'HSQL'
                	inside file spring/data-access.properties

                 -->
            </property>
            <!-- gDickens: BOTH Persistence Unit and Packages to Scan are NOT compatible, persistenceUnit will win -->
            <property name="persistenceUnitName" value="petclinic"/>
            <property name="packagesToScan" value="org.springframework.samples.petclinic"/>
        </bean>

        <!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
              p:entityManagerFactory-ref="entityManagerFactory"/>


        <!--
            Post-processor to perform exception translation on @Repository classes (from native
            exceptions such as JPA PersistenceExceptions to Spring's DataAccessException hierarchy).
        -->
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    </beans>

    <beans profile="jdbc">
        <!-- Transaction manager for a single JDBC DataSource (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
              p:dataSource-ref="dataSource"/>

        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg ref="dataSource"/>
        </bean>

        <bean id="namedParameterJdbcTemplate"
              class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
            <constructor-arg ref="dataSource"/>
        </bean>

        <context:component-scan base-package="org.springframework.samples.petclinic.repository.jdbc"/>
    </beans>

    <beans profile="jpa">
        <!--
            Loads JPA beans
            Will automatically be transactional due to @Transactional.
            EntityManager will be auto-injected due to @PersistenceContext.
            PersistenceExceptions will be auto-translated due to @Repository.
        -->
        <context:component-scan base-package="org.springframework.samples.petclinic.repository.jpa"/>
    </beans>

    <beans profile="spring-data-jpa">
        <jpa:repositories base-package="org.springframework.samples.petclinic.repository.springdatajpa"/>
    </beans>
</beans>
