OPEN - issue BATCH-371: FlatFileItemWriter no longer uses LineAggregator
http://jira.springframework.org/browse/BATCH-371 Changed the interface of LineAggregator for symmetry with LineTokenizer.
This commit is contained in:
@@ -7,11 +7,11 @@ import org.w3c.dom.Element;
|
||||
|
||||
class JobBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
private static final String JOB = "job";
|
||||
public static final String JOB = "job";
|
||||
|
||||
private static final String CHUNKING_STEP = "chunking-step";
|
||||
public static final String CHUNKING_STEP = "chunking-step";
|
||||
|
||||
private static final String TASKLET_STEP ="tasklet-step";
|
||||
public static final String TASKLET_STEP ="tasklet-step";
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
|
||||
|
||||
/**
|
||||
* Class used to create string representing object. Values are separated by
|
||||
@@ -30,13 +32,13 @@ public class DelimitedLineAggregator implements LineAggregator {
|
||||
/**
|
||||
* Method used to create string representing object.
|
||||
*
|
||||
* @param args arrays of strings representing data to be stored
|
||||
* @param fieldSet arrays of strings representing data to be stored
|
||||
* @param lineDescriptor for this implementation this parameter is not
|
||||
* used
|
||||
*/
|
||||
public String aggregate(String[] args) {
|
||||
public String aggregate(FieldSet fieldSet) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
|
||||
String[] args = fieldSet.getValues();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
buffer.append(args[i]);
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.batch.io.file.transform;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -60,14 +61,16 @@ public class FixedLengthLineAggregator implements LineAggregator {
|
||||
* Aggregate provided strings into single line using specified column
|
||||
* ranges.
|
||||
*
|
||||
* @param args
|
||||
* @param fieldSet
|
||||
* arrays of strings representing data to be aggregated
|
||||
* @return aggregated strings
|
||||
*/
|
||||
public String aggregate(String[] args) {
|
||||
public String aggregate(FieldSet fieldSet) {
|
||||
|
||||
Assert.notNull(args);
|
||||
Assert.notNull(fieldSet);
|
||||
Assert.notNull(ranges);
|
||||
|
||||
String[] args = fieldSet.getValues();
|
||||
Assert.isTrue(args.length <= ranges.length,
|
||||
"Number of arguments must match number of fields in a record");
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
|
||||
|
||||
/**
|
||||
* Interface used to create string used to create string representing object.
|
||||
@@ -26,9 +28,8 @@ public interface LineAggregator {
|
||||
/**
|
||||
* Method used to create a string to be stored from the array of values.
|
||||
*
|
||||
* @param args values to be stored
|
||||
* @param lineDescriptor structure of final string
|
||||
* @param fieldSet values to be converted
|
||||
* @return
|
||||
*/
|
||||
public String aggregate(String[] args);
|
||||
public String aggregate(FieldSet fieldSet);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.item.writer.ItemTransformer;
|
||||
|
||||
/**
|
||||
@@ -43,6 +45,14 @@ public class LineAggregatorItemTransformer implements ItemTransformer {
|
||||
* @see org.springframework.batch.item.writer.ItemTransformer#transform(java.lang.Object)
|
||||
*/
|
||||
public Object transform(Object item) throws Exception {
|
||||
return aggregator.aggregate((String[]) item);
|
||||
return aggregator.aggregate(createFieldSet(item));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param item
|
||||
* @return
|
||||
*/
|
||||
protected FieldSet createFieldSet(Object item) {
|
||||
return new DefaultFieldSet((String[]) item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
import org.springframework.batch.io.file.transform.DelimitedLineAggregator;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DelimitedLineAggregator}
|
||||
*
|
||||
@@ -34,7 +34,7 @@ public class DelimitedLineAggregatorTests extends TestCase {
|
||||
|
||||
String[] args = { "a", "bc", "def" };
|
||||
String expectedResult = "a:bc:def";
|
||||
String result = aggregator.aggregate(args);
|
||||
String result = aggregator.aggregate(new DefaultFieldSet(args));
|
||||
assertEquals(result, expectedResult);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.transform.FixedLengthLineAggregator;
|
||||
import org.springframework.batch.io.file.transform.Range;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
String[] args = { "does not matter what is here" };
|
||||
|
||||
try {
|
||||
aggregator.aggregate(args);
|
||||
aggregator.aggregate(new DefaultFieldSet(args));
|
||||
fail("should not work with no ranges specified");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
@@ -55,7 +56,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
aggregator.setColumns(new Range[0]);
|
||||
|
||||
try {
|
||||
aggregator.aggregate(string);
|
||||
aggregator.aggregate(new DefaultFieldSet(string));
|
||||
fail("Exception expected: count of aggregated strings"
|
||||
+ " does not match the number of columns");
|
||||
}
|
||||
@@ -71,7 +72,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
String[] args = { "Oversize" };
|
||||
aggregator.setColumns(new Range[] {new Range(1,args[0].length()-1)});
|
||||
try {
|
||||
aggregator.aggregate(args);
|
||||
aggregator.aggregate(new DefaultFieldSet(args));
|
||||
fail("Invalid text length, exception should have been thrown");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
@@ -85,7 +86,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
public void testAggregate() {
|
||||
String[] args = { "Matchsize", "Smallsize" };
|
||||
aggregator.setColumns(new Range[] {new Range(1,9), new Range(10,18)});
|
||||
String result = aggregator.aggregate(args);
|
||||
String result = aggregator.aggregate(new DefaultFieldSet(args));
|
||||
assertEquals("MatchsizeSmallsize", result);
|
||||
}
|
||||
|
||||
@@ -95,7 +96,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
public void testAggregateWithLastRangeUnbound() {
|
||||
String[] args = { "Matchsize", "Smallsize" };
|
||||
aggregator.setColumns(new Range[] {new Range(1,12), new Range(13)});
|
||||
String result = aggregator.aggregate(args);
|
||||
String result = aggregator.aggregate(new DefaultFieldSet(args));
|
||||
assertEquals("Matchsize Smallsize", result);
|
||||
}
|
||||
|
||||
@@ -107,7 +108,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
String[] args = { "Matchsize", "Smallsize" };
|
||||
aggregator.setAlignment("right");
|
||||
aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,23)});
|
||||
String result = aggregator.aggregate(args);
|
||||
String result = aggregator.aggregate(new DefaultFieldSet(args));
|
||||
assertEquals(23,result.length());
|
||||
assertEquals(result, " Matchsize Smallsize");
|
||||
}
|
||||
@@ -119,7 +120,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
String[] args = { "Matchsize", "Smallsize" };
|
||||
aggregator.setAlignment("center");
|
||||
aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,25)});
|
||||
String result = aggregator.aggregate(args);
|
||||
String result = aggregator.aggregate(new DefaultFieldSet(args));
|
||||
assertEquals(result, " Matchsize Smallsize ");
|
||||
}
|
||||
|
||||
@@ -131,7 +132,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
aggregator.setPadding('.');
|
||||
aggregator.setAlignment("left");
|
||||
aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,24)});
|
||||
String result = aggregator.aggregate(args);
|
||||
String result = aggregator.aggregate(new DefaultFieldSet(args));
|
||||
assertEquals(result, "Matchsize....Smallsize..");
|
||||
}
|
||||
|
||||
@@ -142,7 +143,7 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
String[] args = { "Matchsize", "Smallsize" };
|
||||
aggregator.setAlignment("left");
|
||||
aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,24)});
|
||||
String result = aggregator.aggregate(args);
|
||||
String result = aggregator.aggregate(new DefaultFieldSet(args));
|
||||
assertEquals(result, "Matchsize Smallsize ");
|
||||
}
|
||||
|
||||
@@ -166,6 +167,6 @@ public class FixedLengthLineAggregatorTests extends TestCase {
|
||||
public void testAggregateNullArgument() {
|
||||
String[] args = { null };
|
||||
aggregator.setColumns(new Range[] {new Range(1,3)});
|
||||
assertEquals(" ", aggregator.aggregate(args));
|
||||
assertEquals(" ", aggregator.aggregate(new DefaultFieldSet(args)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
@@ -31,7 +33,7 @@ public class LineAggregatorItemTransformerTests extends TestCase {
|
||||
*/
|
||||
public void testSetAggregator() throws Exception {
|
||||
transformer.setAggregator(new LineAggregator() {
|
||||
public String aggregate(String[] args) {
|
||||
public String aggregate(FieldSet fieldSet) {
|
||||
return "foo";
|
||||
}
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.batch.io.file.transform;
|
||||
|
||||
import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
|
||||
|
||||
/**
|
||||
@@ -24,16 +24,16 @@ import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
*
|
||||
* @author robert.kasanicky
|
||||
*/
|
||||
public class LineAggregatorStub implements LineAggregator {
|
||||
public class StubLineAggregator implements LineAggregator {
|
||||
|
||||
/**
|
||||
* Concatenates arguments. Ignores the LineDescriptor.
|
||||
* Concatenates arguments.
|
||||
*/
|
||||
public String aggregate(String[] args) {
|
||||
public String aggregate(FieldSet fieldSet) {
|
||||
String result = "";
|
||||
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
result = result + args[i];
|
||||
for (int i = 1; i < fieldSet.getFieldCount(); i++) {
|
||||
result = result + fieldSet.readString(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -21,6 +21,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
import org.springframework.batch.item.writer.ItemTransformer;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
@@ -29,89 +31,94 @@ import org.springframework.batch.sample.domain.Customer;
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
|
||||
/**
|
||||
* Converts <code>Order</code> object to a String.
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class OrderTransformer implements ItemTransformer {
|
||||
|
||||
/**
|
||||
* Aggregators for all types of lines in the output file
|
||||
*/
|
||||
private Map aggregators;
|
||||
/**
|
||||
* Aggregators for all types of lines in the output file
|
||||
*/
|
||||
private Map aggregators;
|
||||
|
||||
/**
|
||||
* Converts information from an Order object to a collection of Strings for output.
|
||||
*/
|
||||
public Object transform(Object data) {
|
||||
Order order = (Order) data;
|
||||
|
||||
List result = new ArrayList();
|
||||
/**
|
||||
* Converts information from an Order object to a collection of Strings for
|
||||
* output.
|
||||
*/
|
||||
public Object transform(Object data) {
|
||||
Order order = (Order) data;
|
||||
|
||||
result.add(getAggregator("header").aggregate(OrderFormatterUtils.headerArgs(order)));
|
||||
result.add(getAggregator("customer").aggregate(OrderFormatterUtils.customerArgs(order)));
|
||||
result.add(getAggregator("address").aggregate(OrderFormatterUtils.billingAddressArgs(order)));
|
||||
result.add(getAggregator("billing").aggregate(OrderFormatterUtils.billingInfoArgs(order)));
|
||||
List result = new ArrayList();
|
||||
|
||||
List items = order.getLineItems();
|
||||
LineItem item;
|
||||
result.add(getAggregator("header").aggregate(OrderFormatterUtils.headerArgs(order)));
|
||||
result.add(getAggregator("customer").aggregate(OrderFormatterUtils.customerArgs(order)));
|
||||
result.add(getAggregator("address").aggregate(OrderFormatterUtils.billingAddressArgs(order)));
|
||||
result.add(getAggregator("billing").aggregate(OrderFormatterUtils.billingInfoArgs(order)));
|
||||
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
item = (LineItem) items.get(i);
|
||||
result.add(getAggregator("item").aggregate(OrderFormatterUtils.lineItemArgs(item)));
|
||||
}
|
||||
List items = order.getLineItems();
|
||||
LineItem item;
|
||||
|
||||
result.add(getAggregator("footer").aggregate(OrderFormatterUtils.footerArgs(order)));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setAggregators(Map aggregators) {
|
||||
this.aggregators = aggregators;
|
||||
}
|
||||
|
||||
private LineAggregator getAggregator(String name) {
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
item = (LineItem) items.get(i);
|
||||
result.add(getAggregator("item").aggregate(OrderFormatterUtils.lineItemArgs(item)));
|
||||
}
|
||||
|
||||
result.add(getAggregator("footer").aggregate(OrderFormatterUtils.footerArgs(order)));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setAggregators(Map aggregators) {
|
||||
this.aggregators = aggregators;
|
||||
}
|
||||
|
||||
private LineAggregator getAggregator(String name) {
|
||||
return (LineAggregator) aggregators.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility class encapsulating formatting of <code>Order</code> and its nested objects.
|
||||
*/
|
||||
/**
|
||||
* Utility class encapsulating formatting of <code>Order</code> and its
|
||||
* nested objects.
|
||||
*/
|
||||
private static class OrderFormatterUtils {
|
||||
|
||||
|
||||
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
||||
|
||||
static String[] headerArgs(Order order) {
|
||||
return new String[] { "BEGIN_ORDER:", String.valueOf(order.getOrderId()), dateFormat.format(order.getOrderDate()) };
|
||||
static FieldSet headerArgs(Order order) {
|
||||
return new DefaultFieldSet(new String[] { "BEGIN_ORDER:", String.valueOf(order.getOrderId()),
|
||||
dateFormat.format(order.getOrderDate()) });
|
||||
}
|
||||
|
||||
static String[] footerArgs(Order order) {
|
||||
return new String[] { "END_ORDER:", order.getTotalPrice().toString() };
|
||||
static FieldSet footerArgs(Order order) {
|
||||
return new DefaultFieldSet(new String[] { "END_ORDER:", order.getTotalPrice().toString() });
|
||||
}
|
||||
|
||||
static String[] customerArgs(Order order) {
|
||||
static FieldSet customerArgs(Order order) {
|
||||
Customer customer = order.getCustomer();
|
||||
|
||||
return new String[] { "CUSTOMER:", String.valueOf(customer.getRegistrationId()), customer.getFirstName(),
|
||||
customer.getMiddleName(), customer.getLastName() };
|
||||
return new DefaultFieldSet(new String[] { "CUSTOMER:", String.valueOf(customer.getRegistrationId()),
|
||||
customer.getFirstName(), customer.getMiddleName(), customer.getLastName() });
|
||||
}
|
||||
|
||||
static String[] lineItemArgs(LineItem item) {
|
||||
return new String[] { "ITEM:", String.valueOf(item.getItemId()), item.getPrice().toString() };
|
||||
static FieldSet lineItemArgs(LineItem item) {
|
||||
return new DefaultFieldSet(new String[] { "ITEM:", String.valueOf(item.getItemId()),
|
||||
item.getPrice().toString() });
|
||||
}
|
||||
|
||||
static String[] billingAddressArgs(Order order) {
|
||||
static FieldSet billingAddressArgs(Order order) {
|
||||
Address address = order.getBillingAddress();
|
||||
|
||||
return new String[] { "ADDRESS:", address.getAddrLine1(), address.getCity(), address.getZipCode() };
|
||||
return new DefaultFieldSet(new String[] { "ADDRESS:", address.getAddrLine1(), address.getCity(),
|
||||
address.getZipCode() });
|
||||
}
|
||||
|
||||
static String[] billingInfoArgs(Order order) {
|
||||
static FieldSet billingInfoArgs(Order order) {
|
||||
BillingInfo billingInfo = order.getBilling();
|
||||
|
||||
return new String[] { "BILLING:", billingInfo.getPaymentId(), billingInfo.getPaymentDesc() };
|
||||
return new DefaultFieldSet(new String[] { "BILLING:", billingInfo.getPaymentId(),
|
||||
billingInfo.getPaymentDesc() });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
|
||||
|
||||
@@ -24,16 +25,16 @@ import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
*
|
||||
* @author robert.kasanicky
|
||||
*/
|
||||
public class LineAggregatorStub implements LineAggregator {
|
||||
public class StubLineAggregator implements LineAggregator {
|
||||
|
||||
/**
|
||||
* Concatenates arguments. Ignores the LineDescriptor.
|
||||
*/
|
||||
public String aggregate(String[] args) {
|
||||
public String aggregate(FieldSet fieldSet) {
|
||||
String result = "";
|
||||
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
result = result + args[i];
|
||||
for (int i = 1; i < fieldSet.getFieldCount(); i++) {
|
||||
result = result + fieldSet.readString(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -12,7 +12,7 @@ import junit.framework.TestCase;
|
||||
import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.writer.AbstractItemWriter;
|
||||
import org.springframework.batch.sample.LineAggregatorStub;
|
||||
import org.springframework.batch.sample.StubLineAggregator;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
@@ -56,7 +56,7 @@ public class FlatFileOrderWriterTests extends TestCase {
|
||||
order.setTotalPrice(BigDecimal.valueOf(0));
|
||||
|
||||
//create aggregator stub
|
||||
LineAggregator aggregator = new LineAggregatorStub();
|
||||
LineAggregator aggregator = new StubLineAggregator();
|
||||
|
||||
//create map of aggregators and set it to writer
|
||||
Map aggregators = new HashMap();
|
||||
|
||||
Reference in New Issue
Block a user