Search in sources :

Example 1 with ResultType

use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.

the class GoogleTasksExporter method getTasks.

private ExportResult<TaskContainerResource> getTasks(Tasks tasksService, IdOnlyContainerResource resource, Optional<PaginationData> paginationData) throws IOException {
    Tasks.TasksOperations.List query = tasksService.tasks().list(resource.getId()).setMaxResults(PAGE_SIZE);
    if (paginationData.isPresent()) {
        query.setPageToken(((StringPaginationToken) paginationData.get()).getToken());
    }
    com.google.api.services.tasks.model.Tasks result = query.execute();
    List<TaskModel> newTasks = result.getItems().stream().map(t -> new TaskModel(resource.getId(), t.getTitle(), t.getNotes(), t.getCompleted() != null ? Instant.ofEpochMilli(t.getCompleted().getValue()) : null, t.getDue() != null ? Instant.ofEpochMilli(t.getDue().getValue()) : null)).collect(Collectors.toList());
    PaginationData newPage = null;
    ResultType resultType = ResultType.END;
    if (result.getNextPageToken() != null) {
        newPage = new StringPaginationToken(result.getNextPageToken());
        resultType = ResultType.CONTINUE;
    }
    TaskContainerResource taskContainerResource = new TaskContainerResource(null, newTasks);
    return new ExportResult<>(resultType, taskContainerResource, new ContinuationData(newPage));
}
Also used : ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) GoogleStaticObjects(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects) ExportInformation(org.datatransferproject.types.common.ExportInformation) TaskContainerResource(org.datatransferproject.types.common.models.tasks.TaskContainerResource) ImmutableList(com.google.common.collect.ImmutableList) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Credential(com.google.api.client.auth.oauth2.Credential) TaskLists(com.google.api.services.tasks.model.TaskLists) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) TaskModel(org.datatransferproject.types.common.models.tasks.TaskModel) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) Exporter(org.datatransferproject.spi.transfer.provider.Exporter) IOException(java.io.IOException) Tasks(com.google.api.services.tasks.Tasks) PaginationData(org.datatransferproject.types.common.PaginationData) UUID(java.util.UUID) Instant(java.time.Instant) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Collectors(java.util.stream.Collectors) TaskListModel(org.datatransferproject.types.common.models.tasks.TaskListModel) List(java.util.List) TaskList(com.google.api.services.tasks.model.TaskList) Monitor(org.datatransferproject.api.launcher.Monitor) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) Optional(java.util.Optional) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PaginationData(org.datatransferproject.types.common.PaginationData) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) TaskContainerResource(org.datatransferproject.types.common.models.tasks.TaskContainerResource) TaskModel(org.datatransferproject.types.common.models.tasks.TaskModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 2 with ResultType

use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.

the class GoogleTasksExporter method getTasksList.

private ExportResult<TaskContainerResource> getTasksList(Tasks tasksService, Optional<PaginationData> paginationData) throws IOException {
    Tasks.Tasklists.List query = tasksService.tasklists().list().setMaxResults(PAGE_SIZE);
    if (paginationData.isPresent()) {
        query.setPageToken(((StringPaginationToken) paginationData.get()).getToken());
    }
    TaskLists result = query.execute();
    ImmutableList.Builder<TaskListModel> newTaskListsBuilder = ImmutableList.builder();
    ImmutableList.Builder<IdOnlyContainerResource> newResourcesBuilder = ImmutableList.builder();
    for (TaskList taskList : result.getItems()) {
        newTaskListsBuilder.add(new TaskListModel(taskList.getId(), taskList.getTitle()));
        newResourcesBuilder.add(new IdOnlyContainerResource(taskList.getId()));
    }
    PaginationData newPage = null;
    ResultType resultType = ResultType.END;
    if (result.getNextPageToken() != null) {
        newPage = new StringPaginationToken(result.getNextPageToken());
        resultType = ResultType.CONTINUE;
    }
    List<IdOnlyContainerResource> newResources = newResourcesBuilder.build();
    if (!newResources.isEmpty()) {
        resultType = ResultType.CONTINUE;
    }
    TaskContainerResource taskContainerResource = new TaskContainerResource(newTaskListsBuilder.build(), null);
    ContinuationData continuationData = new ContinuationData(newPage);
    newResourcesBuilder.build().forEach(continuationData::addContainerResource);
    return new ExportResult<>(resultType, taskContainerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ImmutableList(com.google.common.collect.ImmutableList) TaskList(com.google.api.services.tasks.model.TaskList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) TaskContainerResource(org.datatransferproject.types.common.models.tasks.TaskContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) TaskLists(com.google.api.services.tasks.model.TaskLists) TaskListModel(org.datatransferproject.types.common.models.tasks.TaskListModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 3 with ResultType

use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.

the class FlickrPhotosExporter method getPhotos.

private ExportResult<PhotosContainerResource> getPhotos(IdOnlyContainerResource resource, PaginationData paginationData) {
    String photoSetId = resource.getId();
    int page = paginationData == null ? 1 : ((IntPaginationToken) paginationData).getStart();
    PhotoList<Photo> photoSetList;
    try {
        if (Strings.isNullOrEmpty(photoSetId)) {
            RequestContext.getRequestContext().setExtras(EXTRAS);
            perUserRateLimiter.acquire();
            photoSetList = photosInterface.getNotInSet(PHOTO_PER_PAGE, page);
            RequestContext.getRequestContext().setExtras(ImmutableList.of());
        } else {
            perUserRateLimiter.acquire();
            photoSetList = photosetsInterface.getPhotos(photoSetId, ImmutableSet.copyOf(EXTRAS), 0, PHOTO_PER_PAGE, page);
        }
    } catch (FlickrException e) {
        return new ExportResult<>(e);
    }
    boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.isEmpty();
    Collection<PhotoModel> photos = photoSetList.stream().map(p -> toCommonPhoto(p, photoSetId)).collect(Collectors.toList());
    PaginationData newPage = null;
    if (hasMore) {
        newPage = new IntPaginationToken(page + 1);
    }
    // Get result type
    ResultType resultType = ResultType.CONTINUE;
    if (newPage == null) {
        resultType = ResultType.END;
    }
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(null, photos);
    return new ExportResult<>(resultType, photosContainerResource, new ContinuationData(newPage));
}
Also used : FlickrException(com.flickr4java.flickr.FlickrException) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) Photoset(com.flickr4java.flickr.photosets.Photoset) AppCredentials(org.datatransferproject.types.transfer.auth.AppCredentials) REST(com.flickr4java.flickr.REST) TransferServiceConfig(org.datatransferproject.types.transfer.serviceconfig.TransferServiceConfig) RateLimiter(com.google.common.util.concurrent.RateLimiter) ArrayList(java.util.ArrayList) ExportInformation(org.datatransferproject.types.common.ExportInformation) Photo(com.flickr4java.flickr.photos.Photo) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) Flickr(com.flickr4java.flickr.Flickr) PhotoList(com.flickr4java.flickr.photos.PhotoList) Auth(com.flickr4java.flickr.auth.Auth) ImmutableSet(com.google.common.collect.ImmutableSet) PhotosInterface(com.flickr4java.flickr.photos.PhotosInterface) Collection(java.util.Collection) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) Exporter(org.datatransferproject.spi.transfer.provider.Exporter) UUID(java.util.UUID) PaginationData(org.datatransferproject.types.common.PaginationData) Collectors(java.util.stream.Collectors) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Photosets(com.flickr4java.flickr.photosets.Photosets) List(java.util.List) AuthData(org.datatransferproject.types.transfer.auth.AuthData) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RequestContext(com.flickr4java.flickr.RequestContext) PhotosetsInterface(com.flickr4java.flickr.photosets.PhotosetsInterface) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) PaginationData(org.datatransferproject.types.common.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 4 with ResultType

use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.

the class GoogleCalendarExporter method exportCalendars.

private ExportResult<CalendarContainerResource> exportCalendars(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    Calendar.CalendarList.List listRequest;
    CalendarList listResult;
    // Get calendar information
    try {
        listRequest = getOrCreateCalendarInterface(authData).calendarList().list();
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            Preconditions.checkState(paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX), "Token is not applicable");
            listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(CALENDAR_TOKEN_PREFIX.length()));
        }
        listResult = listRequest.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    // Set up continuation data
    PaginationData nextPageData = null;
    if (listResult.getNextPageToken() != null) {
        nextPageData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + listResult.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    // Process calendar list
    List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
    for (CalendarListEntry calendarData : listResult.getItems()) {
        CalendarModel model = convertToCalendarModel(calendarData);
        continuationData.addContainerResource(new IdOnlyContainerResource(calendarData.getId()));
        calendarModels.add(model);
    }
    CalendarContainerResource calendarContainerResource = new CalendarContainerResource(calendarModels, null);
    // Get result type
    ExportResult.ResultType resultType = ResultType.CONTINUE;
    if (calendarModels.isEmpty()) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, calendarContainerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) CalendarList(com.google.api.services.calendar.model.CalendarList) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 5 with ResultType

