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);
}
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);
}
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;
}
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);
}
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();
}
Aggregations