private ArrayList<AbstractFacet> extractStepFacets(final ApiData apiData) {
    ArrayList<AbstractFacet> facets = new ArrayList<AbstractFacet>();
    /* burnJson is a JSONArray that contains a seperate JSONArray and calorie counts for each day
     */
    JSONObject bodymediaResponse = JSONObject.fromObject(apiData.json);
    JSONArray daysArray = bodymediaResponse.getJSONArray("days");
    if (bodymediaResponse.has("lastSync")) {
      DateTime d =
          form.parseDateTime(bodymediaResponse.getJSONObject("lastSync").getString("dateTime"));

      // Get timezone map from UpdateInfo context
      TimezoneMap tzMap = (TimezoneMap) updateInfo.getContext("tzMap");

      // Insert lastSync into the updateInfo context so it's accessible to the updater
      updateInfo.setContext("lastSync", d);

      for (Object o : daysArray) {
        if (o instanceof JSONObject) {
          JSONObject day = (JSONObject) o;
          BodymediaStepsFacet steps = new BodymediaStepsFacet(apiData.updateInfo.apiKey.getId());
          super.extractCommonFacetData(steps, apiData);
          steps.totalSteps = day.getInt("totalSteps");
          steps.date = day.getString("date");
          steps.json = day.getString("hours");
          steps.lastSync = d.getMillis();

          DateTime date = formatter.parseDateTime(day.getString("date"));
          steps.date = dateFormatter.print(date.getMillis());
          if (tzMap != null) {
            // Create a LocalDate object which just captures the date without any
            // timezone assumptions
            LocalDate ld =
                new LocalDate(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth());
            // Use tzMap to convert date into a datetime with timezone information
            DateTime realDateStart = tzMap.getStartOfDate(ld);
            // Set the start and end times for the facet.  The start time is the leading midnight
            // of date according to BodyMedia's idea of what timezone you were in then.
            // Need to figure out what end should be...
            steps.start = realDateStart.getMillis();
            int minutesLength = 1440;
            steps.end = steps.start + DateTimeConstants.MILLIS_PER_MINUTE * minutesLength;
          } else {
            TimeZone timeZone =
                metadataService.getTimeZone(apiData.updateInfo.getGuestId(), date.getMillis());
            long fromMidnight = TimeUtils.fromMidnight(date.getMillis(), timeZone);
            long toMidnight = TimeUtils.toMidnight(date.getMillis(), timeZone);
            steps.start = fromMidnight;
            steps.end = toMidnight;
          }
          facets.add(steps);
        } else throw new JSONException("Days array is not a proper JSONObject");
      }
    }
    return facets;
  }
  /**
   * Extracts facets for each day from the data returned by the api.
   *
   * @param apiData The data returned by the Burn api
   * @return A list of facets for each day provided by the apiData
   */
  private ArrayList<AbstractFacet> extractBurnFacets(ApiData apiData) {
    ArrayList<AbstractFacet> facets = new ArrayList<AbstractFacet>();
    /* burnJson is a JSONArray that contains a seperate JSONArray and calorie counts for each day
     */
    JSONObject bodymediaResponse = JSONObject.fromObject(apiData.json);
    if (bodymediaResponse.has("days") && bodymediaResponse.has("lastSync")) {
      DateTime d =
          form.parseDateTime(bodymediaResponse.getJSONObject("lastSync").getString("dateTime"));
      JSONArray daysArray = bodymediaResponse.getJSONArray("days");
      for (Object o : daysArray) {
        if (o instanceof JSONObject) {
          JSONObject day = (JSONObject) o;
          BodymediaBurnFacet burn = new BodymediaBurnFacet();
          // The following call must be made to load data about he facets
          super.extractCommonFacetData(burn, apiData);
          burn.setTotalCalories(day.getInt("totalCalories"));
          burn.date = day.getString("date");
          burn.setEstimatedCalories(day.getInt("estimatedCalories"));
          burn.setPredictedCalories(day.getInt("predictedCalories"));
          burn.json = day.getString("minutes");
          burn.lastSync = d.getMillis();

          DateTime date = formatter.parseDateTime(day.getString("date"));
          burn.date = dateFormatter.print(date.getMillis());
          TimeZone timeZone =
              metadataService.getTimeZone(apiData.updateInfo.getGuestId(), date.getMillis());
          long fromMidnight = TimeUtils.fromMidnight(date.getMillis(), timeZone);
          long toMidnight = TimeUtils.toMidnight(date.getMillis(), timeZone);
          // Sets the start and end times for the facet so that it can be uniquely defined
          burn.start = fromMidnight;
          burn.end = toMidnight;

          facets.add(burn);
        } else throw new RuntimeException("days array is not a proper JSONObject");
      }
    }
    return facets;
  }
Esempio n. 3
0
 public void setWeek(final int year, final int week) {
   this.timeUnit = TimeUnit.WEEK;
   fromDate = TimeUtils.getBeginningOfWeek(year, week);
 }