View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.sample.domain.order.internal;
18  
19  import java.util.Map;
20  
21  import org.springframework.batch.item.file.transform.LineAggregator;
22  import org.springframework.batch.sample.domain.order.LineItem;
23  import org.springframework.batch.sample.domain.order.Order;
24  
25  /**
26   * Converts <code>Order</code> object to a list of strings.
27   * 
28   * @author Dave Syer
29   * @author Dan Garrette
30   */
31  public class OrderLineAggregator implements LineAggregator<Order> {
32  
33  	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
34  
35  	private Map<String, LineAggregator<Object>> aggregators;
36  
37  	public String aggregate(Order order) {
38  		StringBuilder result = new StringBuilder();
39  
40  		result.append(aggregators.get("header").aggregate(order) + LINE_SEPARATOR);
41  		result.append(aggregators.get("customer").aggregate(order) + LINE_SEPARATOR);
42  		result.append(aggregators.get("address").aggregate(order) + LINE_SEPARATOR);
43  		result.append(aggregators.get("billing").aggregate(order) + LINE_SEPARATOR);
44  
45  		for (LineItem lineItem : order.getLineItems()) {
46  			result.append(aggregators.get("item").aggregate(lineItem) + LINE_SEPARATOR);
47  		}
48  
49  		result.append(aggregators.get("footer").aggregate(order));
50  
51  		return result.toString();
52  	}
53  
54  	/**
55  	 * Set aggregators for all types of lines in the output file
56  	 * 
57  	 * @param aggregators
58  	 */
59  	public void setAggregators(Map<String, LineAggregator<Object>> aggregators) {
60  		this.aggregators = aggregators;
61  	}
62  
63  }