Search in sources :

Example 1 with CalendarModel

use of org.datatransferproject.types.common.models.calendar.CalendarModel in project data-transfer-project by google.

the class ToCalendarModelTransformerTest method testTransform.

@Test
@SuppressWarnings("unchecked")
public void testTransform() throws IOException {
    Map<String, Object> rawEvent = mapper.readValue(SAMPLE_CALENDAR, Map.class);
    CalendarModel calendar = transformer.apply(rawEvent, context);
    Assert.assertEquals("123", calendar.getId());
    Assert.assertEquals("Calendar", calendar.getName());
    Assert.assertEquals("Calendar", calendar.getDescription());
}
Also used : CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) Test(org.junit.Test)

Example 2 with CalendarModel

use of org.datatransferproject.types.common.models.calendar.CalendarModel in project data-transfer-project by google.

the class GoogleCalendarExporter method exportCalendars.

private ExportResult<CalendarContainerResource> exportCalendars(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    Calendar.CalendarList.List listRequest;
    CalendarList listResult;
    // Get calendar information
    try {
        listRequest = getOrCreateCalendarInterface(authData).calendarList().list();
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            Preconditions.checkState(paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX), "Token is not applicable");
            listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(CALENDAR_TOKEN_PREFIX.length()));
        }
        listResult = listRequest.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    // Set up continuation data
    PaginationData nextPageData = null;
    if (listResult.getNextPageToken() != null) {
        nextPageData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + listResult.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    // Process calendar list
    List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
    for (CalendarListEntry calendarData : listResult.getItems()) {
        CalendarModel model = convertToCalendarModel(calendarData);
        continuationData.addContainerResource(new IdOnlyContainerResource(calendarData.getId()));
        calendarModels.add(model);
    }
    CalendarContainerResource calendarContainerResource = new CalendarContainerResource(calendarModels, null);
    // Get result type
    ExportResult.ResultType resultType = ResultType.CONTINUE;
    if (calendarModels.isEmpty()) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, calendarContainerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) CalendarList(com.google.api.services.calendar.model.CalendarList) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 3 with CalendarModel

use of org.datatransferproject.types.common.models.calendar.CalendarModel in project data-transfer-project by google.

the class MicrosoftCalendarExporter method export.

@Override
public ExportResult<CalendarContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
    if (exportInformation.isPresent()) {
        // TODO support pagination
        throw new UnsupportedOperationException();
    }
    Request.Builder calendarsBuilder = getBuilder(baseUrl + CALENDARS_SUBPATH, authData);
    List<CalendarModel> calendarModels = new ArrayList<>();
    try (Response graphResponse = client.newCall(calendarsBuilder.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);
        // TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
        // TODO ContinuationData continuationData = nextLink == null
        // ? null : new ContinuationData(new GraphPagination(nextLink));
        @SuppressWarnings("unchecked") List<Map<String, Object>> rawCalendars = (List<Map<String, Object>>) graphMap.get("value");
        if (rawCalendars == null) {
            return new ExportResult<>(ExportResult.ResultType.END);
        }
        for (Map<String, Object> rawCalendar : rawCalendars) {
            TransformResult<CalendarModel> result = transformerService.transform(CalendarModel.class, rawCalendar);
            if (result.hasProblems()) {
                // FIXME log problem
                continue;
            }
            calendarModels.add(result.getTransformed());
        }
    } catch (IOException e) {
        // FIXME log error
        e.printStackTrace();
        return new ExportResult<>(e);
    }
    List<CalendarEventModel> calendarEventModels = new ArrayList<>();
    for (CalendarModel calendarModel : calendarModels) {
        String id = calendarModel.getId();
        Request.Builder eventsBuilder = getBuilder(calculateEventsUrl(id), authData);
        try (Response graphResponse = client.newCall(eventsBuilder.build()).execute()) {
            ResponseBody body = graphResponse.body();
            if (body == null) {
                return new ExportResult<>(new Exception("Error retrieving calendar: response body was null"));
            }
            String graphBody = new String(body.bytes());
            Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
            // TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
            // TODO ContinuationData continuationData = nextLink == null
            // ? null : new ContinuationData(new GraphPagination(nextLink));
            @SuppressWarnings("unchecked") List<Map<String, Object>> rawEvents = (List<Map<String, Object>>) graphMap.get("value");
            if (rawEvents == null) {
                return new ExportResult<>(ExportResult.ResultType.END);
            }
            for (Map<String, Object> rawEvent : rawEvents) {
                Map<String, String> properties = new HashMap<>();
                properties.put(CALENDAR_ID, id);
                TransformResult<CalendarEventModel> result = transformerService.transform(CalendarEventModel.class, rawEvent, properties);
                if (result.hasProblems()) {
                    // FIXME log problem
                    continue;
                }
                calendarEventModels.add(result.getTransformed());
            }
        } catch (IOException e) {
            // FIXME log error
            e.printStackTrace();
            return new ExportResult<>(e);
        }
    }
    CalendarContainerResource resource = new CalendarContainerResource(calendarModels, calendarEventModels);
    return new ExportResult<>(ExportResult.ResultType.END, resource, null);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) ArrayList(java.util.ArrayList) List(java.util.List) Request(okhttp3.Request) IOException(java.io.IOException) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) HashMap(java.util.HashMap) Map(java.util.Map) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 4 with CalendarModel

