package br.com.senac.dao;

import br.com.senac.model.Email;

import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import br.com.senac.model.Cliente;

public class ClienteDAO extends DAOSpringJdbc<Cliente> {

	public ClienteDAO(DataSource dataSource) {
		super(dataSource);
	}

	@Override
	protected Cliente buildEntity(ResultSet rs) {
		Cliente c = new Cliente();
		try {
			c.setId(rs.getInt("id"));
			c.setNome(rs.getString("nome"));
			c.setEndereco(rs.getString("endereco"));
			
            List<Email> le = new ArrayList<Email>();
            
            DAOFactory f = DAOFactory.getInstance();
            le = f.getEmailDAO().findByCriteria("select * from Email where idCliente = " + c.getId());
            c.setEmails(le);
            
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
		}
		return c;
	}

	@Override
	protected String buildQueryFindAll() {
		return "select id, nome, endereco from Cliente";
	}
	@Override
	protected String buildQueryFindById() {
		return "select id, nome, endereco from Cliente where id = ?";
	}	
}
