Explorar el Código

Cargo tracking web interface now sits immediately on top of the domain layer, forming one of the "user interface extremes" (the other one being complete remote-DTO-separation).

peter_backlund hace 18 años
padre
commit
a96af1896c

+ 0
- 90
dddsample/src/main/java/se/citerus/dddsample/application/remoting/dto/CargoTrackingDTO.java Ver fichero

@@ -1,90 +0,0 @@
1
-package se.citerus.dddsample.application.remoting.dto;
2
-
3
-import se.citerus.dddsample.domain.model.cargo.StatusCode;
4
-
5
-import java.io.Serializable;
6
-import java.util.ArrayList;
7
-import java.util.Collections;
8
-import java.util.Date;
9
-import java.util.List;
10
-
11
-/**
12
- * DTO for a cargo and its delivery history.
13
- */
14
-public final class CargoTrackingDTO implements Serializable {
15
-
16
-  private final String trackingId;
17
-  private final String origin;
18
-  private final String finalDestination;
19
-  private final String currentLocationId;
20
-  private final List<HandlingEventDTO> events;
21
-  private final String carrierMovementId;
22
-  private final StatusCode statusCode;
23
-  private final boolean misdirected;
24
-
25
-  /**
26
-   * Constructor.
27
-   *
28
-   * @param trackingId
29
-   * @param origin
30
-   * @param finalDestination
31
-   * @param statusCode
32
-   * @param currentLocationId
33
-   * @param carrierMovementId
34
-   * @param isMisdirected
35
-   */
36
-  public CargoTrackingDTO(final String trackingId, final String origin, final String finalDestination,
37
-                          final StatusCode statusCode, final String currentLocationId, final String carrierMovementId,
38
-                          final boolean isMisdirected) {
39
-    this.trackingId = trackingId;
40
-    this.origin = origin;
41
-    this.finalDestination = finalDestination;
42
-    this.statusCode = statusCode;
43
-    this.currentLocationId = currentLocationId;
44
-    this.carrierMovementId = carrierMovementId;
45
-    this.misdirected = isMisdirected;
46
-
47
-    this.events = new ArrayList<HandlingEventDTO>();
48
-  }
49
-
50
-  public void addEvent(final String location, final String type, final String carrier, final Date time,
51
-                       final boolean expected) {
52
-    events.add(new HandlingEventDTO(location, type, carrier, time, expected));
53
-  }
54
-
55
-  /**
56
-   * @return An unmodifiable list DTOs.
57
-   */
58
-  public List<HandlingEventDTO> getEvents() {
59
-    return Collections.unmodifiableList(events);
60
-  }
61
-
62
-  public String getFinalDestination() {
63
-    return finalDestination;
64
-  }
65
-
66
-  public String getOrigin() {
67
-    return origin;
68
-  }
69
-
70
-  public String getTrackingId() {
71
-    return trackingId;
72
-  }
73
-
74
-  public String getCurrentLocationId() {
75
-    return currentLocationId;
76
-  }
77
-
78
-  public StatusCode getStatusCode() {
79
-    return statusCode;
80
-  }
81
-
82
-  public String getCarrierMovementId() {
83
-    return carrierMovementId;
84
-  }
85
-
86
-  public boolean isMisdirected() {
87
-    return misdirected;
88
-  }
89
-
90
-}

+ 0
- 44
dddsample/src/main/java/se/citerus/dddsample/application/remoting/dto/assembler/CargoTrackingDTOAssembler.java Ver fichero

