示例#1
0
  /** TODO - Documentar */
  @Override
  public boolean updateUser(Usuario user) throws BusinessException {
    Connection con = null;
    String sql = "UPDATE usuario SET senha=? WHERE login=?";

    try {
      con = Conexao.obterConexao();
      PreparedStatement stmt = con.prepareStatement(sql);

      stmt.setString(1, user.getPassword());
      stmt.setString(2, user.getUsername());

      int qtd = stmt.executeUpdate();

      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.fecharConexao(con);
    }
    return true;
  }
示例#2
0
  /**
   * @param user
   * @return
   */
  public static boolean atualizarPerfil(Usuario user) throws BusinessException {
    // Usa o utilitario para criptografar a senha
    user.setPassword(MD5Encryption.encript(user.getPassword()));

    DAOUsuario dao = new SQLUsuario();
    boolean atualizado = dao.updateUser(user);

    return atualizado;
  }
示例#3
0
  /**
   * @param user
   * @return
   */
  public static boolean criarUser(Usuario user) throws BusinessException {
    // Usa o utilitario para criptografar a senha
    user.setPassword(MD5Encryption.encript(user.getPassword()));

    DAOUsuario dao = new SQLUsuario();

    boolean inserido = dao.addUsuario(user);
    return inserido;
  }
示例#4
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);
    }
  }
示例#5
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);
    }
  }
示例#6
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;
  }