Ejemplo n.º 1
0
 @POST
 @Path("/filters")
 @Produces({MediaType.APPLICATION_JSON})
 public String setConnectorFilterState(@FormParam("filterState") String stateJSON) {
   StatusModel result;
   Guest guest = AuthHelper.getGuest();
   if (guest == null) return "{}";
   try {
     settingsService.setConnectorFilterState(guest.getId(), stateJSON);
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=setConnectorFilterState")
             .append(" filterState=")
             .append(stateJSON)
             .append(" guestId=")
             .append(guest.getId());
     logger.info(sb.toString());
     result = new StatusModel(true, "Successfully updated filters state!");
   } catch (Exception e) {
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=setConnectorFilterState")
             .append(" guestId=")
             .append(guest.getId())
             .append(" filterState=")
             .append(stateJSON)
             .append(" stackTrace=<![CDATA[")
             .append(Utils.stackTrace(e))
             .append("]]>");
     logger.warn(sb.toString());
     result = new StatusModel(false, "Failed to udpate filters state!");
   }
   return gson.toJson(result);
 }
Ejemplo n.º 2
0
 @POST
 @Produces({MediaType.APPLICATION_JSON})
 @Path("/settings/reset/{apiKeyId}")
 public String resetConnectorSettings(@PathParam("apiKeyId") long apiKeyId) {
   settingsService.resetConnectorSettings(apiKeyId);
   StatusModel status = new StatusModel(true, "connector settings reset!");
   return gson.toJson(status);
 }
  @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);
  }
Ejemplo n.º 4
0
 @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);
  }
Ejemplo n.º 6
0
 @POST
 @Path("/settings/{apiKeyId}")
 @Produces({MediaType.APPLICATION_JSON})
 public StatusModel saveConnectorSettings(
     @PathParam("apiKeyId") long apiKeyId, @FormParam("json") String json) {
   final ApiKey apiKey = guestService.getApiKey(apiKeyId);
   final long guestId = AuthHelper.getGuestId();
   try {
     if (apiKey.getGuestId() != guestId)
       throw new RuntimeException("attempt to retrieve ApiKey from another guest!");
     settingsService.saveConnectorSettings(apiKey.getId(), json);
   } catch (Throwable e) {
     return new StatusModel(false, e.getMessage());
   }
   return new StatusModel(true, "saved connector settings");
 }
  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);
  }
Ejemplo n.º 8
0
 @POST
 @Path("/{connector}/channels")
 @Produces({MediaType.APPLICATION_JSON})
 public String setConnectorChannels(
     @PathParam("connector") String connectorName, @FormParam("channels") String channels) {
   StatusModel result;
   Guest guest = AuthHelper.getGuest();
   // If no guest is logged in, return empty array
   if (guest == null) return "{}";
   try {
     ApiKey apiKey = guestService.getApiKey(guest.getId(), Connector.getConnector(connectorName));
     settingsService.setChannelsForConnector(
         guest.getId(), apiKey.getConnector(), channels.split(","));
     result = new StatusModel(true, "Successfully updated channels for " + connectorName + ".");
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=setConnectorChannels")
             .append(" connector=")
             .append(connectorName)
             .append(" channels=")
             .append(channels)
             .append(" guestId=")
             .append(guest.getId());
     logger.info(sb.toString());
   } catch (Exception e) {
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=setConnectorChannels")
             .append(" connector=")
             .append(connectorName)
             .append(" guestId=")
             .append(guest.getId())
             .append(" stackTrace=<![CDATA[")
             .append(Utils.stackTrace(e))
             .append("]]>");
     logger.warn(sb.toString());
     result = new StatusModel(false, "Failed to set channels for " + connectorName + ".");
   }
   return gson.toJson(result);
 }
Ejemplo n.º 9
0
  @GET
  @Path("/{objectTypeName}/data")
  @Produces({MediaType.APPLICATION_JSON})
  public String getData(
      @PathParam("objectTypeName") String objectTypeName,
      @QueryParam("start") long start,
      @QueryParam("end") long end,
      @QueryParam("value") String value) {
    Guest guest = AuthHelper.getGuest();
    if (guest == null) return "[]";

    CoachingBuddy coachee;
    try {
      coachee = AuthHelper.getCoachee();
    } catch (CoachRevokedException e) {
      return gson.toJson(
          new StatusModel(
              false,
              "Sorry, permission to access this data has been revoked. Please reload your browser window"));
    }
    if (coachee != null) {
      guest = guestService.getGuestById(coachee.guestId);
    }

    String[] objectTypeNameParts = objectTypeName.split("-");
    ApiKey apiKey =
        guestService
            .getApiKeys(guest.getId(), Connector.getConnector(objectTypeNameParts[0]))
            .get(0);
    Connector connector = apiKey.getConnector();

    final AbstractBodytrackResponder bodytrackResponder =
        connector.getBodytrackResponder(beanFactory);
    return gson.toJson(
        bodytrackResponder.getFacetVOs(
            settingsService.getSettings(guest.getId()), apiKey, objectTypeName, start, end, value));
  }
