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