HandlingEventServiceImpl.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package se.citerus.dddsample.service;
  2. import org.apache.commons.lang.Validate;
  3. import org.springframework.transaction.annotation.Transactional;
  4. import se.citerus.dddsample.domain.*;
  5. import se.citerus.dddsample.repository.CargoRepository;
  6. import se.citerus.dddsample.repository.CarrierMovementRepository;
  7. import se.citerus.dddsample.repository.HandlingEventRepository;
  8. import se.citerus.dddsample.repository.LocationRepository;
  9. import java.util.Date;
  10. public class HandlingEventServiceImpl implements HandlingEventService {
  11. private CargoRepository cargoRepository;
  12. private CarrierMovementRepository carrierMovementRepository;
  13. private HandlingEventRepository handlingEventRepository;
  14. private LocationRepository locationRepository;
  15. @Transactional(readOnly = false)
  16. public void register(Date completionTime, TrackingId trackingId, CarrierMovementId carrierMovementId, UnLocode unlocode, HandlingEvent.Type type) throws UnknownCarrierMovementIdException, UnknownTrackingIdException, UnknownLocationException {
  17. // Carrier movement may be null for certain event types
  18. Validate.noNullElements(new Object[] {trackingId, unlocode, type});
  19. Cargo cargo = cargoRepository.find(trackingId);
  20. if (cargo == null) throw new UnknownTrackingIdException(trackingId);
  21. CarrierMovement carrierMovement = findCarrierMovement(carrierMovementId);
  22. Location location = findLocation(unlocode);
  23. Date registrationTime = new Date();
  24. HandlingEvent event = new HandlingEvent(cargo, completionTime, registrationTime, type, location, carrierMovement);
  25. handlingEventRepository.save(event);
  26. /*
  27. NOTE:
  28. The cargo instance that's loaded and associated with the handling event is
  29. in an inconsitent state, because the cargo delivery history's collection of
  30. events does not contain the event created here. However, this is not a problem,
  31. because cargo is in a different aggregate from handling event.
  32. The rules of an aggregate dictate that all consistency rules within the aggregate
  33. are enforced synchronously in the transaction, but consistency rules of other aggregates
  34. are enforced by asynchronous updates, after the commit of this transaction.
  35. */
  36. }
  37. private CarrierMovement findCarrierMovement(CarrierMovementId carrierMovementId) throws UnknownCarrierMovementIdException {
  38. if (carrierMovementId == null) {
  39. return null;
  40. }
  41. CarrierMovement carrierMovement = carrierMovementRepository.find(carrierMovementId);
  42. if (carrierMovement == null) {
  43. throw new UnknownCarrierMovementIdException(carrierMovementId);
  44. }
  45. return carrierMovement;
  46. }
  47. private Location findLocation(UnLocode unlocode) throws UnknownLocationException {
  48. if (unlocode == null) {
  49. return Location.UNKNOWN;
  50. }
  51. Location location = locationRepository.find(unlocode);
  52. if (location == null) {
  53. throw new UnknownLocationException(unlocode);
  54. }
  55. return location;
  56. }
  57. public void setCargoRepository(CargoRepository cargoRepository) {
  58. this.cargoRepository = cargoRepository;
  59. }
  60. public void setCarrierRepository(CarrierMovementRepository carrierMovementRepository) {
  61. this.carrierMovementRepository = carrierMovementRepository;
  62. }
  63. public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
  64. this.handlingEventRepository = handlingEventRepository;
  65. }
  66. public void setLocationRepository(LocationRepository locationRepository) {
  67. this.locationRepository = locationRepository;
  68. }
  69. }