Kaynağa Gözat

Added limitation properties such as deadline date to graph traversal service API.

GraphDAO no longer uses the database, simply hardcoded for simplicity.
peter_backlund 17 yıl önce
ebeveyn
işleme
835fb6f73d

+ 2
- 1
dddsample/src/main/java/com/partner/pathfinder/api/GraphTraversalService.java Dosyayı Görüntüle

@@ -1,6 +1,7 @@
1 1
 package com.partner.pathfinder.api;
2 2
 
3 3
 import java.util.List;
4
+import java.util.Properties;
4 5
 
5 6
 /**
6 7
  * Part of the external graph traversal API exposed by the routing team
@@ -9,6 +10,6 @@ import java.util.List;
9 10
  */
10 11
 public interface GraphTraversalService {
11 12
 
12
-  List<TransitPath> findShortestPath(String originUnLocode, String destinationUnLocode);
13
+  List<TransitPath> findShortestPath(String originUnLocode, String destinationUnLocode, Properties limitations);
13 14
 
14 15
 }

+ 12
- 33
dddsample/src/main/java/com/partner/pathfinder/internal/GraphDAO.java Dosyayı Görüntüle

@@ -1,44 +1,23 @@
1 1
 package com.partner.pathfinder.internal;
2 2
 
3
-import org.springframework.jdbc.core.JdbcTemplate;
4
-import org.springframework.jdbc.core.RowCallbackHandler;
5
-
6
-import javax.sql.DataSource;
7
-import java.sql.ResultSet;
8
-import java.sql.SQLException;
9
-import java.util.ArrayList;
3
+import java.util.Arrays;
10 4
 import java.util.List;
5
+import java.util.UUID;
11 6
 
