/// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the OrderService.
      OrderService orderService = (OrderService) user.GetService(DfpService.v201306.OrderService);

      // Sets defaults for page and Statement.
      OrderPage page = new OrderPage();
      Statement statement = new Statement();
      int offset = 0;

      try {
        do {
          // Create a Statement to get all orders.
          statement.query = string.Format("LIMIT 500 OFFSET {0}", offset);

          // Get orders by Statement.
          page = orderService.getOrdersByStatement(statement);

          if (page.results != null && page.results.Length > 0) {
            int i = page.startIndex;
            foreach (Order order in page.results) {
              Console.WriteLine("{0}) Order with ID = '{1}', name = '{2}', and advertiser " +
                  "ID = '{3}' was found.", i, order.id, order.name, order.advertiserId);
              i++;
            }
          }

          offset += 500;
        } while (page.results != null && page.results.Length == 500);

        Console.WriteLine("Number of results found: {0}", page.totalResultSetSize);
      } catch (Exception ex) {
        Console.WriteLine("Failed to get all orders. Exception says \"{0}\"",
            ex.Message);
      }
    }
Exemplo n.º 2
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The DFP user object running the code example.</param>
        public override void Run(DfpUser user)
        {
            // Get the OrderService.
              OrderService orderService =
              (OrderService) user.GetService(DfpService.v201306.OrderService);

              // Create Statement text to select all draft orders.
              string statementText = "WHERE status IN (:status1, :status2) and endDateTime >= :today " +
              "AND isArchived = FALSE LIMIT 500";
              Statement statement = new StatementBuilder("").
              AddValue("status1", OrderStatus.DRAFT.ToString()).
              AddValue("status2", OrderStatus.PENDING_APPROVAL.ToString()).
              AddValue("today", System.DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")).ToStatement();

              // Set defaults for page and offset.
              OrderPage page = new OrderPage();
              int i = 0;
              int offset = 0;
              List<string> orderIds = new List<string>();

              try {
            do {
              // Create a Statement to page through draft orders.
              statement.query = string.Format("{0} OFFSET {1}", statementText, offset);
              // Get orders by Statement.
              page = orderService.getOrdersByStatement(statement);

              if (page.results != null && page.results.Length > 0) {
            foreach (Order order in page.results) {
              Console.WriteLine("{0}) Order with ID = '{1}', name = '{2}', and status ='{3}' " +
                  "will be approved.", i, order.id, order.name, order.status);
              orderIds.Add(order.id.ToString());
              i++;
            }
              }

              offset += 500;
            } while (offset < page.totalResultSetSize);

            Console.WriteLine("Number of orders to be approved: {0}", orderIds.Count);

            if (orderIds.Count > 0) {
              // Create action Statement.
              statement = new StatementBuilder(
              string.Format("WHERE id IN ({0})", string.Join(",", orderIds.ToArray()))).
              ToStatement();

              // Create action.
              ApproveAndOverbookOrders action = new ApproveAndOverbookOrders();

              // Perform action.
              UpdateResult result = orderService.performOrderAction(action, statement);

              // Display results.
              if (result != null && result.numChanges > 0) {
            Console.WriteLine("Number of orders approved: {0}", result.numChanges);
              } else {
            Console.WriteLine("No orders were approved.");
              }
            }
              } catch (Exception ex) {
            Console.WriteLine("Failed to approve orders. Exception says \"{0}\"",
            ex.Message);
              }
        }