@@ -1,44 +0,0 @@
1
-package se.citerus.dddsample.application.remoting.dto.assembler;
2
-
3
-import se.citerus.dddsample.application.remoting.dto.CargoTrackingDTO;
4
-import se.citerus.dddsample.domain.model.cargo.Cargo;
5
-import se.citerus.dddsample.domain.model.cargo.DeliveryHistory;
6
-import se.citerus.dddsample.domain.model.carrier.CarrierMovement;
7
-import se.citerus.dddsample.domain.model.handling.HandlingEvent;
8
-import se.citerus.dddsample.domain.model.location.Location;
9
-
10
-import java.util.List;
11
-
12
-/**
13
- * Assembler class for the CargoTrackingDTO.
14
- */
15
-public class CargoTrackingDTOAssembler {
16
-
17
-  public CargoTrackingDTO toDTO(final Cargo cargo) {
18
-    final DeliveryHistory deliveryHistory = cargo.deliveryHistory();
19
-    final Location currentLocation = deliveryHistory.currentLocation();
20
-    final CarrierMovement currentCarrierMovement = deliveryHistory.currentCarrierMovement();
21
-    final CargoTrackingDTO dto = new CargoTrackingDTO(
22
-      cargo.trackingId().idString(),
23
-      cargo.origin().toString(),
24
-      cargo.destination().toString(),
25
-      deliveryHistory.status(),
26
-      currentLocation == null ? null : currentLocation.unLocode().idString(),
27
-      currentCarrierMovement == null ? null : currentCarrierMovement.carrierMovementId().idString(),
28
-      cargo.isMisdirected()
29
-    );
30
-
31
-    final List<HandlingEvent> events = deliveryHistory.eventsOrderedByCompletionTime();
32
-    for (HandlingEvent event : events) {
33
-      final CarrierMovement cm = event.carrierMovement();
34
-      final String carrierIdString = (cm == null) ? "" : cm.carrierMovementId().idString();
35
-      dto.addEvent(event.location().toString(),
36
-        event.type().toString(),
37
-        carrierIdString,
38
-        event.completionTime(),
39
-        cargo.itinerary().isExpected(event));
40
-    }
41
-    return dto;
42
-  }
43
-
44
-}

+ 2
- 5
dddsample/src/main/java/se/citerus/dddsample/application/web/CargoTrackingController.java Ver fichero

@@ -3,8 +3,6 @@ package se.citerus.dddsample.application.web;
3 3
 import org.springframework.validation.BindException;
4 4
 import org.springframework.web.servlet.ModelAndView;
5 5
 import org.springframework.web.servlet.mvc.SimpleFormController;
6
-import se.citerus.dddsample.application.remoting.dto.CargoTrackingDTO;
7
-import se.citerus.dddsample.application.remoting.dto.assembler.CargoTrackingDTOAssembler;
8 6
 import se.citerus.dddsample.application.web.command.TrackCommand;
9 7
 import se.citerus.dddsample.domain.model.cargo.Cargo;
10 8
 import se.citerus.dddsample.domain.model.cargo.TrackingId;
