View Javadoc

1   package se.citerus.dddsample.application.impl;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.springframework.transaction.annotation.Transactional;
6   import se.citerus.dddsample.application.ApplicationEvents;
7   import se.citerus.dddsample.application.HandlingEventService;
8   import se.citerus.dddsample.domain.model.cargo.TrackingId;
9   import se.citerus.dddsample.domain.model.handling.CannotCreateHandlingEventException;
10  import se.citerus.dddsample.domain.model.handling.HandlingEvent;
11  import se.citerus.dddsample.domain.model.handling.HandlingEventFactory;
12  import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
13  import se.citerus.dddsample.domain.model.location.UnLocode;
14  import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
15  
16  import java.util.Date;
17  
18  public final class HandlingEventServiceImpl implements HandlingEventService {
19  
20    private final ApplicationEvents applicationEvents;
21    private final HandlingEventRepository handlingEventRepository;
22    private final HandlingEventFactory handlingEventFactory;
23    private final Log logger = LogFactory.getLog(HandlingEventServiceImpl.class);
24  
25    public HandlingEventServiceImpl(final HandlingEventRepository handlingEventRepository,
26                                    final ApplicationEvents applicationEvents,
27                                    final HandlingEventFactory handlingEventFactory) {
28      this.handlingEventRepository = handlingEventRepository;
29      this.applicationEvents = applicationEvents;
30      this.handlingEventFactory = handlingEventFactory;
31    }
32  
33    @Override
34    @Transactional(rollbackFor = CannotCreateHandlingEventException.class)
35    public void registerHandlingEvent(final Date completionTime,
36                                      final TrackingId trackingId,
37                                      final VoyageNumber voyageNumber,
38                                      final UnLocode unLocode,
39                                      final HandlingEvent.Type type) throws CannotCreateHandlingEventException {
40      final Date registrationTime = new Date();
41      /* Using a factory to create a HandlingEvent (aggregate). This is where
42         it is determined wether the incoming data, the attempt, actually is capable
43         of representing a real handling event. */
44      final HandlingEvent event = handlingEventFactory.createHandlingEvent(
45        registrationTime, completionTime, trackingId, voyageNumber, unLocode, type
46      );
47  
48      /* Store the new handling event, which updates the persistent
49         state of the handling event aggregate (but not the cargo aggregate -
50         that happens asynchronously!)
51       */
52      handlingEventRepository.store(event);
53  
54      /* Publish an event stating that a cargo has been handled. */
55      applicationEvents.cargoWasHandled(event);
56  
57      logger.info("Registered handling event");
58    }
59  
60  }