Java Reference
In-Depth Information
Writing the Statements
When you look at the expected output from this step, you may think a very complex system of
ItemWriters must be employed. But nothing could be further from the truth. Instead, you use nothing
more than a MultiResourceItemWriter (to generate a single file per customer) that delegates to a regular
FlatFileItemWriter with a header callback. You obtain the complex formatting by implementing your
own LineAggregator to generate the output required. To see the pieces involved, let's start with the code
you need to write first: the LineAggregator implementation and the FlatFileHeaderCallback
Chapter 9 discussed how the LineAggregator interface is similar to the LineMapper interface of the
ItemReader side. The LineAggregator's responsibility is to extract the fields required from each item and
format them into a record to be written by a FlatFileItemWriter. Listing 10-45 shows the code for
StatementFormatter .
Listing 10-45. StatementFormatter
package com.apress.springbatch.statement.writer;
import java.text.NumberFormat;
import org.springframework.batch.item.file.transform.LineAggregator;
import com.apress.springbatch.statement.domain.Customer;
import com.apress.springbatch.statement.domain.Statement;
import com.apress.springbatch.statement.domain.Transaction;
public class StatementFormatter implements LineAggregator<Statement> {
private static final String ADDRESS_FORMAT = "%s %s\n%s\n%s, %s %s\n\n";
private static final String SUMMARY_HEADER_FORMAT = "Account Number %s\n"
+ "\nYour Account Summary\n\n";
private static final String SUMMARY_FORMAT =
"Market Value of Current Securities" +
" %s\nCurrent Cash Balance " +
" %s\nTotal Account Value " +
" %s\n\n";
private static final String CASH_DETAIL_FORMAT =
"Account Detail\n\nCash " +
" %s\n\nSecurities\n\n";
private static final String SECURITY_HOLDING_FORMAT =
" %s " +
" %s %s\n";
private static NumberFormat moneyFormatter = NumberFormat
public String aggregate(Statement statement) {
StringBuilder output = new StringBuilder();
formatAddress(statement, output);
formatSummary(statement, output);
formatDetails(statement, output);
Search WWH ::

Custom Search