12 7
 public class GraphDAO {
13 8
 
14
-  private final JdbcTemplate jt;
15
-
16
-  public GraphDAO(DataSource dataSource) {
17
-    jt = new JdbcTemplate(dataSource);
18
-  }
19
-
20 9
   public List<String> listLocations() {
21
-    final List<String> result = new ArrayList();
22
-
23
-    jt.query("select unlocode from location", new RowCallbackHandler() {
24
-      public void processRow(ResultSet resultSet) throws SQLException {
25
-        result.add(resultSet.getString("unlocode"));
26
-      }
27
-    });
28
-
29
-    return result;
10
+    return Arrays.asList(
11
+      "CNHKG", "AUMEL", "SESTO", "FIHEL", "USCHI", "JNTKO", "DEHAM",
12
+      "CNSHA", "NLRTM", "SEGOT", "CNHGH", "USNYC", "USDAL"
13
+    );
30 14
   }
31 15
 
32
-  // TODO adapt to Voyage
33
-  public void storeCarrierMovementId(String cmId, String from, String to) {
34
-    final String locationSql = "select id from location where unlocode = ?";
35
-
36
-    final Long fromId = jt.queryForLong(locationSql, new Object[]{ from });
37
-    final Long toId = jt.queryForLong(locationSql, new Object[]{ to });
38
-
39
-    final Object[] params = {cmId, fromId, toId};
40
-    jt.update(
41
-      "insert into CarrierMovement (carrier_movement_id,from_id,to_id) " +
42
-      "values (?,?,?)", params);
16
+  public String getVoyageNumber(String from, String to) {
17
+    // TODO return only those that are in the database
18
+    final String random = UUID.randomUUID().toString().toUpperCase();
19
+    final String cmId =  random.substring(0, 4);
20
+    //dao.storeCarrierMovementId(cmId, from, to);
21
+    return cmId;
43 22
   }
44 23
 }

+ 6
- 13
dddsample/src/main/java/com/partner/pathfinder/internal/GraphTraversalServiceImpl.java Dosyayı Görüntüle

@@ -3,7 +3,6 @@ package com.partner.pathfinder.internal;
3 3
 import com.partner.pathfinder.api.GraphTraversalService;
4 4
 import com.partner.pathfinder.api.TransitEdge;
5 5
 import com.partner.pathfinder.api.TransitPath;
6
-import org.springframework.transaction.annotation.Transactional;
7 6
 
8 7
 import java.util.*;
9 8
 
@@ -17,8 +16,9 @@ public class GraphTraversalServiceImpl implements GraphTraversalService {
17 16
     this.random = new Random();
18 17
   }
19 18
 
20
-  @Transactional(readOnly = true)
21
-  public List<TransitPath> findShortestPath(String originUnLocode, String destinationUnLocode) {
19
+  public List<TransitPath> findShortestPath(final String originUnLocode,
20
+                                            final String destinationUnLocode,
21
+                                            final Properties limitations) {
22 22
     List<String> allVertices = dao.listLocations();
23 23
     allVertices.remove(originUnLocode);
24 24
     allVertices.remove(destinationUnLocode);
@@ -32,18 +32,18 @@ public class GraphTraversalServiceImpl implements GraphTraversalService {
32 32
       final String firstLegTo = allVertices.get(0);
33 33
 
34 34
       transitEdges.add(new TransitEdge(
35
-        getRandomCarrierMovementId(originUnLocode, firstLegTo),
35
+        dao.getVoyageNumber(originUnLocode, firstLegTo),
36 36
         originUnLocode, firstLegTo));
37 37
 
38 38
       for (int j = 0; j < allVertices.size() - 1; j++) {
39 39
         final String curr = allVertices.get(j);
40 40
         final String next = allVertices.get(j + 1);
41
-        transitEdges.add(new TransitEdge(getRandomCarrierMovementId(curr, next), curr, next));
41
+        transitEdges.add(new TransitEdge(dao.getVoyageNumber(curr, next), curr, next));
42 42
       }
43 43
 
44 44
       final String lastLegFrom = allVertices.get(allVertices.size() - 1);
45 45
       transitEdges.add(new TransitEdge(
46
-        getRandomCarrierMovementId(lastLegFrom, destinationUnLocode),
46
+        dao.getVoyageNumber(lastLegFrom, destinationUnLocode),
47 47
         lastLegFrom, destinationUnLocode));
48 48
 
49 49
       candidates.add(new TransitPath(transitEdges));
@@ -52,13 +52,6 @@ public class GraphTraversalServiceImpl implements GraphTraversalService {
52 52
     return candidates;
53 53
   }
54 54
 
55
-  private String getRandomCarrierMovementId(String from, String to) {
56
-    final String random = UUID.randomUUID().toString().toUpperCase();
57
-    final String cmId =  random.substring(0, 4);
58
-    dao.storeCarrierMovementId(cmId, from, to);
59
-    return cmId;
60
-  }
61
-
62 55
   private int getRandomNumberOfCandidates() {
63 56
     return 1 + random.nextInt(4);
64 57
   }

+ 15
- 3
dddsample/src/main/java/se/citerus/dddsample/infrastructure/routing/ExternalRoutingService.java Dosyayı Görüntüle

@@ -3,6 +3,8 @@ package se.citerus.dddsample.infrastructure.routing;
3 3
 import com.partner.pathfinder.api.GraphTraversalService;
4 4
 import com.partner.pathfinder.api.TransitEdge;
5 5
 import com.partner.pathfinder.api.TransitPath;
6
+import org.apache.commons.logging.Log;
7
+import org.apache.commons.logging.LogFactory;
6 8
 import se.citerus.dddsample.domain.model.cargo.Itinerary;
7 9
 import se.citerus.dddsample.domain.model.cargo.Leg;
8 10
 import se.citerus.dddsample.domain.model.cargo.RouteSpecification;
@@ -16,6 +18,7 @@ import se.citerus.dddsample.domain.service.RoutingService;
16 18
 import java.util.ArrayList;
17 19
 import java.util.Date;
18 20
 import java.util.List;
21
+import java.util.Properties;
19 22
 
20 23
 /**
21 24
  * Our end of the routing service. This is basically a data model
@@ -28,18 +31,27 @@ public class ExternalRoutingService implements RoutingService {
28 31
   private GraphTraversalService graphTraversalService;
29 32
   private LocationRepository locationRepository;
30 33
   private VoyageRepository voyageRepository;
34
+  private static final Log log = LogFactory.getLog(ExternalRoutingService.class);
31 35
 
32 36
   public List<Itinerary> fetchRoutesForSpecification(RouteSpecification routeSpecification) {
37
+    /*
38
+      The RouteSpecification is picked apart and adapted to the external API.
39
+     */
33 40
     final Location origin = routeSpecification.origin();
34 41
     final Location destination = routeSpecification.destination();
35 42
 
36
-    // TODO send arrival deadline too
43
+    final Properties limitations = new Properties();
44
+    limitations.setProperty("DEADLINE", routeSpecification.arrivalDeadline().toString());
37 45
 
38 46
     final List<TransitPath> transitPaths = graphTraversalService.findShortestPath(
39 47
       origin.unLocode().idString(),
40
-      destination.unLocode().idString()
48
+      destination.unLocode().idString(),
49
+      limitations
41 50
     );
42 51
     
52
+    /*
53
+      The returned result is then translated back into our domain model.
54
+     */
43 55
     final List<Itinerary> itineraries = new ArrayList<Itinerary>();
44 56
 
45 57
     for (TransitPath transitPath : transitPaths) {
@@ -48,7 +60,7 @@ public class ExternalRoutingService implements RoutingService {
48 60
       if (routeSpecification.isSatisfiedBy(itinerary)) {
49 61
         itineraries.add(itinerary);
50 62
       } else {
51
-        // TODO log warning/error? Fail?
63
+        log.warn("Received itinerary that did not satisfy the route specification");
52 64
       }
53 65
     }
54 66
 

+ 19
- 0
dddsample/src/main/resources/com/partner/pathfinder/internal/applicationContext.xml Dosyayı Görüntüle

@@ -0,0 +1,19 @@
1
+<?xml version="1.0"?>
2
+
3
+<beans xmlns="http://www.springframework.org/schema/beans"
4
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6
+
7
+  <bean id="graphTraversalService" class="com.partner.pathfinder.internal.GraphTraversalServiceImpl">
8
+    <constructor-arg ref="graphDAO"/>
9
+  </bean>
10
+
11
+  <bean id="graphDAO" class="com.partner.pathfinder.internal.GraphDAO"/>
12
+
13
+  <bean id="rmiGraphTraversalService" class="org.springframework.remoting.rmi.RmiServiceExporter">
14
+    <property name="serviceInterface" value="com.partner.pathfinder.api.GraphTraversalService"/>
15
+    <property name="service" ref="graphTraversalService"/>
16
+    <property name="serviceName" value="PathFinder"/>
17
+  </bean>
18
+
19
+</beans>

+ 1
- 2
dddsample/src/test/java/se/citerus/dddsample/infrastructure/routing/ExternalRoutingServiceTest.java Dosyayı Görüntüle

@@ -14,7 +14,6 @@ import se.citerus.dddsample.domain.model.location.LocationRepository;
14 14
 import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
15 15
 import se.citerus.dddsample.infrastructure.persistence.inmemory.LocationRepositoryInMem;
16 16
 
17
-import javax.sql.DataSource;
18 17
 import java.util.Arrays;
19 18
 import java.util.Date;
20 19
 import java.util.List;
@@ -32,7 +31,7 @@ public class ExternalRoutingServiceTest extends TestCase {
32 31
     voyageRepository = createMock(VoyageRepository.class);
33 32
     externalRoutingService.setVoyageRepository(voyageRepository);
34 33
 
35
-    GraphTraversalService graphTraversalService = new GraphTraversalServiceImpl(new GraphDAO(createMock(DataSource.class)) {
34
+    GraphTraversalService graphTraversalService = new GraphTraversalServiceImpl(new GraphDAO() {
36 35
       public List<String> listLocations() {
37 36
         return Arrays.asList(TOKYO.unLocode().idString(), STOCKHOLM.unLocode().idString(), GOTHENBURG.unLocode().idString());
38 37
       }