/** Deletes the old feed items for which extension settings have been created. */
  private static void deleteOldFeedItems(
      AdWordsServices adWordsServices, AdWordsSession session, Set<Long> feedItemIds, Feed feed)
      throws Exception {
    // Get the FeedItemService.
    FeedItemServiceInterface feedItemService =
        adWordsServices.get(session, FeedItemServiceInterface.class);

    if (feedItemIds.isEmpty()) {
      return;
    }

    List<FeedItemOperation> operations = Lists.newArrayList();
    for (Long feedItemId : feedItemIds) {
      FeedItemOperation operation = new FeedItemOperation();

      FeedItem feedItem = new FeedItem();
      feedItem.setFeedId(feed.getId());
      feedItem.setFeedItemId(feedItemId);

      operation.setOperand(feedItem);
      operation.setOperator(Operator.REMOVE);

      operations.add(operation);
    }

    feedItemService.mutate(operations.toArray(new FeedItemOperation[operations.size()]));
  }
  /** Returns the feed items for a feed. */
  private static List<FeedItem> getFeedItems(
      AdWordsServices adWordsServices, AdWordsSession session, Feed feed) throws Exception {
    // Get the FeedItemService.
    FeedItemServiceInterface feedItemService =
        adWordsServices.get(session, FeedItemServiceInterface.class);
    String query =
        String.format(
            "SELECT FeedItemId, AttributeValues, Scheduling WHERE Status = 'ENABLED' AND FeedId = %d",
            feed.getId());

    List<FeedItem> feedItems = Lists.newArrayList();
    int offset = 0;
    FeedItemPage feedItemPage;

    do {
      String pageQuery = String.format(query + " LIMIT %d, %d", offset, PAGE_SIZE);
      feedItemPage = feedItemService.query(pageQuery);
      if (feedItemPage.getEntries() != null) {
        feedItems.addAll(Arrays.asList(feedItemPage.getEntries()));
      }
      offset += PAGE_SIZE;
    } while (offset < feedItemPage.getTotalNumEntries());

    return feedItems;
  }