Java Reference
In-Depth Information
</beans:bean>
<beans:bean id="transactionFieldSetMapper"
class="com.apress.springbatch.chapter7.TransactionFieldSetMapper"/>
<beans:bean id="customer" class="com.apress.springbatch.chapter7.Customer"
scope="prototype"/>
The configuration of the customerFileReader is beginning to get a bit verbose. Let's walk through
what will actually happen when this reader is executed. If you look at Figure 7-2, you can follow the flow
of how the customerFileReader will process each line.
PatternMatchingCompositeLineMapper
CustomerLineTokenizer
TransactionLineTokenizer CustomerFieldSetMapper
TransactionFieldSetMapper
applyPattern
if pattern for customer matches
tokenize
mapFieldSet
if pattern for transaction matches
tokenize
mapFieldSet
Fig ure 7-2. Flow of processing for multiple record formats.
As Figure 7-2 shows, the PatternMatchingCompositeLineMapper will look at each record of the file
and apply your pattern to it. If the record begins with CUST,* (where * is zero or more characters), it will
pass the record to the customerLineTokenizer for parsing. Once the record is parsed into a FieldSet, it
will be passed to the customerFieldSetMapper to be mapped to the domain object. However, if the
record begins with TRANS,*, it will be passed to the transactionLineTokenizer for parsing with the
resulting FieldSet being passed to the custom transactionFieldSetMapper.
But why do you need a custom FieldSetMapper? It's necessary for custom type conversion. By
default, the BeanWrapperFieldSetMapper doesn't do any special type conversion. The Transaction
domain object consists of an accountNumber field, which is a String; however, the other two fields,
transactionDate and amount, are a java.util.Date and a Double, respectively. Because of this, you will
need to create a custom FieldSetMapper to do the required type conversions. Listing 7-22 shows the
TransactionFieldSetMapper.
Listing 7-22. TransactionFieldSetMapper
package com.apress.springbatch.chapter7;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class TransactionFieldSetMapper implements FieldSetMapper<Transaction> {
public Transaction mapFieldSet(FieldSet fieldSet) throws BindException {
Transaction trans = new Transaction();
 
 
Search WWH ::




Custom Search