@@ -37,10 +35,9 @@ public final class CargoTrackingController extends SimpleFormController {
37 35
     final String tidStr = trackCommand.getTrackingId();
38 36
     final Cargo cargo = trackingService.track(new TrackingId(tidStr));
39 37
 
40
-    final Map<String, CargoTrackingDTO> model = new HashMap<String, CargoTrackingDTO>();
38
+    final Map<String, Cargo> model = new HashMap<String, Cargo>();
41 39
     if (cargo != null) {
42
-      final CargoTrackingDTO dto = new CargoTrackingDTOAssembler().toDTO(cargo);
43
-      model.put("cargo", dto);
40
+      model.put("cargo", cargo);
44 41
     } else {
45 42
       errors.rejectValue("trackingId", "cargo.unknown_id", new Object[]{trackCommand.getTrackingId()},
46 43
         "Unknown tracking id");

+ 29
- 13
dddsample/src/main/webapp/WEB-INF/jsp/cargo/track.jsp Ver fichero

@@ -1,3 +1,6 @@
1
+<%@ page import="se.citerus.dddsample.domain.model.cargo.Cargo" %>
2
+<%@ page import="se.citerus.dddsample.domain.model.cargo.DeliveryHistory" %>
3
+<%@ page import="se.citerus.dddsample.domain.model.handling.HandlingEvent" %>
1 4
 <html>
2 5
 <head>
3 6
   <title>Cargo search</title>
@@ -30,12 +33,24 @@
30 33
   </form:form>
31 34
   </div>
32 35
 
33
-  <c:if test="${cargo ne null}">
34
-    <div id="result">	
35
-    <h2>Status: <spring:message code="cargo.status.${cargo.statusCode}"/>&nbsp;${cargo.currentLocationId}&nbsp;${cargo.carrierMovementId}</h2>
36
-    <c:if test="${cargo.misdirected}">
36
+  <% final Cargo cargo = (Cargo) request.getAttribute("cargo"); %>
37
+
38
+  <% if (cargo != null) { %>
39
+    <% final DeliveryHistory dh = cargo.deliveryHistory(); %>
40
+    <div id="result">
41
+    <h2>
42
+      <c:set var="statusMessageCode"><%="cargo.status." + dh.status()%></c:set>
43
+      Status: <spring:message code="${statusMessageCode}"/>
44
+      &nbsp;
45
+      <%= dh.currentLocation() != null ?
46
+          dh.currentLocation().name() : "" %>
47
+      &nbsp;
48
+      <%= dh.currentCarrierMovement() != null ?
49
+          dh.currentCarrierMovement().carrierMovementId().idString() : "" %>
50
+    </h2>
51
+    <% if (cargo.isMisdirected()) { %>
37 52
       <p class="notify"><img src="${rc.contextPath}/images/error.png" alt="" />Cargo is misdirected</p>
38
-    </c:if>  
53
+    <% } %>
39 54
     <h3>Tracking History</h3>
40 55
     <table cellspacing="4">
41 56
       <thead>
@@ -47,18 +62,19 @@
47 62
         </tr>
48 63
       </thead>
49 64
       <tbody>
50
-        <c:forEach var="event" items="${cargo.events}">
51
-          <tr class="event-type-${event.type}">
52
-            <td>${event.type}</td>
53
-            <td>${event.location}</td>
54
-            <td>${event.time}</td>
55
-            <td><img src="${rc.contextPath}/images/${event.expected ? "tick" : "cross"}.png" alt=""/></td>
65
+        <% for (HandlingEvent event : dh.eventsOrderedByCompletionTime()) { %>
66
+          <tr class="event-type-<%=event.type()%>">
67
+            <td><%=event.type()%></td>
68
+            <td><%=event.location().name()%></td>
69
+            <td><%=event.completionTime()%></td>
70
+            <td><img src="${rc.contextPath}/images/<%=cargo.itinerary().isExpected(event) ? "tick" : "cross"%>.png" alt=""/></td>
56 71
           </tr>
57
-        </c:forEach>
72
+        <% } %>
58 73
       </tbody>
59 74
     </table>
60 75
   </div>
61
-  </c:if>
76
+  <% } %>
77
+
62 78
 </div>
63 79
 <script type="text/javascript" charset="UTF-8">
64 80
   try {

+ 2
- 3
dddsample/src/test/java/se/citerus/dddsample/application/web/CargoTrackingControllerTest.java Ver fichero

@@ -9,7 +9,6 @@ import org.springframework.validation.BindingResult;
9 9
 import org.springframework.validation.Errors;
10 10
 import org.springframework.validation.FieldError;
11 11
 import org.springframework.web.servlet.ModelAndView;
12
-import se.citerus.dddsample.application.remoting.dto.CargoTrackingDTO;
13 12
 import se.citerus.dddsample.application.web.command.TrackCommand;
14 13
 import se.citerus.dddsample.domain.model.cargo.Cargo;
15 14
 import se.citerus.dddsample.domain.model.cargo.CargoTestHelper;
@@ -80,8 +79,8 @@ public class CargoTrackingControllerTest extends TestCase {
80 79
     assertEquals("test-form", mav.getViewName());
81 80
     // Errors, command are two standard map attributes, the third should be the cargo object
82 81
     assertEquals(3, mav.getModel().size());
83
-    CargoTrackingDTO cargo = (CargoTrackingDTO) mav.getModel().get("cargo");
84
-    assertEquals("CNHKG", cargo.getCurrentLocationId());
82
+    Cargo cargo = (Cargo) mav.getModel().get("cargo");
83
+    assertEquals(HONGKONG, cargo.deliveryHistory().currentLocation());
85 84
   }
86 85
 
87 86
   public void testUnknownCargo() throws Exception {