Ejemplo n.º 10
0
 @GET
 @Path("/filters")
 @Produces({MediaType.APPLICATION_JSON})
 public String getConnectorFilterState() {
   long vieweeId = AuthHelper.getGuestId();
   try {
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=getConnectorFilterState")
             .append(" guestId=")
             .append(vieweeId);
     logger.info(sb.toString());
     return settingsService.getConnectorFilterState(vieweeId);
   } catch (Exception e) {
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=getConnectorFilterState")
             .append(" guestId=")
             .append(vieweeId)
             .append(" stackTrace=<![CDATA[")
             .append(Utils.stackTrace(e))
             .append("]]>");
     logger.warn(sb.toString());
     return gson.toJson(new StatusModel(false, "Failed to get filters: " + e.getMessage()));
   }
 }
Ejemplo n.º 11
0
  @GET
  @Path("/installed")
  @Produces({MediaType.APPLICATION_JSON})
  public String getInstalledConnectors() {
    Guest guest = AuthHelper.getGuest();
    // If no guest is logged in, return empty array
    if (guest == null) return "[]";
    ResourceBundle res = ResourceBundle.getBundle("messages/connectors");
    try {
      List<ConnectorInfo> connectors = sysService.getConnectors();
      JSONArray connectorsArray = new JSONArray();
      for (int i = 0; i < connectors.size(); i++) {
        final ConnectorInfo connectorInfo = connectors.get(i);
        final Connector api = connectorInfo.getApi();
        if (api == null) {
          StringBuilder sb =
              new StringBuilder(
                  "module=API component=connectorStore action=getInstalledConnectors ");
          logger.warn("message=\"null connector for " + connectorInfo.getName() + "\"");
          continue;
        }
        if (!guestService.hasApiKey(guest.getId(), api)
            || api.getName().equals("facebook") /*HACK*/) {
          connectors.remove(i--);
        } else {
          ConnectorInfo connector = connectorInfo;
          JSONObject connectorJson = new JSONObject();
          Connector conn = Connector.fromValue(connector.api);
          ApiKey apiKey = guestService.getApiKey(guest.getId(), conn);

          connectorJson.accumulate("prettyName", conn.prettyName());
          List<String> facetTypes = new ArrayList<String>();
          ObjectType[] objTypes = conn.objectTypes();
          if (objTypes != null) {
            for (ObjectType obj : objTypes) {
              facetTypes.add(connector.connectorName + "-" + obj.getName());
            }
          }
          connectorJson.accumulate("facetTypes", facetTypes);
          connectorJson.accumulate(
              "status", apiKey.status != null ? apiKey.status.toString() : "NA");
          connectorJson.accumulate("name", connector.name);
          connectorJson.accumulate("connectUrl", connector.connectUrl);
          connectorJson.accumulate("image", connector.image);
          connectorJson.accumulate("connectorName", connector.connectorName);
          connectorJson.accumulate("enabled", connector.enabled);
          connectorJson.accumulate("manageable", connector.manageable);
          connectorJson.accumulate("text", connector.text);
          connectorJson.accumulate("api", connector.api);
          connectorJson.accumulate("apiKeyId", apiKey.getId());
          connectorJson.accumulate(
              "lastSync", connector.supportsSync ? getLastSync(apiKey) : Long.MAX_VALUE);
          connectorJson.accumulate("latestData", getLatestData(apiKey));
          final String auditTrail = checkForErrors(apiKey);
          connectorJson.accumulate("errors", auditTrail != null);
          connectorJson.accumulate("auditTrail", auditTrail != null ? auditTrail : "");
          connectorJson.accumulate("syncing", checkIfSyncInProgress(guest.getId(), conn));
          connectorJson.accumulate(
              "channels", settingsService.getChannelsForConnector(guest.getId(), conn));
          connectorJson.accumulate("sticky", connector.connectorName.equals("fluxtream_capture"));
          connectorJson.accumulate("supportsRenewToken", connector.supportsRenewTokens);
          connectorJson.accumulate("supportsSync", connector.supportsSync);
          connectorJson.accumulate("supportsFileUpload", connector.supportsFileUpload);
          connectorJson.accumulate("prettyName", conn.prettyName());
          final String uploadMessageKey = conn.getName() + ".upload";
          if (res.containsKey(uploadMessageKey)) {
            final String uploadMessage = res.getString(uploadMessageKey);
            connectorJson.accumulate("uploadMessage", uploadMessage);
          }
          connectorsArray.add(connectorJson);
        }
      }
      StringBuilder sb =
          new StringBuilder("module=API component=connectorStore action=getInstalledConnectors")
              .append(" guestId=")
              .append(guest.getId());
      logger.info(sb.toString());
      return connectorsArray.toString();
    } catch (Exception e) {
      StringBuilder sb =
          new StringBuilder("module=API component=connectorStore action=getInstalledConnectors")
              .append(" guestId=")
              .append(guest.getId())
              .append(" stackTrace=<![CDATA[")
              .append(Utils.stackTrace(e))
              .append("]]>");
      System.out.println(sb.toString());
      logger.warn(sb.toString());
      return gson.toJson(
          new StatusModel(false, "Failed to get installed connectors: " + e.getMessage()));
    }
  }