View Javadoc

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.ApplicationEvents;
8   import se.citerus.dddsample.application.CargoInspectionService;
9   import se.citerus.dddsample.domain.model.cargo.Cargo;
10  import se.citerus.dddsample.domain.model.cargo.CargoRepository;
11  import se.citerus.dddsample.domain.model.cargo.TrackingId;
12  import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
13  import se.citerus.dddsample.domain.model.handling.HandlingHistory;
14  
15  public class CargoInspectionServiceImpl implements CargoInspectionService {
16  
17    private final ApplicationEvents applicationEvents;
18    private final CargoRepository cargoRepository;
19    private final HandlingEventRepository handlingEventRepository;
20    private final Log logger = LogFactory.getLog(getClass());
21  
22    public CargoInspectionServiceImpl(final ApplicationEvents applicationEvents,
23                                      final CargoRepository cargoRepository,
24                                      final HandlingEventRepository handlingEventRepository) {
25      this.applicationEvents = applicationEvents;
26      this.cargoRepository = cargoRepository;
27      this.handlingEventRepository = handlingEventRepository;
28    }
29  
30    @Override
31    @Transactional
32    public void inspectCargo(final TrackingId trackingId) {
33      Validate.notNull(trackingId, "Tracking ID is required");
34  
35      final Cargo cargo = cargoRepository.find(trackingId);
36      if (cargo == null) {
37        logger.warn("Can't inspect non-existing cargo " + trackingId);
38        return;
39      }
40  
41      final HandlingHistory handlingHistory = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId);
42  
43      cargo.deriveDeliveryProgress(handlingHistory);
44  
45      if (cargo.delivery().isMisdirected()) {
46        applicationEvents.cargoWasMisdirected(cargo);
47      }
48  
49      if (cargo.delivery().isUnloadedAtDestination()) {
50        applicationEvents.cargoHasArrived(cargo);
51      }
52  
53      cargoRepository.store(cargo);
54    }
55  
56  }