package sac.pessoa.endereco.jdbc;


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

import sac.exception.NullArgumentException;
import sac.persistencia.OID;
import sac.persistencia.PersistenceException;
import sac.pessoa.endereco.Endereco;
import sac.pessoa.util.jdbc.PersistenteFactory;


public class PersistenteFactory_Endereco implements PersistenteFactory {

    private static PersistenteFactory_Endereco instancia;

    public static synchronized PersistenteFactory_Endereco getInstancia() throws PersistenceException {
        if (instancia == null) {
            instancia = new PersistenteFactory_Endereco();
        }
        return instancia;
    }

    public Object produce (Connection con, ResultSet rs) throws PersistenceException{
        Endereco end = null;
        try {
            if (rs.next()){
                String rua = rs.getString("tx_endereco");
                String bairro = rs.getString("tx_bairro");
                String cep = rs.getString("tx_cep");
                String cidade = rs.getString("tx_cidade");
                String estado = rs.getString("tx_estado");
                String pais = rs.getString("tx_pais");
                String telefone = rs.getString("tx_telefone");
                String email = rs.getString("tx_email");
                OID oid = new OID (rs.getLong("cd_endereco"));
                end =  new Endereco (rua, bairro, cidade, estado, pais, cep, telefone);
                end.setEmail( email);
                end.setId( oid);
            }
            return end;
        } catch(NullArgumentException nulle){
            throw new PersistenceException ("Problemas no Banco", nulle );
        } catch(SQLException sqle){
            throw new PersistenceException ("Problemas no Banco", sqle );
        }
    }
}