Search in sources :

Example 26 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class GoogleCalendarExporter method getCalendarEvents.

private ExportResult<CalendarContainerResource> getCalendarEvents(TokensAndUrlAuthData authData, String id, Optional<PaginationData> pageData) {
    Calendar.Events.List listRequest;
    Events listResult;
    // Get event information
    try {
        listRequest = getOrCreateCalendarInterface(authData).events().list(id).setMaxAttendees(GoogleStaticObjects.MAX_ATTENDEES);
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            Preconditions.checkState(paginationToken.getToken().startsWith(EVENT_TOKEN_PREFIX), "Token is not applicable");
            listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(EVENT_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(EVENT_TOKEN_PREFIX + listResult.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    // Process event list
    List<CalendarEventModel> eventModels = new ArrayList<>(listResult.getItems().size());
    for (Event eventData : listResult.getItems()) {
        CalendarEventModel model = convertToCalendarEventModel(id, eventData);
        eventModels.add(model);
    }
    CalendarContainerResource calendarContainerResource = new CalendarContainerResource(null, eventModels);
    // Get result type
    ExportResult.ResultType resultType = ResultType.CONTINUE;
    if (nextPageData == null) {
        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) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) Events(com.google.api.services.calendar.model.Events) Event(com.google.api.services.calendar.model.Event) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 27 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class GoogleContactsExporter method exportContacts.

private ExportResult<ContactsModelWrapper> exportContacts(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    try {
        // Set up connection
        Connections.List connectionsListRequest = getOrCreatePeopleService(authData).people().connections().list(SELF_RESOURCE);
        // Get next page, if we have a page token
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            connectionsListRequest.setPageToken(paginationToken.getToken());
        }
        // Get list of connections (nb: not a list containing full info of each Person)
        ListConnectionsResponse response = connectionsListRequest.setPersonFields(PERSON_FIELDS).execute();
        List<Person> peopleList = response.getConnections();
        // Get list of resource names, then get list of Persons
        List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
        GetPeopleResponse batchResponse = getOrCreatePeopleService(authData).people().getBatchGet().setResourceNames(resourceNames).setPersonFields(PERSON_FIELDS).execute();
        List<PersonResponse> personResponseList = batchResponse.getResponses();
        // Convert Persons to VCards
        List<VCard> vCards = personResponseList.stream().map(a -> convert(a.getPerson())).collect(Collectors.toList());
        // Determine if there's a next page
        StringPaginationToken nextPageData = null;
        if (response.getNextPageToken() != null) {
            nextPageData = new StringPaginationToken(response.getNextPageToken());
        }
        ContinuationData continuationData = new ContinuationData(nextPageData);
        ContactsModelWrapper wrapper = new ContactsModelWrapper(makeVCardString(vCards));
        // Get result type
        ResultType resultType = ResultType.CONTINUE;
        if (nextPageData == null) {
            resultType = ResultType.END;
        }
        return new ExportResult<ContactsModelWrapper>(resultType, wrapper, continuationData);
    } catch (IOException e) {
        return new ExportResult<ContactsModelWrapper>(e);
    }
}
Also used : Connections(com.google.api.services.people.v1.PeopleService.People.Connections) VCARD_PRIMARY_PREF(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.VCARD_PRIMARY_PREF) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) Connections(com.google.api.services.people.v1.PeopleService.People.Connections) PhoneNumber(com.google.api.services.people.v1.model.PhoneNumber) StructuredName(ezvcard.property.StructuredName) GoogleStaticObjects(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects) ExportInformation(org.datatransferproject.types.common.ExportInformation) Person(com.google.api.services.people.v1.model.Person) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) PERSON_FIELDS(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.PERSON_FIELDS) Credential(com.google.api.client.auth.oauth2.Credential) LinkedList(java.util.LinkedList) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) VCard(ezvcard.VCard) PeopleService(com.google.api.services.people.v1.PeopleService) SOURCE_PARAM_NAME_TYPE(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.SOURCE_PARAM_NAME_TYPE) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) StringWriter(java.io.StringWriter) Telephone(ezvcard.property.Telephone) Exporter(org.datatransferproject.spi.transfer.provider.Exporter) IOException(java.io.IOException) PaginationData(org.datatransferproject.types.common.PaginationData) UUID(java.util.UUID) JCardWriter(ezvcard.io.json.JCardWriter) Collectors(java.util.stream.Collectors) SELF_RESOURCE(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.SELF_RESOURCE) List(java.util.List) FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) Email(ezvcard.property.Email) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Name(com.google.api.services.people.v1.model.Name) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 28 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class FlickrPhotosExporter method export.

