Search in sources :

Example 11 with ContactsModelWrapper

use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper 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)

Aggregations

ContactsModelWrapper (org.datatransferproject.types.common.models.contacts.ContactsModelWrapper)11 VCard (ezvcard.VCard)6 IOException (java.io.IOException)4 List (java.util.List)4 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)4 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)3 Test (org.junit.Test)3 Person (com.google.api.services.people.v1.model.Person)2 JCardReader (ezvcard.io.json.JCardReader)2 JCardWriter (ezvcard.io.json.JCardWriter)2 StructuredName (ezvcard.property.StructuredName)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 UUID (java.util.UUID)2 AuthTestDriver (org.datatransferproject.auth.microsoft.harness.AuthTestDriver)2 Exporter (org.datatransferproject.spi.transfer.provider.Exporter)2 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)2 Importer (org.datatransferproject.spi.transfer.provider.Importer)2