RESOLVED - issue BATCH-581: Add filter capability to item oriented paradigm

http://jira.springframework.org/browse/BATCH-581

Modified the customerFilterJob to use the ResourceLineReader and processors.
This commit is contained in:
lucasward
2008-10-04 02:35:38 +00:00
parent 56a12d0e77
commit 63f437528c
5 changed files with 49 additions and 48 deletions

View File

@@ -3,6 +3,8 @@
*/
package org.springframework.batch.sample.domain.trade;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.transform.LineTokenizer;
@@ -15,10 +17,11 @@ import org.springframework.batch.item.file.transform.LineTokenizer;
* @author Lucas Ward
* @since 2.0
*/
public class CompositeCustomerUpdateLineTokenizer implements LineTokenizer {
public class CompositeCustomerUpdateLineTokenizer extends StepExecutionListenerSupport implements LineTokenizer {
private LineTokenizer customerTokenizer;
private LineTokenizer footerTokenizer;
private StepExecution stepExecution;
/* (non-Javadoc)
* @see org.springframework.batch.item.file.transform.LineTokenizer#tokenize(java.lang.String)
@@ -27,17 +30,37 @@ public class CompositeCustomerUpdateLineTokenizer implements LineTokenizer {
if(line.charAt(0) == 'F'){
//line starts with F, so the footer tokenizer should tokenize it.
return footerTokenizer.process(line);
FieldSet fs = footerTokenizer.process(line);
long customerUpdateTotal = stepExecution.getReadCount();
long fileUpdateTotal = fs.readLong(1);
if(customerUpdateTotal != fileUpdateTotal){
throw new IllegalStateException("The total number of customer updates in the file footer does not match the " +
"number entered File footer total: [" + fileUpdateTotal + "] Total encountered during processing: [" +
customerUpdateTotal + "]");
}
else{
//return null, because the footer indicates an end of processing.
return null;
}
}
else if(line.charAt(0) == 'A' || line.charAt(0) == 'U' || line.charAt(0) == 'D'){
//line starts with A,U, or D, so it must be a customer operation.
return customerTokenizer.process(line);
}
else if(line.charAt(0) == 'S'){
//header record, ignore
return null;
}
else{
//If the line doesn't start with any of the characters above, it must obviously be invalid.
throw new IllegalArgumentException("Invalid line encountered for tokenizing: " + line);
}
}
@Override
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
/**
* Set the {@link LineTokenizer} that will be used to tokenize any lines that begin with

View File

@@ -5,8 +5,6 @@ package org.springframework.batch.sample.domain.trade;
import java.math.BigDecimal;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
@@ -16,37 +14,15 @@ import org.springframework.batch.item.file.mapping.FieldSetMapper;
* @author Lucas Ward
*
*/
public class CustomerUpdateFieldSetMapper extends StepExecutionListenerSupport implements FieldSetMapper<CustomerUpdate> {
public class CustomerUpdateFieldSetMapper implements FieldSetMapper<CustomerUpdate> {
StepExecution stepExecution;
public CustomerUpdate process(FieldSet fs) {
char code = fs.readChar(0);
if(code == 'F'){
long customerUpdateTotal = stepExecution.getReadCount();
long fileUpdateTotal = fs.readLong(1);
if(customerUpdateTotal != fileUpdateTotal){
throw new IllegalStateException("The total number of customer updates in the file footer does not match the " +
"number entered File footer total: [" + fileUpdateTotal + "] Total encountered during processing: [" +
customerUpdateTotal + "]");
}
else{
//return null, because the footer indicates an end of processing.
return null;
}
}
CustomerOperation operation = CustomerOperation.fromCode(code);
CustomerOperation operation = CustomerOperation.fromCode(fs.readChar(0));
String name = fs.readString(1);
BigDecimal credit = fs.readBigDecimal(2);
return new CustomerUpdate(operation, name, credit);
}
@Override
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
}

View File

@@ -8,7 +8,7 @@ import java.util.List;
import org.springframework.batch.item.ItemWriter;
/**
* @author Lucas Wward
* @author Lucas Ward
*
*/
public class CustomerUpdateWriter implements ItemWriter<CustomerUpdate> {
@@ -24,6 +24,8 @@ public class CustomerUpdateWriter implements ItemWriter<CustomerUpdate> {
customerDao.updateCustomer(customerUpdate.getCustomerName(), customerUpdate.getCredit());
}
}
//flush and/or clear resources
}
public void setCustomerDao(CustomerDao customerDao) {

View File

@@ -11,10 +11,13 @@
<bean name="uploadCustomer" parent="skipLimitStep">
<property name="itemReader" ref="customerFileUploadReader" />
<property name="itemProcessor">
<bean class="org.springframework.batch.sample.domain.trade.CustomerUpdateProcessor" >
<property name="customerDao" ref="customerDao" />
<property name="invalidCustomerLogger" >
<bean class="org.springframework.batch.sample.domain.trade.internal.CommonsLoggingInvalidCustomerLogger" />
<bean class="org.springframework.batch.item.support.CompositeItemProcessor" >
<property name="itemProcessors">
<list>
<ref bean="customerTokenizer"/>
<ref bean="customerMapper" />
<ref bean="customerUpdateProcessor" />
</list>
</property>
</bean>
</property>
@@ -23,17 +26,14 @@
<property name="customerDao" ref="customerDao" />
</bean>
</property>
<property name="listeners" ref="customerMapper" />
<property name="listeners" ref="customerTokenizer" />
</bean>
</list>
</property>
</bean>
<bean name="customerFileUploadReader" class="org.springframework.batch.item.file.FlatFileItemReader" >
<bean name="customerFileUploadReader" class="org.springframework.batch.item.file.ResourceLineReader" >
<property name="resource" ref="customerFileResource" />
<property name="lineTokenizer" ref="customerTokenizer" />
<property name="linesToSkip" value="1" />
<property name="fieldSetMapper" ref="customerMapper" />
</bean>
<bean name="customerMapper" class="org.springframework.batch.sample.domain.trade.CustomerUpdateFieldSetMapper" />
@@ -51,6 +51,13 @@
</property>
</bean>
<bean name="customerUpdateProcessor" class="org.springframework.batch.sample.domain.trade.CustomerUpdateProcessor" >
<property name="customerDao" ref="customerDao" />
<property name="invalidCustomerLogger" >
<bean class="org.springframework.batch.sample.domain.trade.internal.CommonsLoggingInvalidCustomerLogger" />
</property>
</bean>
<bean name="customerDao" class="org.springframework.batch.sample.domain.trade.internal.JdbcCustomerDao" >
<property name="dataSource" ref="dataSource" />
</bean>

View File

@@ -7,6 +7,8 @@ import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.file.mapping.DefaultFieldSet;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.transform.LineTokenizer;
@@ -32,15 +34,6 @@ public class CompositeCustomerUpdateLineTokenizerTests {
compositeTokenizer.setFooterTokenizer(footerTokenizer);
}
@Test
public void testFooter() throws Exception{
String footerLine = "Ffjkdalsfjdaskl;f";
FieldSet fs = compositeTokenizer.process(footerLine);
assertEquals(footerFieldSet, fs);
assertEquals(footerLine, footerTokenizer.getTokenizedLine());
}
@Test
public void testCustomerAdd() throws Exception{