Skip to content

Commit

Permalink
Stuff for #10
Browse files Browse the repository at this point in the history
  • Loading branch information
kumlien committed Feb 13, 2017
1 parent 66270dc commit 1dd8db1
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package se.kumliens.concept2runkeeper.domain.concept2;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.EqualsAndHashCode;
Expand All @@ -11,11 +12,15 @@
import se.kumliens.concept2runkeeper.domain.ExternalActivity;

import org.springframework.data.annotation.PersistenceConstructor;
import se.kumliens.concept2runkeeper.domain.runkeeper.RunKeeperDistance;
import se.kumliens.concept2runkeeper.domain.runkeeper.RunKeeperHeartRate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;

/**
* Created by svante2 on 2016-12-09.
Expand Down Expand Up @@ -55,7 +60,7 @@ public class Concept2CsvActivity implements ExternalActivity {
private String restTimeSeconds;

@JsonProperty("Work Distance")
private String workDistance;
private Double workDistance;

@JsonProperty("Rest Distance")
private String restDistance;
Expand Down Expand Up @@ -90,12 +95,15 @@ public class Concept2CsvActivity implements ExternalActivity {
@JsonProperty("Comments")
private String comments;

@JsonIgnore
private List<Concept2CsvStrokeData> strokeData;

public Concept2CsvActivity() {

}

@PersistenceConstructor
public Concept2CsvActivity(String date, String description, String formattedWorkTime, Double workTimeInSeconds, String formattedRestTime, String restTimeSeconds, String workDistance, String restDistance, String strokeRate, String avgWatts,
public Concept2CsvActivity(String date, String description, String formattedWorkTime, Double workTimeInSeconds, String formattedRestTime, String restTimeSeconds, Double workDistance, String restDistance, String strokeRate, String avgWatts,
String avgHeartRate, String type, String pace, Double calPerHour, String age, String weight, String ranked, String comments) {
this.date = date;
this.description = description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class RecordActivityRequest {

//The total distance traveled, in meters
@JsonProperty("total_distance")
private String distance;
private Double distance;

@JsonProperty("total_calories")
private Double totalCalories;
Expand Down Expand Up @@ -68,7 +68,7 @@ public static RecordActivityRequest from(RunkeeperActivity runkeeperActivity, bo
.type(runkeeperActivity.getType())
//.startTime(runkeeperActivity.getStartTime()) Not needed right now.
.totalCalories(runkeeperActivity.getTotalCalories())
.distance(runkeeperActivity.getDistance().toString())
.distance(runkeeperActivity.getDistance())
.distances(runkeeperActivity.getDistances())
.duration(runkeeperActivity.getDuration())
.equipment(runkeeperActivity.getEquipment())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class UpdateActivityRequest {
@JsonProperty("notes")
private String notes;

@JsonProperty("heart_rate")
@JsonProperty("heartRate")
private List<RunKeeperHeartRate> heartRates;

public String getStartTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;
import se.kumliens.concept2runkeeper.domain.runkeeper.ExternalRunkeeperData;
Expand All @@ -25,6 +27,7 @@

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.text.ParseException;

Expand All @@ -50,6 +53,13 @@ public class RunkeeperService {
public void setup() {
log.debug("Got some props: {}", props);
restTemplate = new RestTemplate();

// set up a buffering request factory, so response body is always buffered
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = new BufferingClientHttpRequestFactory(requestFactory);
requestFactory.setOutputStreaming(false);
restTemplate.setRequestFactory(bufferingClientHttpRequestFactory);

restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
Expand All @@ -58,6 +68,10 @@ public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOExceptio

@Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
InputStream is = clientHttpResponse.getBody();
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
String body = s.hasNext() ? s.next() : "";
log.info("{}", body);
int statusCode = clientHttpResponse.getStatusCode().value();
if(statusCode == 404) {
throw new NoSuchActivityException();
Expand Down Expand Up @@ -154,6 +168,7 @@ public RunkeeperActivity updateActivity(RunkeeperActivity runkeeperActivity, Str
throw new RuntimeException(e);
}
RequestEntity requestEntity = new RequestEntity(request, headers, PUT, URI.create(props.getBaseUrl().toString().concat(runkeeperActivity.getUri())));

ResponseEntity<RunkeeperActivity> response = restTemplate.exchange(requestEntity, RunkeeperActivity.class);
log.info("got an updated activity: {}", response.getBody());
return response.getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static final String getNavigatorViewNameBasedOnView(Class<? extends View>
}

public User getUser() {
return (User) getSession().getAttribute(SESSION_ATTR_USER);
return getSession() != null ? (User) getSession().getAttribute(SESSION_ATTR_USER) : null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@
* Created by svante2 on 2016-12-25.
*/
@Slf4j
public class Concept2DistanceConverter implements Converter<String, String> {
public class Concept2DistanceConverter implements Converter<String, Double> {

@Override
public String convertToModel(String value, Class<? extends String> targetType, Locale locale) throws ConversionException {
return value.replace("m","").replace(" ","").trim();
public String convertToPresentation(Double value, Class<? extends String> targetType, Locale locale) throws ConversionException {
return value.toString();
}

@Override
public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale) throws ConversionException {
try {
public Double convertToModel(String value, Class<? extends Double> targetType, Locale locale) throws ConversionException {
/*try {
value = MoreObjects.firstNonNull(value, "0");
DecimalFormat decimalFormat = new DecimalFormat(Constants.DISTANCE_PATTERN);
return decimalFormat.format(Double.valueOf(value.replace(" ","").trim())) + " m";
} catch (Exception e) {
log.warn("Unable to parse {} to a number", value, e);
return "n/a";
}
}*/
return Double.valueOf(value);
}

@Override
public Class<String> getModelType() {
return String.class;
public Class<Double> getModelType() {
return Double.class;
}

@Override
Expand Down
Loading

0 comments on commit 1dd8db1

Please sign in to comment.