예제 #1
0
  @Override
  public StatusChamado getById(int codigo) throws BusinessException {
    Connection con = null;
    String sql = null;

    try {
      // Obtem a conexão
      con = Conexao.getInstance().obterConexao();

      String origem = Conexao.getInstance().obterOrigem();
      sql = FabricaSql.getSql(origem + PROCURAR_STATUS_BY_ID);

      if (DEBUG) System.out.println("SQL - " + sql);

      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setInt(1, codigo);

      ResultSet rs = stmt.executeQuery();
      StatusChamado status = null;
      while (rs.next()) {
        String nome = rs.getString("NOME");
        status = new StatusChamado(nome);
      }
      rs.close();
      stmt.close();
      return status;

    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return null;

    } finally {
      Conexao.getInstance().fecharConexao(con);
    }
  }
예제 #2
0
  public boolean adicionaStatus(StatusChamado status) throws BusinessException {
    Connection con = null;
    String sql = null;

    try {
      con = Conexao.getInstance().obterConexao();
      con.setAutoCommit(false);

      String origem = Conexao.getInstance().obterOrigem();
      sql = FabricaSql.getSql(origem + INSERIR_STATUS);

      if (DEBUG) System.out.println(sql);

      PreparedStatement stmt = con.prepareStatement(sql);
      // TODO -colocar os parametros

      int qtd = stmt.executeUpdate();

      if (DEBUG) System.out.println("QTDE: " + qtd);

      if (qtd != 1) {
        con.rollback();
        stmt.close();
        throw new BusinessException("Quantidade de linhas afetadas inválida: " + qtd);
      } else con.commit();
      stmt.close();
    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return false;

    } finally {
      Conexao.getInstance().fecharConexao(con);
    }
    return true;
  }
예제 #3
0
  public int procurarStatus(StatusChamado status) throws BusinessException {
    Connection con = null;
    String sql = null;

    try {
      // Obtem a conexão
      con = Conexao.getInstance().obterConexao();

      String origem = Conexao.getInstance().obterOrigem();
      sql = FabricaSql.getSql(origem + PROCURAR_STATUS);

      if (DEBUG) System.out.println("SQL - " + sql);

      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setString(1, status.getNome());

      ResultSet rs = stmt.executeQuery();

      int codigo = -1;

      while (rs.next()) {
        codigo = rs.getInt("CODIGO");
      }
      rs.close();
      stmt.close();
      return codigo;

    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return -1;

    } finally {
      Conexao.getInstance().fecharConexao(con);
    }
  }
예제 #4
0
  @Override
  public Usuario getById(int codigo) throws BusinessException {
    Connection con = null;
    String sql = null;

    try {
      // Obtem a conexão
      con = Conexao.obterConexao();
      con.setAutoCommit(false);

      String origem = Conexao.obterOrigem();
      sql = FabricaSql.getSql(origem + OBTER_USUARIO_BY_ID);

      if (DEBUG) System.out.println("SQL - " + sql);

      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setInt(1, codigo);

      ResultSet rs = stmt.executeQuery();
      Usuario usu = null;
      while (rs.next()) {
        usu = new Usuario();
        String nome = rs.getString("NOME");
        String username = rs.getString("USERNAME");

        usu.setNome(nome);
        usu.setUsername(username);
      }
      rs.close();
      stmt.close();
      return usu;

    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return null;
    } finally {
      Conexao.fecharConexao(con);
    }
  }
예제 #5
0
  /** TODO - Documentar */
  @Override
  public int obterCodigo(Usuario user) throws BusinessException {
    Connection con = null;
    String sql = null;

    if (DEBUG) {
      System.out.println("USER:"******"SENHA:" + user.getPassword());
    }

    try {
      // Obtem a conexão
      con = Conexao.obterConexao();
      con.setAutoCommit(false);

      String origem = Conexao.obterOrigem();
      sql = FabricaSql.getSql(origem + OBTER_CODIGO_USUARIO);

      if (DEBUG) System.out.println("SQL - " + sql);

      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setString(1, user.getUsername());

      ResultSet rs = stmt.executeQuery();
      int r = -1;
      while (rs.next()) {
        r = Integer.parseInt(rs.getString("cod_usuario"));
      }
      rs.close();
      stmt.close();
      return r;

    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return -1;
    } finally {
      Conexao.fecharConexao(con);
    }
  }
예제 #6
0
  /**
   * TODO - Documentar
   *
   * @throws BusinessException
   */
  @Override
  public Usuario autenticar(Usuario user) throws BusinessException {
    Connection con = null;
    String sql = null;

    try {
      // Obtem a conexão
      con = Conexao.obterConexao();
      con.setAutoCommit(false);

      String origem = Conexao.obterOrigem();
      sql = FabricaSql.getSql(origem + AUTENTICAR_USER);

      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setString(1, user.getUsername());
      stmt.setString(2, user.getPassword());

      ResultSet rs = stmt.executeQuery();
      Usuario usuario = null;
      while (rs.next()) {
        int codigo = Integer.parseInt(rs.getString("CODIGO"));
        String nome = rs.getString("NOME");
        usuario = user;
        usuario.setCodigo(codigo);
        usuario.setNome(nome);
      }
      rs.close();
      stmt.close();
      if (Utils.isNullOrEmpty(usuario)) throw new BusinessException("Usuário não encontrado!");
      else if (Utils.isNullOrEmpty(usuario.getNome()))
        throw new BusinessException("Usuário não possui nome.");
      return usuario;
    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return null;
    } finally {
      Conexao.fecharConexao(con);
    }
  }
예제 #7
0
  @Override
  public List<StatusChamado> listarTodos() throws BusinessException {
    Connection con = null;
    String sql = null;

    try {
      // Obtem a conexão
      con = Conexao.getInstance().obterConexao();

      String origem = Conexao.getInstance().obterOrigem();
      sql = FabricaSql.getSql(origem + LISTAR_TODOS_STATUS);

      if (DEBUG) System.out.println("SQL - " + sql);

      PreparedStatement stmt = con.prepareStatement(sql);
      ResultSet rs = stmt.executeQuery();

      List<StatusChamado> listaStatus = new ArrayList<StatusChamado>();
      while (rs.next()) {
        String nome = rs.getString("NOME");
        StatusChamado status = new StatusChamado(nome);
        int codigo = rs.getInt("CODIGO");
        status.setCodigo(codigo);
        listaStatus.add(status);
      }
      rs.close();
      stmt.close();
      return listaStatus;

    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return null;

    } finally {
      Conexao.getInstance().fecharConexao(con);
    }
  }
예제 #8
0
  /** TODO - Documentar */
  @Override
  public boolean addUsuario(Usuario user) throws BusinessException {
    Connection con = null;
    String sql = null;

    try {
      con = Conexao.obterConexao();
      con.setAutoCommit(false);

      String origem = Conexao.obterOrigem();
      sql = FabricaSql.getSql(origem + INSERIR_USER);

      if (DEBUG) System.out.println(sql);

      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setString(1, user.getUsername());
      stmt.setString(2, user.getPassword());

      int qtd = stmt.executeUpdate();

      if (DEBUG) System.out.println("QTDE: " + qtd);

      if (qtd != 1) {
        stmt.close();
        con.rollback();
        throw new BusinessException("Quantidade de linhas afetadas inválida: " + qtd);
      } else con.commit();
      stmt.close();
    } catch (SQLException e) {
      SQLExceptionHandler.tratarSQLException(this.getClass().getName(), e);
      return false;

    } finally {
      Conexao.fecharConexao(con);
    }
    return true;
  }