Skip to content

Commit

Permalink
Add API for routing with coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhalos committed Jan 17, 2018
1 parent 41eaf60 commit ed45fab
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/main/java/net/contargo/iris/route/RouteInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.contargo.iris.GeoLocation;
import net.contargo.iris.container.ContainerType;

import java.util.Objects;


/**
* Encapsulates detail information that are needed to compute a {@link net.contargo.iris.route.Route}.
Expand Down Expand Up @@ -96,4 +98,28 @@ public void setRouteCombo(RouteCombo routeCombo) {

this.routeCombo = routeCombo;
}


@Override
public boolean equals(Object o) {

if (this == o)
return true;

if (o == null || getClass() != o.getClass())
return false;

RouteInformation that = (RouteInformation) o;

return Objects.equals(destination, that.destination) && product == that.product
&& containerType == that.containerType && routeDirection == that.routeDirection
&& routeCombo == that.routeCombo;
}


@Override
public int hashCode() {

return Objects.hash(destination, product, containerType, routeDirection, routeCombo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.wordnik.swagger.annotations.ApiOperation;

import net.contargo.iris.GeoLocation;
import net.contargo.iris.address.dto.GeoLocationDto;
import net.contargo.iris.api.ControllerConstants;
import net.contargo.iris.connection.dto.RouteDataDto;
import net.contargo.iris.connection.dto.RouteDto;
import net.contargo.iris.connection.dto.RoutePartDto;
import net.contargo.iris.connection.dto.SeaportConnectionRoutesDtoService;
import net.contargo.iris.container.ContainerType;
import net.contargo.iris.route.RouteCombo;
import net.contargo.iris.route.RouteInformation;
import net.contargo.iris.route.dto.EnricherDtoService;
import net.contargo.iris.route.dto.RoutingResultDto;
import net.contargo.iris.route.service.RouteUrlSerializationService;
import net.contargo.iris.seaport.dto.SeaportDto;
import net.contargo.iris.seaport.dto.SeaportDtoService;
Expand All @@ -22,23 +28,39 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.invoke.MethodHandles;

import java.math.BigDecimal;
import java.math.BigInteger;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static net.contargo.iris.container.ContainerType.TWENTY_LIGHT;
import static net.contargo.iris.route.RouteCombo.WATERWAY;
import static net.contargo.iris.route.RouteDirection.EXPORT;
import static net.contargo.iris.route.RouteProduct.ONEWAY;
import static net.contargo.iris.route.RouteType.BARGE;

import static org.slf4j.LoggerFactory.getLogger;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import static java.math.BigDecimal.ZERO;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;


/**
* @author Sandra Thieme - thieme@synyx.de
* @author Ben Antony - antony@synyx.de
*/
@Controller
public class RoutesApiController {
Expand All @@ -50,15 +72,17 @@ public class RoutesApiController {
private final SeaportDtoService seaportDtoService;
private final SeaportConnectionRoutesDtoService seaportConnectionRoutesDtoService;
private final RouteUrlSerializationService routeUrlSerializationService;
private final EnricherDtoService enricherDtoService;

@Autowired
public RoutesApiController(SeaportDtoService seaportDtoService,
SeaportConnectionRoutesDtoService seaportConnectionRoutesDtoService,
RouteUrlSerializationService routeUrlSerializationService) {
RouteUrlSerializationService routeUrlSerializationService, EnricherDtoService enricherDtoService) {

this.seaportDtoService = seaportDtoService;
this.seaportConnectionRoutesDtoService = seaportConnectionRoutesDtoService;
this.routeUrlSerializationService = routeUrlSerializationService;
this.enricherDtoService = enricherDtoService;
}

/**
Expand Down Expand Up @@ -124,4 +148,48 @@ public RoutesResponse getRoutes(@PathVariable("seaportuid") BigInteger seaportUi

return response;
}


@ApiOperation(value = "Returns a list of all possible routes to a location.")
@RequestMapping(value = "/routes", method = GET)
@ResponseBody
public List<RoutingResultDto> getRoutesWithCoordinates(@RequestParam(value = "lat") double latitude,
@RequestParam(value = "lon") double longitude) {

RouteInformation routeInformation = new RouteInformation(new GeoLocation(BigDecimal.valueOf(latitude),
BigDecimal.valueOf(longitude)), ONEWAY, TWENTY_LIGHT, EXPORT, WATERWAY);

return seaportDtoService.getAllActive()
.stream()
.map(s -> seaportConnectionRoutesDtoService.getAvailableSeaportConnectionRoutes(s, routeInformation))
.flatMap(Collection::stream)
.map(enricherDtoService::enrich)
.map(this::toRoutingResultDto)
.sorted(comparing(RoutingResultDto::getDistance))
.collect(toList());
}


private RoutingResultDto toRoutingResultDto(RouteDto routeDto) {

RouteDataDto data = routeDto.getData();
BigDecimal bargeDistance = data.getParts().stream().filter(p -> p.getRouteType() == BARGE)
.map(p -> p.getData().getBargeDieselDistance())
.reduce(ZERO, BigDecimal::add);

List<GeoLocationDto> stops = new ArrayList<>();
stops.add(data.getParts().get(0).getOrigin());
stops.addAll(data.getParts().stream().map(RoutePartDto::getDestination).collect(toList()));

return new RoutingResultDto.Builder().withCo2(data.getCo2())
.withCo2DirectTruck(data.getCo2DirectTruck())
.withDistance(data.getTotalDistance())
.withDuration(data.getTotalDuration())
.withOnewayTruckDistance(data.getTotalOnewayTruckDistance())
.withRealTollDistance(data.getTotalRealTollDistance())
.withTollDistance(data.getTotalTollDistance())
.withBargeDistance(bargeDistance)
.withStops(stops)
.build();
}
}
180 changes: 180 additions & 0 deletions src/main/java/net/contargo/iris/route/dto/RoutingResultDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package net.contargo.iris.route.dto;

import net.contargo.iris.address.dto.GeoLocationDto;

import java.math.BigDecimal;

import java.util.List;


/**
* @author Ben Antony - antony@synyx.de
*/
public class RoutingResultDto {

private final BigDecimal co2;
private final BigDecimal co2DirectTruck;
private final BigDecimal distance;
private final BigDecimal bargeDistance;
private final BigDecimal onewayTruckDistance;
private final BigDecimal realTollDistance;
private final BigDecimal tollDistance;
private final BigDecimal duration;
private final List<GeoLocationDto> stops;

private RoutingResultDto(Builder builder) {

this.co2 = builder.co2;
this.co2DirectTruck = builder.co2DirectTruck;
this.distance = builder.distance;
this.bargeDistance = builder.bargeDistance;
this.onewayTruckDistance = builder.onewayTruckDistance;
this.realTollDistance = builder.realTollDistance;
this.tollDistance = builder.tollDistance;
this.duration = builder.duration;
this.stops = builder.stops;
}

public BigDecimal getCo2() {

return co2;
}


public BigDecimal getCo2DirectTruck() {

return co2DirectTruck;
}


public BigDecimal getDistance() {

return distance;
}


public BigDecimal getBargeDistance() {

return bargeDistance;
}


public BigDecimal getOnewayTruckDistance() {

return onewayTruckDistance;
}


public BigDecimal getRealTollDistance() {

return realTollDistance;
}


public BigDecimal getTollDistance() {

return tollDistance;
}


public BigDecimal getDuration() {

return duration;
}


public List<GeoLocationDto> getStops() {

return stops;
}

public static class Builder {

private BigDecimal co2;
private BigDecimal co2DirectTruck;
private BigDecimal distance;
private BigDecimal bargeDistance;
private BigDecimal onewayTruckDistance;
private BigDecimal realTollDistance;
private BigDecimal tollDistance;
private BigDecimal duration;
private List<GeoLocationDto> stops;

public RoutingResultDto build() {

return new RoutingResultDto(this);
}


public Builder withCo2(BigDecimal co2) {

this.co2 = co2;

return this;
}


public Builder withCo2DirectTruck(BigDecimal co2DirectTruck) {

this.co2DirectTruck = co2DirectTruck;

return this;
}


public Builder withDistance(BigDecimal distance) {

this.distance = distance;

return this;
}


public Builder withBargeDistance(BigDecimal bargeDistance) {

this.bargeDistance = bargeDistance;

return this;
}


public Builder withOnewayTruckDistance(BigDecimal onewayTruckDistance) {

this.onewayTruckDistance = onewayTruckDistance;

return this;
}


public Builder withRealTollDistance(BigDecimal realTollDistance) {

this.realTollDistance = realTollDistance;

return this;
}


public Builder withTollDistance(BigDecimal tollDistance) {

this.tollDistance = tollDistance;

return this;
}


public Builder withDuration(BigDecimal duration) {

this.duration = duration;

return this;
}


public Builder withStops(List<GeoLocationDto> stops) {

this.stops = stops;

return this;
}
}
}
Loading

0 comments on commit ed45fab

Please sign in to comment.