Example #1
0
  /**
   * Prompts the user for a set of operations and submits them in a batch request.
   *
   * @param reader to read input from the keyboard.
   * @throws ServiceException when the request causes an error in the Google Spreadsheets service.
   * @throws IOException when an error occurs in communication with the Google Spreadsheets service.
   */
  public void processBatchRequest(BufferedReader reader) throws IOException, ServiceException {

    final String BATCH_PROMPT =
        "Enter set operations one by one, "
            + "then enter submit to send the batch request:\n"
            + " set row# col# value  [[add a set operation]]\n"
            + " submit               [[submit the request]]";

    CellFeed batchRequest = new CellFeed();

    // Prompt user for operation
    System.out.println(BATCH_PROMPT);
    String operation = reader.readLine();
    while (!operation.startsWith("submit")) {
      String[] s = operation.split(" ", 4);
      if (s.length != 4 || !s[0].equals("set")) {
        System.out.println("Invalid command: " + operation);
        operation = reader.readLine();
        continue;
      }

      // Create a new cell entry and add it to the batch request.
      int row = Integer.parseInt(s[1]);
      int col = Integer.parseInt(s[2]);
      String value = s[3];
      CellEntry batchOperation = createUpdateOperation(row, col, value);
      batchRequest.getEntries().add(batchOperation);

      // Display the current entries in the batch request.
      printBatchRequest(batchRequest);

      // Prompt for another operation.
      System.out.println(BATCH_PROMPT);
      operation = reader.readLine();
    }

    // Get the batch feed URL and submit the batch request
    CellFeed feed = service.getFeed(cellFeedUrl, CellFeed.class);
    Link batchLink = feed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
    URL batchUrl = new URL(batchLink.getHref());
    CellFeed batchResponse = service.batch(batchUrl, batchRequest);

    // Print any errors that may have happened.
    boolean isSuccess = true;
    for (CellEntry entry : batchResponse.getEntries()) {
      String batchId = BatchUtils.getBatchId(entry);
      if (!BatchUtils.isSuccess(entry)) {
        isSuccess = false;
        BatchStatus status = BatchUtils.getBatchStatus(entry);
        System.out.println(
            "\n" + batchId + " failed (" + status.getReason() + ") " + status.getContent());
      }
    }
    if (isSuccess) {
      System.out.println("Batch operations successful.");
    }
  }
  public void performBatchUpdate(CellFeed batchRequest, URL cellFeedUrl)
      throws IOException, ServiceException {

    logger.info("\n Begin Batch Update \n");

    long startTime = System.currentTimeMillis();

    // printBatchRequest(batchRequest);

    CellFeed cellFeed = spreadsheetService.getFeed(cellFeedUrl, CellFeed.class);

    // Submit the update
    // set header work around for http://code.google.com/p/gdata-java-client/issues/detail?id=103
    spreadsheetService.setHeader("If-Match", "*");
    Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
    //        CellFeed batchResponse = spreadsheetService.batch(new URL(batchLink.getHref()),
    // batchRequest);
    spreadsheetService.batch(new URL(batchLink.getHref()), batchRequest);
    spreadsheetService.setHeader("If-Match", null);

    logger.info("\n ms elapsed for batch update: \n" + (System.currentTimeMillis() - startTime));

    // Check the results
    //        boolean isSuccess = true;
    //        for (CellEntry entry : batchResponse.getEntries()) {
    //           String batchId = BatchUtils.getBatchId(entry);
    //           if (!BatchUtils.isSuccess(entry)) {
    //             isSuccess = false;
    //             BatchStatus status = BatchUtils.getBatchStatus(entry);
    //             System.out.printf("%s failed (%s) %s", batchId, status.getReason(),
    // status.getContent());
    //
    //           }
    //
    //            break;
    //        }
    //
    //        logger.info(isSuccess ? "\nBatch operations successful." : "\nBatch operations
    // failed");
    // System.out.printf("\n%s ms elapsed\n", System.currentTimeMillis() - startTime);

  }
Example #3
0
 /**
  * Gets a link to the batch update URL for this worksheet.
  *
  * @return
  */
 Link getBatchUpdateLink() {
   return cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
 }