|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+package se.citerus.dddsample.application.routing;
|
|
|
2
|
+
|
|
|
3
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
4
|
+import se.citerus.dddsample.domain.model.cargo.Itinerary;
|
|
|
5
|
+import se.citerus.dddsample.domain.model.cargo.Leg;
|
|
|
6
|
+import se.citerus.dddsample.domain.model.cargo.RouteSpecification;
|
|
|
7
|
+import se.citerus.dddsample.domain.model.carrier.CarrierMovementId;
|
|
|
8
|
+import se.citerus.dddsample.domain.model.carrier.CarrierMovementRepository;
|
|
|
9
|
+import se.citerus.dddsample.domain.model.location.Location;
|
|
|
10
|
+import se.citerus.dddsample.domain.model.location.LocationRepository;
|
|
|
11
|
+import se.citerus.dddsample.domain.model.location.UnLocode;
|
|
|
12
|
+import se.citerus.dddsample.domain.service.RoutingService;
|
|
|
13
|
+import se.citerus.routingteam.GraphTraversalService;
|
|
|
14
|
+import se.citerus.routingteam.TransitEdge;
|
|
|
15
|
+import se.citerus.routingteam.TransitPath;
|
|
|
16
|
+
|
|
|
17
|
+import java.util.ArrayList;
|
|
|
18
|
+import java.util.List;
|
|
|
19
|
+
|
|
|
20
|
+/**
|
|
|
21
|
+ * Our end of the routing service.
|
|
|
22
|
+ *
|
|
|
23
|
+ */
|
|
|
24
|
+public class ExternalRoutingService implements RoutingService {
|
|
|
25
|
+
|
|
|
26
|
+ private GraphTraversalService graphTraversalService;
|
|
|
27
|
+ private LocationRepository locationRepository;
|
|
|
28
|
+ private CarrierMovementRepository carrierMovementRepository;
|
|
|
29
|
+
|
|
|
30
|
+ @Transactional(readOnly = true)
|
|
|
31
|
+ public List<Itinerary> fetchRoutesForSpecification(RouteSpecification routeSpecification) {
|
|
|
32
|
+ final Location origin = routeSpecification.origin();
|
|
|
33
|
+ final Location destination = routeSpecification.destination();
|
|
|
34
|
+
|
|
|
35
|
+ final List<TransitPath> transitPaths = graphTraversalService.performHeavyCalculations(
|
|
|
36
|
+ origin.unLocode().idString(),
|
|
|
37
|
+ destination.unLocode().idString()
|
|
|
38
|
+ );
|
|
|
39
|
+
|
|
|
40
|
+ final List<Itinerary> itineraries = new ArrayList<Itinerary>(transitPaths.size());
|
|
|
41
|
+
|
|
|
42
|
+ for (TransitPath transitPath : transitPaths) {
|
|
|
43
|
+ final Itinerary itinerary = toItinerary(transitPath);
|
|
|
44
|
+ itineraries.add(itinerary);
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ return itineraries;
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ private Itinerary toItinerary(TransitPath transitPath) {
|
|
|
51
|
+ List<Leg> legs = new ArrayList(transitPath.getTransitEdges().size());
|
|
|
52
|
+ for (TransitEdge edge : transitPath.getTransitEdges()) {
|
|
|
53
|
+ legs.add(toLeg(edge));
|
|
|
54
|
+ }
|
|
|
55
|
+ return new Itinerary(legs);
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ private Leg toLeg(TransitEdge edge) {
|
|
|
59
|
+ return new Leg(
|
|
|
60
|
+ carrierMovementRepository.find(new CarrierMovementId(edge.getCarrierMovementId())),
|
|
|
61
|
+ locationRepository.find(new UnLocode(edge.getFromUnLocode())),
|
|
|
62
|
+ locationRepository.find(new UnLocode(edge.getToUnLocode()))
|
|
|
63
|
+ );
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ public void setGraphTraversalService(GraphTraversalService graphTraversalService) {
|
|
|
67
|
+ this.graphTraversalService = graphTraversalService;
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+ public void setLocationRepository(LocationRepository locationRepository) {
|
|
|
71
|
+ this.locationRepository = locationRepository;
|
|
|
72
|
+ }
|
|
|
73
|
+
|
|
|
74
|
+ public void setCarrierMovementRepository(CarrierMovementRepository carrierMovementRepository) {
|
|
|
75
|
+ this.carrierMovementRepository = carrierMovementRepository;
|
|
|
76
|
+ }
|
|
|
77
|
+}
|