Search in sources :

Example 21 with Person

use of com.google.api.services.people.v1.model.Person in project data-transfer-project by google.

the class GoogleContactsExporter method convertToVCardNamesAndPopulate.

private static void convertToVCardNamesAndPopulate(VCard vCard, List<Name> personNames) {
    // TODO(olsona): what if there's more than one primary name in a Google Contact?
    StructuredName primaryStructuredName = null;
    LinkedList<StructuredName> alternateStructuredNames = new LinkedList<>();
    for (Name personName : personNames) {
        StructuredName structuredName = convertToVCardNameSingle(personName);
        Boolean isNamePrimary = personName.getMetadata().getPrimary();
        if (isNamePrimary != null && isNamePrimary) {
            // This is the (a?) primary name for the Person, so it should be the primary name in the
            // VCard.
            primaryStructuredName = structuredName;
        } else {
            alternateStructuredNames.add(structuredName);
        }
    }
    if (primaryStructuredName == null) {
        primaryStructuredName = alternateStructuredNames.pop();
    }
    vCard.addProperty(primaryStructuredName);
    vCard.addPropertyAlt(StructuredName.class, alternateStructuredNames);
}
Also used : StructuredName(ezvcard.property.StructuredName) LinkedList(java.util.LinkedList) StructuredName(ezvcard.property.StructuredName) Name(com.google.api.services.people.v1.model.Name)

Example 22 with Person

use of com.google.api.services.people.v1.model.Person in project data-transfer-project by google.

the class GoogleContactsImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, TokensAndUrlAuthData authData, ContactsModelWrapper data) {
    JCardReader reader = new JCardReader(data.getVCards());
    try {
        // TODO(olsona): address any other problems that might arise in conversion
        List<VCard> vCardList = reader.readAll();
        for (VCard vCard : vCardList) {
            Person person = convert(vCard);
            getOrCreatePeopleService(authData).people().createContact(person).execute();
        }
        return ImportResult.OK;
    } catch (IOException e) {
        return new ImportResult(ImportResult.ResultType.ERROR, e.getMessage());
    }
}
Also used : ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) IOException(java.io.IOException) JCardReader(ezvcard.io.json.JCardReader) VCard(ezvcard.VCard) Person(com.google.api.services.people.v1.model.Person)

Example 23 with Person

use of com.google.api.services.people.v1.model.Person in project data-transfer-project by google.

the class GoogleContactsImporter method convert.

// TODO(olsona): can we guarantee that <VCARDPROPERTY>.getPref() will always return a value?
@VisibleForTesting
static Person convert(VCard vCard) {
    Person person = new Person();
    Preconditions.checkArgument(atLeastOneNamePresent(vCard), "At least one name must be present");
    person.setNames(Collections.singletonList(getPrimaryGoogleName(vCard.getStructuredNames())));
    if (vCard.getAddresses() != null) {
        person.setAddresses(vCard.getAddresses().stream().map(GoogleContactsImporter::convertToGoogleAddress).collect(Collectors.toList()));
    }
    if (vCard.getTelephoneNumbers() != null) {
        person.setPhoneNumbers(vCard.getTelephoneNumbers().stream().map(GoogleContactsImporter::convertToGooglePhoneNumber).collect(Collectors.toList()));
    }
    if (vCard.getEmails() != null) {
        person.setEmailAddresses(vCard.getEmails().stream().map(GoogleContactsImporter::convertToGoogleEmail).collect(Collectors.toList()));
    }
    return person;
}
Also used : Person(com.google.api.services.people.v1.model.Person) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 24 with Person

use of com.google.api.services.people.v1.model.Person in project data-transfer-project by google.

the class GoogleContactsService method importItem.

@Override
public void importItem(ContactsModelWrapper wrapper) throws IOException {
    // TODO(olsona): continuation information
    // First, assume no ContinuationInformation
    Collection<VCard> vCardCollection = wrapper.getVCards();
    for (VCard vCard : vCardCollection) {
        Person person = VCardToGoogleContactConverter.convert(vCard);
        peopleService.people().createContact(person).execute();
        logger.debug("Imported {}", person);
    }
}
Also used : VCard(ezvcard.VCard) Person(com.google.api.services.people.v1.model.Person)

Example 25 with Person

use of com.google.api.services.people.v1.model.Person in project data-transfer-project by google.

the class GoogleContactsService method export.

public ContactsModelWrapper export(ExportInformation continuationInformation) throws IOException {
    // Set up connection
    Connections.List connectionsList = peopleService.people().connections().list(GoogleContactsConstants.SELF_RESOURCE);
    // Get next page, if we have a page token
    if (continuationInformation.getPaginationInformation().isPresent()) {
        String pageToken = ((StringPaginationToken) continuationInformation.getPaginationInformation().get()).getId();
        connectionsList.setPageToken(pageToken);
    }
    // Get list of connections (nb: not a list containing full info of each Person)
    ListConnectionsResponse response = connectionsList.setPersonFields(GoogleContactsConstants.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 = peopleService.people().getBatchGet().setResourceNames(resourceNames).setPersonFields(GoogleContactsConstants.PERSON_FIELDS).execute();
    List<PersonResponse> personResponseList = batchResponse.getResponses();
    // Convert Persons to VCards
    List<VCard> vCards = personResponseList.stream().map(a -> GoogleContactToVCardConverter.convert(a.getPerson())).collect(Collectors.toList());
    // Determine if there's a next page
    StringPaginationToken newPage = null;
    if (response.getNextPageToken() != null) {
        newPage = new StringPaginationToken(response.getNextPageToken());
    }
    ContinuationInformation newContinuationInformation = null;
    if (newPage != null) {
        newContinuationInformation = new ContinuationInformation(null, newPage);
    }
    return new ContactsModelWrapper(vCards, newContinuationInformation);
}
Also used : Connections(com.google.api.services.people.v1.PeopleService.People.Connections) VCard(ezvcard.VCard) PeopleService(com.google.api.services.people.v1.PeopleService) Importer(org.dataportabilityproject.dataModels.Importer) Logger(org.slf4j.Logger) Connections(com.google.api.services.people.v1.PeopleService.People.Connections) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Exporter(org.dataportabilityproject.dataModels.Exporter) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) Person(com.google.api.services.people.v1.model.Person) List(java.util.List) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) JobDataCache(org.dataportabilityproject.cloud.interfaces.JobDataCache) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Credential(com.google.api.client.auth.oauth2.Credential) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GoogleStaticObjects(org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) PersonResponse(com.google.api.services.people.v1.model.PersonResponse)

Aggregations

Person (com.google.api.services.people.v1.model.Person)25 VCard (ezvcard.VCard)20 Test (org.junit.Test)16 EmailAddress (com.google.api.services.people.v1.model.EmailAddress)15 Name (com.google.api.services.people.v1.model.Name)13 PhoneNumber (com.google.api.services.people.v1.model.PhoneNumber)13 Email (ezvcard.property.Email)13 StructuredName (ezvcard.property.StructuredName)13 Telephone (ezvcard.property.Telephone)13 Person (model.Person)13 List (java.util.List)12 Collectors (java.util.stream.Collectors)12 Truth.assertThat (com.google.common.truth.Truth.assertThat)10 Pair (com.google.gdata.util.common.base.Pair)10 Collections (java.util.Collections)10 Function (java.util.function.Function)10 Address (com.google.api.services.people.v1.model.Address)9 Nullable (com.google.gdata.util.common.base.Nullable)8 Before (org.junit.Before)8 LinkedList (java.util.LinkedList)6