22
33import com .wordnik .swagger .annotations .ApiOperation ;
44
5+ import net .contargo .iris .GeoLocation ;
6+ import net .contargo .iris .address .dto .GeoLocationDto ;
57import net .contargo .iris .api .ControllerConstants ;
8+ import net .contargo .iris .connection .dto .RouteDataDto ;
69import net .contargo .iris .connection .dto .RouteDto ;
10+ import net .contargo .iris .connection .dto .RoutePartDto ;
711import net .contargo .iris .connection .dto .SeaportConnectionRoutesDtoService ;
812import net .contargo .iris .container .ContainerType ;
913import net .contargo .iris .route .RouteCombo ;
1014import net .contargo .iris .route .RouteInformation ;
15+ import net .contargo .iris .route .dto .EnricherDtoService ;
16+ import net .contargo .iris .route .dto .RoutingResultDto ;
1117import net .contargo .iris .route .service .RouteUrlSerializationService ;
1218import net .contargo .iris .seaport .dto .SeaportDto ;
1319import net .contargo .iris .seaport .dto .SeaportDtoService ;
2228import org .springframework .web .bind .annotation .PathVariable ;
2329import org .springframework .web .bind .annotation .RequestMapping ;
2430import org .springframework .web .bind .annotation .RequestParam ;
31+ import org .springframework .web .bind .annotation .ResponseBody ;
2532
2633import java .lang .invoke .MethodHandles ;
2734
35+ import java .math .BigDecimal ;
2836import java .math .BigInteger ;
2937
38+ import java .util .ArrayList ;
39+ import java .util .Collection ;
3040import java .util .List ;
3141
42+ import static net .contargo .iris .container .ContainerType .TWENTY_LIGHT ;
43+ import static net .contargo .iris .route .RouteCombo .WATERWAY ;
44+ import static net .contargo .iris .route .RouteDirection .EXPORT ;
45+ import static net .contargo .iris .route .RouteProduct .ONEWAY ;
46+ import static net .contargo .iris .route .RouteType .BARGE ;
47+
3248import static org .slf4j .LoggerFactory .getLogger ;
3349
3450import static org .springframework .hateoas .mvc .ControllerLinkBuilder .linkTo ;
3551import static org .springframework .hateoas .mvc .ControllerLinkBuilder .methodOn ;
3652
3753import static org .springframework .web .bind .annotation .RequestMethod .GET ;
3854
55+ import static java .math .BigDecimal .ZERO ;
56+
57+ import static java .util .Comparator .comparing ;
58+ import static java .util .stream .Collectors .toList ;
59+
3960
4061/**
4162 * @author Sandra Thieme - thieme@synyx.de
63+ * @author Ben Antony - antony@synyx.de
4264 */
4365@ Controller
4466public class RoutesApiController {
@@ -50,15 +72,17 @@ public class RoutesApiController {
5072 private final SeaportDtoService seaportDtoService ;
5173 private final SeaportConnectionRoutesDtoService seaportConnectionRoutesDtoService ;
5274 private final RouteUrlSerializationService routeUrlSerializationService ;
75+ private final EnricherDtoService enricherDtoService ;
5376
5477 @ Autowired
5578 public RoutesApiController (SeaportDtoService seaportDtoService ,
5679 SeaportConnectionRoutesDtoService seaportConnectionRoutesDtoService ,
57- RouteUrlSerializationService routeUrlSerializationService ) {
80+ RouteUrlSerializationService routeUrlSerializationService , EnricherDtoService enricherDtoService ) {
5881
5982 this .seaportDtoService = seaportDtoService ;
6083 this .seaportConnectionRoutesDtoService = seaportConnectionRoutesDtoService ;
6184 this .routeUrlSerializationService = routeUrlSerializationService ;
85+ this .enricherDtoService = enricherDtoService ;
6286 }
6387
6488 /**
@@ -124,4 +148,48 @@ public RoutesResponse getRoutes(@PathVariable("seaportuid") BigInteger seaportUi
124148
125149 return response ;
126150 }
151+
152+
153+ @ ApiOperation (value = "Returns a list of all possible routes to a location." )
154+ @ RequestMapping (value = "/routes" , method = GET )
155+ @ ResponseBody
156+ public List <RoutingResultDto > getRoutesWithCoordinates (@ RequestParam (value = "lat" ) double latitude ,
157+ @ RequestParam (value = "lon" ) double longitude ) {
158+
159+ RouteInformation routeInformation = new RouteInformation (new GeoLocation (BigDecimal .valueOf (latitude ),
160+ BigDecimal .valueOf (longitude )), ONEWAY , TWENTY_LIGHT , EXPORT , WATERWAY );
161+
162+ return seaportDtoService .getAllActive ()
163+ .stream ()
164+ .map (s -> seaportConnectionRoutesDtoService .getAvailableSeaportConnectionRoutes (s , routeInformation ))
165+ .flatMap (Collection ::stream )
166+ .map (enricherDtoService ::enrich )
167+ .map (this ::toRoutingResultDto )
168+ .sorted (comparing (RoutingResultDto ::getDistance ))
169+ .collect (toList ());
170+ }
171+
172+
173+ private RoutingResultDto toRoutingResultDto (RouteDto routeDto ) {
174+
175+ RouteDataDto data = routeDto .getData ();
176+ BigDecimal bargeDistance = data .getParts ().stream ().filter (p -> p .getRouteType () == BARGE )
177+ .map (p -> p .getData ().getBargeDieselDistance ())
178+ .reduce (ZERO , BigDecimal ::add );
179+
180+ List <GeoLocationDto > stops = new ArrayList <>();
181+ stops .add (data .getParts ().get (0 ).getOrigin ());
182+ stops .addAll (data .getParts ().stream ().map (RoutePartDto ::getDestination ).collect (toList ()));
183+
184+ return new RoutingResultDto .Builder ().withCo2 (data .getCo2 ())
185+ .withCo2DirectTruck (data .getCo2DirectTruck ())
186+ .withDistance (data .getTotalDistance ())
187+ .withDuration (data .getTotalDuration ())
188+ .withOnewayTruckDistance (data .getTotalOnewayTruckDistance ())
189+ .withRealTollDistance (data .getTotalRealTollDistance ())
190+ .withTollDistance (data .getTotalTollDistance ())
191+ .withBargeDistance (bargeDistance )
192+ .withStops (stops )
193+ .build ();
194+ }
127195}
0 commit comments