Search in sources :

Example 1 with TempCalendarData

use of org.dataportabilityproject.spi.transfer.types.TempCalendarData in project data-transfer-project by google.

the class GoogleCalendarImporter method importSingleCalendar.

@VisibleForTesting
void importSingleCalendar(UUID jobId, AuthData authData, CalendarModel calendarModel) throws IOException {
    com.google.api.services.calendar.model.Calendar toInsert = convertToGoogleCalendar(calendarModel);
    com.google.api.services.calendar.model.Calendar calendarResult = getOrCreateCalendarInterface(authData).calendars().insert(toInsert).execute();
    TempCalendarData calendarMappings = jobStore.findData(TempCalendarData.class, jobId);
    if (calendarMappings == null) {
        calendarMappings = new TempCalendarData(jobId);
        jobStore.create(jobId, calendarMappings);
    }
    calendarMappings.addIdMapping(calendarModel.getId(), calendarResult.getId());
    jobStore.update(jobId, calendarMappings);
}
Also used : TempCalendarData(org.dataportabilityproject.spi.transfer.types.TempCalendarData) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with TempCalendarData

use of org.dataportabilityproject.spi.transfer.types.TempCalendarData in project data-transfer-project by google.

the class MicrosoftCalendarImporter method importItem.

@SuppressWarnings("unchecked")
@Override
public ImportResult importItem(UUID jobId, TokenAuthData authData, CalendarContainerResource data) {
    TempCalendarData calendarMappings = jobStore.findData(TempCalendarData.class, jobId);
    if (calendarMappings == null) {
        calendarMappings = new TempCalendarData(jobId);
        jobStore.create(jobId, calendarMappings);
    }
    Map<String, String> requestIdToExportedId = new HashMap<>();
    List<String> problems = new ArrayList<>();
    int requestId = 1;
    List<Map<String, Object>> calendarRequests = new ArrayList<>();
    for (CalendarModel calendar : data.getCalendars()) {
        Map<String, Object> request = createRequestItem(calendar, requestId, CALENDAR_SUBPATH, problems);
        requestIdToExportedId.put(String.valueOf(requestId), calendar.getId());
        requestId++;
        calendarRequests.add(request);
    }
    if (!problems.isEmpty()) {
    // TODO log problems
    }
    RequestHelper.BatchResponse calendarResponse = RequestHelper.batchRequest(authData, calendarRequests, baseUrl, client, objectMapper);
    if (ImportResult.ResultType.OK != calendarResponse.getResult().getType()) {
        // TODO log problems
        return calendarResponse.getResult();
    }
    List<Map<String, Object>> batchResponses = calendarResponse.getBatchResponse();
    for (Map<String, Object> batchResponse : batchResponses) {
        String batchRequestId = (String) batchResponse.get("id");
        if (batchRequestId == null) {
            problems.add("Null request id returned by batch response");
            continue;
        }
        Integer status = (Integer) batchResponse.get("status");
        if (status == null || 201 != status) {
            problems.add("Error creating calendar: " + batchRequestId);
            continue;
        }
        Map<String, Object> body = (Map<String, Object>) batchResponse.get("body");
        if (body == null) {
            problems.add("Invalid body returned from batch calendar create: " + batchRequestId);
            continue;
        }
        calendarMappings.addIdMapping(requestIdToExportedId.get(batchRequestId), (String) body.get("id"));
    }
    jobStore.update(jobId, calendarMappings);
    List<Map<String, Object>> eventRequests = new ArrayList<>();
    requestId = 1;
    for (CalendarEventModel event : data.getEvents()) {
        String importedId = calendarMappings.getImportedId(event.getCalendarId());
        Map<String, Object> request = createRequestItem(event, requestId, String.format(EVENT_SUBPATH, importedId), problems);
        requestId++;
        eventRequests.add(request);
    }
    if (!problems.isEmpty()) {
    // TODO log problems
    }
    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 : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) TempCalendarData(org.dataportabilityproject.spi.transfer.types.TempCalendarData) RequestHelper(org.dataportabilityproject.transfer.microsoft.common.RequestHelper) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with TempCalendarData

use of org.dataportabilityproject.spi.transfer.types.TempCalendarData in project data-transfer-project by google.

the class TempCalendarDataTest method verifySerializeDeserialize.

@Test
public void verifySerializeDeserialize() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    TempCalendarData calendarData = new TempCalendarData(JOB_ID, Collections.singletonMap("old1", "new1"));
    String serialized = objectMapper.writeValueAsString(calendarData);
    TempCalendarData deserialized = objectMapper.readValue(serialized, TempCalendarData.class);
    Assert.assertEquals(JOB_ID, deserialized.getJobId());
    Assert.assertEquals("new1", deserialized.getImportedId("old1"));
}
Also used : TempCalendarData(org.dataportabilityproject.spi.transfer.types.TempCalendarData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with TempCalendarData

use of org.dataportabilityproject.spi.transfer.types.TempCalendarData in project data-transfer-project by google.

the class GoogleCalendarImporter method importSingleEvent.

@VisibleForTesting
void importSingleEvent(UUID jobId, AuthData authData, CalendarEventModel eventModel) throws IOException {
    Event event = convertToGoogleCalendarEvent(eventModel);
    // calendarMappings better not be null!
    TempCalendarData calendarMappings = jobStore.findData(TempCalendarData.class, jobId);
    String newCalendarId = calendarMappings.getImportedId(eventModel.getCalendarId());
    getOrCreateCalendarInterface(authData).events().insert(newCalendarId, event).execute();
}
Also used : TempCalendarData(org.dataportabilityproject.spi.transfer.types.TempCalendarData) Event(com.google.api.services.calendar.model.Event) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

TempCalendarData (org.dataportabilityproject.spi.transfer.types.TempCalendarData)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Event (com.google.api.services.calendar.model.Event)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 RequestHelper (org.dataportabilityproject.transfer.microsoft.common.RequestHelper)1 CalendarEventModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel)1 CalendarModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarModel)1 Test (org.junit.Test)1