@Before public void setup() throws IOException { calendarClient = mock(Calendar.class); calendarCalendars = mock(Calendar.Calendars.class); calendarInsertRequest = mock(Calendar.Calendars.Insert.class); calendarEvents = mock(Calendar.Events.class); eventInsertRequest = mock(Calendar.Events.Insert.class); credentialFactory = mock(GoogleCredentialFactory.class); jobStore = new LocalJobStore(); calendarService = new GoogleCalendarImporter(credentialFactory, jobStore, calendarClient); when(calendarClient.calendars()).thenReturn(calendarCalendars); when(calendarClient.events()).thenReturn(calendarEvents); verifyZeroInteractions(credentialFactory); }
@Before public void setup() throws IOException { calendarClient = mock(Calendar.class); calendarCalendars = mock(Calendar.Calendars.class); calendarCalendarList = mock(Calendar.CalendarList.class); calendarListRequest = mock(Calendar.CalendarList.List.class); calendarEvents = mock(Calendar.Events.class); eventListRequest = mock(Calendar.Events.List.class); credentialFactory = mock(GoogleCredentialFactory.class); googleCalendarExporter = new GoogleCalendarExporter(credentialFactory, calendarClient); when(calendarClient.calendars()).thenReturn(calendarCalendars); when(calendarClient.calendarList()).thenReturn(calendarCalendarList); when(calendarCalendarList.list()).thenReturn(calendarListRequest); when(calendarClient.events()).thenReturn(calendarEvents); when(calendarEvents.list(CALENDAR_ID)).thenReturn(eventListRequest); when(eventListRequest.setMaxAttendees(MAX_ATTENDEES)).thenReturn(eventListRequest); verifyZeroInteractions(credentialFactory); }
@VisibleForTesting void importSingleEvent(UUID jobId, TokensAndUrlAuthData authData, CalendarEventModel eventModel) throws IOException { Event event = convertToGoogleCalendarEvent(eventModel); // calendarMappings better not be null! TempCalendarData calendarMappings = jobStore.findData(jobId, createCacheKey(), TempCalendarData.class); String newCalendarId = calendarMappings.getImportedId(eventModel.getCalendarId()); getOrCreateCalendarInterface(authData) .events() .insert(newCalendarId, event) .execute(); }
listRequest = getOrCreateCalendarInterface(authData) .events() .list(id) .setMaxAttendees(GoogleStaticObjects.MAX_ATTENDEES);
private static void showEvents(Calendar calendar) throws IOException { View.header("Show Events"); Events feed = client.events().list(calendar.getId()).execute(); View.display(feed); }
private static void addEvent(Calendar calendar) throws IOException { View.header("Add Event"); Event event = newEvent(); Event result = client.events().insert(calendar.getId(), event).execute(); View.display(result); }
@Override public void deleteAppointment(final AppointmentDto appointment) throws IOException { final GoogleAppointmentDto googleAppointmentDto = (GoogleAppointmentDto) appointment; client.events().delete(calendarId, googleAppointmentDto.getGoogleId()).execute(); LOG.info("Deleted Google appointment " + appointment.getSummary()); }
@Override public void delete(String eventId, CalendarSettings calendarSettings) { googleCalendarClient = getOrCreateGoogleCalendarClient(); if (googleCalendarClient != null) { String calendarId = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings().getCalendarId(); try { googleCalendarClient.events().delete(calendarId, eventId).execute(); LOG.info(String.format("Event %s has been deleted in calendar '%s'.", eventId, calendarId)); } catch (IOException ex) { LOG.warn(String.format("Could not delete event %s in calendar '%s'", eventId, calendarId), ex); mailService.sendCalendarDeleteErrorNotification(calendarId, eventId, ex.getMessage()); } } }
@Override public void updateAppointment(final AppointmentDto appointment) throws IOException { final GoogleAppointmentDto googleAppointmentDto = (GoogleAppointmentDto) appointment; final Event event = client.events().get(calendarId, googleAppointmentDto.getGoogleId()).execute(); populateEventFromAppointmentDto(appointment, event); client.events().update(calendarId, event.getId(), event).execute(); LOG.info("Updated Google appointment " + appointment.getSummary()); }
@Override public void update(Absence absence, String eventId, CalendarSettings calendarSettings) { googleCalendarClient = getOrCreateGoogleCalendarClient(); if (googleCalendarClient != null) { String calendarId = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings().getCalendarId(); try { // gather exiting event Event event = googleCalendarClient.events().get(calendarId, eventId).execute(); // update event with absence fillEvent(absence, event); // sync event to calendar googleCalendarClient.events().patch(calendarId, eventId, event).execute(); LOG.info(String.format("Event %s has been updated in calendar '%s'.", eventId, calendarId)); } catch (IOException ex) { LOG.warn(String.format("Could not update event %s in calendar '%s'.", eventId, calendarId), ex); mailService.sendCalendarUpdateErrorNotification(calendarId, absence, eventId, ex.getMessage()); } } }
@Override public void addAppointment(final AppointmentDto appointment) throws IOException { final Event event = new Event(); final Map<String, String> privateProperties = new HashMap<String, String>(); privateProperties.put(EXT_PROPERTY_EXCHANGE_ID, appointment.getExchangeId()); final ExtendedProperties extProperties = new ExtendedProperties(); extProperties.setPrivate(privateProperties); event.setExtendedProperties(extProperties); populateEventFromAppointmentDto(appointment, event); client.events().insert(calendarId, event).execute(); LOG.info("Added Google appointment " + appointment.getSummary()); }
@Override public Optional<String> add(Absence absence, CalendarSettings calendarSettings) { googleCalendarClient = getOrCreateGoogleCalendarClient(); if (googleCalendarClient != null) { GoogleCalendarSettings googleCalendarSettings = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings(); String calendarId = googleCalendarSettings.getCalendarId(); try { Event eventToCommit = new Event(); fillEvent(absence, eventToCommit); Event eventInCalendar = googleCalendarClient.events().insert(calendarId, eventToCommit).execute(); LOG.info(String.format("Event %s for '%s' added to calendar '%s'.", eventInCalendar.getId(), absence.getPerson().getNiceName(), eventInCalendar.getSummary())); return Optional.of(eventInCalendar.getId()); } catch (IOException ex) { LOG.warn(String.format("An error occurred while trying to add appointment to calendar %s", calendarId), ex); mailService.sendCalendarSyncErrorNotification(calendarId, absence, ex.toString()); } } return Optional.empty(); }
int page = 1; LOG.info("Retrieving Google Calendar events page " + page); Events feed = client.events().list(calendarId).execute(); while (true) { if (feed.getItems() != null) { page++; LOG.info("Retrieving Google Calendar events page " + page); feed = client.events().list(calendarId).setPageToken(pageToken).execute(); } else { break;
public static boolean eventExists(GuildSettings settings, String eventId) { //TODO: Support multiple calendars... String calendarId = DatabaseManager.getManager().getMainCalendar(settings.getGuildID()).getCalendarAddress(); try { Calendar service = CalendarAuth.getCalendarService(settings); return service.events().get(calendarId, eventId).execute() != null; } catch (Exception e) { //Failed to check event, probably doesn't exist, safely ignore. } return false; }
@Before public void setUp() { try { CalendarList calendarListModel = new com.google.api.services.calendar.model.CalendarList(); when(client.calendars()).thenReturn(calendars); when(calendars.insert(anyObject())).thenReturn(calendarsInsert); when(calendarsInsert.execute()).thenReturn(new com.google.api.services.calendar.model.Calendar()); when(client.calendarList()).thenReturn(calendarsList); when(calendarsList.list()).thenReturn(calendarsListList); when(calendarsListList.execute()).thenReturn(calendarListModel); when(auth.getAuthorizedCalendar(anyString(), anyString())).thenReturn(client); when(client.events()).thenReturn(clientEvents); when(clientEvents.insert(anyString(), anyObject())).thenReturn(calendarEventsInsert); when(calendarEventsInsert.execute()).thenReturn(new com.google.api.services.calendar.model.Event()); when(clientEvents.list(anyString())).thenReturn(calendarEventsList); when(calendarEventsList.execute()).thenReturn(new com.google.api.services.calendar.model.Events()); } catch (Exception e) { fail(e.getMessage()); } }
public static void main(String... args) throws IOException, GeneralSecurityException { // Build a new authorized API client service. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) .setApplicationName(APPLICATION_NAME) .build(); // List the next 10 events from the primary calendar. DateTime now = new DateTime(System.currentTimeMillis()); Events events = service.events().list("primary") .setMaxResults(10) .setTimeMin(now) .setOrderBy("startTime") .setSingleEvents(true) .execute(); List<Event> items = events.getItems(); if (items.isEmpty()) { System.out.println("No upcoming events found."); } else { System.out.println("Upcoming events"); for (Event event : items) { DateTime start = event.getStart().getDateTime(); if (start == null) { start = event.getStart().getDate(); } System.out.printf("%s (%s)\n", event.getSummary(), start); } } } }
@Override protected int poll() throws Exception { com.google.api.services.calendar.Calendar.Events.List request = getClient().events().list(getConfiguration().getCalendarId()).setOrderBy("updated"); if (ObjectHelper.isNotEmpty(getConfiguration().getQuery())) { request.setQ(getConfiguration().getQuery());
/** * Deletes an event from the calendar. * * @param settings Guild settings * @param eventId The ID of the event to delete. * @return <code>true</code> if successfully deleted, otherwise <code>false</code>. */ public static Boolean deleteEvent(GuildSettings settings, String eventId) { //TODO: Support multiple calendars... String calendarId = DatabaseManager.getManager().getMainCalendar(settings.getGuildID()).getCalendarAddress(); try { Calendar service = CalendarAuth.getCalendarService(settings); try { service.events().delete(calendarId, eventId).execute(); } catch (Exception e) { //Failed to delete event... return false; } DatabaseManager.getManager().deleteAnnouncementsForEvent(settings.getGuildID(), eventId); DatabaseManager.getManager().deleteEventData(eventId); return true; } catch (Exception e) { System.out.println("Something weird happened when deleting an event!"); Logger.getLogger().exception(null, "Failed to delete event.", e, EventUtils.class); e.printStackTrace(); } return false; }
public static boolean inPast(String eventId, GuildSettings settings) { //TODO: Support multiple calendars if (EventUtils.eventExists(settings, eventId)) { try { Calendar service = CalendarAuth.getCalendarService(settings); CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID()); Event e = service.events().get(calendarData.getCalendarId(), eventId).execute(); return inPast(e); } catch (Exception e) { Logger.getLogger().exception(null, "Failed to get calendar auth", e, TimeUtils.class); //Return false and allow RSVP so user is not adversely affected. return false; } } return false; }
break; case EVENTS: apiProxy = getClient().events(); break; case FREEBUSY: