/**
 * 
 */
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.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;

/**
 * @author David
 * 
 */
public class ClienteDAOTest extends TestCase {

	public static Logger log = Logger.getLogger(ClienteDAOTest.class);

	ClienteDAO dao;
	ApplicationContext contexto;

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();

		contexto = new ClassPathXmlApplicationContext("applicationContext.xml");
		dao = (ClienteDAO) contexto.getBean("clienteDAO");
	}

	/**
	 * 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());
		 */
		Cliente cliente = (Cliente) dao.findById(new Integer(1));
		if (cliente != null) {
			log.debug(cliente.toString());

		}

		assertNotNull(cliente);
	}

	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 Clientes");

			// Run an SQL query on the table.
			ResultSet rs = ps.executeQuery();
			// Display all the data in the table
			Cliente cliente = null;
			while (rs.next()) {
				 cliente = new Cliente();
				String numCliente = rs.getString(1);
				String nombre = rs.getString(2);
				String direccion = rs.getString(3);
				String poblacion = rs.getString(4);
				String CP = rs.getString(5);
				String provincia = rs.getString(6);
				String CIF =rs.getString(7);
				String telefono = rs.getString(8);
				String fax = rs.getString(9);
				boolean activo = rs.getBoolean(10);
				cliente.setIdentificador(Integer.valueOf(numCliente));
				cliente.setNombre(nombre);
				cliente.setDireccion(direccion);
				cliente.setPoblacion(poblacion);
				cliente.setCp(CP);
				cliente.setProvincia(provincia);
				cliente.setCif(String.valueOf(CIF));
				cliente.setTelefono(telefono);
				
				if(telefono==null)
					cliente.setTelefono("");
				
				if(fax==null)
					cliente.setFax("");
				else 
					cliente.setFax(fax);
				cliente.setActivo(Boolean.valueOf(activo));
				log.debug(cliente.toString());
				dao.merge(cliente);
				
			}

			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());
		}

	}

}
