示例#1
0
  @Override
  public boolean crearPedido(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();

        stmt.addBatch(
            "INSERT INTO pedidos (id_proveedor,fecha, estado) "
                + "VALUES("
                + pedido.getIdProveedor()
                + ", STR_TO_DATE('"
                + pedido.getFecha()
                + "','%d-%m-%Y'), 'P')");

        for (int i = 0; i < pedido.getNumLineasPedido(); i++)
          stmt.addBatch(
              "INSERT INTO linea_pedido (id_pedido, id_prod, precio_uni, cantidad) "
                  + "VALUES("
                  + "(SELECT coalesce(MAX(id),'1') FROM pedidos), "
                  + pedido.getLineaPedido(i).getIdProducto()
                  + ", "
                  + pedido.getLineaPedido(i).getPrecio()
                  + ", "
                  + pedido.getLineaPedido(i).getCantidad()
                  + ")");

        for (int current : stmt.executeBatch()) if (current != 1) error = true;

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

        stmt.addBatch(
            "INSERT INTO pedidos (id_proveedor,fecha, estado) "
                + "VALUES("
                + pedido.getIdProveedor()
                + ", STR_TO_DATE('"
                + pedido.getFecha()
                + "','%d-%m-%Y'), 'P')");

        for (int i = 0; i < pedido.getNumLineasPedido(); i++)
          stmt.addBatch(
              "INSERT INTO linea_pedido (id_pedido, id_prod, precio_uni, cantidad) "
                  + "VALUES("
                  + "(SELECT coalesce(MAX(id),'1') FROM pedidos), "
                  + pedido.getLineaPedido(i).getIdProducto()
                  + ", "
                  + pedido.getLineaPedido(i).getPrecio()
                  + ", "
                  + pedido.getLineaPedido(i).getCantidad()
                  + ")");

        for (int current : stmt.executeBatch()) if (current != 1) error = true;

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

    return !error;
  }