|
|
@@ -1,9 +1,9 @@
|
|
1
|
1
|
package se.citerus.dddsample.application.impl;
|
|
2
|
2
|
|
|
3
|
3
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
4
|
+import se.citerus.dddsample.application.ApplicationEvents;
|
|
4
|
5
|
import se.citerus.dddsample.application.HandlingEventRegistrationAttempt;
|
|
5
|
6
|
import se.citerus.dddsample.application.HandlingEventService;
|
|
6
|
|
-import se.citerus.dddsample.application.SystemEvents;
|
|
7
|
7
|
import se.citerus.dddsample.domain.model.handling.CannotCreateHandlingEventException;
|
|
8
|
8
|
import se.citerus.dddsample.domain.model.handling.HandlingEvent;
|
|
9
|
9
|
import se.citerus.dddsample.domain.model.handling.HandlingEventFactory;
|
|
|
@@ -11,42 +11,45 @@ import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
|
|
11
|
11
|
|
|
12
|
12
|
public final class HandlingEventServiceImpl implements HandlingEventService {
|
|
13
|
13
|
|
|
|
14
|
+ private final ApplicationEvents applicationEvents;
|
|
14
|
15
|
private final HandlingEventRepository handlingEventRepository;
|
|
15
|
|
- private final SystemEvents systemEvents;
|
|
16
|
16
|
private final HandlingEventFactory handlingEventFactory;
|
|
17
|
17
|
|
|
18
|
|
- public HandlingEventServiceImpl(HandlingEventRepository handlingEventRepository, SystemEvents systemEvents, HandlingEventFactory handlingEventFactory) {
|
|
|
18
|
+ public HandlingEventServiceImpl(final HandlingEventRepository handlingEventRepository,
|
|
|
19
|
+ final ApplicationEvents applicationEvents,
|
|
|
20
|
+ final HandlingEventFactory handlingEventFactory) {
|
|
19
|
21
|
this.handlingEventRepository = handlingEventRepository;
|
|
20
|
|
- this.systemEvents = systemEvents;
|
|
|
22
|
+ this.applicationEvents = applicationEvents;
|
|
21
|
23
|
this.handlingEventFactory = handlingEventFactory;
|
|
22
|
24
|
}
|
|
23
|
25
|
|
|
24
|
|
- /*
|
|
25
|
|
- NOTE:
|
|
26
|
|
- The cargo instance that's loaded and associated with the handling event is
|
|
27
|
|
- in an inconsitent state, because the cargo delivery history's collection of
|
|
28
|
|
- events does not contain the event created here. However, this is not a problem,
|
|
29
|
|
- because cargo is in a different aggregate from handling event.
|
|
30
|
|
-
|
|
31
|
|
- The rules of an aggregate dictate that all consistency rules within the aggregate
|
|
32
|
|
- are enforced synchronously in the transaction, but consistency rules of other aggregates
|
|
33
|
|
- are enforced by asynchronous updates, after the commit of this transaction.
|
|
34
|
|
- */
|
|
35
|
26
|
@Override
|
|
36
|
27
|
@Transactional
|
|
37
|
|
- public void register(HandlingEventRegistrationAttempt attempt) {
|
|
|
28
|
+ public void registerHandlingEvent(final HandlingEventRegistrationAttempt attempt) {
|
|
38
|
29
|
try {
|
|
|
30
|
+ /* Using a factory to create a HandlingEvent (aggregate). This is where
|
|
|
31
|
+ it is determined wether the incoming data, the attempt, actually is capable
|
|
|
32
|
+ of representing a real handling event. */
|
|
39
|
33
|
final HandlingEvent event = handlingEventFactory.createHandlingEvent(
|
|
40
|
|
- attempt.getDate(),
|
|
|
34
|
+ attempt.getCompletionTime(),
|
|
41
|
35
|
attempt.getTrackingId(),
|
|
42
|
36
|
attempt.getVoyageNumber(),
|
|
43
|
37
|
attempt.getUnLocode(),
|
|
44
|
38
|
attempt.getType()
|
|
45
|
39
|
);
|
|
|
40
|
+
|
|
|
41
|
+ /* Store the new handling event, which updates the persistent
|
|
|
42
|
+ state of the handling event aggregate (but not the cargo aggregate -
|
|
|
43
|
+ that happens asynchronously!)
|
|
|
44
|
+ */
|
|
46
|
45
|
handlingEventRepository.save(event);
|
|
47
|
|
- systemEvents.cargoWasHandled(event);
|
|
|
46
|
+
|
|
|
47
|
+ /* Publish a system event stating that a cargo has been handled. */
|
|
|
48
|
+ applicationEvents.cargoWasHandled(event);
|
|
48
|
49
|
} catch (CannotCreateHandlingEventException e) {
|
|
49
|
|
- systemEvents.rejectHandlingEventRegistrationAttempt(attempt, e);
|
|
|
50
|
+ /* This may be a bogus attempt, for example containing a tracking id
|
|
|
51
|
+ that doesn't match any cargo that we're tracking. */
|
|
|
52
|
+ applicationEvents.rejectedHandlingEventRegistrationAttempt(attempt, e);
|
|
50
|
53
|
}
|
|
51
|
54
|
}
|
|
52
|
55
|
|