001/*
002 * Copyright 2006-2018 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.item.file;
018
019import java.util.List;
020
021import org.springframework.batch.item.file.transform.LineAggregator;
022import org.springframework.batch.item.support.AbstractFileItemWriter;
023import org.springframework.core.io.Resource;
024import org.springframework.util.Assert;
025import org.springframework.util.ClassUtils;
026
027/**
028 * This class is an item writer that writes data to a file or stream. The writer
029 * also provides restart. The location of the output file is defined by a
030 * {@link Resource} and must represent a writable file.<br>
031 * 
032 * Uses buffered writer to improve performance.<br>
033 * 
034 * The implementation is <b>not</b> thread-safe.
035 * 
036 * @author Waseem Malik
037 * @author Tomas Slanina
038 * @author Robert Kasanicky
039 * @author Dave Syer
040 * @author Michael Minella
041 * @author Mahmoud Ben Hassine
042 */
043public class FlatFileItemWriter<T> extends AbstractFileItemWriter<T> {
044
045        protected LineAggregator<T> lineAggregator;
046
047        public FlatFileItemWriter() {
048                this.setExecutionContextName(ClassUtils.getShortName(FlatFileItemWriter.class));
049        }
050
051        /**
052         * Assert that mandatory properties (lineAggregator) are set.
053         * 
054         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
055         */
056        @Override
057        public void afterPropertiesSet() throws Exception {
058                Assert.notNull(lineAggregator, "A LineAggregator must be provided.");
059                if (append) {
060                        shouldDeleteIfExists = false;
061                }
062        }
063
064        /**
065         * Public setter for the {@link LineAggregator}. This will be used to
066         * translate the item into a line for output.
067         * 
068         * @param lineAggregator the {@link LineAggregator} to set
069         */
070        public void setLineAggregator(LineAggregator<T> lineAggregator) {
071                this.lineAggregator = lineAggregator;
072        }
073
074        @Override
075        public String doWrite(List<? extends T> items) {
076                StringBuilder lines = new StringBuilder();
077                for (T item : items) {
078                        lines.append(this.lineAggregator.aggregate(item)).append(this.lineSeparator);
079                }
080                return lines.toString();
081        }
082
083}