<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:batch="http://www.springframework.org/schema/batch"
	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.xsd">

	<batch:job-repository />

	<job id="tradeJob" xmlns="http://www.springframework.org/schema/batch">
		<step id="step1" next="step2">
			<tasklet>
				<chunk reader="fileItemReader" processor="validatingProcessor" writer="tradeWriter"
		               commit-interval="1">
					<streams>
						<stream ref="fileItemReader" />
					</streams>
				</chunk>
				<transaction-attributes isolation="READ_COMMITTED" />
			</tasklet>
		</step>
		<step id="step2" next="step3">
			<tasklet>
				<chunk reader="tradeSqlItemReader" writer="customerWriter" commit-interval="1" />
			</tasklet>
		</step>
		<step id="step3">
			<tasklet>
				<chunk reader="customerSqlItemReader" writer="creditWriter" commit-interval="1" />
			</tasklet>
		</step>
	</job>

	<bean id="validatingProcessor" class="org.springframework.batch.item.validator.ValidatingItemProcessor">
		<constructor-arg ref="tradeValidator" />
	</bean>

	<bean id="tradeWriter" class="org.springframework.batch.sample.domain.trade.internal.TradeWriter">
		<property name="dao" ref="tradeDao" />
	</bean>

	<bean id="customerWriter" class="org.springframework.batch.sample.domain.trade.internal.CustomerUpdateWriter">
		<property name="dao" ref="customerDao" />
	</bean>

	<bean id="creditWriter" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditUpdateWriter">
		<property name="dao" ref="customerReportItemWriter" />
	</bean>

	<bean id="tradeSqlItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
		<property name="dataSource" ref="dataSource" />
		<property name="sql" value="SELECT isin, quantity, price, customer, id, version from TRADE" />
		<property name="rowMapper">
			<bean class="org.springframework.batch.sample.domain.trade.internal.TradeRowMapper" />
		</property>
		<property name="verifyCursorPosition" value="${batch.verify.cursor.position}"/>
	</bean>

	<bean id="customerSqlItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
		<property name="dataSource" ref="dataSource" />
		<property name="sql" value="SELECT id, name, credit FROM CUSTOMER " />
		<property name="rowMapper">
			<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper" />
		</property>
	</bean>

	<bean id="tradeDao" class="org.springframework.batch.sample.domain.trade.internal.JdbcTradeDao"
		p:dataSource-ref="dataSource">
		<property name="incrementer">
			<bean parent="incrementerParent">
				<property name="incrementerName" value="TRADE_SEQ" />
			</bean>
		</property>
	</bean>

	<bean id="customerDao" class="org.springframework.batch.sample.domain.trade.internal.JdbcCustomerDebitDao"
		p:dataSource-ref="dataSource" />

	<bean id="customerReportItemWriter" class="org.springframework.batch.sample.domain.trade.internal.FlatFileCustomerCreditDao">
		<property name="itemWriter">
			<bean class="org.springframework.batch.item.file.FlatFileItemWriter">
				<property name="resource" value="file:target/test-outputs/20070122.testStream.CustomerReportStep.TEMP.txt" />
				<property name="lineAggregator">
					<bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
				</property>
			</bean>
		</property>
	</bean>

	<bean id="fileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
		<property name="resource" value="classpath:data/tradeJob/input/20070122.teststream.ImportTradeDataStep.txt" />
		<property name="lineMapper">
			<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
				<property name="lineTokenizer">
					<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
						<property name="names" value="ISIN, Quantity, Price, Customer" />
					</bean>
				</property>
				<property name="fieldSetMapper">
					<bean class="org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper" />
				</property>
			</bean>
		</property>
	</bean>

	<bean id="tradeValidator" class="org.springframework.batch.item.validator.SpringValidator">
		<property name="validator">
			<bean class="org.springmodules.validation.valang.ValangValidator">
				<property name="valang">
					<value>
						<![CDATA[{ isin : length(?) < 13 : 'ISIN too long' : 'isin_length' : 12}]]>
					</value>
				</property>
			</bean>
		</property>
	</bean>

</beans>