Search in sources :

Example 1 with PERSON_FIELDS

use of org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.PERSON_FIELDS 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

Credential (com.google.api.client.auth.oauth2.Credential)1 PeopleService (com.google.api.services.people.v1.PeopleService)1 Connections (com.google.api.services.people.v1.PeopleService.People.Connections)1 EmailAddress (com.google.api.services.people.v1.model.EmailAddress)1 FieldMetadata (com.google.api.services.people.v1.model.FieldMetadata)1 GetPeopleResponse (com.google.api.services.people.v1.model.GetPeopleResponse)1 ListConnectionsResponse (com.google.api.services.people.v1.model.ListConnectionsResponse)1 Name (com.google.api.services.people.v1.model.Name)1 Person (com.google.api.services.people.v1.model.Person)1 PersonResponse (com.google.api.services.people.v1.model.PersonResponse)1 PhoneNumber (com.google.api.services.people.v1.model.PhoneNumber)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 VCard (ezvcard.VCard)1 JCardWriter (ezvcard.io.json.JCardWriter)1 Email (ezvcard.property.Email)1 StructuredName (ezvcard.property.StructuredName)1 Telephone (ezvcard.property.Telephone)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1