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

Merge pull request #3 from orende/clean-code

Refactored code to remove warnings
daneidmark преди 11 години
родител
ревизия
8a8cc396d7

+ 54
- 58
src/main/java/se/citerus/dddsample/domain/model/handling/HandlingHistory.java Целия файл

@@ -4,71 +4,67 @@ import org.apache.commons.lang.Validate;
4 4
 import se.citerus.dddsample.domain.shared.ValueObject;
5 5
 
6 6
 import java.util.*;
7
+
7 8
 import static java.util.Collections.sort;
8 9
 
9 10
 /**
10 11
  * The handling history of a cargo.
11
- *
12 12
  */
13 13
 public class HandlingHistory implements ValueObject<HandlingHistory> {
14 14
 
15
-  private final List<HandlingEvent> handlingEvents;
16
-
17
-  public static final HandlingHistory EMPTY = new HandlingHistory(Collections.<HandlingEvent>emptyList());
18
-
19
-  public HandlingHistory(Collection<HandlingEvent> handlingEvents) {
20
-    Validate.notNull(handlingEvents, "Handling events are required");
21
-
22
-    this.handlingEvents = new ArrayList<HandlingEvent>(handlingEvents);
23
-  }
24
-
25
-  /**
26
-   * @return A distinct list (no duplicate registrations) of handling events, ordered by completion time.
27
-   */
28
-  public List<HandlingEvent> distinctEventsByCompletionTime() {
29
-    final List<HandlingEvent> ordered = new ArrayList<HandlingEvent>(
30
-      new HashSet<HandlingEvent>(handlingEvents)
31
-    );
32
-    sort(ordered, BY_COMPLETION_TIME_COMPARATOR);
33
-    return Collections.unmodifiableList(ordered);
34
-  }
35
-
36
-  /**
37
-   * @return Most recently completed event, or null if the delivery history is empty.
38
-   */
39
-  public HandlingEvent mostRecentlyCompletedEvent() {
40
-    final List<HandlingEvent> distinctEvents = distinctEventsByCompletionTime();
41
-    if (distinctEvents.isEmpty()) {
42
-      return null;
43
-    } else {
44
-      return distinctEvents.get(distinctEvents.size() - 1);
15
+    private final List<HandlingEvent> handlingEvents;
16
+
17
+    public static final HandlingHistory EMPTY = new HandlingHistory(Collections.<HandlingEvent>emptyList());
18
+
19
+    public HandlingHistory(Collection<HandlingEvent> handlingEvents) {
20
+        Validate.notNull(handlingEvents, "Handling events are required");
21
+
22
+        this.handlingEvents = new ArrayList<>(handlingEvents);
23
+    }
24
+
25
+    /**
26
+     * @return A distinct list (no duplicate registrations) of handling events, ordered by completion time.
27
+     */
28
+    public List<HandlingEvent> distinctEventsByCompletionTime() {
29
+        final List<HandlingEvent> ordered = new ArrayList<>(
30
+                new HashSet<>(handlingEvents)
31
+        );
32
+        sort(ordered, BY_COMPLETION_TIME_COMPARATOR);
33
+        return Collections.unmodifiableList(ordered);
34
+    }
35
+
36
+    /**
37
+     * @return Most recently completed event, or null if the delivery history is empty.
38
+     */
39
+    public HandlingEvent mostRecentlyCompletedEvent() {
40
+        final List<HandlingEvent> distinctEvents = distinctEventsByCompletionTime();
41
+        if (distinctEvents.isEmpty()) {
42
+            return null;
43
+        } else {
44
+            return distinctEvents.get(distinctEvents.size() - 1);
45
+        }
45 46
     }
46
-  }
47
-
48
-  @Override
49
-  public boolean sameValueAs(HandlingHistory other) {
50
-    return other != null && this.handlingEvents.equals(other.handlingEvents);
51
-  }
52
-
53
-  @Override
54
-  public boolean equals(Object o) {
55
-    if (this == o) return true;
56
-    if (o == null || getClass() != o.getClass()) return false;
57
-
58
-    final HandlingHistory other = (HandlingHistory) o;
59
-    return sameValueAs(other);
60
-  }
61
-
62
-  @Override
63
-  public int hashCode() {
64
-    return handlingEvents.hashCode();
65
-  }
66
-
67
-  private static final Comparator<HandlingEvent> BY_COMPLETION_TIME_COMPARATOR =
68
-    new Comparator<HandlingEvent>() {
69
-      public int compare(final HandlingEvent he1, final HandlingEvent he2) {
70
-        return he1.completionTime().compareTo(he2.completionTime());
71
-      }
72
-    };
47
+
48
+    @Override
49
+    public boolean sameValueAs(HandlingHistory other) {
50
+        return other != null && this.handlingEvents.equals(other.handlingEvents);
51
+    }
52
+
53
+    @Override
54
+    public boolean equals(Object o) {
55
+        if (this == o) return true;
56
+        if (o == null || getClass() != o.getClass()) return false;
57
+
58
+        final HandlingHistory other = (HandlingHistory) o;
59
+        return sameValueAs(other);
60
+    }
61
+
62
+    @Override
63
+    public int hashCode() {
64
+        return handlingEvents.hashCode();
65
+    }
66
+
67
+    private static final Comparator<HandlingEvent> BY_COMPLETION_TIME_COMPARATOR =
68
+            (he1, he2) -> he1.completionTime().compareTo(he2.completionTime());
73 69
 
74 70
 }

+ 116
- 115
src/main/java/se/citerus/dddsample/domain/model/voyage/SampleVoyages.java Целия файл

@@ -1,7 +1,9 @@
1 1
 package se.citerus.dddsample.domain.model.voyage;
2 2
 
3 3
 import static se.citerus.dddsample.application.util.DateTestUtil.toDate;
4
+
4 5
 import se.citerus.dddsample.domain.model.location.Location;
6
+
5 7
 import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
6 8
 
7 9
 import java.lang.reflect.Field;
@@ -9,126 +11,125 @@ import java.util.*;
9 11
 
10 12
 /**
11 13
  * Sample carrier movements, for test purposes.
12
- *
13 14
  */
14 15
 public class SampleVoyages {
15 16
 
16
-  public static final Voyage CM001 = createVoyage("CM001", STOCKHOLM, HAMBURG);
17
-  public static final Voyage CM002 = createVoyage("CM002", HAMBURG, HONGKONG);
18
-  public static final Voyage CM003 = createVoyage("CM003", HONGKONG, NEWYORK);
19
-  public static final Voyage CM004 = createVoyage("CM004", NEWYORK, CHICAGO);
20
-  public static final Voyage CM005 = createVoyage("CM005", CHICAGO, HAMBURG);
21
-  public static final Voyage CM006 = createVoyage("CM006", HAMBURG, HANGZOU);
22
-  private static Voyage createVoyage(String id, Location from, Location to) {
23
-    return new Voyage(new VoyageNumber(id), new Schedule(Arrays.asList(
24
-      new CarrierMovement(from, to, new Date(), new Date())
25
-    )));
26
-  }
27
-
28
-  // TODO CM00[1-6] and createVoyage are deprecated. Remove and refactor tests.
29
-
30
-  public final static Voyage v100 = new Voyage.Builder(new VoyageNumber("V100"), HONGKONG).
31
-    addMovement(TOKYO, toDate("2009-03-03"), toDate("2009-03-05")).
32
-    addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-09")).
33
-    build();
34
-  public final static Voyage v200 = new Voyage.Builder(new VoyageNumber("V200"), TOKYO).
35
-      addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-08")).
36
-      addMovement(CHICAGO, toDate("2009-03-10"), toDate("2009-03-14")).
37
-      addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-16")).
38
-      build();
39
-  public final static Voyage v300 = new Voyage.Builder(new VoyageNumber("V300"), TOKYO).
40
-        addMovement(ROTTERDAM, toDate("2009-03-08"), toDate("2009-03-11")).
41
-        addMovement(HAMBURG, toDate("2009-03-11"), toDate("2009-03-12")).
42
-        addMovement(MELBOURNE, toDate("2009-03-14"), toDate("2009-03-18")).
43
-        addMovement(TOKYO, toDate("2009-03-19"), toDate("2009-03-21")).
44
-        build();
45
-  public final static Voyage v400 = new Voyage.Builder(new VoyageNumber("V400"), HAMBURG).
46
-          addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-15")).
47
-          addMovement(HELSINKI, toDate("2009-03-15"), toDate("2009-03-16")).
48
-          addMovement(HAMBURG, toDate("2009-03-20"), toDate("2009-03-22")).
49
-          build();
50
-
51
-  /**
52
-   * Voyage number 0100S (by ship)
53
-   *
54
-   * Hongkong - Hangzou - Tokyo - Melbourne - New York
55
-   */
56
-  public static final Voyage HONGKONG_TO_NEW_YORK =
57
-    new Voyage.Builder(new VoyageNumber("0100S"), HONGKONG).
58
-      addMovement(HANGZOU, toDate("2008-10-01", "12:00"), toDate("2008-10-03", "14:30")).
59
-      addMovement(TOKYO, toDate("2008-10-03", "21:00"), toDate("2008-10-06", "06:15")).
60
-      addMovement(MELBOURNE, toDate("2008-10-06", "11:00"), toDate("2008-10-12", "11:30")).
61
-      addMovement(NEWYORK, toDate("2008-10-14", "12:00"), toDate("2008-10-23", "23:10")).
62
-      build();
63
-
64
-
65
-  /**
66
-   * Voyage number 0200T (by train)
67
-   *
68
-   * New York - Chicago - Dallas
69
-   */
70
-  public static final Voyage NEW_YORK_TO_DALLAS =
71
-    new Voyage.Builder(new VoyageNumber("0200T"), NEWYORK).
72
-      addMovement(CHICAGO, toDate("2008-10-24", "07:00"), toDate("2008-10-24", "17:45")).
73
-      addMovement(DALLAS, toDate("2008-10-24", "21:25"), toDate("2008-10-25", "19:30")).
74
-      build();
75
-
76
-  /**
77
-   * Voyage number 0300A (by airplane)
78
-   *
79
-   * Dallas - Hamburg - Stockholm - Helsinki
80
-   */
81
-  public static final Voyage DALLAS_TO_HELSINKI =
82
-    new Voyage.Builder(new VoyageNumber("0300A"), DALLAS).
83
-      addMovement(HAMBURG, toDate("2008-10-29", "03:30"), toDate("2008-10-31", "14:00")).
84
-      addMovement(STOCKHOLM, toDate("2008-11-01", "15:20"), toDate("2008-11-01", "18:40")).
85
-      addMovement(HELSINKI, toDate("2008-11-02", "09:00"), toDate("2008-11-02", "11:15")).
86
-      build();
87
-
88
-  /**
89
-   * Voyage number 0301S (by ship)
90
-   *
91
-   * Dallas - Hamburg - Stockholm - Helsinki, alternate route
92
-   */
93
-  public static final Voyage DALLAS_TO_HELSINKI_ALT =
94
-    new Voyage.Builder(new VoyageNumber("0301S"), DALLAS).
95
-      addMovement(HELSINKI, toDate("2008-10-29", "03:30"), toDate("2008-11-05", "15:45")).
96
-      build();
97
-
98
-  /**
99
-   * Voyage number 0400S (by ship)
100
-   *
101
-   * Helsinki - Rotterdam - Shanghai - Hongkong
102
-   *
103
-   */
104
-  public static final Voyage HELSINKI_TO_HONGKONG =
105
-    new Voyage.Builder(new VoyageNumber("0400S"), HELSINKI).
106
-      addMovement(ROTTERDAM, toDate("2008-11-04", "05:50"), toDate("2008-11-06", "14:10")).
107
-      addMovement(SHANGHAI, toDate("2008-11-10", "21:45"), toDate("2008-11-22", "16:40")).
108
-      addMovement(HONGKONG, toDate("2008-11-24", "07:00"), toDate("2008-11-28", "13:37")).
109
-      build();
110
-
111
-  public static final Map<VoyageNumber, Voyage> ALL = new HashMap();
112
-
113
-  static {
114
-    for (Field field : SampleVoyages.class.getDeclaredFields()) {
115
-      if (field.getType().equals(Voyage.class)) {
116
-        try {
117
-          Voyage voyage = (Voyage) field.get(null);
118
-          ALL.put(voyage.voyageNumber(), voyage);
119
-        } catch (IllegalAccessException e) {
120
-          throw new RuntimeException(e);
17
+    public static final Voyage CM001 = createVoyage("CM001", STOCKHOLM, HAMBURG);
18
+    public static final Voyage CM002 = createVoyage("CM002", HAMBURG, HONGKONG);
19
+    public static final Voyage CM003 = createVoyage("CM003", HONGKONG, NEWYORK);
20
+    public static final Voyage CM004 = createVoyage("CM004", NEWYORK, CHICAGO);
21
+    public static final Voyage CM005 = createVoyage("CM005", CHICAGO, HAMBURG);
22
+    public static final Voyage CM006 = createVoyage("CM006", HAMBURG, HANGZOU);
23
+
24
+    private static Voyage createVoyage(String id, Location from, Location to) {
25
+        return new Voyage(new VoyageNumber(id), new Schedule(Collections.singletonList(
26
+                new CarrierMovement(from, to, new Date(), new Date())
27
+        )));
28
+    }
29
+
30
+    // TODO CM00[1-6] and createVoyage are deprecated. Remove and refactor tests.
31
+
32
+    public final static Voyage v100 = new Voyage.Builder(new VoyageNumber("V100"), HONGKONG).
33
+            addMovement(TOKYO, toDate("2009-03-03"), toDate("2009-03-05")).
34
+            addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-09")).
35
+            build();
36
+    public final static Voyage v200 = new Voyage.Builder(new VoyageNumber("V200"), TOKYO).
37
+            addMovement(NEWYORK, toDate("2009-03-06"), toDate("2009-03-08")).
38
+            addMovement(CHICAGO, toDate("2009-03-10"), toDate("2009-03-14")).
39
+            addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-16")).
40
+            build();
41
+    public final static Voyage v300 = new Voyage.Builder(new VoyageNumber("V300"), TOKYO).
42
+            addMovement(ROTTERDAM, toDate("2009-03-08"), toDate("2009-03-11")).
43
+            addMovement(HAMBURG, toDate("2009-03-11"), toDate("2009-03-12")).
44
+            addMovement(MELBOURNE, toDate("2009-03-14"), toDate("2009-03-18")).
45
+            addMovement(TOKYO, toDate("2009-03-19"), toDate("2009-03-21")).
46
+            build();
47
+    public final static Voyage v400 = new Voyage.Builder(new VoyageNumber("V400"), HAMBURG).
48
+            addMovement(STOCKHOLM, toDate("2009-03-14"), toDate("2009-03-15")).
49
+            addMovement(HELSINKI, toDate("2009-03-15"), toDate("2009-03-16")).
50
+            addMovement(HAMBURG, toDate("2009-03-20"), toDate("2009-03-22")).
51
+            build();
52
+
53
+    /**
54
+     * Voyage number 0100S (by ship)
55
+     * <p>
56
+     * Hongkong - Hangzou - Tokyo - Melbourne - New York
57
+     */
58
+    public static final Voyage HONGKONG_TO_NEW_YORK =
59
+            new Voyage.Builder(new VoyageNumber("0100S"), HONGKONG).
60
+                    addMovement(HANGZOU, toDate("2008-10-01", "12:00"), toDate("2008-10-03", "14:30")).
61
+                    addMovement(TOKYO, toDate("2008-10-03", "21:00"), toDate("2008-10-06", "06:15")).
62
+                    addMovement(MELBOURNE, toDate("2008-10-06", "11:00"), toDate("2008-10-12", "11:30")).
63
+                    addMovement(NEWYORK, toDate("2008-10-14", "12:00"), toDate("2008-10-23", "23:10")).
64
+                    build();
65
+
66
+
67
+    /**
68
+     * Voyage number 0200T (by train)
69
+     * <p>
70
+     * New York - Chicago - Dallas
71
+     */
72
+    public static final Voyage NEW_YORK_TO_DALLAS =
73
+            new Voyage.Builder(new VoyageNumber("0200T"), NEWYORK).
74
+                    addMovement(CHICAGO, toDate("2008-10-24", "07:00"), toDate("2008-10-24", "17:45")).
75
+                    addMovement(DALLAS, toDate("2008-10-24", "21:25"), toDate("2008-10-25", "19:30")).
76
+                    build();
77
+
78
+    /**
79
+     * Voyage number 0300A (by airplane)
80
+     * <p>
81
+     * Dallas - Hamburg - Stockholm - Helsinki
82
+     */
83
+    public static final Voyage DALLAS_TO_HELSINKI =
84
+            new Voyage.Builder(new VoyageNumber("0300A"), DALLAS).
85
+                    addMovement(HAMBURG, toDate("2008-10-29", "03:30"), toDate("2008-10-31", "14:00")).
86
+                    addMovement(STOCKHOLM, toDate("2008-11-01", "15:20"), toDate("2008-11-01", "18:40")).
87
+                    addMovement(HELSINKI, toDate("2008-11-02", "09:00"), toDate("2008-11-02", "11:15")).
88
+                    build();
89
+
90
+    /**
91
+     * Voyage number 0301S (by ship)
92
+     * <p>
93
+     * Dallas - Hamburg - Stockholm - Helsinki, alternate route
94
+     */
95
+    public static final Voyage DALLAS_TO_HELSINKI_ALT =
96
+            new Voyage.Builder(new VoyageNumber("0301S"), DALLAS).
97
+                    addMovement(HELSINKI, toDate("2008-10-29", "03:30"), toDate("2008-11-05", "15:45")).
98
+                    build();
99
+
100
+    /**
101
+     * Voyage number 0400S (by ship)
102
+     * <p>
103
+     * Helsinki - Rotterdam - Shanghai - Hongkong
104
+     */
105
+    public static final Voyage HELSINKI_TO_HONGKONG =
106
+            new Voyage.Builder(new VoyageNumber("0400S"), HELSINKI).
107
+                    addMovement(ROTTERDAM, toDate("2008-11-04", "05:50"), toDate("2008-11-06", "14:10")).
108
+                    addMovement(SHANGHAI, toDate("2008-11-10", "21:45"), toDate("2008-11-22", "16:40")).
109
+                    addMovement(HONGKONG, toDate("2008-11-24", "07:00"), toDate("2008-11-28", "13:37")).
110
+                    build();
111
+
112
+    public static final Map<VoyageNumber, Voyage> ALL = new HashMap<>();
113
+
114
+    static {
115
+        for (Field field : SampleVoyages.class.getDeclaredFields()) {
116
+            if (field.getType().equals(Voyage.class)) {
117
+                try {
118
+                    Voyage voyage = (Voyage) field.get(null);
119
+                    ALL.put(voyage.voyageNumber(), voyage);
120
+                } catch (IllegalAccessException e) {
121
+                    throw new RuntimeException(e);
122
+                }
123
+            }
121 124
         }
122
-      }
123 125
     }
124
-  }
125 126
 
126
-  public static List<Voyage> getAll() {
127
-    return new ArrayList(ALL.values());
128
-  }
127
+    public static List<Voyage> getAll() {
128
+        return new ArrayList<>(ALL.values());
129
+    }
130
+
131
+    public static Voyage lookup(VoyageNumber voyageNumber) {
132
+        return ALL.get(voyageNumber);
133
+    }
129 134
 
130
-  public static Voyage lookup(VoyageNumber voyageNumber) {
131
-    return ALL.get(voyageNumber);
132
-  }
133
-  
134 135
 }

+ 1
- 5
src/main/java/se/citerus/dddsample/interfaces/booking/web/RouteAssignmentCommand.java Целия файл

@@ -78,11 +78,7 @@ public class RouteAssignmentCommand {
78 78
     }
79 79
 
80 80
     public static Factory factory() {
81
-      return new Factory() {
82
-        public Object create() {
83
-          return new LegCommand();
84
-        }
85
-      };
81
+      return LegCommand::new;
86 82
     }
87 83
 
88 84
   }

+ 37
- 40
src/main/java/se/citerus/dddsample/interfaces/tracking/CargoTrackingController.java Целия файл

@@ -2,11 +2,9 @@ package se.citerus.dddsample.interfaces.tracking;
2 2
 
3 3
 import org.springframework.context.MessageSource;
4 4
 import org.springframework.stereotype.Controller;
5
-import org.springframework.validation.BindException;
6 5
 import org.springframework.validation.BindingResult;
7
-import org.springframework.validation.Errors;
8
-import org.springframework.web.bind.WebDataBinder;
9
-import org.springframework.web.bind.annotation.*;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.RequestMethod;
10 8
 import org.springframework.web.servlet.support.RequestContextUtils;
11 9
 import se.citerus.dddsample.domain.model.cargo.Cargo;
12 10
 import se.citerus.dddsample.domain.model.cargo.CargoRepository;
@@ -24,59 +22,58 @@ import java.util.Map;
24 22
  * Controller for tracking cargo. This interface sits immediately on top of the
25 23
  * domain layer, unlike the booking interface which has a a remote facade and supporting
26 24
  * DTOs in between.
27
- * <p/>
25
+ * <p>
28 26
  * An adapter class, designed for the tracking use case, is used to wrap the domain model
29 27
  * to make it easier to work with in a web page rendering context. We do not want to apply
30 28
  * view rendering constraints to the design of our domain model, and the adapter
31
- * helps us shield the domain model classes. 
32
- * <p/>
29
+ * helps us shield the domain model classes.
30
+ * <p>
33 31
  *
34 32
  * @eee se.citerus.dddsample.application.web.CargoTrackingViewAdapter
35 33
  * @see se.citerus.dddsample.interfaces.booking.web.CargoAdminController
36
- *
37 34
  */
38 35
 @Controller
39 36
 public final class CargoTrackingController {
40 37
 
41
-  private CargoRepository cargoRepository;
42
-  private HandlingEventRepository handlingEventRepository;
43
-  private MessageSource messageSource;
38
+    private CargoRepository cargoRepository;
39
+    private HandlingEventRepository handlingEventRepository;
40
+    private MessageSource messageSource;
44 41
 
45
-  @RequestMapping(method = RequestMethod.GET)
46
-  public Map<String, CargoTrackingViewAdapter> get(final TrackCommand trackCommand) {
47
-      return new HashMap<>();
48
-  }
42
+    @RequestMapping(method = RequestMethod.GET)
43
+    public Map<String, CargoTrackingViewAdapter> get(final TrackCommand trackCommand) {
44
+        return new HashMap<>();
45
+    }
49 46
 
50
-  @RequestMapping(method = RequestMethod.POST)
51
-  protected Map<String, CargoTrackingViewAdapter> onSubmit(final HttpServletRequest request,
52
-                                                           final TrackCommand command,
53
-                                                           final BindingResult bindingResult) {
54
-    new TrackCommandValidator().validate(command, bindingResult);
47
+    @RequestMapping(method = RequestMethod.POST)
48
+    protected Map<String, CargoTrackingViewAdapter> onSubmit(final HttpServletRequest request,
49
+                                                             final TrackCommand command,
50
+                                                             final BindingResult bindingResult) {
51
+        new TrackCommandValidator().validate(command, bindingResult);
55 52
 
56
-    final TrackingId trackingId = new TrackingId(command.getTrackingId());
57
-    final Cargo cargo = cargoRepository.find(trackingId);
53
+        final TrackingId trackingId = new TrackingId(command.getTrackingId());
54
+        final Cargo cargo = cargoRepository.find(trackingId);
58 55
 
59
-    final Map<String, CargoTrackingViewAdapter> model = new HashMap<String, CargoTrackingViewAdapter>();
60
-    if (cargo != null) {
61
-      final Locale locale = RequestContextUtils.getLocale(request);
62
-      final List<HandlingEvent> handlingEvents = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
63
-      model.put("cargo", new CargoTrackingViewAdapter(cargo, messageSource, locale, handlingEvents));
64
-    } else {
65
-      bindingResult.rejectValue("trackingId", "cargo.unknown_id", new Object[]{command.getTrackingId()}, "Unknown tracking id");
56
+        final Map<String, CargoTrackingViewAdapter> model = new HashMap<String, CargoTrackingViewAdapter>();
57
+        if (cargo != null) {
58
+            final Locale locale = RequestContextUtils.getLocale(request);
59
+            final List<HandlingEvent> handlingEvents = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
60
+            model.put("cargo", new CargoTrackingViewAdapter(cargo, messageSource, locale, handlingEvents));
61
+        } else {
62
+            bindingResult.rejectValue("trackingId", "cargo.unknown_id", new Object[]{command.getTrackingId()}, "Unknown tracking id");
63
+        }
64
+        return model;
66 65
     }
67
-    return model;
68
-  }
69 66
 
70
-  public void setCargoRepository(CargoRepository cargoRepository) {
71
-    this.cargoRepository = cargoRepository;
72
-  }
67
+    public void setCargoRepository(CargoRepository cargoRepository) {
68
+        this.cargoRepository = cargoRepository;
69
+    }
73 70
 
74
-  public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
75
-    this.handlingEventRepository = handlingEventRepository;
76
-  }
71
+    public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
72
+        this.handlingEventRepository = handlingEventRepository;
73
+    }
77 74
 
78
-  public void setMessageSource(MessageSource messageSource) {
79
-      this.messageSource = messageSource;
80
-  }
75
+    public void setMessageSource(MessageSource messageSource) {
76
+        this.messageSource = messageSource;
77
+    }
81 78
 
82 79
 }

+ 7
- 7
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/CargoRepositoryTest.java Целия файл

@@ -23,19 +23,19 @@ import se.citerus.dddsample.domain.model.location.UnLocode;
23 23
 import se.citerus.dddsample.domain.model.voyage.Voyage;
24 24
 import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
25 25
 import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
26
-import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
27
-import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.*;
28
-import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.*;
29 26
 
30 27
 import javax.sql.DataSource;
31
-
32 28
 import java.lang.reflect.Field;
33
-import java.util.Arrays;
29
+import java.util.Collections;
34 30
 import java.util.Date;
35 31
 import java.util.List;
36 32
 import java.util.Map;
37 33
 
38 34
 import static org.junit.Assert.*;
35
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.LOAD;
36
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.RECEIVE;
37
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
38
+import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.CM004;
39 39
 
40 40
 @RunWith(SpringJUnit4ClassRunner.class)
41 41
 @ContextConfiguration(value = {"/context-infrastructure-persistence.xml", "/context-domain.xml"})
@@ -146,7 +146,7 @@ public class CargoRepositoryTest {
146 146
         Cargo cargo = new Cargo(trackingId, new RouteSpecification(origin, destination, new Date()));
147 147
         cargoRepository.store(cargo);
148 148
 
149
-        cargo.assignToRoute(new Itinerary(Arrays.asList(
149
+        cargo.assignToRoute(new Itinerary(Collections.singletonList(
150 150
                 new Leg(
151 151
                         voyageRepository.find(new VoyageNumber("0101")),
152 152
                         locationRepository.find(STOCKHOLM.unLocode()),
@@ -181,7 +181,7 @@ public class CargoRepositoryTest {
181 181
 
182 182
         Location legFrom = locationRepository.find(new UnLocode("FIHEL"));
183 183
         Location legTo = locationRepository.find(new UnLocode("DEHAM"));
184
-        Itinerary newItinerary = new Itinerary(Arrays.asList(new Leg(CM004, legFrom, legTo, new Date(), new Date())));
184
+        Itinerary newItinerary = new Itinerary(Collections.singletonList(new Leg(CM004, legFrom, legTo, new Date(), new Date())));
185 185
 
186 186
         cargo.assignToRoute(newItinerary);
187 187
 

+ 69
- 68
src/test/java/se/citerus/dddsample/infrastructure/persistence/inmemory/CargoRepositoryInMem.java Целия файл

@@ -7,6 +7,7 @@ import se.citerus.dddsample.domain.model.cargo.TrackingId;
7 7
 import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
8 8
 import se.citerus.dddsample.domain.model.handling.HandlingHistory;
9 9
 import se.citerus.dddsample.domain.model.location.Location;
10
+
10 11
 import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
11 12
 
12 13
 import java.util.*;
@@ -14,77 +15,77 @@ import java.util.*;
14 15
 /**
15 16
  * CargoRepositoryInMem implement the CargoRepository interface but is a test
16 17
  * class not intended for usage in real application.
17
- * <p/>
18
+ * <p>
18 19
  * It setup a simple local hash with a number of Cargo's with TrackingId as key
19 20
  * defined at compile time.
20
- * <p/>
21
+ * <p>
21 22
  */
22 23
 public class CargoRepositoryInMem implements CargoRepository {
23 24
 
24
-  private Map<String, Cargo> cargoDb;
25
-  private HandlingEventRepository handlingEventRepository;
26
-
27
-  /**
28
-   * Constructor.
29
-   */
30
-  public CargoRepositoryInMem() {
31
-    cargoDb = new HashMap<String, Cargo>();
32
-  }
33
-
34
-  public Cargo find(final TrackingId trackingId) {
35
-    return cargoDb.get(trackingId.idString());
36
-  }
37
-
38
-  public void store(final Cargo cargo) {
39
-    cargoDb.put(cargo.trackingId().idString(), cargo);
40
-  }
41
-
42
-  public TrackingId nextTrackingId() {
43
-    String random = UUID.randomUUID().toString().toUpperCase();
44
-    return new TrackingId(
45
-      random.substring(0, random.indexOf("-"))
46
-    );
47
-  }
48
-
49
-  public List<Cargo> findAll() {
50
-    return new ArrayList(cargoDb.values());
51
-  }
52
-
53
-  public void init() throws Exception {
54
-    final TrackingId xyz = new TrackingId("XYZ");
55
-    final Cargo cargoXYZ = createCargoWithDeliveryHistory(
56
-      xyz, STOCKHOLM, MELBOURNE, handlingEventRepository.lookupHandlingHistoryOfCargo(xyz));
57
-    cargoDb.put(xyz.idString(), cargoXYZ);
58
-
59
-    final TrackingId zyx = new TrackingId("ZYX");
60
-    final Cargo cargoZYX = createCargoWithDeliveryHistory(
61
-      zyx, MELBOURNE, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(zyx));
62
-    cargoDb.put(zyx.idString(), cargoZYX);
63
-
64
-    final TrackingId abc = new TrackingId("ABC");
65
-    final Cargo cargoABC = createCargoWithDeliveryHistory(
66
-      abc, STOCKHOLM, HELSINKI, handlingEventRepository.lookupHandlingHistoryOfCargo(abc));
67
-    cargoDb.put(abc.idString(), cargoABC);
68
-
69
-    final TrackingId cba = new TrackingId("CBA");
70
-    final Cargo cargoCBA = createCargoWithDeliveryHistory(
71
-      cba, HELSINKI, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(cba));
72
-    cargoDb.put(cba.idString(), cargoCBA);
73
-  }
74
-
75
-  public void setHandlingEventRepository(final HandlingEventRepository handlingEventRepository) {
76
-    this.handlingEventRepository = handlingEventRepository;
77
-  }
78
-
79
-  public static Cargo createCargoWithDeliveryHistory(TrackingId trackingId,
80
-                                                     Location origin,
81
-                                                     Location destination,
82
-                                                     HandlingHistory handlingHistory) {
83
-
84
-    final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, new Date());
85
-    final Cargo cargo = new Cargo(trackingId, routeSpecification);
86
-    cargo.deriveDeliveryProgress(handlingHistory);
87
-
88
-    return cargo;
89
-  }
25
+    private Map<String, Cargo> cargoDb;
26
+    private HandlingEventRepository handlingEventRepository;
27
+
28
+    /**
29
+     * Constructor.
30
+     */
31
+    public CargoRepositoryInMem() {
32
+        cargoDb = new HashMap<>();
33
+    }
34
+
35
+    public Cargo find(final TrackingId trackingId) {
36
+        return cargoDb.get(trackingId.idString());
37
+    }
38
+
39
+    public void store(final Cargo cargo) {
40
+        cargoDb.put(cargo.trackingId().idString(), cargo);
41
+    }
42
+
43
+    public TrackingId nextTrackingId() {
44
+        String random = UUID.randomUUID().toString().toUpperCase();
45
+        return new TrackingId(
46
+                random.substring(0, random.indexOf("-"))
47
+        );
48
+    }
49
+
50
+    public List<Cargo> findAll() {
51
+        return new ArrayList<>(cargoDb.values());
52
+    }
53
+
54
+    public void init() throws Exception {
55
+        final TrackingId xyz = new TrackingId("XYZ");
56
+        final Cargo cargoXYZ = createCargoWithDeliveryHistory(
57
+                xyz, STOCKHOLM, MELBOURNE, handlingEventRepository.lookupHandlingHistoryOfCargo(xyz));
58
+        cargoDb.put(xyz.idString(), cargoXYZ);
59
+
60
+        final TrackingId zyx = new TrackingId("ZYX");
61
+        final Cargo cargoZYX = createCargoWithDeliveryHistory(
62
+                zyx, MELBOURNE, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(zyx));
63
+        cargoDb.put(zyx.idString(), cargoZYX);
64
+
65
+        final TrackingId abc = new TrackingId("ABC");
66
+        final Cargo cargoABC = createCargoWithDeliveryHistory(
67
+                abc, STOCKHOLM, HELSINKI, handlingEventRepository.lookupHandlingHistoryOfCargo(abc));
68
+        cargoDb.put(abc.idString(), cargoABC);
69
+
70
+        final TrackingId cba = new TrackingId("CBA");
71
+        final Cargo cargoCBA = createCargoWithDeliveryHistory(
72
+                cba, HELSINKI, STOCKHOLM, handlingEventRepository.lookupHandlingHistoryOfCargo(cba));
73
+        cargoDb.put(cba.idString(), cargoCBA);
74
+    }
75
+
76
+    public void setHandlingEventRepository(final HandlingEventRepository handlingEventRepository) {
77
+        this.handlingEventRepository = handlingEventRepository;
78
+    }
79
+
80
+    public static Cargo createCargoWithDeliveryHistory(TrackingId trackingId,
81
+                                                       Location origin,
82
+                                                       Location destination,
83
+                                                       HandlingHistory handlingHistory) {
84
+
85
+        final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, new Date());
86
+        final Cargo cargo = new Cargo(trackingId, routeSpecification);
87
+        cargo.deriveDeliveryProgress(handlingHistory);
88
+
89
+        return cargo;
90
+    }
90 91
 }

+ 0
- 1
src/test/java/se/citerus/dddsample/interfaces/booking/web/ItinerarySelectionCommandTest.java Целия файл

@@ -1,6 +1,5 @@
1 1
 package se.citerus.dddsample.interfaces.booking.web;
2 2
 
3
-import junit.framework.TestCase;
4 3
 import org.junit.Test;
5 4
 import org.springframework.mock.web.MockHttpServletRequest;
6 5
 import org.springframework.web.bind.ServletRequestDataBinder;