Exemplo n.º 1
0
  @Override
  public boolean borrarPedido(TransferPedido pedido) throws DAOException {
    boolean error = false;

    Statement stmt = null;

    // Get the connection from the transaction
    Connection connection = null;
    try {
      connection = (Connection) TransactionManager.getInstancia().getTransaction().getResource();
    } catch (ClassCastException ex) {
    }
    ;

    if (connection == null) {
      // if we dont find the transaction, make the sentence with no transaction
      try {
        connection = BBDDConnection.getConnection();
        stmt = connection.createStatement();
        int editadas =
            stmt.executeUpdate(
                "UPDATE pedidos SET "
                    + "activo = 0 "
                    + "WHERE id = "
                    + pedido.getId()
                    + " AND activo = 1");
        if (editadas != 1) {
          error = true;
        }

      } catch (SQLException ex) {
        throw new DAOException(ex);
      }
    } else {
      try {
        stmt = connection.createStatement();

        int editadas =
            stmt.executeUpdate(
                "UPDATE pedidos SET "
                    + "activo = 0 "
                    + "WHERE id = "
                    + pedido.getId()
                    + " AND activo = 1");
        if (editadas != 1) {
          error = true;
        }

      } catch (SQLException ex) {
        throw new DAOException(ex);
      }
    }

    return !error;
  }
Exemplo n.º 2
0
  @Override
  public TransferPedido consultarPedido(TransferPedido pedido, int lockMode) throws DAOException {

    TransferPedido out = new TransferPedido();

    Statement stmt = null;
    ResultSet rs = null;

    // Get the connection from the transaction
    Connection connection = null;
    try {
      connection = (Connection) TransactionManager.getInstancia().getTransaction().getResource();
    } catch (ClassCastException ex) {
    }
    ;

    if (connection == null || lockMode == 0) {
      // if we dont find the transaction, make the sentence with no transaction
      try {
        if (connection == null) connection = BBDDConnection.getConnection();
        stmt = connection.createStatement();

        stmt.execute(
            "SELECT id, fecha, id_proveedor, estado FROM pedidos "
                + "WHERE id = '"
                + pedido.getId()
                + "' AND activo = '1'");
        rs = stmt.getResultSet();

        if (rs.next()) {

          out.setId(rs.getInt(1));
          out.setFecha(rs.getString(2));
          out.setIdProveedor(rs.getInt(3));
          out.setEstado(rs.getString(4).toCharArray()[0]);

          stmt.execute(
              "SELECT id_prod, cantidad, precio_uni, id_pedido FROM linea_pedido "
                  + "WHERE id_pedido = '"
                  + pedido.getId()
                  + "'");

          rs = stmt.getResultSet();
          while (rs.next()) {
            out.addLineaPedido(rs.getInt(1), rs.getInt(2), rs.getDouble(3), rs.getInt(4));
          }

        } else {
          out.setId(-1);
        }
      } catch (SQLException sqlex) {
        throw new DAOException(sqlex);
      }
    } else {
      try {
        stmt = connection.createStatement();

        stmt.execute(
            "SELECT id, fecha, id_proveedor, estado FROM pedidos "
                + "WHERE id = '"
                + pedido.getId()
                + "' AND activo = '1' FOR UPDATE");
        rs = stmt.getResultSet();

        if (rs.next()) {

          out.setId(rs.getInt(1));
          out.setFecha(rs.getString(2));
          out.setIdProveedor(rs.getInt(3));
          out.setEstado(rs.getString(4).toCharArray()[0]);

          stmt.execute(
              "SELECT id_prod, cantidad, precio_uni, id_pedido FROM linea_pedido "
                  + "WHERE id_pedido = '"
                  + pedido.getId()
                  + "' FOR UPDATE");

          rs = stmt.getResultSet();
          while (rs.next()) {
            out.addLineaPedido(rs.getInt(1), rs.getInt(2), rs.getDouble(3), rs.getInt(4));
          }

        } else {
          out.setId(-1);
        }
      } catch (SQLException sqlex) {
        throw new DAOException(sqlex);
      }
    }

    return out;
  }