Search in sources :

Example 1 with StructuredName

use of ezvcard.property.StructuredName in project android by nextcloud.

the class ContactOperations method convertName.

private void convertName(List<NonEmptyContentValues> contentValues, VCard vcard) {
    NonEmptyContentValues values = new NonEmptyContentValues(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    String firstName = null, lastName = null, namePrefix = null, nameSuffix = null;
    StructuredName n = vcard.getStructuredName();
    if (n != null) {
        firstName = n.getGiven();
        values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
        lastName = n.getFamily();
        values.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
        List<String> prefixes = n.getPrefixes();
        if (!prefixes.isEmpty()) {
            namePrefix = prefixes.get(0);
            values.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, namePrefix);
        }
        List<String> suffixes = n.getSuffixes();
        if (!suffixes.isEmpty()) {
            nameSuffix = suffixes.get(0);
            values.put(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, nameSuffix);
        }
    }
    FormattedName fn = vcard.getFormattedName();
    String formattedName = (fn == null) ? null : fn.getValue();
    String displayName;
    if (isEmpty(formattedName)) {
        StringBuilder sb = new StringBuilder();
        if (!isEmpty(namePrefix)) {
            sb.append(namePrefix).append(' ');
        }
        if (!isEmpty(firstName)) {
            sb.append(firstName).append(' ');
        }
        if (!isEmpty(lastName)) {
            sb.append(lastName).append(' ');
        }
        if (!isEmpty(nameSuffix)) {
            if (sb.length() > 0) {
                // delete space character
                sb.deleteCharAt(sb.length() - 1);
                sb.append(", ");
            }
            sb.append(nameSuffix);
        }
        displayName = sb.toString().trim();
    } else {
        displayName = formattedName;
    }
    values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
    RawProperty xPhoneticFirstName = vcard.getExtendedProperty("X-PHONETIC-FIRST-NAME");
    String firstPhoneticName = (xPhoneticFirstName == null) ? null : xPhoneticFirstName.getValue();
    values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_GIVEN_NAME, firstPhoneticName);
    RawProperty xPhoneticLastName = vcard.getExtendedProperty("X-PHONETIC-LAST-NAME");
    String lastPhoneticName = (xPhoneticLastName == null) ? null : xPhoneticLastName.getValue();
    values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME, lastPhoneticName);
    contentValues.add(values);
}
Also used : RawProperty(ezvcard.property.RawProperty) FormattedName(ezvcard.property.FormattedName) StructuredName(ezvcard.property.StructuredName)

Example 2 with StructuredName

use of ezvcard.property.StructuredName in project qksms by moezbhatti.

the class ContactOperations method convertName.

private void convertName(List<NonEmptyContentValues> contentValues, VCard vcard) {
    NonEmptyContentValues values = new NonEmptyContentValues(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    String firstName = null, lastName = null, namePrefix = null, nameSuffix = null;
    StructuredName n = vcard.getStructuredName();
    if (n != null) {
        firstName = n.getGiven();
        values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
        lastName = n.getFamily();
        values.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
        List<String> prefixes = n.getPrefixes();
        if (!prefixes.isEmpty()) {
            namePrefix = prefixes.get(0);
            values.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, namePrefix);
        }
        List<String> suffixes = n.getSuffixes();
        if (!suffixes.isEmpty()) {
            nameSuffix = suffixes.get(0);
            values.put(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, nameSuffix);
        }
    }
    FormattedName fn = vcard.getFormattedName();
    String formattedName = (fn == null) ? null : fn.getValue();
    String displayName;
    if (isEmpty(formattedName)) {
        StringBuilder sb = new StringBuilder();
        if (!isEmpty(namePrefix)) {
            sb.append(namePrefix).append(' ');
        }
        if (!isEmpty(firstName)) {
            sb.append(firstName).append(' ');
        }
        if (!isEmpty(lastName)) {
            sb.append(lastName).append(' ');
        }
        if (!isEmpty(nameSuffix)) {
            if (sb.length() > 0) {
                // delete space character
                sb.deleteCharAt(sb.length() - 1);
                sb.append(", ");
            }
            sb.append(nameSuffix);
        }
        displayName = sb.toString().trim();
    } else {
        displayName = formattedName;
    }
    values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
    RawProperty xPhoneticFirstName = vcard.getExtendedProperty("X-PHONETIC-FIRST-NAME");
    String firstPhoneticName = (xPhoneticFirstName == null) ? null : xPhoneticFirstName.getValue();
    values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_GIVEN_NAME, firstPhoneticName);
    RawProperty xPhoneticLastName = vcard.getExtendedProperty("X-PHONETIC-LAST-NAME");
    String lastPhoneticName = (xPhoneticLastName == null) ? null : xPhoneticLastName.getValue();
    values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME, lastPhoneticName);
    contentValues.add(values);
}
Also used : RawProperty(ezvcard.property.RawProperty) FormattedName(ezvcard.property.FormattedName) StructuredName(ezvcard.property.StructuredName)

