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