浏览代码

Moved enum and logic into the domain model.

peter_backlund 18 年前
父节点
当前提交
2972ae0f44

+ 37
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/DeliveryHistory.java 查看文件

@@ -66,4 +66,41 @@ public class DeliveryHistory {
66 66
   // Needed by Hibernate
67 67
   DeliveryHistory() {}
68 68
 
69
+  public StatusCode status() {
70
+    if (lastEvent() == null)
71
+      return StatusCode.notReceived;
72
+
73
+    HandlingEvent.Type type = lastEvent().type();
74
+    if (type == HandlingEvent.Type.LOAD)
75
+      return StatusCode.onBoardCarrier;
76
+
77
+    if (type == HandlingEvent.Type.UNLOAD)
78
+      return StatusCode.inPort;
79
+
80
+    if (type == HandlingEvent.Type.RECEIVE)
81
+      return StatusCode.inPort;
82
+
83
+    if (type == HandlingEvent.Type.CLAIM)
84
+      return StatusCode.claimed;
85
+
86
+
87
+    return null;
88
+  }
89
+
90
+  public Location currentLocation() {
91
+    if (status().equals(StatusCode.inPort)) {
92
+      return lastEvent().location();
93
+    } else {
94
+      return null;
95
+    }
96
+  }
97
+
98
+  public CarrierMovement currentCarrierMovement() {
99
+    if (status().equals(StatusCode.onBoardCarrier)) {
100
+      return lastEvent().carrierMovement();
101
+    } else {
102
+      return null;
103
+    }
104
+  }
105
+  
69 106
 }

+ 8
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/StatusCode.java 查看文件

@@ -0,0 +1,8 @@
1
+package se.citerus.dddsample.domain;
2
+
3
+/**
4
+ *
5
+ */
6
+public enum StatusCode {
7
+  notReceived, inPort, onBoardCarrier, claimed
8
+}

+ 8
- 40
dddsample/src/main/java/se/citerus/dddsample/service/CargoServiceImpl.java 查看文件

@@ -1,10 +1,7 @@
1 1
 package se.citerus.dddsample.service;
2 2
 
3 3
 import org.springframework.transaction.annotation.Transactional;
4
-import se.citerus.dddsample.domain.Cargo;
5
-import se.citerus.dddsample.domain.CarrierMovement;
6
-import se.citerus.dddsample.domain.HandlingEvent;
7
-import se.citerus.dddsample.domain.TrackingId;
4
+import se.citerus.dddsample.domain.*;
8 5
 import se.citerus.dddsample.repository.CargoRepository;
9 6
 import se.citerus.dddsample.service.dto.CargoWithHistoryDTO;
10 7
 import se.citerus.dddsample.service.dto.HandlingEventDTO;
@@ -22,30 +19,22 @@ public class CargoServiceImpl implements CargoService {
22 19
       return null;
23 20
     }
24 21
 
25
-    HandlingEvent lastEvent = cargo.deliveryHistory().lastEvent();
22
+    DeliveryHistory deliveryHistory = cargo.deliveryHistory();
26 23
 
27 24
     //CargoWithHistoryDTO
28 25
 
29
-    String currentLocationId = null;
30
-    String carrierMovementId = null;
31
-
32
-    if (lastEvent.type() == HandlingEvent.Type.UNLOAD || lastEvent.type() == HandlingEvent.Type.RECEIVE)
33
-      currentLocationId = cargo.lastKnownLocation().unLocode().idString();
34
-
35
-    if (lastEvent.type() == HandlingEvent.Type.LOAD)
36
-      carrierMovementId = lastEvent.carrierMovement().carrierId().idString();
37
-
38
-
26
+    Location currentLocation = deliveryHistory.currentLocation();
27
+    CarrierMovement currentCarrierMovement = deliveryHistory.currentCarrierMovement();
39 28
     final CargoWithHistoryDTO dto = new CargoWithHistoryDTO(
40 29
             cargo.trackingId().idString(),
41 30
             cargo.origin().toString(),
42 31
             cargo.finalDestination().toString(),
43
-            statusForLastEvent(lastEvent),
44
-            currentLocationId,
45
-            carrierMovementId
32
+            deliveryHistory.status(),
33
+            currentLocation == null ? null : currentLocation.unLocode().idString(),
34
+            currentCarrierMovement == null ? null : currentCarrierMovement.carrierId().idString()
46 35
     );