Example 3 with StructuredName

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

the class GoogleContactsImportConversionTest method makeStructuredName.

private static StructuredName makeStructuredName(String givenName, String familyName, @Nullable String sourceType) {
    StructuredName structuredName = new StructuredName();
    structuredName.setGiven(givenName);
    structuredName.setFamily(familyName);
    if (sourceType != null) {
        structuredName.setParameter(SOURCE_PARAM_NAME_TYPE, sourceType);
    }
    return structuredName;
}
Also used : StructuredName(ezvcard.property.StructuredName)

Example 4 with StructuredName

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

the class GoogleContactsImportConversionTest method testConversionToGoogleNames.

@Test
public void testConversionToGoogleNames() {
    // Set up vCard with a primary name and one secondary name
    String primaryGivenName = "Mark";
    String primaryFamilyName = "Twain";
    String primarySourceType = "CONTACT";
    StructuredName primaryName = makeStructuredName(primaryGivenName, primaryFamilyName, primarySourceType);
    String altGivenName = "Samuel";
    String altFamilyName = "Clemens";
    String altSourceType = "PROFILE";
    StructuredName altName = makeStructuredName(altGivenName, altFamilyName, altSourceType);
    VCard vCard = new VCard();
    vCard.addProperty(primaryName);
    vCard.addPropertyAlt(StructuredName.class, Collections.singleton(altName));
    // Run test
    Person person = GoogleContactsImporter.convert(vCard);
    // Check results
    // Correct number of names
    assertThat(person.getNames().size()).isEqualTo(1);
    // Check primary names
    List<Name> actualPrimaryNames = person.getNames().stream().filter(a -> a.getMetadata().getPrimary()).collect(Collectors.toList());
    List<Pair<String, String>> actualPrimaryNameValues = actualPrimaryNames.stream().map(GoogleContactsImportConversionTest::getGivenAndFamilyValues).collect(Collectors.toList());
    assertThat(actualPrimaryNameValues).containsExactly(Pair.of(primaryGivenName, primaryFamilyName));
    List<String> actualPrimaryNameSourceValues = actualPrimaryNames.stream().map(a -> a.getMetadata().getSource().getType()).collect(Collectors.toList());
    assertThat(actualPrimaryNameSourceValues).containsExactly(primarySourceType);
    // Check secondary names - there shouldn't be any
    List<Name> actualSecondaryNames = person.getNames().stream().filter(a -> !a.getMetadata().getPrimary()).collect(Collectors.toList());
    assertThat(actualSecondaryNames).isEmpty();
}
Also used : VCard(ezvcard.VCard) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) Telephone(ezvcard.property.Telephone) 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) StructuredName(ezvcard.property.StructuredName) SOURCE_PARAM_NAME_TYPE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SOURCE_PARAM_NAME_TYPE) Pair(com.google.gdata.util.common.base.Pair) Person(com.google.api.services.people.v1.model.Person) List(java.util.List) CONTACT_SOURCE_TYPE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.CONTACT_SOURCE_TYPE) Nullable(com.google.gdata.util.common.base.Nullable) Address(com.google.api.services.people.v1.model.Address) Email(ezvcard.property.Email) VCARD_PRIMARY_PREF(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.VCARD_PRIMARY_PREF) Name(com.google.api.services.people.v1.model.Name) Collections(java.util.Collections) Before(org.junit.Before) StructuredName(ezvcard.property.StructuredName) VCard(ezvcard.VCard) Person(com.google.api.services.people.v1.model.Person) 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 5 with StructuredName

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

the class GoogleContactsImporterTest 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);
    }
    String vCardString = GoogleContactsExporter.makeVCardString(vCardList);
    ContactsModelWrapper wrapper = new ContactsModelWrapper(vCardString);
    // Run test
    contactsService.importItem(UUID.randomUUID(), null, 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.types.transfer.models.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