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