package com.freiheit.fuava.simplebatch.persist;

import java.io.File;
import java.io.FileWriter;
import java.io.OutputStreamWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.freiheit.fuava.simplebatch.result.Result;
import com.google.common.base.Preconditions;


/**
 * @param <Input>
 * @param <Output>
 */
public class ControlFilePersistence<Input> extends SingleItemPersistence<Input, FilePersistenceOutputInfo, ControlFilePersistenceOutputInfo> {
    private static final Logger LOG = LoggerFactory.getLogger( ControlFilePersistence.class );
    
    public interface Configuration {
    	String getDownloadDirPath();
    	String getControlFileEnding();
    }

    
	private Configuration config;
	private File basedir;
    
    public ControlFilePersistence(Configuration config) {
		this.config = config;
		basedir = new File( Preconditions.checkNotNull(config.getDownloadDirPath()) );
	}
    

    @Override
    public Result<Input, ControlFilePersistenceOutputInfo> persistItem(Result<Input, FilePersistenceOutputInfo> r) {
		if (r.isFailed()) {
			return Result.<Input, ControlFilePersistenceOutputInfo>builder(r).failed();
		}
		Input input = r.getInput();
		try {		
			File f = r.getOutput().getDataFile();
			
			final File ctl = new File( basedir, String.valueOf( System.currentTimeMillis() ) + "_done" + config.getControlFileEnding() );
			LOG.info("Writing ctl file " + ctl  + " (exists: " + ctl.exists()  + ") " + trimOut(r.getOutput()));
			OutputStreamWriter fos2 = new FileWriter( ctl );
			try {
				fos2.write( f.getName() );
			} finally {
				fos2.flush();
				fos2.close();
			}
			if (!ctl.exists()) {
				return Result.failed(input, "Control file does not exist after write: " + ctl);
			}
			LOG.info("Wroite ctl file ");
			return Result.success(input, new ControlFilePersistenceOutputInfo(ctl));

		} catch ( final Throwable t ) {
			return Result.failed(input, t);
		}
	}
    
	private String trimOut(FilePersistenceOutputInfo output) {
		String os = output == null ? "null" : output.toString();
		return output == null? "null" : os.substring(0,  Math.min(20, os.length()));
	}

}

@@ -695,39 +695,8 @@
    %0A
-%09private Configuration config;%0A
 %09pri
@@ -714,16 +714,16 @@
 asedir;%0A
+
     %0A   
@@ -781,32 +781,8 @@
 ) %7B%0A
-%09%09this.config = config;%0A
 %09%09ba
@@ -1231,74 +1231,33 @@
 ir, 
-String.valueOf( System.currentTimeMillis() ) + %22_done%22 + config.ge
+f.getName() + %22.ctl%22/*nex
 tCon
@@ -1264,24 +1264,24 @@
 trolFile
-Ending()
+name()*/
  );%0A%09%09%09L
@@ -1824,19 +1824,20 @@
 t);%0A%09%09%7D%0A
-
 %09%7D%0A
+%0A
     %0A%09pr

use downloaded file name for control file name, make sure the downloaded file name is more uniqu