package main.java.org.bankio.writer;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.beanio.BeanIOConfigurationException;
import org.beanio.BeanWriter;
import org.beanio.StreamFactory;

import main.java.org.bankio.bean.BeanCfonb120;
import main.java.org.bankio.bean.BeanCfonb120Additional;
import main.java.org.bankio.bean.BeanCfonb120Movement;
import main.java.org.bankio.bean.BeanCfonb160;
import main.java.org.bankio.bean.BeanCfonb160Recipient;
import main.java.org.bankio.reader.ReaderCfonb;

public class WriterCfonb {
	private static final String CFONB120 = "cfonb120";
	private static final String CFONB120_XML = "cfonb120.xml";
	private static final String CFONB160 = "cfonb160";
	private static final String CFONB160_XML = "cfonb160.xml";
	
	public static void setBeanCfonb120ToFile(BeanCfonb120 beanCfonb120) throws BeanIOConfigurationException, IOException {
	        // create a StreamFactory
	        StreamFactory factory = StreamFactory.newInstance();
	        factory.load(ReaderCfonb.class.getClassLoader().getResourceAsStream(CFONB120_XML));
	        
	        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	        Date date = new Date();
	        
	        // set writer mapping file
	        BeanWriter writer = factory.createWriter(CFONB120, new File(CFONB120 + dateFormat.format(date)));
	        
	        writer.write(beanCfonb120.getOldAmount());
	        
	        HashMap<Integer, BeanCfonb120Movement> beanCfonb120Movements = beanCfonb120.getMovements();
	        for (BeanCfonb120Movement beanCfonb120Movement : beanCfonb120Movements.values()) {
	        	writer.write(beanCfonb120Movement);
	        	List<BeanCfonb120Additional> lstBeanCfonb120Additional = beanCfonb120Movement.getAdditionals();
	        	for (BeanCfonb120Additional beanCfonb120Additional : lstBeanCfonb120Additional) {
	        		 writer.write(beanCfonb120Additional);
				}
	        }
	        
	        writer.write(beanCfonb120.getNewAmount());
	        
	        closeWriter(writer);
	    }

	public static void setBeanCfonb160ToFile(BeanCfonb160 beanCfonb160) throws BeanIOConfigurationException, IOException {
        // create a StreamFactory
        StreamFactory factory = StreamFactory.newInstance();
        factory.load(ReaderCfonb.class.getClassLoader().getResourceAsStream(CFONB160_XML));
        
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        
        // set writer mapping file
        BeanWriter writer = factory.createWriter(CFONB160, new File(CFONB160 + dateFormat.format(date)));
        
        writer.write(beanCfonb160.getSender());
        
        HashMap<Integer, BeanCfonb160Recipient> beanCfonb160Recipients = beanCfonb160.getRecipients();
        for (BeanCfonb160Recipient beanCfonb160Recipient : beanCfonb160Recipients.values()) {
        	writer.write(beanCfonb160Recipient);
        	writer.write(beanCfonb160Recipient.getRecipientAdditional());
        }
        
        writer.write(beanCfonb160.getTotal());
        
        closeWriter(writer);
    }
	
	private static void closeWriter(BeanWriter writer) {
		writer.flush();
		writer.close();
	}
}

@@ -927,36 +927,36 @@
 %0A%09public static 
-void
+File
  setBeanCfonb120
@@ -1354,32 +1354,115 @@
 te();%0D%0A%09        
+File outputFile = new File(CFONB120 + dateFormat.format(date));%0D%0A%09        %09        
 %0D%0A%09        // se
@@ -1547,53 +1547,19 @@
 120,
- new File(CFONB120 + dateFormat.format(date))
+outputFile 
 );%0D%0A
@@ -2247,24 +2247,65 @@
 r(writer);%0D%0A
+%09        %0D%0A%09        return outputFile; %0D%0A
 %09    %7D%0D%0A%0D%0A%09p
@@ -2317,20 +2317,20 @@
  static 
-void
+File
  setBean
@@ -2725,24 +2725,97 @@
 ew Date();%0D%0A
+        File outputFile = new File(CFONB160 + dateFormat.format(date));%0D%0A
         %0D%0A  
@@ -2911,52 +2911,18 @@
 60, 
-new File(CFONB160 + dateFormat.format(date))
+outputFile
 );%0D%0A
@@ -3413,24 +3413,63 @@
 r(writer);%0D%0A
+        %0D%0A        return outputFile; %0D%0A
     %7D%0D%0A%09%0D%0A%09p

Improve writer