package com.ejie.p06b.pruebaWS;


import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

public class P06BServicioWebPrueba  {
	
	private String instanceId = "ID000001";
	private FileOutputStream out = null;
	private File fichero = null;
	
	public P06BServicioWebPrueba(){}
	
	public String AppendRequest(String XMLRequest)
    {
		System.out.println("AppendRequest");
        return "ID000001";
    }
	
	public String InitializeFile(String InstanceId, String FileName, String Overwrite )
    {
		if(!instanceId.equals(InstanceId)){
			return "ERROR";
		}
		System.out.println("InitializeFile - FileName:"+FileName);
		System.out.println("InitializeFile - Overwrite:"+Overwrite);
		
		try {
			fichero = new File("c:\\simuladorServMinisterio\\"+FileName+new Date().getTime()+".xml");
			out = new FileOutputStream(fichero);
		} catch (Exception e) {
			return "ERROR";
		}
        return "OK";
    }
	
	public String AppendChunk(String InstanceId, String Offset, String BufferSize)
    {
		if(!instanceId.equals(InstanceId)){
			return "ERROR";
		}
		System.out.println("AppendChunk - Offset:"+Offset);
		System.out.println("AppendChunk - BufferSize:"+BufferSize);
		try {
			String insertar="InstanceId:"+InstanceId;
			insertar+=" - ";
			insertar+="Offset:"+Offset;
			insertar+=" - ";
			insertar+="BufferSize:"+BufferSize;
			insertar+="\n";
			out.write(insertar.getBytes());
		} catch (Exception e) {
			return "ERROR";
		}
		System.out.println("AppendChunk");
        return "OK";
    }
	
	public String RemoveInstance(String InstanceId)
    {
		if(!instanceId.equals(InstanceId)){
			return "ERROR";
		}
		
		try {
			out.close();
			out=null;
		} catch (Exception e) {
			return "ERROR";
		}
		System.out.println("RemoveInstance");
        return "OK";
    }
}
