@Override
  public List<ImpiegatoTO> readAll() {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    List<ImpiegatoTO> listImpiegato = new ArrayList<ImpiegatoTO>();
    ;
    ResultSet resultSet = null;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("read_all_impiegato"));
      resultSet = statement.executeQuery();

      while (resultSet.next()) {
        ImpiegatoTO impiegato = new ImpiegatoTO();
        impiegato.setCf(resultSet.getString("cf"));
        impiegato.setNome(resultSet.getString("nome"));
        impiegato.setCognome(resultSet.getString("cognome"));
        impiegato.setDataNascita(resultSet.getDate("data_nascita").toLocalDate());
        impiegato.setTelefono(resultSet.getString("telefono"));
        impiegato.setAgenzia(resultSet.getString("agenzia"));
        impiegato.setUsername(resultSet.getString("username"));
        listImpiegato.add(impiegato);
      }

    } catch (SQLException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return listImpiegato;
  }
  private boolean fire(String cf) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    int result;
    boolean response = false;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("update_stato_impiegato"));
      int i = 1;
      statement.setString(i++, cf);

      result = statement.executeUpdate();

      if (result > 0) response = true;

    } catch (SQLException e) {
      e.printStackTrace();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return response;
  }
  @Override
  public boolean isPresent(String cf) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    boolean present = false;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("read_impiegato"));
      statement.setString(1, cf);

      resultSet = statement.executeQuery();
      if (resultSet.next()) present = true;

    } catch (SQLException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return present;
  }
  @Override
  public String readAgenzia(String cf) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    String agenzia = null;
    try {
      statement = conn.prepareStatement(queryFactory.getQuery("read_agenzia_impiegato"));
      statement.setString(1, cf);

      resultSet = statement.executeQuery();
      while (resultSet.next()) {
        agenzia = resultSet.getString("agenzia");
      }

    } catch (SQLException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return agenzia;
  }
  @Override
  public boolean create(ImpiegatoTO impiegato) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;

    int result = 0;
    boolean response = false;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("create_impiegato"));
      int i = 1;
      statement.setString(i++, impiegato.getCf());
      statement.setString(i++, impiegato.getNome());
      statement.setString(i++, impiegato.getCognome());
      statement.setDate(i++, java.sql.Date.valueOf(impiegato.getDataNascita()));
      statement.setString(i++, impiegato.getTelefono());
      statement.setString(i++, impiegato.getAgenzia());
      statement.setString(i++, impiegato.getUsername());
      statement.setString(i++, "attivo");

      result = statement.executeUpdate();

      if (result > 0) response = true;

    } catch (SQLException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return response;
  }
  @Override
  public List<AgenziaTO> readAll() {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    List<AgenziaTO> listAgenzia = new ArrayList<AgenziaTO>();
    ;
    ResultSet resultSet = null;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("read_all_agenzia"));
      resultSet = statement.executeQuery();

      while (resultSet.next()) {
        AgenziaTO agenzia = new AgenziaTO();
        agenzia.setId(resultSet.getString("id"));
        agenzia.setCitta(resultSet.getString("citta"));
        agenzia.setIndirizzo(resultSet.getString("indirizzo"));
        agenzia.setTelefono(resultSet.getString("telefono"));
        listAgenzia.add(agenzia);
      }

    } catch (SQLException e) {
      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return listAgenzia;
  }
  @Override
  public AgenziaTO read(String id) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    AgenziaTO agenzia = null;
    ResultSet resultSet = null;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("read_agenzia"));
      statement.setInt(1, Integer.parseInt(id));

      resultSet = statement.executeQuery();
      while (resultSet.next()) {
        agenzia = new AgenziaTO();
        agenzia.setId(resultSet.getString("id"));
        agenzia.setCitta(resultSet.getString("citta"));
        agenzia.setIndirizzo(resultSet.getString("indirizzo"));
        agenzia.setTelefono(resultSet.getString("telefono"));
      }

    } catch (SQLException e) {
      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return agenzia;
  }
  @Override
  public boolean deleteCitta(String citta) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    int result;
    boolean response = false;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("delete_citta_agenzia"));
      int i = 1;

      statement.setString(i++, citta);
      result = statement.executeUpdate();
      System.out.println(result);
      if (result > 0) {
        response = true;
      }

    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return response;
  }
  @Override
  public boolean create(AgenziaTO agenzia) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;

    int result = 0;
    boolean response = false;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("create_agenzia"));
      int i = 1;
      statement.setString(i++, agenzia.getCitta());
      statement.setString(i++, agenzia.getIndirizzo());
      statement.setString(i++, agenzia.getTelefono());
      statement.setString(i++, "aperta");

      result = statement.executeUpdate();

      if (result > 0) {
        response = true;
      }

    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return response;
  }
  @Override
  public ManagerTO readManager(String id) {
    Connection conn = MySqlConnectionFactory.getConnection();
    PreparedStatement statement = null;
    ManagerTO manager = null;
    ResultSet resultSet = null;

    try {
      statement = conn.prepareStatement(queryFactory.getQuery("read_manager_agenzia"));
      int i = 1;
      statement.setString(i++, id);
      statement.setString(i++, "manager");

      resultSet = statement.executeQuery();
      while (resultSet.next()) {
        manager = new ManagerTO();
        manager.setCf(resultSet.getString("cf"));
        manager.setNome(resultSet.getString("nome"));
        manager.setCognome(resultSet.getString("cognome"));
        manager.setDataNascita(resultSet.getDate("data_nascita").toLocalDate());
        manager.setTelefono(resultSet.getString("telefono"));
        manager.setAgenzia(resultSet.getString("agenzia"));
        manager.setUsername(resultSet.getString("username"));
      }

    } catch (SQLException e) {
      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    } finally {
      DbEntityCloser.close(statement);
      DbEntityCloser.close(conn);
    }

    return manager;
  }