|
|
@@ -1,107 +0,0 @@
|
|
1
|
|
-package se.citerus.dddsample.application.impl;
|
|
2
|
|
-
|
|
3
|
|
-import org.apache.commons.lang.Validate;
|
|
4
|
|
-import org.apache.commons.logging.Log;
|
|
5
|
|
-import org.apache.commons.logging.LogFactory;
|
|
6
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
7
|
|
-import se.citerus.dddsample.application.BookingService;
|
|
8
|
|
-import se.citerus.dddsample.application.CargoLockingService;
|
|
9
|
|
-import se.citerus.dddsample.application.CargoLockingServiceInMem;
|
|
10
|
|
-import se.citerus.dddsample.domain.model.cargo.*;
|
|
11
|
|
-import se.citerus.dddsample.domain.model.location.Location;
|
|
12
|
|
-import se.citerus.dddsample.domain.model.location.LocationRepository;
|
|
13
|
|
-import se.citerus.dddsample.domain.model.location.UnLocode;
|
|
14
|
|
-import se.citerus.dddsample.domain.service.RoutingService;
|
|
15
|
|
-
|
|
16
|
|
-import java.util.Collections;
|
|
17
|
|
-import java.util.Date;
|
|
18
|
|
-import java.util.List;
|
|
19
|
|
-
|
|
20
|
|
-public final class BookingServiceImpl implements BookingService {
|
|
21
|
|
-
|
|
22
|
|
- private final RoutingService routingService;
|
|
23
|
|
- private final CargoFactory cargoFactory;
|
|
24
|
|
- private final CargoRepository cargoRepository;
|
|
25
|
|
- private final LocationRepository locationRepository;
|
|
26
|
|
- private final CargoLockingService cargoLockingService;
|
|
27
|
|
- private final Log logger = LogFactory.getLog(getClass());
|
|
28
|
|
-
|
|
29
|
|
- public BookingServiceImpl(final RoutingService routingService,
|
|
30
|
|
- final CargoFactory cargoFactory,
|
|
31
|
|
- final CargoRepository cargoRepository,
|
|
32
|
|
- final LocationRepository locationRepository) {
|
|
33
|
|
- this.routingService = routingService;
|
|
34
|
|
- this.cargoFactory = cargoFactory;
|
|
35
|
|
- this.cargoRepository = cargoRepository;
|
|
36
|
|
- this.locationRepository = locationRepository;
|
|
37
|
|
- this.cargoLockingService = new CargoLockingServiceInMem();
|
|
38
|
|
- }
|
|
39
|
|
-
|
|
40
|
|
- @Override
|
|
41
|
|
- @Transactional
|
|
42
|
|
- public TrackingId bookNewCargo(final UnLocode originUnLocode,
|
|
43
|
|
- final UnLocode destinationUnLocode,
|
|
44
|
|
- final Date arrivalDeadline) {
|
|
45
|
|
- final Cargo cargo = cargoFactory.newCargo(originUnLocode, destinationUnLocode, arrivalDeadline);
|
|
46
|
|
- cargoRepository.store(cargo);
|
|
47
|
|
- logger.info("Booked new cargo with tracking id " + cargo.trackingId().stringValue());
|
|
48
|
|
-
|
|
49
|
|
- return cargo.trackingId();
|
|
50
|
|
- }
|
|
51
|
|
-
|
|
52
|
|
- @Override
|
|
53
|
|
- @Transactional(readOnly = true)
|
|
54
|
|
- public List<Itinerary> requestPossibleRoutesForCargo(final TrackingId trackingId) {
|
|
55
|
|
- final Cargo cargo = cargoRepository.find(trackingId);
|
|
56
|
|
-
|
|
57
|
|
- if (cargo == null) {
|
|
58
|
|
- return Collections.emptyList();
|
|
59
|
|
- }
|
|
60
|
|
-
|
|
61
|
|
- return routingService.fetchRoutesForSpecification(cargo.routeSpecification());
|
|
62
|
|
- }
|
|
63
|
|
-
|
|
64
|
|
- @Override
|
|
65
|
|
- @Transactional
|
|
66
|
|
- public void assignCargoToRoute(final Itinerary itinerary, final TrackingId trackingId) {
|
|
67
|
|
- cargoLockingService.assertLocked(trackingId);
|
|
68
|
|
-
|
|
69
|
|
- final Cargo cargo = cargoRepository.find(trackingId);
|
|
70
|
|
- Validate.notNull(cargo, "Can't assign itinerary to non-existing cargo " + trackingId);
|
|
71
|
|
- cargo.assignToRoute(itinerary);
|
|
72
|
|
- cargoRepository.store(cargo);
|
|
73
|
|
-
|
|
74
|
|
- logger.info("Assigned cargo " + trackingId + " to new route");
|
|
75
|
|
-
|
|
76
|
|
- cargoLockingService.unlock(trackingId);
|
|
77
|
|
- }
|
|
78
|
|
-
|
|
79
|
|
- @Override
|
|
80
|
|
- @Transactional
|
|
81
|
|
- public void changeDestination(final TrackingId trackingId, final UnLocode unLocode) {
|
|
82
|
|
- cargoLockingService.assertLocked(trackingId);
|
|
83
|
|
-
|
|
84
|
|
- final Cargo cargo = cargoRepository.find(trackingId);
|
|
85
|
|
- Validate.notNull(cargo, "Can't change destination of non-existing cargo " + trackingId);
|
|
86
|
|
- final Location newDestination = locationRepository.find(unLocode);
|
|
87
|
|
-
|
|
88
|
|
- final RouteSpecification routeSpecification = cargo.routeSpecification().withDestination(newDestination);
|
|
89
|
|
- cargo.specifyNewRoute(routeSpecification);
|
|
90
|
|
-
|
|
91
|
|
- cargoRepository.store(cargo);
|
|
92
|
|
- logger.info("Changed destination for cargo " + trackingId + " to " + routeSpecification.destination());
|
|
93
|
|
-
|
|
94
|
|
- cargoLockingService.unlock(trackingId);
|
|
95
|
|
- }
|
|
96
|
|
-
|
|
97
|
|
- @Override
|
|
98
|
|
- @Transactional(readOnly = true)
|
|
99
|
|
- public Cargo loadCargoForRouting(final TrackingId trackingId) {
|
|
100
|
|
- final Cargo cargo = cargoRepository.find(trackingId);
|
|
101
|
|
- if (cargo != null) {
|
|
102
|
|
- cargoLockingService.lock(trackingId);
|
|
103
|
|
- }
|
|
104
|
|
- return cargo;
|
|
105
|
|
- }
|
|
106
|
|
-
|
|
107
|
|
-}
|