Ejemplo n.º 1
1
  public Calendar addCalendarsUsingBatch(String nameCalendar) throws IOException {
    View.header("Add Calendars using Batch");
    BatchRequest batch = client.batch();

    // Create the callback.
    JsonBatchCallback<Calendar> callback =
        new JsonBatchCallback<Calendar>() {

          public void onSuccess(Calendar calendar, GoogleHeaders responseHeaders) {
            View.display(calendar);
            addedCalendarsUsingBatch.add(calendar);
          }

          @Override
          public void onFailure(GoogleJsonError e, GoogleHeaders responseHeaders) {
            System.out.println("Error Message: " + e.getMessage());
          }
        };

    // Create 2 Calendar Entries to insert.
    Calendar entry1 = new Calendar().setSummary(nameCalendar);
    client.calendars().insert(entry1).queue(batch, callback);

    //    Calendar entry2 = new Calendar().setSummary("Calendar for Testing 2");
    //    client.calendars().insert(entry2).queue(batch, callback);

    batch.execute();
    return entry1;
  }
Ejemplo n.º 2
1
  public void deleteCalendarsUsingBatch() throws IOException {
    View.header("Delete Calendars Using Batch");
    BatchRequest batch = client.batch();
    for (Calendar calendar : addedCalendarsUsingBatch) {
      client
          .calendars()
          .delete(calendar.getId())
          .queue(
              batch,
              new JsonBatchCallback<Void>() {

                public void onSuccess(Void content, GoogleHeaders responseHeaders) {
                  System.out.println("Delete is successful!");
                }

                @Override
                public void onFailure(GoogleJsonError e, GoogleHeaders responseHeaders) {
                  System.out.println("Error Message: " + e.getMessage());
                }
              });
    }

    batch.execute();
  }
Ejemplo n.º 3
0
  @Override
  protected void doInBackground() throws IOException {
    Log.d(TAG, "delete the event" + ": " + birthday);
    BatchRequest batch = client.batch();
    try {
      for (String eventId : birthday.getEventId()) {
        client
            .events()
            .delete(calendar_id, eventId)
            .queue(
                batch,
                new JsonBatchCallback<Void>() {

                  @Override
                  public void onFailure(GoogleJsonError err, HttpHeaders headers)
                      throws IOException {
                    if (fragment.isCancelAyncTasks()) return;
                    Utils.logAndShowError(fragment, TAG, err.getMessage());
                  }

                  public void onSuccess(Void arg0, HttpHeaders arg1) throws IOException {
                    Log.d(TAG, "delete one event");
                    // TODO Auto-generated method stub

                  }
                });
      }
      //            if(fragment.isCancelAyncTasks()) {
      //            	cancel(true);
      //            	return;
      //            }
      batch.execute();
    } catch (GoogleJsonResponseException e) {
      if (e.getStatusCode() != 404) {
        throw e;
      }
    }
  }
  /**
   * Incrementally completes a transfer request.
   *
   * <p>Google drive has an API rate limit of 10 requests per second, and a maximum request duration
   * of 60 seconds. At the theoretical maximum, 600 requests could be completed in this time span,
   * however in practice this value is likely much lower. As such, an incremental approach is
   * required to allow for such large requests. The returned TransferRequest will contain the
   * updated state after all requests that took place this session have completed.
   *
   * <p>The limit parameter can be used to help implement a progress bar, as the total number of
   * requests is known and the number of requests per call is configurable. To get a higher
   * precision, use a smaller limit.
   *
   * @param requestId The id of the transfer request
   * @param limit The maximum number of files to transfer during this request. This is required to
   *     ensure we do not exceed the rate limit imposed by the Drive API. If not specified, files
   *     will be transferred until the request is forced to end.
   * @param user The user who is completing the transfer request
   * @return the current state of the transfer request, with successfully transfered files removed.
   * @throws BadRequestException If the requested limit is greater than 600
   * @throws ForbiddenException If the user is not authorized to complete this transfer request
   * @throws NotFoundException If the transfer request cannot be found
   */
  @ApiMethod(name = "user.request.accept", httpMethod = HttpMethod.POST)
  public TransferRequest acceptRequest(
      @Named("request") long requestId, @Named("limit") @Nullable Integer limit, User user)
      throws BadRequestException, ForbiddenException, NotFoundException,
          InternalServerErrorException {

    TransferRequest request = getRequest(requestId, user);
    if (limit == null) {
      limit = 600;
    } else if (limit > 600 || limit <= 0) {
      throw new BadRequestException(
          String.format(Constants.Error.INVALID_TRANSFER_LIMIT, limit, 0, 600));
    }

    final Drive service = Utils.createDriveFromUser(user);
    final List<TransferableFile> success = new ArrayList<>();
    final Permission owner = new Permission();
    owner.setRole("owner");
    owner.setType("user");
    owner.setValue(request.getRequestingUser().getEmail());

    BatchRequest updateBatch = service.batch();
    final BatchRequest insertBatch = service.batch();

    for (final TransferableFile file : request.getFiles()) {

      try {
        service
            .permissions()
            .update(file.getFileId(), request.getRequestingUser().getPermission(), owner)
            .setTransferOwnership(true)
            .queue(
                updateBatch,
                new JsonBatchCallback<Permission>() {
                  @Override
                  public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders)
                      throws IOException {
                    log.log(
                        Level.INFO,
                        "Could not update ownership of file "
                            + file.getFileId()
                            + " ("
                            + file.getFileName()
                            + ").  Attempting to insert permission",
                        e);
                    service
                        .permissions()
                        .insert(file.getFileId(), owner)
                        .queue(
                            insertBatch,
                            new JsonBatchCallback<Permission>() {
                              @Override
                              public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders)
                                  throws IOException {
                                log.log(
                                    Level.SEVERE,
                                    "Could not transfer ownership of file "
                                        + file.getFileId()
                                        + " ("
                                        + file.getFileName()
                                        + ")",
                                    e);
                                log.log(Level.WARNING, e.getMessage() + ": " + e.getErrors());
                              }

                              @Override
                              public void onSuccess(
                                  Permission permission, HttpHeaders responseHeaders)
                                  throws IOException {
                                if (success.size() <= 5) {
                                  if (success.size() == 5) {
                                    log.info("...");
                                  } else {
                                    log.info(
                                        "Transferred file "
                                            + file.getFileId()
                                            + " ("
                                            + file.getFileName()
                                            + ")");
                                  }
                                }
                                success.add(file);
                              }
                            });
                  }

                  @Override
                  public void onSuccess(Permission permission, HttpHeaders responseHeaders)
                      throws IOException {
                    success.add(file);
                  }
                });
        success.add(file);
      } catch (IOException e) {
        throw new InternalServerErrorException(Constants.Error.FAILED_DRIVE_REQUEST, e);
      }

      if (updateBatch.size() >= limit) {
        break;
      }
    }

    try {
      updateBatch.execute();
    } catch (IOException e) {
      log.log(Level.SEVERE, "Error executing batch request", e);
    }

    if (insertBatch.size() > 0) {
      log.log(
          Level.INFO,
          "There were failures.  Attempting to insert " + insertBatch.size() + " new permissions");
      try {
        insertBatch.execute();
      } catch (IOException e) {
        log.log(Level.SEVERE, "Error executing batch request", e);
      }
    }

    request.getFiles().removeAll(success);

    // If there are no files left, delete the request, otherwise save it for future completion.
    if (request.getFiles().isEmpty()) {
      OfyService.ofy().delete().entity(request).now();
    } else {
      OfyService.ofy().save().entity(request).now();
    }
    OfyService.ofy().clear();
    return request;
  }