<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:batch="http://www.springframework.org/schema/batch"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<batch:job id="ioSampleJob">
		<batch:step id="step1">
			<batch:tasklet>
				<batch:chunk reader="itemReader" writer="itemWriter" commit-interval="2"/>
			</batch:tasklet>
		</batch:step>
	</batch:job>

	<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
		<property name="resource" value="data/iosample/input/multiRecordType.txt" />
		<property name="lineMapper" ref="prefixMatchingLineMapper"/>
	</bean>

	<bean id="prefixMatchingLineMapper"
		class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper">
		<property name="tokenizers">
			<map>
				<entry key="TRAD*" value-ref="tradeLineTokenizer" />
				<entry key="CUST*" value-ref="customerLineTokenizer" />
			</map>
		</property>
		<property name="fieldSetMappers">
			<map>
				<entry key="TRAD*" value-ref="tradeFieldSetMapper" />
				<entry key="CUST*" value-ref="customerFieldSetMapper" />
			</map>
		</property>
	</bean>

	<bean id="tradeLineTokenizer"
		class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
		<property name="names" value="isin,quantity,price,customer" />
		<property name="columns" value="5-16,17-19,20-25,26-34" />
	</bean>

	<bean id="customerLineTokenizer"
		class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
		<property name="names" value="id,name,credit" />
		<property name="columns" value="5-9,10-18,19-26" />
	</bean>

	<bean id="tradeFieldSetMapper"
		class="org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper" />

	<bean id="customerFieldSetMapper"
		class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditFieldSetMapper" />


	<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
		<property name="resource" value="file:target/test-outputs/multiRecordTypeOutput.txt" />
		<property name="lineAggregator">
			<bean class="org.springframework.batch.sample.iosample.internal.DelegatingTradeLineAggregator">
				<property name="tradeLineAggregator" ref="tradeLineAggregator" />
				<property name="customerLineAggregator" ref="customerLineAggregator" />
			</bean>
		</property>
	</bean>

	<bean id="tradeLineAggregator"
		class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
		<property name="fieldExtractor">
			<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
				<property name="names" value="isin,quantity,price,customer" />
			</bean>
		</property>
		<property name="format" value="TRAD%-12s%-3d%6s%-9s" />
	</bean>

	<bean id="customerLineAggregator"
		class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
		<property name="fieldExtractor">
			<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
				<property name="names" value="id,name,credit" />
			</bean>
		</property>
		<property name="format" value="CUST%05d%-9s%08.0f" />
	</bean>
</beans>
