|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+package se.citerus.dddsample.application.web;
|
|
|
2
|
+
|
|
|
3
|
+import org.springframework.context.MessageSource;
|
|
|
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.text.SimpleDateFormat;
|
|
|
11
|
+import java.util.ArrayList;
|
|
|
12
|
+import java.util.Collections;
|
|
|
13
|
+import java.util.List;
|
|
|
14
|
+import java.util.Locale;
|
|
|
15
|
+
|
|
|
16
|
+/**
|
|
|
17
|
+ * View adapter for displaying a cargo in a tracking context.
|
|
|
18
|
+ */
|
|
|
19
|
+public final class CargoTrackingViewAdapter {
|
|
|
20
|
+
|
|
|
21
|
+ private final Cargo cargo;
|
|
|
22
|
+ private final MessageSource messageSource;
|
|
|
23
|
+ private final Locale locale;
|
|
|
24
|
+ private final List<HandlingEventViewAdapter> events;
|
|
|
25
|
+
|
|
|
26
|
+ /**
|
|
|
27
|
+ * Constructor.
|
|
|
28
|
+ *
|
|
|
29
|
+ * @param cargo
|
|
|
30
|
+ * @param messageSource
|
|
|
31
|
+ * @param locale
|
|
|
32
|
+ */
|
|
|
33
|
+ public CargoTrackingViewAdapter(Cargo cargo, MessageSource messageSource, Locale locale) {
|
|
|
34
|
+ this.messageSource = messageSource;
|
|
|
35
|
+ this.locale = locale;
|
|
|
36
|
+ this.cargo = cargo;
|
|
|
37
|
+
|
|
|
38
|
+ final List<HandlingEvent> handlingEvents = cargo.deliveryHistory().eventsOrderedByCompletionTime();
|
|
|
39
|
+ this.events = new ArrayList<HandlingEventViewAdapter>(handlingEvents.size());
|
|
|
40
|
+ for (HandlingEvent handlingEvent : handlingEvents) {
|
|
|
41
|
+ events.add(new HandlingEventViewAdapter(handlingEvent));
|
|
|
42
|
+ }
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ /**
|
|
|
46
|
+ * @param location a location
|
|
|
47
|
+ * @return A formatted string for displaying the location.
|
|
|
48
|
+ */
|
|
|
49
|
+ private String getDisplayText(Location location) {
|
|
|
50
|
+ return location.unLocode().idString() + " (" + location.name() + ")";
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ /**
|
|
|
54
|
+ * @return An unmodifiable list of handling event view adapters.
|
|
|
55
|
+ */
|
|
|
56
|
+ public List<HandlingEventViewAdapter> getEvents() {
|
|
|
57
|
+ return Collections.unmodifiableList(events);
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ /**
|
|
|
61
|
+ * @return A translated string describing the cargo status.
|
|
|
62
|
+ */
|
|
|
63
|
+ public String getStatusText() {
|
|
|
64
|
+ final DeliveryHistory deliveryHistory = cargo.deliveryHistory();
|
|
|
65
|
+ final String code = "cargo.status." + deliveryHistory.status().name();
|
|
|
66
|
+
|
|
|
67
|
+ final Object[] args;
|
|
|
68
|
+ switch (deliveryHistory.status()) {
|
|
|
69
|
+ case IN_PORT:
|
|
|
70
|
+ args = new Object[] {getDisplayText(deliveryHistory.currentLocation())};
|
|
|
71
|
+ break;
|
|
|
72
|
+ case ONBOARD_CARRIER:
|
|
|
73
|
+ args = new Object[] {deliveryHistory.currentCarrierMovement().carrierMovementId().idString()};
|
|
|
74
|
+ break;
|
|
|
75
|
+ case CLAIMED:
|
|
|
76
|
+ case NOT_RECEIVED:
|
|
|
77
|
+ case UNKNOWN:
|
|
|
78
|
+ default:
|
|
|
79
|
+ args = null;
|
|
|
80
|
+ break;
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ return messageSource.getMessage(code, args, "[Unknown status]", locale);
|
|
|
84
|
+ }
|
|
|
85
|
+
|
|
|
86
|
+ /**
|
|
|
87
|
+ * @return Cargo destination location.
|
|
|
88
|
+ */
|
|
|
89
|
+ public String getDestination() {
|
|
|
90
|
+ return getDisplayText(cargo.destination());
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+ /**
|
|
|
94
|
+ * @return Cargo osigin location.
|
|
|
95
|
+ */
|
|
|
96
|
+ public String getOrigin() {
|
|
|
97
|
+ return getDisplayText(cargo.origin());
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ /**
|
|
|
101
|
+ * @return Cargo tracking id.
|
|
|
102
|
+ */
|
|
|
103
|
+ public String getTrackingId() {
|
|
|
104
|
+ return cargo.trackingId().idString();
|
|
|
105
|
+ }
|
|
|
106
|
+
|
|
|
107
|
+ /**
|
|
|
108
|
+ * @return True if cargo is misdirected.
|
|
|
109
|
+ */
|
|
|
110
|
+ public boolean isMisdirected() {
|
|
|
111
|
+ return cargo.isMisdirected();
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ /**
|
|
|
115
|
+ * Handling event view adapter component.
|
|
|
116
|
+ */
|
|
|
117
|
+ public final class HandlingEventViewAdapter {
|
|
|
118
|
+
|
|
|
119
|
+ private final HandlingEvent handlingEvent;
|
|
|
120
|
+ private final String FORMAT = "yyyy-MM-dd hh:mm";
|
|
|
121
|
+
|
|
|
122
|
+ /**
|
|
|
123
|
+ * Constructor.
|
|
|
124
|
+ *
|
|
|
125
|
+ * @param handlingEvent handling event
|
|
|
126
|
+ */
|
|
|
127
|
+ public HandlingEventViewAdapter(HandlingEvent handlingEvent) {
|
|
|
128
|
+ this.handlingEvent = handlingEvent;
|
|
|
129
|
+ }
|
|
|
130
|
+
|
|
|
131
|
+ /**
|
|
|
132
|
+ * @return Location where the event occurred.
|
|
|
133
|
+ */
|
|
|
134
|
+ public String getLocation() {
|
|
|
135
|
+ return handlingEvent.location().unLocode().idString();
|
|
|
136
|
+ }
|
|
|
137
|
+
|
|
|
138
|
+ /**
|
|
|
139
|
+ * @return Time when the event was completed.
|
|
|
140
|
+ */
|
|
|
141
|
+ public String getTime() {
|
|
|
142
|
+ return new SimpleDateFormat(FORMAT).format(handlingEvent.completionTime());
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ /**
|
|
|
146
|
+ * @return Type of event.
|
|
|
147
|
+ */
|
|
|
148
|
+ public String getType() {
|
|
|
149
|
+ return handlingEvent.type().toString();
|
|
|
150
|
+ }
|
|
|
151
|
+
|
|
|
152
|
+ /**
|
|
|
153
|
+ * @return Carrier movement id, or empty string if not applicable.
|
|
|
154
|
+ */
|
|
|
155
|
+ public String getCarrierMovement() {
|
|
|
156
|
+ final CarrierMovement cm = handlingEvent.carrierMovement();
|
|
|
157
|
+ return cm != null ? cm.carrierMovementId().toString() : "";
|
|
|
158
|
+ }
|
|
|
159
|
+
|
|
|
160
|
+ /**
|
|
|
161
|
+ * @return True if the event was expected, according to the cargo's itinerary.
|
|
|
162
|
+ */
|
|
|
163
|
+ public boolean isExpected() {
|
|
|
164
|
+ return cargo.itinerary().isExpected(handlingEvent);
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ }
|
|
|
168
|
+}
|