47 36
 
48
-    final List<HandlingEvent> events = cargo.deliveryHistory().eventsOrderedByCompletionTime();
37
+    final List<HandlingEvent> events = deliveryHistory.eventsOrderedByCompletionTime();
49 38
     for (HandlingEvent event : events) {
50 39
       CarrierMovement cm = event.carrierMovement();
51 40
       String carrierIdString = (cm == null) ? "" : cm.carrierId().idString();
@@ -64,25 +53,4 @@ public class CargoServiceImpl implements CargoService {
64 53
     this.cargoRepository = cargoRepository;
65 54
   }
66 55
 
67
-  public CargoWithHistoryDTO.StatusCode statusForLastEvent(HandlingEvent lastEvent) {
68
-
69
-    if (lastEvent == null)
70
-      return CargoWithHistoryDTO.StatusCode.notReceived;
71
-
72
-    HandlingEvent.Type type = lastEvent.type();
73
-    if (type == HandlingEvent.Type.LOAD)
74
-      return CargoWithHistoryDTO.StatusCode.onBoardCarrier;
75
-
76
-    if (type == HandlingEvent.Type.UNLOAD)
77
-      return CargoWithHistoryDTO.StatusCode.inPort;
78
-
79
-    if (type == HandlingEvent.Type.RECEIVE)
80
-      return CargoWithHistoryDTO.StatusCode.inPort;
81
-
82
-    if (type == HandlingEvent.Type.CLAIM)
83
-      return CargoWithHistoryDTO.StatusCode.claimed;
84
-
85
-
86
-    return null;
87
-  }
88 56
 }

+ 2
- 8
dddsample/src/main/java/se/citerus/dddsample/service/dto/CargoWithHistoryDTO.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package se.citerus.dddsample.service.dto;
2 2
 
3
+import se.citerus.dddsample.domain.StatusCode;
4
+
3 5
 import java.io.Serializable;
4 6
 import java.util.ArrayList;
5 7
 import java.util.Collections;
