Search in sources :

Example 6 with StructuredName

use of ezvcard.property.StructuredName in project data-transfer-project by google.

the class GoogleContactToVCardConverter 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 7 with StructuredName

use of ezvcard.property.StructuredName in project data-transfer-project by google.

the class VCardToGoogleContactConverter method getPrimaryGoogleName.

private static Name getPrimaryGoogleName(List<StructuredName> vCardNameList) {
    StructuredName primaryVCardName;
    // first look if there's a primary name (or names)
    // if no primary name exists, simply pick the first "alt" name
    List<StructuredName> primaryNames = vCardNameList.stream().filter(a -> a.getAltId() == null).collect(Collectors.toList());
    if (primaryNames.size() > 0) {
        primaryVCardName = primaryNames.get(0);
    } else {
        primaryVCardName = vCardNameList.get(0);
    }
    return convertToGoogleName(primaryVCardName);
}
Also used : VCard(ezvcard.VCard) Logger(org.slf4j.Logger) VCARD_PRIMARY_PREF(org.dataportabilityproject.serviceProviders.google.contacts.GoogleContactsConstants.VCARD_PRIMARY_PREF) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) Telephone(ezvcard.property.Telephone) LoggerFactory(org.slf4j.LoggerFactory) SOURCE_PARAM_NAME_TYPE(org.dataportabilityproject.serviceProviders.google.contacts.GoogleContactsConstants.SOURCE_PARAM_NAME_TYPE) PhoneNumber(com.google.api.services.people.v1.model.PhoneNumber) Collectors(java.util.stream.Collectors) StructuredName(ezvcard.property.StructuredName) Person(com.google.api.services.people.v1.model.Person) List(java.util.List) FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) Source(com.google.api.services.people.v1.model.Source) CONTACT_SOURCE_TYPE(org.dataportabilityproject.serviceProviders.google.contacts.GoogleContactsConstants.CONTACT_SOURCE_TYPE) Email(ezvcard.property.Email) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Name(com.google.api.services.people.v1.model.Name) Collections(java.util.Collections) StructuredName(ezvcard.property.StructuredName)

Example 8 with StructuredName

use of ezvcard.property.StructuredName in project data-transfer-project by google.

the class VCardToGoogleContactConverter method convertToGoogleName.

private static Name convertToGoogleName(StructuredName vCardName) {
    Name name = new Name();
    name.setFamilyName(vCardName.getFamily());
    name.setGivenName(vCardName.getGiven());
    FieldMetadata fieldMetadata = new FieldMetadata();
    boolean isPrimary = (vCardName.getAltId() == null);
    fieldMetadata.setPrimary(isPrimary);
    String vCardNameSource = vCardName.getParameter(SOURCE_PARAM_NAME_TYPE);
    if (CONTACT_SOURCE_TYPE.equals(vCardNameSource)) {
        Source source = new Source().setType(vCardNameSource);
        fieldMetadata.setSource(source);
    }
    name.setMetadata(fieldMetadata);
    return name;
}
Also used : FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) Source(com.google.api.services.people.v1.model.Source) StructuredName(ezvcard.property.StructuredName) Name(com.google.api.services.people.v1.model.Name)

Example 9 with StructuredName

use of ezvcard.property.StructuredName in project data-transfer-project by google.

the class GoogleContactToVCardConverterTest method testConversionToVCardNames.

