@Override public void syncSharedConnectorSettings( final long apiKeyId, final SharedConnector sharedConnector) { JSONObject jsonSettings = new JSONObject(); if (sharedConnector.filterJson != null) jsonSettings = JSONObject.fromObject(sharedConnector.filterJson); // get calendars, add new configs for new calendars... // we use the data in the connector settings, which have either just been synched (see // UpdateWorker's syncSettings) // or were synched when the connector was last updated; in either cases, we know that the data // is up-to-date final GoogleCalendarConnectorSettings connectorSettings = (GoogleCalendarConnectorSettings) settingsService.getConnectorSettings(apiKeyId); final List<CalendarConfig> calendars = connectorSettings.calendars; JSONArray sharingSettingsCalendars = new JSONArray(); if (jsonSettings.has("calendars")) sharingSettingsCalendars = jsonSettings.getJSONArray("calendars"); there: for (CalendarConfig calendarConfig : calendars) { for (int i = 0; i < sharingSettingsCalendars.size(); i++) { JSONObject sharingSettingsCalendar = sharingSettingsCalendars.getJSONObject(i); if (sharingSettingsCalendar.getString("id").equals(calendarConfig.id)) continue there; } JSONObject sharingConfig = new JSONObject(); sharingConfig.accumulate("id", calendarConfig.id); sharingConfig.accumulate("summary", calendarConfig.summary); sharingConfig.accumulate("description", calendarConfig.description); sharingConfig.accumulate("shared", false); sharingSettingsCalendars.add(sharingConfig); } // and remove configs for deleted notebooks - leave others untouched JSONArray settingsToDelete = new JSONArray(); there: for (int i = 0; i < sharingSettingsCalendars.size(); i++) { JSONObject sharingSettingsCalendar = sharingSettingsCalendars.getJSONObject(i); for (CalendarConfig calendarConfig : calendars) { if (sharingSettingsCalendar.getString("id").equals(calendarConfig.id)) continue there; } settingsToDelete.add(sharingSettingsCalendar); } for (int i = 0; i < settingsToDelete.size(); i++) { JSONObject toDelete = settingsToDelete.getJSONObject(i); for (int j = 0; j < sharingSettingsCalendars.size(); j++) { if (sharingSettingsCalendars .getJSONObject(j) .getString("id") .equals(toDelete.getString("id"))) { sharingSettingsCalendars.remove(j); } } } jsonSettings.put("calendars", sharingSettingsCalendars); String toPersist = jsonSettings.toString(); coachingService.setSharedConnectorFilter(sharedConnector.getId(), toPersist); }
@GET @Path("/settings/{apiKeyId}") @Produces({MediaType.APPLICATION_JSON}) public String getConnectorSettings(@PathParam("apiKeyId") long apiKeyId) throws UpdateFailedException, IOException { final ApiKey apiKey = guestService.getApiKey(apiKeyId); final long guestId = AuthHelper.getGuestId(); if (apiKey.getGuestId() != guestId) throw new RuntimeException("attempt to retrieve ApiKey from another guest!"); final Object settings = settingsService.getConnectorSettings(apiKey.getId()); String json = mapper.writeValueAsString(settings); return json; }
private void initChannelMapping(ApiKey apiKey, final List<CalendarConfig> calendarConfigs) { bodyTrackHelper.deleteChannelMappings(apiKey); ChannelMapping mapping = new ChannelMapping(); mapping.deviceName = "google_calendar"; mapping.channelName = "events"; mapping.timeType = ChannelMapping.TimeType.gmt; mapping.channelType = ChannelMapping.ChannelType.timespan; mapping.guestId = apiKey.getGuestId(); mapping.apiKeyId = apiKey.getId(); bodyTrackHelper.persistChannelMapping(mapping); BodyTrackHelper.ChannelStyle channelStyle = new BodyTrackHelper.ChannelStyle(); channelStyle.timespanStyles = new BodyTrackHelper.MainTimespanStyle(); channelStyle.timespanStyles.defaultStyle = new BodyTrackHelper.TimespanStyle(); channelStyle.timespanStyles.defaultStyle.fillColor = "#92da46"; channelStyle.timespanStyles.defaultStyle.borderColor = "#92da46"; channelStyle.timespanStyles.defaultStyle.borderWidth = 2; channelStyle.timespanStyles.defaultStyle.top = 1.0; channelStyle.timespanStyles.defaultStyle.bottom = 1.0; channelStyle.timespanStyles.values = new HashMap(); GoogleCalendarConnectorSettings connectorSettings = (GoogleCalendarConnectorSettings) settingsService.getConnectorSettings(apiKey.getId()); int n = calendarConfigs.size(); if (connectorSettings != null) { n = 0; for (CalendarConfig calendar : connectorSettings.calendars) { if (!calendar.hidden) n++; } } double rowHeight = 1.f / (n * 2 + 1); int i = 0; for (CalendarConfig config : calendarConfigs) { if (connectorSettings != null && config.hidden) continue; BodyTrackHelper.TimespanStyle stylePart = new BodyTrackHelper.TimespanStyle(); final int rowsFromTop = (i + 1) * 2 - 1; stylePart.top = (double) rowsFromTop * rowHeight - (rowHeight * 0.25); stylePart.bottom = stylePart.top + rowHeight + (rowHeight * 0.25); stylePart.fillColor = config.backgroundColor; stylePart.borderColor = config.backgroundColor; channelStyle.timespanStyles.values.put(config.id, stylePart); i++; } bodyTrackHelper.setDefaultStyle(apiKey.getGuestId(), "google_calendar", "events", channelStyle); }
private void loadHistory(UpdateInfo updateInfo, boolean incremental) throws Exception { Calendar calendar = getCalendar(updateInfo.apiKey); String pageToken = null; long apiKeyId = updateInfo.apiKey.getId(); settingsService.getConnectorSettings(updateInfo.apiKey.getId()); List<String> existingCalendarIds = getExistingCalendarIds(apiKeyId); List<CalendarListEntry> remoteCalendars = new ArrayList<CalendarListEntry>(); List<CalendarConfig> configs = new ArrayList<CalendarConfig>(); do { final long then = System.currentTimeMillis(); final Calendar.CalendarList.List list = calendar.calendarList().list().setPageToken(pageToken); final String query = list.getUriTemplate(); CalendarList calendarList = null; try { calendarList = list.execute(); countSuccessfulApiCall(updateInfo.apiKey, updateInfo.objectTypes, then, query); } catch (IOException e) { countFailedApiCall( updateInfo.apiKey, updateInfo.objectTypes, then, query, ExceptionUtils.getStackTrace(e), list.getLastStatusCode(), list.getLastStatusMessage()); } if (calendarList == null) throw new Exception("Could not get calendar list, apiKeyId=" + updateInfo.apiKey.getId()); List<CalendarListEntry> items = calendarList.getItems(); for (CalendarListEntry item : items) { existingCalendarIds.remove(item.getId()); remoteCalendars.add(item); configs.add(entry2Config(item)); } pageToken = calendarList.getNextPageToken(); } while (pageToken != null); initChannelMapping(updateInfo.apiKey, configs); updateInfo.setContext(REMOTE_CALLENDARS_KEY, remoteCalendars); for (CalendarListEntry remoteCalendar : remoteCalendars) loadCalendarHistory(calendar, remoteCalendar, updateInfo, incremental); deleteCalendars(apiKeyId, existingCalendarIds); }