|
|
@@ -1,108 +1,99 @@
|
|
1
|
|
-package se.citerus.dddsample.interfaces.handling;
|
|
2
|
|
-
|
|
3
|
|
-import org.apache.commons.lang.StringUtils;
|
|
4
|
|
-import org.apache.commons.logging.Log;
|
|
5
|
|
-import org.apache.commons.logging.LogFactory;
|
|
6
|
|
-import se.citerus.dddsample.application.HandlingEventRegistrationAttempt;
|
|
7
|
|
-import se.citerus.dddsample.application.SystemEvents;
|
|
8
|
|
-import se.citerus.dddsample.domain.model.cargo.TrackingId;
|
|
9
|
|
-import se.citerus.dddsample.domain.model.carrier.VoyageNumber;
|
|
10
|
|
-import se.citerus.dddsample.domain.model.handling.HandlingEvent;
|
|
11
|
|
-import se.citerus.dddsample.domain.model.location.UnLocode;
|
|
12
|
|
-
|
|
13
|
|
-import javax.jws.WebService;
|
|
14
|
|
-import java.text.ParseException;
|
|
15
|
|
-import java.text.SimpleDateFormat;
|
|
16
|
|
-import java.util.ArrayList;
|
|
17
|
|
-import java.util.Arrays;
|
|
18
|
|
-import java.util.Date;
|
|
19
|
|
-import java.util.List;
|
|
20
|
|
-
|
|
21
|
|
-/**
|
|
22
|
|
- * This web service endpoint implementation performs basic validation and parsing
|
|
23
|
|
- * of incoming data, and in case of a valid registration attempt, sends an asynchronous message
|
|
24
|
|
- * with the informtion to the handling event registration system for proper registration.
|
|
25
|
|
- *
|
|
26
|
|
- */
|
|
27
|
|
-@WebService(endpointInterface = "se.citerus.dddsample.interfaces.handling.HandlingEventServiceEndpoint")
|
|
28
|
|
-public class HandlingEventServiceEndpointImpl implements HandlingEventServiceEndpoint {
|
|
29
|
|
-
|
|
30
|
|
- private SystemEvents systemEvents;
|
|
31
|
|
- private static final Log logger = LogFactory.getLog(HandlingEventServiceEndpointImpl.class);
|
|
32
|
|
-
|
|
33
|
|
- public static final String ISO_8601_FORMAT = "yyyy-mm-dd HH:MM:SS.SSS";
|
|
34
|
|
-
|
|
35
|
|
- public void register(final String completionTime, final String trackingId, final String voyageNumberString,
|
|
36
|
|
- final String unlocode, final String eventType) throws RegistrationFailure {
|
|
37
|
|
- final List<String> errors = new ArrayList<String>();
|
|
38
|
|
-
|
|
39
|
|
- final Date date = parseDate(completionTime, errors);
|
|
40
|
|
- final TrackingId tid = parseTrackingId(trackingId, errors);
|
|
41
|
|
- final VoyageNumber voyageNumber = parseVoyageNumber(voyageNumberString, errors);
|
|
42
|
|
- final HandlingEvent.Type type = parseEventType(eventType, errors);
|
|
43
|
|
- final UnLocode ul = parseUnLocode(unlocode, errors);
|
|
44
|
|
-
|
|
45
|
|
- if (errors.isEmpty()) {
|
|
46
|
|
- final HandlingEventRegistrationAttempt attempt = new HandlingEventRegistrationAttempt(new Date(), date, tid, voyageNumber, type, ul);
|
|
47
|
|
- systemEvents.receivedHandlingEventRegistrationAttempt(attempt);
|
|
48
|
|
- } else {
|
|
49
|
|
- logger.warn("Handling event registration attempt failed: " + errors);
|
|
50
|
|
- throw new RegistrationFailure(errors);
|
|
51
|
|
- }
|
|
52
|
|
- }
|
|
53
|
|
-
|
|
54
|
|
- private UnLocode parseUnLocode(final String unlocode, final List<String> errors) {
|
|
55
|
|
- try {
|
|
56
|
|
- return new UnLocode(unlocode);
|
|
57
|
|
- } catch (IllegalArgumentException e) {
|
|
58
|
|
- errors.add(e.getMessage());
|
|
59
|
|
- return null;
|
|
60
|
|
- }
|
|
61
|
|
- }
|
|
62
|
|
-
|
|
63
|
|
- private TrackingId parseTrackingId(final String trackingId, final List<String> errors) {
|
|
64
|
|
- try {
|
|
65
|
|
- return new TrackingId(trackingId);
|
|
66
|
|
- } catch (IllegalArgumentException e) {
|
|
67
|
|
- errors.add(e.getMessage());
|
|
68
|
|
- return null;
|
|
69
|
|
- }
|
|
70
|
|
- }
|
|
71
|
|
-
|
|
72
|
|
- private VoyageNumber parseVoyageNumber(final String voyageNumber, final List<String> errors) {
|
|
73
|
|
- if (StringUtils.isNotEmpty(voyageNumber)) {
|
|
74
|
|
- try {
|
|
75
|
|
- return new VoyageNumber(voyageNumber);
|
|
76
|
|
- } catch (IllegalArgumentException e) {
|
|
77
|
|
- errors.add(e.getMessage());
|
|
78
|
|
- return null;
|
|
79
|
|
- }
|
|
80
|
|
- } else {
|
|
81
|
|
- return null;
|
|
82
|
|
- }
|
|
83
|
|
- }
|
|
84
|
|
-
|
|
85
|
|
- private Date parseDate(final String completionTime, final List<String> errors) {
|
|
86
|
|
- Date date;
|
|
87
|
|
- try {
|
|
88
|
|
- date = new SimpleDateFormat(ISO_8601_FORMAT).parse(completionTime);
|
|
89
|
|
- } catch (ParseException e) {
|
|
90
|
|
- errors.add("Invalid date format: " + completionTime + ", must be on ISO 8601 format: " + ISO_8601_FORMAT);
|
|
91
|
|
- date = null;
|
|
92
|
|
- }
|
|
93
|
|
- return date;
|
|
94
|
|
- }
|
|
95
|
|
-
|
|
96
|
|
- private HandlingEvent.Type parseEventType(final String eventType, final List<String> errors) {
|
|
97
|
|
- try {
|
|
98
|
|
- return HandlingEvent.Type.valueOf(eventType);
|
|
99
|
|
- } catch (IllegalArgumentException e) {
|
|
100
|
|
- errors.add(eventType + " is not a valid handling event type. Valid types are: " + Arrays.toString(HandlingEvent.Type.values()));
|
|
101
|
|
- return null;
|
|
102
|
|
- }
|
|
103
|
|
- }
|
|
104
|
|
-
|
|
105
|
|
- public void setSystemEvents(SystemEvents systemEvents) {
|
|
106
|
|
- this.systemEvents = systemEvents;
|
|
107
|
|
- }
|
|
108
|
|
-}
|
|
|
1
|
+package se.citerus.dddsample.interfaces.handling;
|
|
|
2
|
+
|
|
|
3
|
+import org.apache.commons.lang.StringUtils;
|
|
|
4
|
+import org.apache.commons.logging.Log;
|
|
|
5
|
+import org.apache.commons.logging.LogFactory;
|
|
|
6
|
+import se.citerus.dddsample.application.HandlingEventRegistrationAttempt;
|
|
|
7
|
+import se.citerus.dddsample.application.SystemEvents;
|
|
|
8
|
+import se.citerus.dddsample.domain.model.cargo.TrackingId;
|
|
|
9
|
+import se.citerus.dddsample.domain.model.carrier.VoyageNumber;
|
|
|
10
|
+import se.citerus.dddsample.domain.model.handling.HandlingEvent;
|
|
|
11
|
+import se.citerus.dddsample.domain.model.location.UnLocode;
|
|
|
12
|
+import se.citerus.dddsample.interfaces.handling.ws.HandlingReportServiceImpl;
|
|
|
13
|
+
|
|
|
14
|
+import java.text.ParseException;
|
|
|
15
|
+import java.text.SimpleDateFormat;
|
|
|
16
|
+import java.util.ArrayList;
|
|
|
17
|
+import java.util.Arrays;
|
|
|
18
|
+import java.util.Date;
|
|
|
19
|
+import java.util.List;
|
|
|
20
|
+
|
|
|
21
|
+public class RegistrationParser {
|
|
|
22
|
+
|
|
|
23
|
+ private SystemEvents systemEvents;
|
|
|
24
|
+
|
|
|
25
|
+ private static final Log logger = LogFactory.getLog(RegistrationParser.class);
|
|
|
26
|
+
|
|
|
27
|
+ public void convertAndSend(String completionTime, String trackingId, String voyageNumberString, String unlocode, String eventType) throws RegistrationFailure {
|
|
|
28
|
+ final List<String> errors = new ArrayList<String>();
|
|
|
29
|
+
|
|
|
30
|
+ final Date date = parseDate(completionTime, errors);
|
|
|
31
|
+ final TrackingId tid = parseTrackingId(trackingId, errors);
|
|
|
32
|
+ final VoyageNumber voyageNumber = parseVoyageNumber(voyageNumberString, errors);
|
|
|
33
|
+ final HandlingEvent.Type type = parseEventType(eventType, errors);
|
|
|
34
|
+ final UnLocode ul = parseUnLocode(unlocode, errors);
|
|
|
35
|
+
|
|
|
36
|
+ if (errors.isEmpty()) {
|
|
|
37
|
+ final HandlingEventRegistrationAttempt attempt = new HandlingEventRegistrationAttempt(new Date(), date, tid, voyageNumber, type, ul);
|
|
|
38
|
+ systemEvents.receivedHandlingEventRegistrationAttempt(attempt);
|
|
|
39
|
+ } else {
|
|
|
40
|
+ logger.warn("Handling event registration attempt failed: " + errors);
|
|
|
41
|
+ throw new RegistrationFailure(errors);
|
|
|
42
|
+ }
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ private UnLocode parseUnLocode(final String unlocode, final List<String> errors) {
|
|
|
46
|
+ try {
|
|
|
47
|
+ return new UnLocode(unlocode);
|
|
|
48
|
+ } catch (IllegalArgumentException e) {
|
|
|
49
|
+ errors.add(e.getMessage());
|
|
|
50
|
+ return null;
|
|
|
51
|
+ }
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ private TrackingId parseTrackingId(final String trackingId, final List<String> errors) {
|
|
|
55
|
+ try {
|
|
|
56
|
+ return new TrackingId(trackingId);
|
|
|
57
|
+ } catch (IllegalArgumentException e) {
|
|
|
58
|
+ errors.add(e.getMessage());
|
|
|
59
|
+ return null;
|
|
|
60
|
+ }
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ private VoyageNumber parseVoyageNumber(final String voyageNumber, final List<String> errors) {
|
|
|
64
|
+ if (StringUtils.isNotEmpty(voyageNumber)) {
|
|
|
65
|
+ try {
|
|
|
66
|
+ return new VoyageNumber(voyageNumber);
|
|
|
67
|
+ } catch (IllegalArgumentException e) {
|
|
|
68
|
+ errors.add(e.getMessage());
|
|
|
69
|
+ return null;
|
|
|
70
|
+ }
|
|
|
71
|
+ } else {
|
|
|
72
|
+ return null;
|
|
|
73
|
+ }
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ private Date parseDate(final String completionTime, final List<String> errors) {
|
|
|
77
|
+ Date date;
|
|
|
78
|
+ try {
|
|
|
79
|
+ date = new SimpleDateFormat(HandlingReportServiceImpl.ISO_8601_FORMAT).parse(completionTime);
|
|
|
80
|
+ } catch (ParseException e) {
|
|
|
81
|
+ errors.add("Invalid date format: " + completionTime + ", must be on ISO 8601 format: " + HandlingReportServiceImpl.ISO_8601_FORMAT);
|
|
|
82
|
+ date = null;
|
|
|
83
|
+ }
|
|
|
84
|
+ return date;
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ private HandlingEvent.Type parseEventType(final String eventType, final List<String> errors) {
|
|
|
88
|
+ try {
|
|
|
89
|
+ return HandlingEvent.Type.valueOf(eventType);
|
|
|
90
|
+ } catch (IllegalArgumentException e) {
|
|
|
91
|
+ errors.add(eventType + " is not a valid handling event type. Valid types are: " + Arrays.toString(HandlingEvent.Type.values()));
|
|
|
92
|
+ return null;
|
|
|
93
|
+ }
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ public void setSystemEvents(SystemEvents systemEvents) {
|
|
|
97
|
+ this.systemEvents = systemEvents;
|
|
|
98
|
+ }
|
|
|
99
|
+}
|