001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.sample.domain.order.internal;
018
019import java.util.Map;
020
021import org.springframework.batch.item.file.transform.LineAggregator;
022import org.springframework.batch.sample.domain.order.LineItem;
023import org.springframework.batch.sample.domain.order.Order;
024
025/**
026 * Converts <code>Order</code> object to a list of strings.
027 * 
028 * @author Dave Syer
029 * @author Dan Garrette
030 */
031public class OrderLineAggregator implements LineAggregator<Order> {
032
033        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
034
035        private Map<String, LineAggregator<Object>> aggregators;
036
037        @Override
038        public String aggregate(Order order) {
039                StringBuilder result = new StringBuilder();
040
041                result.append(aggregators.get("header").aggregate(order) + LINE_SEPARATOR);
042                result.append(aggregators.get("customer").aggregate(order) + LINE_SEPARATOR);
043                result.append(aggregators.get("address").aggregate(order) + LINE_SEPARATOR);
044                result.append(aggregators.get("billing").aggregate(order) + LINE_SEPARATOR);
045
046                for (LineItem lineItem : order.getLineItems()) {
047                        result.append(aggregators.get("item").aggregate(lineItem) + LINE_SEPARATOR);
048                }
049
050                result.append(aggregators.get("footer").aggregate(order));
051
052                return result.toString();
053        }
054
055        /**
056         * Set aggregators for all types of lines in the output file
057         * 
058         * @param aggregators Map of LineAggregators used to map the various record types for
059         *                                      each order
060         */
061        public void setAggregators(Map<String, LineAggregator<Object>> aggregators) {
062                this.aggregators = aggregators;
063        }
064
065}