Writing header and footer to the output file for a database to file job
I have a db to file job.
The item writer is as below:
Code:
<beans:bean id="myReportFooterCallback" class="com.myPackage.util.MyReportFooterCallback">
<beans:property name="delegate" ref="myReportFileItemWriter" />
</beans:bean>
<beans:bean id="myReportFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<beans:property name="resource" value="file:c:\TestData\output_report.dat" />
<beans:property name="lineAggregator">
<beans:bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<beans:property name="names" value="BUSINESS_DATE, FIELD_ONE, FIELD_TWO, FIELD_THREE, FIELD_FOUR, FIELD_FIVE, FIELD_SIX " />
</beans:bean>
</beans:property>
<beans:property name="format" value="%-12s%-20s%-20s%-20s%-5s%-5s%-10s" />
</beans:bean>
</beans:property>
<beans:property name="footerCallback" ref="myReportFooterCallback" />
</beans:bean>
And the step is as below:
Code:
<step id="ReportFileGenerator">
<tasklet transaction-manager="jobRepository-transactionManager">
<chunk reader="ReportFileItemReader" writer="myReportFooterCallback" commit-interval="1000"/>
</tasklet>
</step>
The class MyReportFooterCallback is as below:
Code:
public class MyReportFooterCallback implements ItemWriter<MyReportBean>, FlatFileFooterCallback {
private ItemWriter<MyReportBean> delegate;
private double totalDebitAmount = 0.0;
private double totalCreditAmount = 0.0;
private int totalDebit = 0;
private int totalCredit = 0;
public void write(final List<? extends MyReportBean> items) throws Exception {
System.out.println("in write method!");
int chunkDebitCount = 0;
int chunkCreditCount = 0;
double chunkDebitTot = 0.0;
double chunkCreditTot = 0.0;
for (MyReportBeanitem : items) {
if (item.getIndicator().equalsIgnoreCase("Debit")) {
// its a debit
chunkDebitCount = chunkDebitCount + 1;
chunkDebitTot = chunkDebitTot + Double.parseDouble(item.getTRANS_AMT());
} else {
// its a credit
chunkCreditCount = chunkCreditCount + 1;
chunkCreditTot = chunkCreditTot + Double.parseDouble(item.getTRANS_AMT());
}
}
this.delegate.write(items);
this.totalDebit = this.totalDebit + chunkDebitCount;
this.totalDebitAmount = this.totalDebitAmount + chunkDebitTot;
this.totalCredit = this.totalCredit + chunkCreditCount;
this.totalCreditAmount = this.totalCreditAmount + chunkCreditTot;
}
public void writeFooter(final Writer writer) throws IOException {
writer.write("\n");
writer.write("\n");
writer.write("Total Debits Processed: " + this.totalDebit);
writer.write("\n");
writer.write("Total Debit Amount: " + this.totalDebitAmount);
writer.write("\n");
writer.write("\n");
writer.write("Total Credits Processed: " + this.totalCredit);
writer.write("\n");
writer.write("Total Credit Amount: " + this.totalCreditAmount);
}
public void setDelegate(final ItemWriter<MyReportBean> delegate) {
this.delegate = delegate;
}
}
However, above code is not working as expected:
1) The file output_report.dat gets generated with the seven columns.However, the footer doesn't show the total values. All four values are 0 (the count and the total).
Total Debits Processed: 0
Total Debit Amount: 0.0
Total Credits Processed: 0
Total Credit Amount: 0.0
What am I missing ?
2) Also, is it possible to add the column names to the top of each column ?
3) Any way to add page numbers to the output report ?
Thanks for reading!