/**
 * 
 */
package es.dalocar.software.daos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import es.dalocar.software.beans.Cliente;
import es.dalocar.software.beans.Factura;
import es.dalocar.software.beans.FacturaId;

/**
 * @author David
 *
 */
public class FacturaDAOTest extends TestCase {
	
	public static Logger log = Logger.getLogger(FacturaDAOTest.class);
	
	FacturaDAO dao;
	ApplicationContext contexto;
	
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	
		contexto = new ClassPathXmlApplicationContext("applicationContext.xml");
		dao = (FacturaDAO)contexto.getBean("facturaDAO");
	}

	

	/**
	 * Test method for {@link es.dalocar.software.daos.ClienteDAO#findById(java.lang.Integer)}.
	 */
	public void testFindById() {
		/*Cliente cliente = (Cliente) dao.findById(new Integer(1));
		System.out.println(cliente.toString());*/
		FacturaId idFactura =  new FacturaId(new Integer(1), new Integer(2009));
		Factura factura = (Factura) dao.findById(idFactura);
		if(factura != null) {
			log.debug(factura.toString());
			
		}
		
		assertNotNull(factura);
	}
	
	/**
	 * Test method for {@link es.dalocar.software.daos.ClienteDAO#findById(java.lang.Integer)}.
	 */
	public void testInsert() {
		
		dao.attachDirty(getFactura());		

	}

	
	public Factura getFactura() {
		Factura aux = new Factura();
		FacturaId id = new FacturaId(new Integer(1),new Integer(2009));
		aux.setId(id);
		aux.setCobrada(new Boolean(true));
		aux.setFecha(new Date());
		aux.setFechacobro(new Date());
		
		aux.setImportefactura(new Double(90.20));
		aux.setIrpf(new Double(8.0));
		aux.setModopago("Tarjeta");
		Cliente cliente = new Cliente();
		cliente.setIdentificador(new Integer(1));
		aux.setCliente(cliente);
		return aux;		
		
	}

	public void testPopulateFromODBC() {

		// Suggestion: Load these from a properties object.
		String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
		String URL = "jdbc:odbc:FACTURACION";

		// Register the Native JDBC driver. If we can't
		// register the driver, the test can't continue.
		try {
			Class.forName(DRIVER);
		} catch (Exception e) {
			System.out.println("Driver failed to register.");
			System.out.println(e.getMessage());
			System.exit(1);
		}

		Connection c = null;
		Statement s = null;

		// 1) First, this program handles creating the table that
		// will be used by our prepared statements later.
		try {
			// Create the connection properties
			Properties properties = new Properties();

			// Connect to the local iSeries 400 database
			c = DriverManager.getConnection(URL, properties);
			PreparedStatement ps = c.prepareStatement("SELECT * FROM Factura");

			// Run an SQL query on the table.
			ResultSet rs = ps.executeQuery();
			// Display all the data in the table
			Factura factura = null;
			while (rs.next()) {
				factura = new Factura();
				Integer Numero=Integer.valueOf(rs.getInt(1));	
				Date fecha = rs.getDate(2);
				Integer Id_Cliente = Integer.valueOf(rs.getInt(3));
				Double IRPF = Double.valueOf(rs.getDouble(4));
				Integer any = Integer.valueOf(rs.getInt(5));
				boolean Pagada = rs.getBoolean(6);
				Date fecha_Pago = rs.getDate(7);
				String Modo_Pago = rs.getString(8);		
				if(Modo_Pago == null)
					Modo_Pago ="";
				Double TotalFactura = Double.valueOf(rs.getDouble(9));
				
				factura.setId(new FacturaId(Numero, any));
				factura.setFecha(fecha);
				factura.setFechacobro(fecha_Pago);
				factura.setImportefactura(TotalFactura);
				factura.setIrpf(IRPF);
				factura.setModopago(Modo_Pago);
				factura.setCliente(new Cliente(Id_Cliente));
				factura.setCobrada(Boolean.valueOf(Pagada));
				log.debug(factura.toString());
				dao.merge(factura);
				
			}

			List lista = dao.findAll();
			System.out.println(lista.size());
		} catch (SQLException sqle) {
			System.out.println("Database processing has failed.");
			System.out.println("Reason: " + sqle.getMessage());
		}

	}

}
