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 */
016package org.springframework.batch.sample.common;
017
018import org.apache.commons.io.FilenameUtils;
019import org.springframework.batch.core.StepExecution;
020import org.springframework.batch.core.annotation.BeforeStep;
021import org.springframework.batch.item.ExecutionContext;
022
023/**
024 * @author Dave Syer
025 * 
026 */
027public class OutputFileListener {
028
029        private String outputKeyName = "outputFile";
030
031        private String inputKeyName = "fileName";
032
033        private String path = "file:./build/output/";
034        
035        public void setPath(String path) {
036                this.path = path;
037        }
038
039        public void setOutputKeyName(String outputKeyName) {
040                this.outputKeyName = outputKeyName;
041        }
042
043        public void setInputKeyName(String inputKeyName) {
044                this.inputKeyName = inputKeyName;
045        }
046
047        @BeforeStep
048        public void createOutputNameFromInput(StepExecution stepExecution) {
049                ExecutionContext executionContext = stepExecution.getExecutionContext();
050                String inputName = stepExecution.getStepName().replace(":", "-");
051                if (executionContext.containsKey(inputKeyName)) {
052                        inputName = executionContext.getString(inputKeyName);
053                }
054                if (!executionContext.containsKey(outputKeyName)) {
055                        executionContext.putString(outputKeyName, path + FilenameUtils.getBaseName(inputName)
056                                        + ".csv");
057                }
058        }
059
060}