use of org.datatransferproject.types.common.models.calendar.CalendarModel in project data-transfer-project by google.

the class MicrosoftCalendarImporter method importItem.

@SuppressWarnings("unchecked")
@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentImportExecutor, TokenAuthData authData, CalendarContainerResource data) throws Exception {
    for (CalendarModel calendar : data.getCalendars()) {
        idempotentImportExecutor.executeAndSwallowIOExceptions(calendar.getId(), calendar.getName(), () -> importCalendar(authData, calendar));
    }
    List<Map<String, Object>> eventRequests = new ArrayList<>();
    int requestId = 1;
    for (CalendarEventModel event : data.getEvents()) {
        // get the imported calendar id for the event from the mappings
        String importedId = idempotentImportExecutor.getCachedValue(event.getCalendarId());
        Map<String, Object> request = createRequestItem(event, requestId, String.format(EVENT_SUBPATH, importedId));
        requestId++;
        eventRequests.add(request);
    }
    RequestHelper.BatchResponse eventResponse = RequestHelper.batchRequest(authData, eventRequests, baseUrl, client, objectMapper);
    if (ImportResult.ResultType.OK != eventResponse.getResult().getType()) {
        // TODO log problems
        return eventResponse.getResult();
    }
    return eventResponse.getResult();
}
Also used : RequestHelper(org.datatransferproject.transfer.microsoft.common.RequestHelper) ArrayList(java.util.ArrayList) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with CalendarModel

use of org.datatransferproject.types.common.models.calendar.CalendarModel in project data-transfer-project by google.

the class MicrosoftCalendarImportTest method testImport.

