Java Reference
In-Depth Information
// Accessors removed
...
}
package com.apress.springbatch.statement.domain;
public class Address {
private String address1;
private String city;
private String state;
private String zip;
// Accessors removed
...
}
The fact that the Customer and Transaction domain objects have a number of different data types
brings up an interesting issue. Although this is good in the world of objects, it should immediately signal
to you that you need to write custom FieldSetMappers for each object to handle the appropriate data-
type conversions required. Because the Transaction FieldSetMapper is simpler, you look at that one first
in Listing 10-9.
Listing 10-9. TransactionFieldSetMapper
package com.apress.springbatch.statement.reader;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
import com.apress.springbatch.statement.domain.Transaction;
public class TransactionFieldSetMapper implements FieldSetMapper<Transaction> {
public Transaction mapFieldSet(FieldSet fieldSet) throws BindException {
Transaction trans = new Transaction();
trans.setAccountNumber(fieldSet.readString("accountNumber"));
trans.setQuantity(fieldSet.readLong("quantity"));
trans.setTicker(fieldSet.readString("stockTicker"));
trans.setTradeTimestamp(fieldSet.readDate("timestamp", "yyyy-MM-dd HH:mm:ss"));
trans.setDollarAmount(fieldSet.readBigDecimal("price"));
return trans;
}
}
As you can see in Listing 10-9, you use the data-type conversion features of the FieldSet
implementation to convert the strings read in from your file to the data types required. In this case, you
convert the strings to a long and a date (by specifying the correct pattern for the date). The other
FieldSetMapper you need is for the Customer object (see Listing 10-10).
 
Search WWH ::




Custom Search