Java Reference
In-Depth Information
trans.setAccountNumber(fieldSet.readString("accountNumber"));
trans.setAmount(fieldSet.readDouble("amount"));
trans.setTransactionDate(fieldSet.readDate("transactionDate",
"yyyy-MM-dd HH:mm:ss"));
return trans;
}
}
As you can see, the FieldSet interface, like the ResultSet interface of the JDBC world, provides
custom methods for each data type. In the case of the Transaction domain object, you use the
readDouble method to have the String in your file converted into a Java.lang.Double and you use the
readDate method to parse the string contained in your file into a Java.util.Date. For the date conversion,
you specify not only the field's name but also the format of the date to be parsed.
Unfortunately, with two different item types now being processed by the step at the same time, you
won't be able to use the same ItemWriter you have been up to now. I would love to be able to tell you
that Spring Batch has the equivalent delegator for the writer side as it does with the reader side and the
PatternMatchingCompositeLineMapper. Unfortunately, it doesn't. Instead, you will need to create a
custom ItemWriter that will delegate to the appropriate writer based upon the type of item to be printed.
Chapter 9 covers the details of this writer implementation. However, to be able to see the results of the
job, Listing 7-23 shows the implementation of the LineAggregator interface that will delegate the items
accordingly.
Listing 7-23. CustomerLineAggregator
package com.apress.springbatch.chapter7;
import org.springframework.batch.item.file.transform.LineAggregator;
public class CustomerLineAggregator implements LineAggregator<Object> {
private LineAggregator<Customer> customerLineAggregator;
private LineAggregator<Transaction> transactionLineAggregator;
public String aggregate(Object record) {
if(record instanceof Customer) {
return customerLineAggregator.aggregate((Customer) record);
} else {
return transactionLineAggregator.aggregate((Transaction) record);
}
}
public void setCustomerLineAggregator(
LineAggregator<Customer> customerLineAggregator) {
this.customerLineAggregator = customerLineAggregator;
}
public void setTransactionLineAggregator(
LineAggregator<Transaction> transactionLineAggregator) {
this.transactionLineAggregator = transactionLineAggregator;
 
Search WWH ::




Custom Search