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.trade.internal;
018
019import java.util.Collections;
020
021import org.springframework.batch.item.ExecutionContext;
022import org.springframework.batch.item.ItemStream;
023import org.springframework.batch.item.ItemWriter;
024import org.springframework.batch.sample.domain.trade.CustomerCredit;
025import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
026import org.springframework.beans.factory.DisposableBean;
027
028/**
029 * Writes customer's credit information in a file.
030 *
031 * @see CustomerCreditDao
032 * @author Robert Kasanicky
033 */
034public class FlatFileCustomerCreditDao implements CustomerCreditDao,
035                DisposableBean {
036
037        private ItemWriter<String> itemWriter;
038
039        private String separator = "\t";
040
041        private volatile boolean opened = false;
042
043        @Override
044        public void writeCredit(CustomerCredit customerCredit) throws Exception {
045
046                if (!opened) {
047                        open(new ExecutionContext());
048                }
049
050                String line = "" + customerCredit.getName() + separator
051                                + customerCredit.getCredit();
052
053                itemWriter.write(Collections.singletonList(line));
054        }
055
056        public void setSeparator(String separator) {
057                this.separator = separator;
058        }
059
060        public void setItemWriter(ItemWriter<String> itemWriter) {
061                this.itemWriter = itemWriter;
062        }
063
064        public void open(ExecutionContext executionContext) throws Exception {
065                if (itemWriter instanceof ItemStream) {
066                        ((ItemStream) itemWriter).open(executionContext);
067                }
068                opened = true;
069        }
070
071        public void close() throws Exception {
072                if (itemWriter instanceof ItemStream) {
073                        ((ItemStream) itemWriter).close();
074                }
075        }
076
077        /*
078         * (non-Javadoc)
079         *
080         * @see org.springframework.beans.factory.DisposableBean#destroy()
081         */
082        @Override
083        public void destroy() throws Exception {
084                close();
085        }
086
087}