Просмотр исходного кода

Added LocationDTO with assembler, and made all DTOs Serializable

peter_backlund 18 лет назад
Родитель
Сommit
209cc0a317

+ 2
- 1
dddsample/src/main/java/se/citerus/dddsample/service/dto/CargoRoutingDTO.java Просмотреть файл

@@ -1,5 +1,6 @@
1 1
 package se.citerus.dddsample.service.dto;
2 2
 
3
+import java.io.Serializable;
3 4
 import java.util.ArrayList;
4 5
 import java.util.Collections;
5 6
 import java.util.List;
@@ -7,7 +8,7 @@ import java.util.List;
7 8
 /**
8 9
  * DTO for registering and routing a cargo.
9 10
  */
10
-public final class CargoRoutingDTO {
11
+public final class CargoRoutingDTO implements Serializable {
11 12
 
12 13
   private final String trackingId;
13 14
   private final String origin;

+ 2
- 1
dddsample/src/main/java/se/citerus/dddsample/service/dto/ItineraryCandidateDTO.java Просмотреть файл

@@ -1,12 +1,13 @@
1 1
 package se.citerus.dddsample.service.dto;
2 2
 
3
+import java.io.Serializable;
3 4
 import java.util.Collections;
4 5
 import java.util.List;
5 6
 
6 7
 /**
7 8
  * DTO for presenting and selecting an itinerary from a collection of candidates.
8 9
  */
9
-public final class ItineraryCandidateDTO {
10
+public final class ItineraryCandidateDTO implements Serializable {
10 11
 
11 12
   private final List<LegDTO> legs;
12 13
 

+ 3
- 1
dddsample/src/main/java/se/citerus/dddsample/service/dto/LegDTO.java Просмотреть файл

@@ -1,9 +1,11 @@
1 1
 package se.citerus.dddsample.service.dto;
2 2
 
3
+import java.io.Serializable;
4
+
3 5
 /**
4 6
  * DTO for a leg in an itinerary.
5 7
  */
6
-public final class LegDTO {
8
+public final class LegDTO implements Serializable {
7 9
 
8 10
   private final String carrierMovementId;
9 11
   private final String from;

+ 26
- 0
dddsample/src/main/java/se/citerus/dddsample/service/dto/LocationDTO.java Просмотреть файл

@@ -0,0 +1,26 @@
1
+package se.citerus.dddsample.service.dto;
2
+
3
+import java.io.Serializable;
4
+
5
+/**
6
+ * Location DTO.
7
+ */
8
+public class LocationDTO implements Serializable {
9
+
10
+  private final String unLocode;
11
+  private final String name;
12
+
13
+  public LocationDTO(String unLocode, String name) {
14
+    this.unLocode = unLocode;
15
+    this.name = name;
16
+  }
17
+
18
+  public String getUnLocode() {
19
+    return unLocode;
20
+  }
21
+
22
+  public String getName() {
23
+    return name;
24
+  }
25
+  
26
+}

+ 22
- 0
dddsample/src/main/java/se/citerus/dddsample/service/dto/assembler/LocationDTOAssembler.java Просмотреть файл

@@ -0,0 +1,22 @@
1
+package se.citerus.dddsample.service.dto.assembler;
2
+
3
+import se.citerus.dddsample.domain.Location;
4
+import se.citerus.dddsample.service.dto.LocationDTO;
5
+
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+public class LocationDTOAssembler {
10
+
11
+  public LocationDTO toDTO(Location location) {
12
+    return new LocationDTO(location.unLocode().idString(), location.name());
13
+  }
14
+
15
+  public List<LocationDTO> toDTOList(List<Location> allLocations) {
16
+    final List<LocationDTO> dtoList = new ArrayList<LocationDTO>(allLocations.size());
17
+    for (Location location : allLocations) {
18
+      dtoList.add(toDTO(location));
19
+    }
20
+    return dtoList;
21
+  }
22
+}

+ 31
- 0
dddsample/src/test/java/se/citerus/dddsample/service/dto/assembler/LocationDTOAssemblerTest.java Просмотреть файл

@@ -0,0 +1,31 @@
1
+package se.citerus.dddsample.service.dto.assembler;
2
+
3
+import junit.framework.TestCase;
4
+import se.citerus.dddsample.domain.Location;
5
+import static se.citerus.dddsample.domain.SampleLocations.HAMBURG;
6
+import static se.citerus.dddsample.domain.SampleLocations.STOCKHOLM;
7
+import se.citerus.dddsample.service.dto.LocationDTO;
8
+
9
+import java.util.Arrays;
10
+import java.util.List;
11
+
12
+public class LocationDTOAssemblerTest extends TestCase {
13
+
14
+  public void testToDTOList() {
15
+    final LocationDTOAssembler assembler = new LocationDTOAssembler();
16
+    final List<Location> locationList = Arrays.asList(STOCKHOLM, HAMBURG);
17
+
18
+    final List<LocationDTO> dtos = assembler.toDTOList(locationList);
19
+
20
+    assertEquals(2, dtos.size());
21
+
22
+    LocationDTO dto = dtos.get(0);
23
+    assertEquals("SESTO", dto.getUnLocode());
24
+    assertEquals("Stockholm", dto.getName());
25
+
26
+    dto = dtos.get(1);
27
+    assertEquals("DEHAM", dto.getUnLocode());
28
+    assertEquals("Hamburg", dto.getName());
29
+  }
30
+
31
+}