Search in sources :

Example 1 with CalendarList

use of com.google.api.services.calendar.model.CalendarList in project data-transfer-project by google.

the class GoogleCalendarExporter method exportCalendars.

private ExportResult<CalendarContainerResource> exportCalendars(AuthData 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<CalendarContainerResource>(ResultType.ERROR, e.getMessage());
    }
    // 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<CalendarContainerResource>(resultType, calendarContainerResource, continuationData);
}
Also used : PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) ArrayList(java.util.ArrayList) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarList(com.google.api.services.calendar.model.CalendarList) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 2 with CalendarList

use of com.google.api.services.calendar.model.CalendarList in project data-transfer-project by google.

the class GoogleCalendarExporterTest method exportCalendarFirstSet.

@Test
public void exportCalendarFirstSet() throws IOException {
    setUpSingleCalendarResponse();
    // Looking at first page, with at least one page after it
    calendarListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(JOB_ID, null);
    // Check results
    // Verify correct methods were called
    verify(calendarClient).calendarList();
    verify(calendarCalendarList).list();
    verify(calendarListRequest).execute();
    // Check pagination token
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(CALENDAR_TOKEN_PREFIX + NEXT_TOKEN);
    // Check calendars
    Collection<CalendarModel> actualCalendars = result.getExportedData().getCalendars();
    assertThat(actualCalendars.stream().map(CalendarModel::getId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
    // Check events (should be empty, even though there is an event in the calendar
    Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
    assertThat(actualEvents).isEmpty();
    // Should be one container in the resource list
    List<ContainerResource> actualResources = continuationData.getContainerResources();
    assertThat(actualResources.stream().map(a -> ((IdOnlyContainerResource) a).getId()).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
}
Also used : IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) Events(com.google.api.services.calendar.model.Events) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Calendar(com.google.api.services.calendar.Calendar) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) Before(org.junit.Before) Event(com.google.api.services.calendar.model.Event) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) InOrder(org.mockito.InOrder) EVENT_TOKEN_PREFIX(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.EVENT_TOKEN_PREFIX) CalendarList(com.google.api.services.calendar.model.CalendarList) Collection(java.util.Collection) Test(org.junit.Test) IOException(java.io.IOException) MAX_ATTENDEES(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.MAX_ATTENDEES) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) List(java.util.List) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) CALENDAR_TOKEN_PREFIX(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.CALENDAR_TOKEN_PREFIX) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) ContainerResource(org.dataportabilityproject.types.transfer.models.ContainerResource) Collections(java.util.Collections) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) Mockito.mock(org.mockito.Mockito.mock) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) ContainerResource(org.dataportabilityproject.types.transfer.models.ContainerResource) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Test(org.junit.Test)

Example 3 with CalendarList

use of com.google.api.services.calendar.model.CalendarList in project data-transfer-project by google.

the class GoogleCalendarService method exportCalendars.

private CalendarModelWrapper exportCalendars(Optional<PaginationInformation> pageInfo) throws IOException {
    Calendar.CalendarList.List listRequest = calendarClient.calendarList().list();
    if (pageInfo.isPresent()) {
        listRequest.setPageToken(((StringPaginationToken) pageInfo.get()).getId());
    }
    CalendarList listResult = listRequest.execute();
    List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
    List<Resource> resources = new ArrayList<>(listResult.getItems().size());
    for (CalendarListEntry calendarData : listResult.getItems()) {
        CalendarModel model = GoogleCalendarToModelConverter.convertToCalendarModel(calendarData);
        resources.add(new IdOnlyResource(calendarData.getId()));
        calendarModels.add(model);
    }
    PaginationInformation newPageInfo = null;
    if (listResult.getNextPageToken() != null) {
        newPageInfo = new StringPaginationToken(listResult.getNextPageToken());
    }
    return new CalendarModelWrapper(calendarModels, null, new ContinuationInformation(resources, newPageInfo));
}
Also used : ArrayList(java.util.ArrayList) Resource(org.dataportabilityproject.dataModels.Resource) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) CalendarModel(org.dataportabilityproject.dataModels.calendar.CalendarModel) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) CalendarModelWrapper(org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) CalendarList(com.google.api.services.calendar.model.CalendarList) PaginationInformation(org.dataportabilityproject.dataModels.PaginationInformation) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken)

Example 4 with CalendarList

use of com.google.api.services.calendar.model.CalendarList in project data-transfer-project by google.

the class GoogleCalendarServiceTest method testExportCalendarFirstSet.

