<?xml version="1.0" encoding="UTF-8"?>

<beans:beans
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:batch="http://www.springframework.org/schema/batch"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
          http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <beans:import resource="classpath:META-INF/spring/module-context.xml"/>

    <context:property-placeholder location="classpath:batch.properties"/>

    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="${batch.drop.script}"/>
        <jdbc:script location="${person.sql.location}"/>
        <jdbc:script location="${batch.schema.script}"/>
    </jdbc:initialize-database>

    <batch:job-repository id="jobRepository"/>

    <beans:bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <beans:property name="resource" value="${person.test.data.location}"/>
        <beans:property name="lineMapper">
            <beans:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <beans:property name="lineTokenizer">
                    <beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <beans:property name="names" value="firstName,lastName"/>
                    </beans:bean>
                </beans:property>
                <beans:property name="fieldSetMapper">
                    <beans:bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <beans:property name="targetType" value="example.Person"/>
                    </beans:bean>
                </beans:property>
            </beans:bean>
        </beans:property>
    </beans:bean>

    <beans:bean id="itemProcessor" class="example.PersonItemProcessor"/>

    <beans:bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
        <beans:property name="itemSqlParameterSourceProvider">
            <beans:bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
        </beans:property>
        <beans:property name="sql" value="${person.insert.sql}" />
        <beans:property name="dataSource" ref="dataSource" />
    </beans:bean>

    <beans:bean id="jobParametersIncrementer" class="org.springframework.batch.core.launch.support.RunIdIncrementer" />

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                p:driverClassName="${batch.jdbc.driver}"
                p:url="${batch.jdbc.url}"
                p:username="${batch.jdbc.user}"
                p:password="${batch.jdbc.password}"/>

    <beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
                p:dataSource-ref="dataSource"/>

    <beans:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
                p:jobRepository-ref="jobRepository"/>
</beans:beans>