use of org.datatransferproject.spi.transfer.provider.ExportResult.ResultType in project data-transfer-project by google.

the class GoogleMailExporter method export.

@Override
public ExportResult<MailContainerResource> export(UUID id, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
    // Create a new gmail service for the authorized user
    Gmail gmail = getOrCreateGmail(authData);
    Messages.List request = null;
    try {
        request = gmail.users().messages().list(USER).setMaxResults(PAGE_SIZE);
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    if (exportInformation.isPresent() && exportInformation.get().getPaginationData() != null) {
        request.setPageToken(((StringPaginationToken) exportInformation.get().getPaginationData()).getToken());
    }
    ListMessagesResponse response = null;
    try {
        response = request.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    List<MailMessageModel> results = new ArrayList<>(response.getMessages().size());
    // as we can't store all the mail messages in memory at once.
    for (Message listMessage : response.getMessages()) {
        Message getResponse = null;
        try {
            getResponse = gmail.users().messages().get(USER, listMessage.getId()).setFormat("raw").execute();
        } catch (IOException e) {
            return new ExportResult<>(e);
        }
        // TODO: note this doesn't transfer things like labels
        results.add(new MailMessageModel(getResponse.getRaw(), getResponse.getLabelIds()));
    }
    PaginationData newPage = null;
    ResultType resultType = ResultType.END;
    if (response.getNextPageToken() != null) {
        newPage = new StringPaginationToken(response.getNextPageToken());
        resultType = ResultType.CONTINUE;
    }
    MailContainerResource mailContainerResource = new MailContainerResource(null, results);
    return new ExportResult<>(resultType, mailContainerResource, new ContinuationData(newPage));
}
Also used : Messages(com.google.api.services.gmail.Gmail.Users.Messages) PaginationData(org.datatransferproject.types.common.PaginationData) Message(com.google.api.services.gmail.model.Message) Gmail(com.google.api.services.gmail.Gmail) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Aggregations

ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)14 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)14 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)14 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)12 PaginationData (org.datatransferproject.types.common.PaginationData)11 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)7 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)6 ImmutableList (com.google.common.collect.ImmutableList)4 ExportInformation (org.datatransferproject.types.common.ExportInformation)4 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)4 FlickrException (com.flickr4java.flickr.FlickrException)2 Photoset (com.flickr4java.flickr.photosets.Photoset)2 Photosets (com.flickr4java.flickr.photosets.Photosets)2 Credential (com.google.api.client.auth.oauth2.Credential)2 TaskList (com.google.api.services.tasks.model.TaskList)2 TaskLists (com.google.api.services.tasks.model.TaskLists)2 InputStream (java.io.InputStream)2