Search in sources :

Example 1 with CalendarEventModel

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

the class ToCalendarEventModelTransformer method apply.

@Override
@SuppressWarnings("unchecked")
public CalendarEventModel apply(Map<String, Object> event, TransformerContext context) {
    if (!"singleInstance".equals(event.get("type"))) {
        // support single instances for now;recurring events later
        return null;
    }
    String calendarId = context.getProperty(CALENDAR_ID);
    String title = (String) event.getOrDefault("subject", "");
    String location = TransformerHelper.getOrDefault("location", "displayName", event, "");
    // Notes is itemBody resource type defined as: { "content": "string",  "contentType": "String"}
    String notes = TransformerHelper.getOrDefault("body", "content", event, "");
    List<Map<String, Object>> rawAttendees = (List<Map<String, Object>>) event.getOrDefault("attendees", emptyList());
    List<CalendarAttendeeModel> attendees = new ArrayList<>();
    for (Object rawAttendee : rawAttendees) {
        CalendarAttendeeModel attendee = context.transform(CalendarAttendeeModel.class, rawAttendee);
        if (attendee != null) {
            attendees.add(attendee);
        }
    }
    CalendarEventModel.CalendarEventTime startTime = context.transform(CalendarEventModel.CalendarEventTime.class, event.get("start"));
    if (startTime == null) {
        context.problem("Could not parse start time. Skipping event.");
        return null;
    }
    CalendarEventModel.CalendarEventTime endTime = context.transform(CalendarEventModel.CalendarEventTime.class, event.get("end"));
    if (endTime == null) {
        context.problem("Could not parse end time. Skipping event.");
        return null;
    }
    return new CalendarEventModel(calendarId, title, notes, attendees, location, startTime, endTime, null);
}
Also used : CalendarAttendeeModel(org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel) ArrayList(java.util.ArrayList) List(java.util.List) Collections.emptyList(java.util.Collections.emptyList) ArrayList(java.util.ArrayList) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) Map(java.util.Map)

Example 2 with CalendarEventModel

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

the class GoogleCalendarExporterTest method exportEventFirstSet.

@Test
public void exportEventFirstSet() throws IOException {
    setUpSingleEventResponse();
    // Looking at first page, with at least one page after it
    ContainerResource containerResource = new IdOnlyContainerResource(CALENDAR_ID);
    ExportInformation exportInformation = new ExportInformation(null, containerResource);
    eventListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(UUID.randomUUID(), null, Optional.of(exportInformation));
    // Check results
    // Verify correct methods were called
    verify(calendarEvents).list(CALENDAR_ID);
    verify(eventListRequest).setMaxAttendees(MAX_ATTENDEES);
    verify(eventListRequest).execute();
    // Check events
    Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
    assertThat(actualEvents.stream().map(CalendarEventModel::getCalendarId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
    assertThat(actualEvents.stream().map(CalendarEventModel::getTitle).collect(Collectors.toList())).containsExactly(EVENT_DESCRIPTION);
    // Check pagination token
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(EVENT_TOKEN_PREFIX + NEXT_TOKEN);
}
Also used : ExportInformation(org.datatransferproject.types.common.ExportInformation) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 3 with CalendarEventModel

use of org.datatransferproject.types.common.models.calendar.CalendarEventModel 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 CalendarEventModel

use of org.datatransferproject.types.common.models.calendar.CalendarEventModel 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 CalendarEventModel

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

the class ToCalendarEventModelTransformerTest method testTransform.

@SuppressWarnings("unchecked")
@Test
public void testTransform() throws IOException {
    context.setProperty(CALENDAR_ID, "123");
    Map<String, Object> rawEvent = mapper.readValue(SAMPLE_CALENDAR_EVENT, Map.class);
    CalendarEventModel event = transformer.apply(rawEvent, context);
    Assert.assertEquals("123", event.getCalendarId());
    Assert.assertEquals("Some Place", event.getLocation());
    Assert.assertEquals("Test Appointment 1", event.getTitle());
    Assert.assertTrue(event.getNotes().length() > 5);
    Assert.assertEquals(1, event.getAttendees().size());
    CalendarAttendeeModel attendee = event.getAttendees().get(0);
    Assert.assertEquals("Test Test1", attendee.getDisplayName());
    Assert.assertEquals("foo@foo.com", attendee.getEmail());
    Assert.assertFalse(attendee.getOptional());
    Assert.assertEquals(18, event.getStartTime().getDateTime().getHour());
    Assert.assertEquals(0, event.getStartTime().getDateTime().getMinute());
    Assert.assertEquals(18, event.getEndTime().getDateTime().getHour());
    Assert.assertEquals(30, event.getEndTime().getDateTime().getMinute());
}
Also used : CalendarAttendeeModel(org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) Test(org.junit.Test)

Aggregations

CalendarEventModel (org.datatransferproject.types.common.models.calendar.CalendarEventModel)9 CalendarContainerResource (org.datatransferproject.types.common.models.calendar.CalendarContainerResource)6 CalendarModel (org.datatransferproject.types.common.models.calendar.CalendarModel)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 Event (com.google.api.services.calendar.model.Event)3 IOException (java.io.IOException)3 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)3 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)3 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)3 CalendarAttendeeModel (org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel)3 Events (com.google.api.services.calendar.model.Events)2 UUID (java.util.UUID)2 ExportInformation (org.datatransferproject.types.common.ExportInformation)2 PaginationData (org.datatransferproject.types.common.PaginationData)2 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)2 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)2 Calendar (com.google.api.services.calendar.Calendar)1