use of com.google.api.services.people.v1.model.Name in project data-transfer-project by google.
the class VCardToGoogleContactConverter 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(VCardToGoogleContactConverter::convertToGoogleAddress).collect(Collectors.toList()));
}
if (vCard.getTelephoneNumbers() != null) {
person.setPhoneNumbers(vCard.getTelephoneNumbers().stream().map(VCardToGoogleContactConverter::convertToGooglePhoneNumber).collect(Collectors.toList()));
}
if (vCard.getEmails() != null) {
person.setEmailAddresses(vCard.getEmails().stream().map(VCardToGoogleContactConverter::convertToGoogleEmail).collect(Collectors.toList()));
}
return person;
}
use of com.google.api.services.people.v1.model.Name 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 com.google.api.services.people.v1.model.Name 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);
}
use of com.google.api.services.people.v1.model.Name in project data-transfer-project by google.
the class GoogleContactsImporter 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 com.google.api.services.people.v1.model.Name 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;
}
Aggregations