@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, AuthData authData, Optional<ExportInformation> exportInformation) {
    Auth auth;
    try {
        auth = FlickrUtils.getAuth(authData, flickr);
    } catch (FlickrException e) {
        return new ExportResult<>(e);
    }
    RequestContext.getRequestContext().setAuth(auth);
    // in that container instead of the whole user library
    if (exportInformation.isPresent() && exportInformation.get().getContainerResource() instanceof PhotosContainerResource) {
        return exportPhotosContainer((PhotosContainerResource) exportInformation.get().getContainerResource());
    }
    PaginationData paginationData = exportInformation.isPresent() ? exportInformation.get().getPaginationData() : null;
    IdOnlyContainerResource resource = exportInformation.isPresent() ? (IdOnlyContainerResource) exportInformation.get().getContainerResource() : null;
    if (resource != null) {
        return getPhotos(resource, paginationData);
    } else {
        return getAlbums(paginationData, auth);
    }
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PaginationData(org.datatransferproject.types.common.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) Auth(com.flickr4java.flickr.auth.Auth) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource)

Example 29 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class FlickrPhotosExporter method getAlbums.

private ExportResult<PhotosContainerResource> getAlbums(PaginationData paginationData, Auth auth) {
    ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
    List<IdOnlyContainerResource> subResources = new ArrayList<>();
    int page = paginationData == null ? 1 : ((IntPaginationToken) paginationData).getStart();
    Photosets photoSetList;
    try {
        perUserRateLimiter.acquire();
        photoSetList = photosetsInterface.getList(auth.getUser().getId(), PHOTO_SETS_PER_PAGE, page, PHOTOSET_EXTRAS);
    } catch (FlickrException e) {
        return new ExportResult<>(e);
    }
    for (Photoset photoSet : photoSetList.getPhotosets()) {
        // Saving data to the album allows the target service to recreate the album structure
        albumBuilder.add(new PhotoAlbum(photoSet.getId(), photoSet.getTitle(), photoSet.getDescription()));
        // Adding subresources tells the framework to recall export to get all the photos
        subResources.add(new IdOnlyContainerResource(photoSet.getId()));
    }
    PaginationData newPage = null;
    boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.getPhotosets().isEmpty();
    if (hasMore) {
        newPage = new IntPaginationToken(page + 1);
    } else {
        // No more albums to get, add a resource for albumless items
        subResources.add(new IdOnlyContainerResource(""));
    }
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), null);
    ContinuationData continuationData = new ContinuationData(newPage);
    subResources.forEach(resource -> continuationData.addContainerResource(resource));
    // Get result type
    ResultType resultType = ResultType.CONTINUE;
    if (newPage == null) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
Also used : IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) PaginationData(org.datatransferproject.types.common.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) Photoset(com.flickr4java.flickr.photosets.Photoset) Photosets(com.flickr4java.flickr.photosets.Photosets) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 30 with PaginationData

use of org.datatransferproject.types.common.PaginationData in project data-transfer-project by google.

the class GooglePhotosExporter method exportPhotos.

@VisibleForTesting
ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, Optional<IdOnlyContainerResource> albumData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
    Optional<String> albumId = Optional.empty();
    if (albumData.isPresent()) {
        albumId = Optional.of(albumData.get().getId());
    }
    Optional<String> paginationToken = getPhotosPaginationToken(paginationData);
    MediaItemSearchResponse mediaItemSearchResponse = getOrCreatePhotosInterface(authData).listMediaItems(albumId, paginationToken);
    PaginationData nextPageData = null;
    if (!Strings.isNullOrEmpty(mediaItemSearchResponse.getNextPageToken())) {
        nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + mediaItemSearchResponse.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    PhotosContainerResource containerResource = null;
    GoogleMediaItem[] mediaItems = mediaItemSearchResponse.getMediaItems();
    if (mediaItems != null && mediaItems.length > 0) {
        List<PhotoModel> photos = convertPhotosList(albumId, mediaItems, jobId);
        containerResource = new PhotosContainerResource(null, photos);
    }
    ResultType resultType = ResultType.CONTINUE;
    if (nextPageData == null) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, containerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

PaginationData (org.datatransferproject.types.common.PaginationData)30 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)23 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)18 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)17 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)15 ArrayList (java.util.ArrayList)12 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)11 ExportInformation (org.datatransferproject.types.common.ExportInformation)11 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)11 Test (org.junit.Test)9 InOrder (org.mockito.InOrder)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 IOException (java.io.IOException)7 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)7 ImmutableList (com.google.common.collect.ImmutableList)5 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)5 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)4 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)4 CalendarContainerResource (org.datatransferproject.types.common.models.calendar.CalendarContainerResource)4 FlickrException (com.flickr4java.flickr.FlickrException)3