Search in sources :

Example 6 with ContactsModelWrapper

use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class GoogleContactsImporterTest method importFirstResources.

@Test
public void importFirstResources() throws Exception {
    // 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);
        vCard.setFormattedName("First " + structuredName.getFamily());
        vCardList.add(vCard);
    }
    String vCardString = GoogleContactsExporter.makeVCardString(vCardList);
    ContactsModelWrapper wrapper = new ContactsModelWrapper(vCardString);
    // Run test
    contactsService.importItem(UUID.randomUUID(), executor, 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.datatransferproject.types.common.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)

Example 7 with ContactsModelWrapper

use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class MicrosoftContactsExporter method doExport.

@SuppressWarnings("unchecked")
private ExportResult<ContactsModelWrapper> doExport(TokensAndUrlAuthData authData, String url) {
    Request.Builder graphReqBuilder = new Request.Builder().url(url);
    graphReqBuilder.header("Authorization", "Bearer " + authData.getAccessToken());
    try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) {
        ResponseBody body = graphResponse.body();
        if (body == null) {
            return new ExportResult<>(new Exception("Error retrieving contacts: response body was null"));
        }
        String graphBody = new String(body.bytes());
        Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
        String nextLink = (String) graphMap.get(ODATA_NEXT);
        ContinuationData continuationData = nextLink == null ? null : new ContinuationData(new GraphPagination(nextLink));
        List<Map<String, Object>> rawContacts = (List<Map<String, Object>>) graphMap.get("value");
        if (rawContacts == null) {
            return new ExportResult<>(ExportResult.ResultType.END);
        }
        ContactsModelWrapper wrapper = transform(rawContacts);
        return new ExportResult<>(ExportResult.ResultType.CONTINUE, wrapper, continuationData);
    } catch (IOException e) {
        // FIXME log error
        e.printStackTrace();
        return new ExportResult<>(e);
    }
}
Also used : GraphPagination(org.datatransferproject.transfer.microsoft.types.GraphPagination) Request(okhttp3.Request) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) IOException(java.io.IOException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) List(java.util.List) Map(java.util.Map) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 8 with ContactsModelWrapper

use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class MicrosoftContactsImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentImportExecutor, TokenAuthData authData, ContactsModelWrapper wrapper) {
    JCardReader reader = new JCardReader(wrapper.getVCards());
    try {
        List<VCard> cards = reader.readAll();
        List<String> problems = new ArrayList<>();
        int[] id = new int[] { 1 };
        List<Map<String, Object>> requests = cards.stream().map(card -> {
            TransformResult<LinkedHashMap> result = transformerService.transform(LinkedHashMap.class, card);
            problems.addAll(result.getProblems());
            LinkedHashMap contact = result.getTransformed();
            Map<String, Object> request = createRequest(id[0], CONTACTS_URL, contact);
            id[0]++;
            return request;
        }).collect(toList());
        if (!problems.isEmpty()) {
        // TODO log problems
        }
        return batchRequest(authData, requests, baseUrl, client, objectMapper).getResult();
    } catch (IOException e) {
        // TODO log
        e.printStackTrace();
        return new ImportResult(e);
    }
}
Also used : VCard(ezvcard.VCard) IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) UUID(java.util.UUID) RequestHelper.createRequest(org.datatransferproject.transfer.microsoft.common.RequestHelper.createRequest) TokenAuthData(org.datatransferproject.types.transfer.auth.TokenAuthData) ArrayList(java.util.ArrayList) TransformerService(org.datatransferproject.transfer.microsoft.transformer.TransformerService) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) OkHttpClient(okhttp3.OkHttpClient) TransformResult(org.datatransferproject.transfer.microsoft.transformer.TransformResult) Map(java.util.Map) JCardReader(ezvcard.io.json.JCardReader) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) Importer(org.datatransferproject.spi.transfer.provider.Importer) RequestHelper.batchRequest(org.datatransferproject.transfer.microsoft.common.RequestHelper.batchRequest) TransformResult(org.datatransferproject.transfer.microsoft.transformer.TransformResult) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) JCardReader(ezvcard.io.json.JCardReader) VCard(ezvcard.VCard) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with ContactsModelWrapper

use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class MicrosoftContactsExporter method transform.

private ContactsModelWrapper transform(List<Map<String, Object>> rawContacts) {
    StringWriter stringWriter = new StringWriter();
    try (JCardWriter writer = new JCardWriter(stringWriter)) {
        for (Map<String, Object> rawContact : rawContacts) {
            TransformResult<VCard> result = transformerService.transform(VCard.class, rawContact);
            if (result.hasProblems()) {
                // FIXME log problem
                continue;
            }
            writer.write(result.getTransformed());
        }
    } catch (IOException e) {
        // TODO log
        e.printStackTrace();
        return new ContactsModelWrapper("");
    }
    return new ContactsModelWrapper(stringWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) JCardWriter(ezvcard.io.json.JCardWriter) IOException(java.io.IOException) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) VCard(ezvcard.VCard)

Example 10 with ContactsModelWrapper

use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.

the class LocalExportTestRunner method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    AuthTestDriver authTestDriver = new AuthTestDriver();
    MicrosoftTransferExtension serviceProvider = new MicrosoftTransferExtension();
    TokenAuthData token = authTestDriver.getOAuthTokenCode();
    Exporter<TokenAuthData, ContactsModelWrapper> contacts = (Exporter<TokenAuthData, ContactsModelWrapper>) serviceProvider.getExporter("CONTACTS");
    ExportResult<ContactsModelWrapper> wrapper = contacts.export(UUID.randomUUID(), token, Optional.empty());
}
Also used : TokenAuthData(org.datatransferproject.types.transfer.auth.TokenAuthData) MicrosoftTransferExtension(org.datatransferproject.transfer.microsoft.MicrosoftTransferExtension) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) AuthTestDriver(org.datatransferproject.auth.microsoft.harness.AuthTestDriver) Exporter(org.datatransferproject.spi.transfer.provider.Exporter)

Aggregations

ContactsModelWrapper (org.datatransferproject.types.common.models.contacts.ContactsModelWrapper)11 VCard (ezvcard.VCard)6 IOException (java.io.IOException)4 List (java.util.List)4 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)4 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)3 Test (org.junit.Test)3 Person (com.google.api.services.people.v1.model.Person)2 JCardReader (ezvcard.io.json.JCardReader)2 JCardWriter (ezvcard.io.json.JCardWriter)2 StructuredName (ezvcard.property.StructuredName)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 UUID (java.util.UUID)2 AuthTestDriver (org.datatransferproject.auth.microsoft.harness.AuthTestDriver)2 Exporter (org.datatransferproject.spi.transfer.provider.Exporter)2 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)2 Importer (org.datatransferproject.spi.transfer.provider.Importer)2