@@ -17,7 +19,6 @@ public class CargoWithHistoryDTO implements Serializable {
17 19
   String currentLocationId;
18 20
   List<HandlingEventDTO> events;
19 21
   String carrierMovementId;
20
-  String locationId;
21 22
   StatusCode statusCode;
22 23
 
23 24
   public CargoWithHistoryDTO(String trackingId, String origin, String finalDestination,
@@ -64,11 +65,4 @@ public class CargoWithHistoryDTO implements Serializable {
64 65
     return carrierMovementId;
65 66
   }
66 67
 
67
-  public String getLocationId() {
68
-    return locationId;
69
-  }
70
-
71
-  public enum StatusCode {
72
-    notReceived, inPort, onBoardCarrier, claimed
73
-  }
74 68
 }

+ 33
- 1
dddsample/src/test/java/se/citerus/dddsample/domain/DeliveryHistoryTest.java 查看文件

@@ -9,6 +9,7 @@ import java.util.Date;
9 9
 import java.util.List;
10 10
 
11 11
 public class DeliveryHistoryTest extends TestCase {
12
+  private Location ham = new Location(new UnLocode("DE", "HAM"), "Hamburg");
12 13
 
13 14
   public void testEvensOrderedByTimeOccured() throws Exception {
14 15
     DeliveryHistory dh = new DeliveryHistory();
@@ -32,5 +33,36 @@ public class DeliveryHistoryTest extends TestCase {
32 33
     assertSame(he3, orderEvents.get(3));
33 34
   }
34 35
 
35
-  
36
+  public void testCargoStatusFromLastHandlingEvent() {
37
+    DeliveryHistory deliveryHistory = new DeliveryHistory();
38
+
39
+    assertEquals(StatusCode.notReceived, deliveryHistory.status());
40
+
41
+    deliveryHistory.addEvent(new HandlingEvent(null, new Date(10), null, HandlingEvent.Type.RECEIVE, ham));
42
+    assertEquals(StatusCode.inPort, deliveryHistory.status());
43
+
44
+    CarrierMovement carrierMovement = new CarrierMovement(new CarrierMovementId("ABC"), ham, ham);
45
+    deliveryHistory.addEvent(new HandlingEvent(null, new Date(20), null, HandlingEvent.Type.LOAD, ham, carrierMovement));
46
+    assertEquals(StatusCode.onBoardCarrier, deliveryHistory.status());
47
+
48
+    deliveryHistory.addEvent(new HandlingEvent(null, new Date(30), null, HandlingEvent.Type.UNLOAD, ham, carrierMovement));
49
+    assertEquals(StatusCode.inPort, deliveryHistory.status());
50
+
51
+    deliveryHistory.addEvent(new HandlingEvent(null, new Date(40), null, HandlingEvent.Type.CLAIM, ham));
52
+    assertEquals(StatusCode.claimed, deliveryHistory.status());
53
+  }
54
+
55
+  public void testCurrentLocation() throws Exception {
56
+    DeliveryHistory deliveryHistory = new DeliveryHistory();
57
+
58
+    assertNull(deliveryHistory.currentLocation());
59
+
60
+    deliveryHistory.addEvent(new HandlingEvent(null, new Date(10), null, HandlingEvent.Type.RECEIVE, ham));
61
+    assertEquals(ham, deliveryHistory.currentLocation());
62
+
63
+    CarrierMovement carrierMovement = new CarrierMovement(new CarrierMovementId("ABC"), ham, ham);
64
+    deliveryHistory.addEvent(new HandlingEvent(null, new Date(20), null, HandlingEvent.Type.LOAD, ham, carrierMovement));
65
+    assertNull(deliveryHistory.currentLocation());
66
+  }
67
+
36 68
 }

+ 0
- 25
dddsample/src/test/java/se/citerus/dddsample/service/CargoServiceImplTest.java 查看文件

@@ -1,32 +1,7 @@
1 1
 package se.citerus.dddsample.service;
2 2
 
3 3
 import junit.framework.TestCase;
4
-import se.citerus.dddsample.domain.*;
5
-import se.citerus.dddsample.service.dto.CargoWithHistoryDTO;
6 4
 
7 5
 public class CargoServiceImplTest extends TestCase {
8 6
 
9
-  public void testCargoStatusFromLastHandlingEvent() {
10
-
11
-    CargoServiceImpl cargoServiceImpl = new CargoServiceImpl();
12
-
13
-    HandlingEvent lastEvent = null;
14
-    assertEquals(CargoWithHistoryDTO.StatusCode.notReceived, cargoServiceImpl.statusForLastEvent(lastEvent));
15
-
16
-    Location ham = new Location(new UnLocode("DE", "HAM"), "Hamburg");
17
-    lastEvent = new HandlingEvent(null, null, null, HandlingEvent.Type.RECEIVE, ham);
18
-    assertEquals(CargoWithHistoryDTO.StatusCode.inPort, cargoServiceImpl.statusForLastEvent(lastEvent));
19
-
20
-    CarrierMovement carrierMovement = new CarrierMovement(new CarrierMovementId("ABC"), ham, ham);
21
-    lastEvent = new HandlingEvent(null, null, null, HandlingEvent.Type.LOAD, ham, carrierMovement);
22
-    assertEquals(CargoWithHistoryDTO.StatusCode.onBoardCarrier, cargoServiceImpl.statusForLastEvent(lastEvent));
23
-
24
-    lastEvent = new HandlingEvent(null, null, null, HandlingEvent.Type.UNLOAD, ham, carrierMovement);
25
-    assertEquals(CargoWithHistoryDTO.StatusCode.inPort, cargoServiceImpl.statusForLastEvent(lastEvent));
26
-
27
-    lastEvent = new HandlingEvent(null, null, null, HandlingEvent.Type.CLAIM, ham);
28
-    assertEquals(CargoWithHistoryDTO.StatusCode.claimed, cargoServiceImpl.statusForLastEvent(lastEvent));
29
-
30
-  }
31
-
32 7
 }

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/web/CargoTrackingControllerTest.java 查看文件

@@ -51,7 +51,7 @@ public class CargoTrackingControllerTest extends TestCase {
51 51
                 cargo.trackingId().idString(),
52 52
                 cargo.origin().unLocode().idString(),
53 53
                 cargo.finalDestination().unLocode().idString(),
54
-                CargoWithHistoryDTO.StatusCode.claimed, 
54
+                StatusCode.claimed,
55 55
                 "AAAAA",
56 56
                 "BALO");
57 57
         cargoDTO.addEvent(new HandlingEventDTO(