@Test
public void testConversionToVCardNames() {
    // Set up Person with a primary name and two secondary names
    String primaryGivenName = "Mark";
    String primaryFamilyName = "Twain";
    Name primaryName = new Name().setGivenName(primaryGivenName).setFamilyName(primaryFamilyName).setMetadata(PRIMARY_FIELD_METADATA);
    String alternateGivenName1 = "Samuel";
    String alternateFamilyName1 = "Clemens";
    String alternateSourceType1 = "PROFILE";
    Name alternateName1 = new Name().setGivenName(alternateGivenName1).setFamilyName(alternateFamilyName1).setMetadata(new FieldMetadata().setPrimary(false).setSource(new Source().setType(alternateSourceType1)));
    String alternateGivenName2 = "Louis";
    String alternateFamilyName2 = "de Conte";
    String alternateSourceType2 = "PEN_NAME";
    Name alternateName2 = new Name().setGivenName(alternateGivenName2).setFamilyName(alternateFamilyName2).setMetadata(new FieldMetadata().setPrimary(false).setSource(new Source().setType(alternateSourceType2)));
    // Order shouldn't matter
    Person person = new Person().setNames(Arrays.asList(alternateName2, alternateName1, primaryName));
    // Run test
    VCard vCard = GoogleContactToVCardConverter.convert(person);
    // Check name conversion correctness
    List<StructuredName> structuredNames = vCard.getStructuredNames();
    assertThat(structuredNames.size()).isEqualTo(3);
    // Check primary (non-alternate) names
    List<StructuredName> actualPrimaryNames = structuredNames.stream().filter(n -> n.getAltId() == null).collect(Collectors.toList());
    List<Pair<String, String>> actualPrimaryNamesValues = actualPrimaryNames.stream().map(GoogleContactToVCardConverterTest::getGivenAndFamilyNames).collect(Collectors.toList());
    assertThat(actualPrimaryNamesValues).containsExactly(Pair.of(primaryGivenName, primaryFamilyName));
    List<String> actualPrimarySourceValues = actualPrimaryNames.stream().map(a -> a.getParameter(SOURCE_PARAM_NAME_TYPE)).collect(Collectors.toList());
    assertThat(actualPrimarySourceValues).containsExactly(DEFAULT_SOURCE_TYPE);
    // Check alternate names
    List<StructuredName> actualAlternateNames = structuredNames.stream().filter(n -> n.getAltId() != null).collect(Collectors.toList());
    List<Pair<String, String>> actualAlternateNamesValues = actualAlternateNames.stream().map(GoogleContactToVCardConverterTest::getGivenAndFamilyNames).collect(Collectors.toList());
    assertThat(actualAlternateNamesValues).containsExactly(Pair.of(alternateGivenName1, alternateFamilyName1), Pair.of(alternateGivenName2, alternateFamilyName2));
    List<String> actualAlternateSourceValues = actualAlternateNames.stream().map(a -> a.getParameter(SOURCE_PARAM_NAME_TYPE)).collect(Collectors.toList());
    assertThat(actualAlternateSourceValues).containsExactly(alternateSourceType1, alternateSourceType2);
}
Also used : VCard(ezvcard.VCard) Arrays(java.util.Arrays) VCARD_PRIMARY_PREF(org.dataportabilityproject.serviceProviders.google.contacts.GoogleContactsConstants.VCARD_PRIMARY_PREF) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) VCardProperty(ezvcard.property.VCardProperty) Telephone(ezvcard.property.Telephone) SOURCE_PARAM_NAME_TYPE(org.dataportabilityproject.serviceProviders.google.contacts.GoogleContactsConstants.SOURCE_PARAM_NAME_TYPE) Test(org.junit.Test) PhoneNumber(com.google.api.services.people.v1.model.PhoneNumber) Truth.assertThat(com.google.common.truth.Truth.assertThat) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Address(ezvcard.property.Address) StructuredName(ezvcard.property.StructuredName) Pair(com.google.gdata.util.common.base.Pair) Person(com.google.api.services.people.v1.model.Person) List(java.util.List) FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) Source(com.google.api.services.people.v1.model.Source) Email(ezvcard.property.Email) VCardParameters(ezvcard.parameter.VCardParameters) Name(com.google.api.services.people.v1.model.Name) TextProperty(ezvcard.property.TextProperty) Collections(java.util.Collections) FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StructuredName(ezvcard.property.StructuredName) Source(com.google.api.services.people.v1.model.Source) StructuredName(ezvcard.property.StructuredName) Name(com.google.api.services.people.v1.model.Name) Pair(com.google.gdata.util.common.base.Pair) Test(org.junit.Test)

Example 10 with StructuredName

use of ezvcard.property.StructuredName in project data-transfer-project by google.

the class GoogleContactsServiceTest method importFirstResources.

@Test
public void importFirstResources() throws IOException {
    // Set up: small number of VCards to be imported
    int numberOfVCards = 5;
    List<VCard> vCardList = new LinkedList<>();
    for (int i = 0; i < numberOfVCards; i++) {
        StructuredName structuredName = new StructuredName();
        structuredName.setFamily("Family" + i);
        structuredName.setParameter(SOURCE_PARAM_NAME_TYPE, CONTACT_SOURCE_TYPE);
        VCard vCard = new VCard();
        vCard.setStructuredName(structuredName);
        vCardList.add(vCard);
    }
    ContactsModelWrapper wrapper = new ContactsModelWrapper(vCardList, null);
    // Run test
    contactsService.importItem(wrapper);
    // Check that the right methods were called
    verify(people, times(numberOfVCards)).createContact(any(Person.class));
    verify(createContact, times(numberOfVCards)).execute();
}
Also used : ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) VCard(ezvcard.VCard) StructuredName(ezvcard.property.StructuredName) Person(com.google.api.services.people.v1.model.Person) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

StructuredName (ezvcard.property.StructuredName)37 VCard (ezvcard.VCard)17 Telephone (ezvcard.property.Telephone)14 Test (org.junit.Test)13 Address (ezvcard.property.Address)11 Name (com.google.api.services.people.v1.model.Name)10 Email (ezvcard.property.Email)9 Person (com.google.api.services.people.v1.model.Person)8 EmailAddress (com.google.api.services.people.v1.model.EmailAddress)6 FieldMetadata (com.google.api.services.people.v1.model.FieldMetadata)6 PhoneNumber (com.google.api.services.people.v1.model.PhoneNumber)6 Source (com.google.api.services.people.v1.model.Source)6 Timezone (ezvcard.property.Timezone)6 TelUri (ezvcard.util.TelUri)6 UtcOffset (ezvcard.util.UtcOffset)6 Collections (java.util.Collections)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Birthday (ezvcard.property.Birthday)5 Truth.assertThat (com.google.common.truth.Truth.assertThat)4