Example usage for java.time OffsetDateTime toLocalDate

List of usage examples for java.time OffsetDateTime toLocalDate

Introduction

In this page you can find the example usage for java.time OffsetDateTime toLocalDate.

Prototype

public LocalDate toLocalDate() 

Source Link

Document

Gets the LocalDate part of this date-time.

Usage

From source file:org.openmhealth.shim.fitbit.FitbitShim.java

@Override
public ShimDataResponse getData(ShimDataRequest shimDataRequest) throws ShimException {

    AccessParameters accessParameters = shimDataRequest.getAccessParameters();
    String accessToken = accessParameters.getAccessToken();
    String tokenSecret = accessParameters.getTokenSecret();

    FitbitDataType fitbitDataType;//from   w ww  . j  a v a  2s.  c o  m
    try {
        fitbitDataType = FitbitDataType.valueOf(shimDataRequest.getDataTypeKey().trim().toUpperCase());
    } catch (NullPointerException | IllegalArgumentException e) {
        throw new ShimException("Null or Invalid data type parameter: " + shimDataRequest.getDataTypeKey()
                + " in shimDataRequest, cannot retrieve data.");
    }

    /***
     * Setup default date parameters
     */
    OffsetDateTime today = LocalDate.now().atStartOfDay(ZoneId.of("Z")).toOffsetDateTime();

    OffsetDateTime startDate = shimDataRequest.getStartDateTime() == null ? today.minusDays(1)
            : shimDataRequest.getStartDateTime();

    OffsetDateTime endDate = shimDataRequest.getEndDateTime() == null ? today.plusDays(1)
            : shimDataRequest.getEndDateTime();

    OffsetDateTime currentDate = startDate;

    if (fitbitDataType.equals(FitbitDataType.WEIGHT)) {
        return getRangeData(startDate, endDate, fitbitDataType, shimDataRequest.getNormalize(), accessToken,
                tokenSecret);
    } else {
        /**
         * Fitbit's API limits you to making a request for each given day
         * of data. Thus we make a request for each day in the submitted time
         * range and then aggregate the response based on the normalization parameter.
         */
        List<ShimDataResponse> dayResponses = new ArrayList<>();

        while (currentDate.toLocalDate().isBefore(endDate.toLocalDate())
                || currentDate.toLocalDate().isEqual(endDate.toLocalDate())) {

            dayResponses.add(getDaysData(currentDate, fitbitDataType, shimDataRequest.getNormalize(),
                    accessToken, tokenSecret));
            currentDate = currentDate.plusDays(1);
        }

        ShimDataResponse shimDataResponse = shimDataRequest.getNormalize() ? aggregateNormalized(dayResponses)
                : aggregateIntoList(dayResponses);

        return shimDataResponse;
    }
}

From source file:org.openmhealth.shim.fitbit.FitbitShim.java

private ShimDataResponse getRangeData(OffsetDateTime fromTime, OffsetDateTime toTime,
        FitbitDataType fitbitDataType, boolean normalize, String accessToken, String tokenSecret)
        throws ShimException {

    String fromDateString = fromTime.toLocalDate().toString();
    String toDateString = toTime.toLocalDate().toString();

    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(DATA_URL).path(
            "/1/user/-/{fitbitDataTypeEndpoint}/date/{fromDateString}/{toDateString}{stepTimeSeries}.json");
    String endpointUrl = uriComponentsBuilder.buildAndExpand(fitbitDataType.getEndPoint(), fromDateString,
            toDateString, (fitbitDataType == FitbitDataType.STEPS ? "/1d/1min" : "")).encode().toUriString();

    return executeRequest(endpointUrl, accessToken, tokenSecret, normalize, fitbitDataType);
}

From source file:org.openmhealth.shim.fitbit.FitbitShim.java

private ShimDataResponse getDaysData(OffsetDateTime dateTime, FitbitDataType fitbitDataType, boolean normalize,
        String accessToken, String tokenSecret) throws ShimException {

    String dateString = dateTime.toLocalDate().toString();

    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(DATA_URL)
            .path("/1/user/-/{fitbitDataTypeEndpoint}/date/{dateString}{stepTimeSeries}.json");
    String endpointUrl = uriComponentsBuilder.buildAndExpand(fitbitDataType.getEndPoint(), dateString,
            (fitbitDataType == FitbitDataType.STEPS ? "/1d/1min" : "")).encode().toString();

    ApplicationAccessParameters parameters = findApplicationAccessParameters();
    HttpRequestBase dataRequest = OAuth1Utils.getSignedRequest(HttpMethod.GET, endpointUrl,
            parameters.getClientId(), parameters.getClientSecret(), accessToken, tokenSecret, null);

    HttpResponse response;//  w ww. j  a v a2 s.c  om
    try {
        response = httpClient.execute(dataRequest);
        HttpEntity responseEntity = response.getEntity();

        StringWriter writer = new StringWriter();
        IOUtils.copy(responseEntity.getContent(), writer);

        ObjectMapper objectMapper = new ObjectMapper();

        if (normalize) {

            JsonNode jsonNode = objectMapper.readValue(writer.toString(), JsonNode.class);

            FitbitDataPointMapper dataPointMapper;

            switch (fitbitDataType) {
            case STEPS:
                dataPointMapper = new FitbitStepCountDataPointMapper();
                break;
            case ACTIVITY:
                dataPointMapper = new FitbitPhysicalActivityDataPointMapper();
                break;
            case WEIGHT:
                dataPointMapper = new FitbitBodyWeightDataPointMapper();
                break;
            case SLEEP:
                dataPointMapper = new FitbitSleepDurationDataPointMapper();
                break;
            case BODY_MASS_INDEX:
                dataPointMapper = new FitbitBodyMassIndexDataPointMapper();
                break;
            default:
                throw new UnsupportedOperationException();
            }

            return ShimDataResponse.result(FitbitShim.SHIM_KEY,
                    dataPointMapper.asDataPoints(singletonList(jsonNode)));

        } else {
            /**
             * The fitbit API's system works by retrieving each day's
             * data. The date captured is not returned in the data from fitbit because
             * it's implicit so we create a JSON wrapper that includes it.
             */
            String jsonContent = "{\"result\": {\"date\": \"" + dateString + "\" " + ",\"content\": "
                    + writer.toString() + "}}";

            return ShimDataResponse.result(FitbitShim.SHIM_KEY, objectMapper.readTree(jsonContent));
        }
    } catch (IOException e) {
        throw new ShimException("Could not fetch data", e);
    } finally {
        dataRequest.releaseConnection();
    }
}

From source file:org.silverpeas.core.calendar.Recurrence.java

private Temporal decode(final OffsetDateTime dateTime) {
    return getStartDate() instanceof LocalDate ? dateTime.toLocalDate() : dateTime;
}