Преглед на файлове

Fixed binding to proper command object for selected itinerary

peter_backlund преди 18 години
родител
ревизия
e747e828d0

+ 6
- 13
dddsample/src/main/java/se/citerus/dddsample/application/web/CargoAdminController.java Целия файл

@@ -7,6 +7,7 @@ import se.citerus.dddsample.application.remoting.dto.ItineraryCandidateDTO;
7 7
 import se.citerus.dddsample.application.remoting.dto.LegDTO;
8 8
 import se.citerus.dddsample.application.remoting.dto.LocationDTO;
9 9
 import se.citerus.dddsample.application.web.command.RegistrationCommand;
10
+import se.citerus.dddsample.application.web.command.RouteAssignmentCommand;
10 11
 
11 12
 import javax.servlet.http.HttpServletRequest;
12 13
 import javax.servlet.http.HttpServletResponse;
@@ -86,23 +87,15 @@ public final class CargoAdminController extends MultiActionController {
86 87
     return map;
87 88
   }
88 89
 
89
-  public void assignItinerary(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
90
-    final String trackingId = request.getParameter("trackingId");
91
-
92
-    // TODO:  gah, stuck on indexoutofbounds (legs[0].fromUnlocode etc) when trying to bind...
93
-    // Revisit and fix this with a proper command object, this is just hideous
94
-    final String[] cmIds = (String[]) request.getParameterMap().get("legs.carrierMovementId");
95
-    final String[] fromUnlocodes = (String[]) request.getParameterMap().get("legs.fromUnlocode");
96
-    final String[] toUnlocodes = (String[]) request.getParameterMap().get("legs.toUnlocode");
97
-
98
-    final List<LegDTO> legDTOs = new ArrayList<LegDTO>(cmIds.length);
99
-    for (int i = 0; i < cmIds.length; i++) {
100
-      legDTOs.add(new LegDTO(cmIds[i], fromUnlocodes[i], toUnlocodes[i]));
90
+  public void assignItinerary(final HttpServletRequest request, final HttpServletResponse response, RouteAssignmentCommand command) throws Exception {
91
+    final List<LegDTO> legDTOs = new ArrayList<LegDTO>(command.getLegs().size());
92
+    for (RouteAssignmentCommand.LegCommand leg : command.getLegs()) {
93
+      legDTOs.add(new LegDTO(leg.getCarrierMovementId(), leg.getFromUnLocode(), leg.getToUnLocode()));
101 94
     }
102 95
 
103 96
     final ItineraryCandidateDTO selectedItinerary = new ItineraryCandidateDTO(legDTOs);
104 97
 
105
-    bookingServiceFacade.assignCargoToRoute(trackingId, selectedItinerary);
98
+    bookingServiceFacade.assignCargoToRoute(command.getTrackingId(), selectedItinerary);
106 99
 
107 100
     response.sendRedirect("list.html");
108 101
   }

+ 70
- 0
dddsample/src/main/java/se/citerus/dddsample/application/web/command/RouteAssignmentCommand.java Целия файл

@@ -0,0 +1,70 @@
1
+package se.citerus.dddsample.application.web.command;
2
+
3
+import org.apache.commons.collections.Factory;
4
+import org.apache.commons.collections.ListUtils;
5
+
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+public class RouteAssignmentCommand {
10
+
11
+  private String trackingId;
12
+  private List<LegCommand> legs = ListUtils.lazyList(
13
+    new ArrayList(), LegCommand.factory()
14
+  );
15
+
16
+  public String getTrackingId() {
17
+    return trackingId;
18
+  }
19
+
20
+  public void setTrackingId(String trackingId) {
21
+    this.trackingId = trackingId;
22
+  }
23
+
24
+  public List<LegCommand> getLegs() {
25
+    return legs;
26
+  }
27
+
28
+  public void setLegs(List<LegCommand> legs) {
29
+    this.legs = legs;
30
+  }
31
+
32
+  public static final class LegCommand {
33
+    private String carrierMovementId;
34
+    private String fromUnLocode;
35
+    private String toUnLocode;
36
+
37
+    public String getCarrierMovementId() {
38
+      return carrierMovementId;
39
+    }
40
+
41
+    public void setCarrierMovementId(final String carrierMovementId) {
42
+      this.carrierMovementId = carrierMovementId;
43
+    }
44
+
45
+    public String getFromUnLocode() {
46
+      return fromUnLocode;
47
+    }
48
+
49
+    public void setFromUnLocode(final String fromUnLocode) {
50
+      this.fromUnLocode = fromUnLocode;
51
+    }
52
+
53
+    public String getToUnLocode() {
54
+      return toUnLocode;
55
+    }
56
+
57
+    public void setToUnLocode(final String toUnLocode) {
58
+      this.toUnLocode = toUnLocode;
59
+    }
60
+
61
+    public static Factory factory() {
62
+      return new Factory() {
63
+        public Object create() {
64
+          return new LegCommand();
65
+        }
66
+      };
67
+    }
68
+    
69
+  }
70
+}

+ 0
- 68
dddsample/src/main/java/se/citerus/dddsample/application/web/command/RoutingCommand.java Целия файл

@@ -1,68 +0,0 @@
1
-package se.citerus.dddsample.application.web.command;
2
-
3
-import java.util.ArrayList;
4
-import java.util.List;
5
-
6
-public final class RoutingCommand {
7
-
8
-  private List<ItineraryCandidateCommand> itineraryCandidates = new ArrayList<ItineraryCandidateCommand>();
9
-
10
-  public List<ItineraryCandidateCommand> getItineraryCandidates() {
11
-    return itineraryCandidates;
12
-  }
13
-
14
-  public void setItineraryCandidates(final List<ItineraryCandidateCommand> itineraryCandidates) {
15
-    this.itineraryCandidates = itineraryCandidates;
16
-  }
17
-
18
-  public static final class ItineraryCandidateCommand {
19
-    private String trackingId;
20
-    private List<LegCommand> legs = new ArrayList<LegCommand>();
21
-
22
-    public String getTrackingId() {
23
-      return trackingId;
24
-    }
25
-
26
-    public void setTrackingId(final String trackingId) {
27
-      this.trackingId = trackingId;
28
-    }
29
-
30
-    public List<LegCommand> getLegs() {
31
-      return legs;
32
-    }
33
-
34
-    public void setLegs(final List<LegCommand> legs) {
35
-      this.legs = legs;
36
-    }
37
-  }
38
-
39
-  public static final class LegCommand {
40
-    private String carrierMovementId;
41
-    private String fromUnlocode;
42
-    private String toUnlocode;
43
-
44
-    public String getCarrierMovementId() {
45
-      return carrierMovementId;
46
-    }
47
-
48
-    public void setCarrierMovementId(final String carrierMovementId) {
49
-      this.carrierMovementId = carrierMovementId;
50
-    }
51
-
52
-    public String getFromUnlocode() {
53
-      return fromUnlocode;
54
-    }
55
-
56
-    public void setFromUnlocode(final String fromUnlocode) {
57
-      this.fromUnlocode = fromUnlocode;
58
-    }
59
-
60
-    public String getToUnlocode() {
61
-      return toUnlocode;
62
-    }
63
-
64
-    public void setToUnlocode(final String toUnlocode) {
65
-      this.toUnlocode = toUnlocode;
66
-    }
67
-  }
68
-}

+ 3
- 3
dddsample/src/main/webapp/WEB-INF/jsp/admin/selectItinerary.jsp Целия файл

@@ -51,9 +51,9 @@
51 51
           </thead>
52 52
           <tbody>
53 53
             <c:forEach items="${it.legs}" var="leg" varStatus="legStatus">
54
-              <input type="hidden" name="legs.carrierMovementId" value="${leg.carrierMovementId}"/>
55
-              <input type="hidden" name="legs.fromUnlocode" value="${leg.from}"/>
56
-              <input type="hidden" name="legs.toUnlocode" value="${leg.to}"/>
54
+              <input type="hidden" name="legs[${legStatus.index}].carrierMovementId" value="${leg.carrierMovementId}"/>
55
+              <input type="hidden" name="legs[${legStatus.index}].fromUnLocode" value="${leg.from}"/>
56
+              <input type="hidden" name="legs[${legStatus.index}].toUnLocode" value="${leg.to}"/>
57 57
               <tr>
58 58
                 <td>${leg.carrierMovementId}</td>
59 59
                 <td>${leg.from}</td>

+ 29
- 0
dddsample/src/test/java/se/citerus/dddsample/application/web/CargoAdminControllerTest.java Целия файл

@@ -0,0 +1,29 @@
1
+package se.citerus.dddsample.application.web;
2
+
3
+import junit.framework.TestCase;
4
+import org.easymock.EasyMock;
5
+import org.springframework.mock.web.MockHttpServletRequest;
6
+import org.springframework.mock.web.MockHttpServletResponse;
7
+import se.citerus.dddsample.application.remoting.BookingServiceFacade;
8
+
9
+public class CargoAdminControllerTest extends TestCase {
10
+
11
+  CargoAdminController controller;
12
+  BookingServiceFacade bookingServiceFacade;
13
+  MockHttpServletRequest request;
14
+  MockHttpServletResponse response;
15
+
16
+  public CargoAdminControllerTest() {
17
+    controller = new CargoAdminController();
18
+    bookingServiceFacade = EasyMock.createMock(BookingServiceFacade.class);
19
+    controller.setBookingServiceFacade(bookingServiceFacade);
20
+  }
21
+
22
+  public void testAssignItinerary() throws Exception {
23
+    request = new MockHttpServletRequest("GET","assignItinerary.html");
24
+    response = new MockHttpServletResponse();
25
+
26
+    controller.handleRequest(request, response);
27
+  }
28
+
29
+}

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/application/web/CargoTrackingControllerTest.java Целия файл

@@ -107,7 +107,7 @@ public class CargoTrackingControllerTest extends TestCase {
107 107
     public Cargo track(TrackingId trackingId) {
108 108
       return null;
109 109
     }
110
-    public void notify(TrackingId trackingId) {
110
+    public void inspectCargo(TrackingId trackingId) {
111 111
     }
112 112
   }
113 113
 }

+ 48
- 0
dddsample/src/test/java/se/citerus/dddsample/application/web/command/ItinerarySelectionCommandTest.java Целия файл

@@ -0,0 +1,48 @@
1
+package se.citerus.dddsample.application.web.command;
2
+
3
+import junit.framework.TestCase;
4
+import org.springframework.mock.web.MockHttpServletRequest;
5
+import org.springframework.web.bind.ServletRequestDataBinder;
6
+
7
+import java.util.List;
8
+
9
+public class ItinerarySelectionCommandTest extends TestCase {
10
+
11
+  RouteAssignmentCommand command;
12
+  MockHttpServletRequest request;
13
+
14
+  public void testBind() {
15
+    command = new RouteAssignmentCommand();
16
+    request = new MockHttpServletRequest();
17
+
18
+    request.addParameter("legs[0].carrierMovementId", "CM01");
19
+    request.addParameter("legs[0].fromUnLocode", "AAAAA");
20
+    request.addParameter("legs[0].toUnLocode", "BBBBB");
21
+
22
+    request.addParameter("legs[1].carrierMovementId", "CM02");
23
+    request.addParameter("legs[1].fromUnLocode", "CCCCC");
24
+    request.addParameter("legs[1].toUnLocode", "DDDDD");
25
+
26
+    request.addParameter("trackingId", "XYZ");
27
+
28
+    ServletRequestDataBinder binder = new ServletRequestDataBinder(command);
29
+    binder.bind(request);
30
+
31
+    List<RouteAssignmentCommand.LegCommand> legs = command.getLegs();
32
+    assertEquals(2, legs.size());
33
+
34
+    RouteAssignmentCommand.LegCommand leg = legs.get(0);
35
+    assertEquals("CM01", leg.getCarrierMovementId());
36
+    assertEquals("AAAAA", leg.getFromUnLocode());
37
+    assertEquals("BBBBB", leg.getToUnLocode());
38
+
39
+    leg = legs.get(1);
40
+    assertEquals("CM02", leg.getCarrierMovementId());
41
+    assertEquals("CCCCC", leg.getFromUnLocode());
42
+    assertEquals("DDDDD", leg.getToUnLocode());
43
+
44
+    assertEquals("XYZ", command.getTrackingId());
45
+  }
46
+
47
+
48
+}