@Test
@SuppressWarnings("unchecked")
public void testImport() throws Exception {
    server.enqueue(new MockResponse().setBody(BATCH_CALENDAR_RESPONSE));
    server.enqueue(new MockResponse().setResponseCode(201).setBody(BATCH_EVENT_RESPONSE));
    server.start();
    HttpUrl baseUrl = server.url("");
    MicrosoftCalendarImporter importer = new MicrosoftCalendarImporter(baseUrl.toString(), client, mapper, transformerService);
    CalendarModel calendarModel = new CalendarModel("OldId1", "name", "name");
    CalendarAttendeeModel attendeeModel = new CalendarAttendeeModel("Test Attendee", "test@test.com", false);
    CalendarEventModel.CalendarEventTime start = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
    CalendarEventModel.CalendarEventTime end = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
    CalendarEventModel eventModel = new CalendarEventModel("OldId1", "Event1", "Test Notes", singletonList(attendeeModel), "Location1", start, end, null);
    CalendarContainerResource resource = new CalendarContainerResource(singleton(calendarModel), singleton(eventModel));
    FakeIdempotentImportExecutor executor = new FakeIdempotentImportExecutor();
    ImportResult result = importer.importItem(JOB_ID, executor, token, resource);
    Assert.assertEquals(ImportResult.ResultType.OK, result.getType());
    // verify the batch calendar request
    RecordedRequest calendarBatch = server.takeRequest();
    Map<String, Object> calendarBody = (Map<String, Object>) mapper.readValue(calendarBatch.getBody().readUtf8(), Map.class);
    List<Map<String, Object>> calendarRequests = (List<Map<String, Object>>) calendarBody.get("requests");
    Assert.assertNotNull(calendarRequests);
    Assert.assertEquals(1, calendarRequests.size());
    Map<String, Object> calendarRequest = calendarRequests.get(0);
    Assert.assertNotNull(calendarRequest.get("headers"));
    Assert.assertEquals("POST", calendarRequest.get("method"));
    Assert.assertEquals("/v1.0/me/calendars", calendarRequest.get("url"));
    Map<String, Object> calendarRequestBody = (Map<String, Object>) calendarRequest.get("body");
    Assert.assertNotNull(calendarRequestBody);
    Assert.assertEquals("name", calendarRequestBody.get("name"));
    // verify the calendar id mapping from old id to new id was saved
    Assert.assertEquals("NewId1", executor.getCachedValue("OldId1"));
    // verify the batch event request
    RecordedRequest eventBatch = server.takeRequest();
    Map<String, Object> eventRequests = (Map<String, Object>) mapper.readValue(eventBatch.getBody().readUtf8(), Map.class);
    Map<String, Object> eventRequest = (Map<String, Object>) ((List<Map<String, Object>>) eventRequests.get("requests")).get(0);
    Assert.assertNotNull(eventRequest.get("headers"));
    Assert.assertEquals("POST", eventRequest.get("method"));
    Assert.assertEquals("/v1.0/me/calendars/NewId1/events", // verify the URL is contructed correctly with NewId
    eventRequest.get("url"));
    Map<String, Object> eventRequestBody = (Map<String, Object>) eventRequest.get("body");
    Assert.assertNotNull(eventRequestBody);
    Assert.assertEquals("Event1", eventRequestBody.get("subject"));
    Map<String, Object> location = (Map<String, Object>) eventRequestBody.get("location");
    Assert.assertEquals("Location1", location.get("displayName"));
    Assert.assertEquals("Default", location.get("locationType"));
    Map<String, Object> body = (Map<String, Object>) eventRequestBody.get("body");
    Assert.assertEquals("Test Notes", body.get("content"));
    Assert.assertEquals("HTML", body.get("contentType"));
    List<Map<String, Object>> attendees = (List<Map<String, Object>>) eventRequestBody.get("attendees");
    Assert.assertEquals(1, attendees.size());
    Map<String, Object> attendee = (Map<String, Object>) attendees.get(0);
    Assert.assertEquals("required", attendee.get("type"));
    Map<String, Object> emailAddress = (Map<String, Object>) attendee.get("emailAddress");
    Assert.assertEquals("test@test.com", emailAddress.get("address"));
    Assert.assertEquals("Test Attendee", emailAddress.get("name"));
    // verify dates
    Map<String, Object> startDate = (Map<String, Object>) eventRequestBody.get("start");
    Assert.assertNotNull(startDate.get("dateTime"));
    Assert.assertEquals("UTC", startDate.get("timeZone"));
    Map<String, Object> endDate = (Map<String, Object>) eventRequestBody.get("end");
    Assert.assertNotNull(endDate.get("dateTime"));
    Assert.assertEquals("UTC", endDate.get("timeZone"));
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MicrosoftCalendarImporter(org.datatransferproject.transfer.microsoft.calendar.MicrosoftCalendarImporter) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) FakeIdempotentImportExecutor(org.datatransferproject.test.types.FakeIdempotentImportExecutor) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) HttpUrl(com.squareup.okhttp.HttpUrl) CalendarAttendeeModel(org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Aggregations

CalendarModel (org.datatransferproject.types.common.models.calendar.CalendarModel)7 CalendarContainerResource (org.datatransferproject.types.common.models.calendar.CalendarContainerResource)5 CalendarEventModel (org.datatransferproject.types.common.models.calendar.CalendarEventModel)5 Test (org.junit.Test)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)3 CalendarList (com.google.api.services.calendar.model.CalendarList)2 CalendarListEntry (com.google.api.services.calendar.model.CalendarListEntry)2 Event (com.google.api.services.calendar.model.Event)2 UUID (java.util.UUID)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 PaginationData (org.datatransferproject.types.common.PaginationData)2 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)2 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)2 Calendar (com.google.api.services.calendar.Calendar)1 Events (com.google.api.services.calendar.model.Events)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1