@Test
public void testExportCalendarFirstSet() throws IOException {
    setUpSingleCalendarResponse();
    // Looking at first page, with at least one page after it
    ExportInformation emptyExportInformation = new ExportInformation(Optional.empty(), Optional.empty());
    calendarListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    CalendarModelWrapper wrapper = calendarService.export(emptyExportInformation);
    // Check results
    // Verify correct methods were called
    verify(calendarClient).calendarList();
    verify(calendarCalendarList).list();
    verify(calendarListRequest).execute();
    // Check pagination token
    StringPaginationToken paginationToken = (StringPaginationToken) wrapper.getContinuationInformation().getPaginationInformation();
    assertThat(paginationToken.getId()).isEqualTo(NEXT_TOKEN);
    // Check calendars
    Collection<CalendarModel> calendars = wrapper.getCalendars();
    assertThat(calendars.stream().map(CalendarModel::getId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
    // Check events (should be empty, even though there is an event in the calendar)
    Collection<CalendarEventModel> events = wrapper.getEvents();
    assertThat(events).isEmpty();
    // Should be one event in the resource list
    Collection<? extends Resource> subResources = wrapper.getContinuationInformation().getSubResources();
    assertThat(subResources.stream().map(a -> ((IdOnlyResource) a).getId()).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
}
Also used : CalendarModelWrapper(org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper) Events(com.google.api.services.calendar.model.Events) InMemoryJobDataCache(org.dataportabilityproject.cloud.local.InMemoryJobDataCache) PaginationInformation(org.dataportabilityproject.dataModels.PaginationInformation) JobDataCache(org.dataportabilityproject.cloud.interfaces.JobDataCache) Calendar(com.google.api.services.calendar.Calendar) GoogleStaticObjects(org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) Before(org.junit.Before) Event(com.google.api.services.calendar.model.Event) InOrder(org.mockito.InOrder) Resource(org.dataportabilityproject.dataModels.Resource) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) CalendarList(com.google.api.services.calendar.model.CalendarList) Collection(java.util.Collection) CalendarModelWrapper(org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper) IOException(java.io.IOException) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) CalendarEventModel(org.dataportabilityproject.dataModels.calendar.CalendarEventModel) CalendarModel(org.dataportabilityproject.dataModels.calendar.CalendarModel) Mockito(org.mockito.Mockito) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Optional(java.util.Optional) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) CalendarModel(org.dataportabilityproject.dataModels.calendar.CalendarModel) CalendarEventModel(org.dataportabilityproject.dataModels.calendar.CalendarEventModel) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) Test(org.junit.Test)

Example 5 with CalendarList

use of com.google.api.services.calendar.model.CalendarList in project drbookings by DrBookings.

the class RunnableImportGoogleCalendar method process.

@Override
protected List<Booking> process(final IProgressMonitor monitor) throws Exception {
    try {
        if (logger.isInfoEnabled()) {
            logger.info("Adding from " + url);
        }
        // initialize the transport
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        // initialize the data store factory
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // authorization
        final Credential credential = authorize();
        // set up global Calendar instance
        final Calendar client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("drbookings").build();
        final CalendarList list = client.calendarList().list().execute();
        String id = null;
        for (final CalendarListEntry item : list.getItems()) {
            if (item.getSummary().contains("airbnb")) {
                id = item.getId();
            }
        }
        final Events feed = client.events().list(id).execute();
        final List<Booking> result = new ArrayList<>();
        for (final Event item : feed.getItems()) {
            System.out.println(item);
            result.add(new BookingBean(item.getSummary(), LocalDate.parse(item.getStart().getDate().toString()), LocalDate.parse(item.getEnd().getDate().toString())));
        }
        return result;
    } finally {
        monitor.done();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) Calendar(com.google.api.services.calendar.Calendar) ArrayList(java.util.ArrayList) Booking(com.github.drbookings.core.datamodel.api.Booking) BookingBean(com.github.drbookings.core.datamodel.impl.BookingBean) FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Events(com.google.api.services.calendar.model.Events) Event(com.google.api.services.calendar.model.Event) CalendarList(com.google.api.services.calendar.model.CalendarList)

Aggregations

CalendarList (com.google.api.services.calendar.model.CalendarList)9 CalendarListEntry (com.google.api.services.calendar.model.CalendarListEntry)8 IOException (java.io.IOException)4 Calendar (com.google.api.services.calendar.Calendar)3 Event (com.google.api.services.calendar.model.Event)3 Events (com.google.api.services.calendar.model.Events)3 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collectors (java.util.stream.Collectors)2 PaginationInformation (org.dataportabilityproject.dataModels.PaginationInformation)2 Resource (org.dataportabilityproject.dataModels.Resource)2 CalendarModel (org.dataportabilityproject.dataModels.calendar.CalendarModel)2 CalendarModelWrapper (org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper)2 IdOnlyResource (org.dataportabilityproject.shared.IdOnlyResource)2 StringPaginationToken (org.dataportabilityproject.shared.StringPaginationToken)2 ExportResult (org.dataportabilityproject.spi.transfer.provider.ExportResult)2 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)2