/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.siso.horario.MySqlDAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import pe.siso.horario.Beans.UbigeoDocente;
import pe.siso.horario.DAO.UbigeoDocenteDAO;

/**
 *
 * @author sistem16user
 */
class MySqlUbigeoDocenteDAO implements UbigeoDocenteDAO {

    public MySqlUbigeoDocenteDAO() {
    }

    @Override
    public ArrayList<UbigeoDocente> cargarDepartamento() throws Exception {
        ArrayList<UbigeoDocente> lista = new ArrayList<UbigeoDocente>();
        String base = "horario";

        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();

            String query = "SELECT DISTINCT cod_dep_ubi,nom_dep_ubi FROM ubigeo ORDER BY nom_dep_ubi";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                UbigeoDocente ubigeo = new UbigeoDocente();
                ubigeo.setCod_dep_ubi(rs.getInt("cod_dep_ubi"));
                ubigeo.setNom_dep_ubi(rs.getString("nom_dep_ubi"));
                lista.add(ubigeo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;

    }

    @Override
    public ArrayList<UbigeoDocente> cargarProvincia(int codigoDepartamento) throws Exception {
        ArrayList<UbigeoDocente> lista = new ArrayList<UbigeoDocente>();
        String base = "horario";

        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT DISTINCT cod_pro_ubi,nom_pro_ubi FROM ubigeo WHERE cod_dep_ubi='" + codigoDepartamento + "' ORDER BY nom_pro_ubi";

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                UbigeoDocente ubigeo = new UbigeoDocente();
                ubigeo.setCod_pro_ubi(rs.getInt("cod_pro_ubi"));
                ubigeo.setNom_pro_ubi(rs.getString("nom_pro_ubi"));
                lista.add(ubigeo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

    @Override
    public ArrayList<UbigeoDocente> cargarDistrito(int codigoProv, int codDep) throws Exception {
        ArrayList<UbigeoDocente> lista = new ArrayList<UbigeoDocente>();
        String base = "horario";

        Connection cnx = null;
        try {
            cnx = MySqlDAOFactory.obtenerConexion(base);
            Statement st = cnx.createStatement();
            String query = "SELECT DISTINCT cod_dis_ubi,nom_dis_ubi,cod_ubi FROM ubigeo WHERE cod_dep_ubi='" + codDep + "' and  cod_pro_ubi='" + codigoProv + "' ORDER BY nom_dis_ubi";

            ResultSet rs = st.executeQuery(query);
            System.out.println(query);
            while (rs.next()) {
                UbigeoDocente ubigeo = new UbigeoDocente();
                ubigeo.setCod_dis_ubi(rs.getInt("cod_dis_ubi"));
                ubigeo.setNom_dis_ubi(rs.getString("nom_dis_ubi"));
                lista.add(ubigeo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }
        return lista;
    }

}
