use of com.google.api.services.calendar.Calendar in project openhab1-addons by openhab.
the class GCalGoogleOAuth method getCalendarId.
/**
* Return calendarID based on calendar name.
* if calendar name is "primary" not check primary - just return primary
*
* @param calendar object
*/
public static CalendarListEntry getCalendarId(String calendar_name) {
CalendarListEntry calendarID = null;
if (calendarList == null) {
Calendar client = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredential(false)).setApplicationName("openHAB").build();
try {
calendarList = client.calendarList().list().execute();
} catch (com.google.api.client.auth.oauth2.TokenResponseException ae) {
logger.error("authentication failed: {}", ae.getMessage());
return null;
} catch (IOException e1) {
logger.error("authentication I/O exception: {}", e1.getMessage());
return null;
}
}
if (calendarList != null && calendarList.getItems() != null) {
for (CalendarListEntry entry : calendarList.getItems()) {
if ((entry.getSummary().equals(calendar_name)) || (calendar_name.equals("primary")) && entry.isPrimary()) {
calendarID = entry;
logger.debug("Got calendar {} CalendarID: {}", calendar_name, calendarID.getId());
}
}
}
if (calendarID == null) {
logger.warn("Calendar {} not found", calendar_name);
}
return calendarID;
}
use of com.google.api.services.calendar.Calendar 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.Calendar 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.Calendar in project DisCal-Discord-Bot by NovaFox161.
the class EventListCommand method moduleDay.
@SuppressWarnings("Duplicates")
private void moduleDay(String[] args, MessageReceivedEvent event, GuildSettings settings) {
if (args.length == 1) {
// Get the upcoming events in the next 24 hours.
try {
Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
DateTime now = new DateTime(System.currentTimeMillis());
DateTime twentyFourHoursFromNow = new DateTime(now.getValue() + 86400000L);
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
Events events = service.events().list(calendarData.getCalendarAddress()).setMaxResults(20).setTimeMin(now).setTimeMax(twentyFourHoursFromNow).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
List<Event> items = events.getItems();
if (items.size() == 0) {
Message.sendMessage(MessageManager.getMessage("Event.List.Found.None", settings), event);
} else if (items.size() == 1) {
Message.sendMessage(EventMessageFormatter.getEventEmbed(items.get(0), settings), MessageManager.getMessage("Event.List.Found.One", settings), event);
} else {
// List events by Id only.
Message.sendMessage(MessageManager.getMessage("Event.List.Found.Many", "%amount%", items.size() + "", settings), event);
for (Event e : items) {
Message.sendMessage(EventMessageFormatter.getCondensedEventEmbed(e, settings), event);
}
}
} catch (Exception e) {
Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
Logger.getLogger().exception(event.getAuthor(), "Failed to list events.", e, this.getClass(), true);
e.printStackTrace();
}
}
}
use of com.google.api.services.calendar.Calendar in project DisCal-Discord-Bot by NovaFox161.
the class EventListCommand method moduleSimpleList.
@SuppressWarnings("Duplicates")
private void moduleSimpleList(String[] args, MessageReceivedEvent event, GuildSettings settings) {
if (args.length == 0) {
try {
Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
DateTime now = new DateTime(System.currentTimeMillis());
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
Events events = service.events().list(calendarData.getCalendarAddress()).setMaxResults(1).setTimeMin(now).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
List<Event> items = events.getItems();
if (items.size() == 0) {
Message.sendMessage(MessageManager.getMessage("Event.List.Found.None", settings), event);
} else if (items.size() == 1) {
Message.sendMessage(EventMessageFormatter.getEventEmbed(items.get(0), settings), MessageManager.getMessage("Event.List.Found.One", settings), event);
}
} catch (Exception e) {
Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
Logger.getLogger().exception(event.getAuthor(), "Failed to list events.", e, this.getClass(), true);
e.printStackTrace();
}
} else if (args.length == 1) {
try {
Integer eventNum = Integer.valueOf(args[0]);
if (eventNum > 15) {
Message.sendMessage(MessageManager.getMessage("Event.List.Amount.Over", settings), event);
return;
}
if (eventNum < 1) {
Message.sendMessage(MessageManager.getMessage("Event.List.Amount.Under", settings), event);
return;
}
try {
Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
DateTime now = new DateTime(System.currentTimeMillis());
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(event.getGuild().getLongID());
Events events = service.events().list(calendarData.getCalendarAddress()).setMaxResults(eventNum).setTimeMin(now).setOrderBy("startTime").setSingleEvents(true).execute();
List<Event> items = events.getItems();
if (items.size() == 0) {
Message.sendMessage(MessageManager.getMessage("Event.List.Found.None", settings), event);
} else if (items.size() == 1) {
Message.sendMessage(EventMessageFormatter.getEventEmbed(items.get(0), settings), MessageManager.getMessage("Event.List.Found.One", settings), event);
} else {
// List events by Id only.
Message.sendMessage(MessageManager.getMessage("Event.List.Found.Many", "%amount%", items.size() + "", settings), event);
for (Event e : items) {
Message.sendMessage(EventMessageFormatter.getCondensedEventEmbed(e, settings), event);
}
}
} catch (Exception e) {
Message.sendMessage(MessageManager.getMessage("Notification.Error.Unknown", settings), event);
Logger.getLogger().exception(event.getAuthor(), "Failed to list events.", e, this.getClass(), true);
e.printStackTrace();
}
} catch (NumberFormatException e) {
Message.sendMessage(MessageManager.getMessage("Notification.Args.Value.Integer", settings), event);
}
} else {
Message.sendMessage(MessageManager.getMessage("Event.List.Args.Many", settings), event);
}
}
Aggregations