View Javadoc

1   package se.citerus.dddsample.interfaces.booking.facade.internal;
2   
3   import org.apache.log4j.Logger;
4   import se.citerus.dddsample.application.BookingService;
5   import se.citerus.dddsample.domain.model.cargo.Cargo;
6   import se.citerus.dddsample.domain.model.cargo.CargoRepository;
7   import se.citerus.dddsample.domain.model.cargo.Itinerary;
8   import se.citerus.dddsample.domain.model.cargo.TrackingId;
9   import se.citerus.dddsample.domain.model.location.Location;
10  import se.citerus.dddsample.domain.model.location.LocationRepository;
11  import se.citerus.dddsample.domain.model.location.UnLocode;
12  import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
13  import se.citerus.dddsample.interfaces.booking.facade.BookingServiceFacade;
14  import se.citerus.dddsample.interfaces.booking.facade.dto.CargoRoutingDTO;
15  import se.citerus.dddsample.interfaces.booking.facade.dto.LocationDTO;
16  import se.citerus.dddsample.interfaces.booking.facade.dto.RouteCandidateDTO;
17  import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.CargoRoutingDTOAssembler;
18  import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.ItineraryCandidateDTOAssembler;
19  import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.LocationDTOAssembler;
20  
21  import java.rmi.RemoteException;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  
26  
27  /**
28   * This implementation has additional support from the infrastructure, for exposing as an RMI
29   * service and for keeping the OR-mapper unit-of-work open during DTO assembly,
30   * analogous to the view rendering for web interfaces.
31   *
32   */
33  public class BookingServiceFacadeImpl implements BookingServiceFacade {
34  
35    private BookingService bookingService;
36    private LocationRepository locationRepository;
37    private CargoRepository cargoRepository;
38    private VoyageRepository voyageRepository;
39    private final Logger logger = Logger.getLogger(BookingServiceFacadeImpl.class);
40  
41    @Override
42    public List<LocationDTO> listShippingLocations() {
43      final List<Location> allLocations = locationRepository.findAll();
44      final LocationDTOAssembler assembler = new LocationDTOAssembler();
45      return assembler.toDTOList(allLocations);
46    }
47  
48    @Override
49    public String bookNewCargo(String origin, String destination, Date arrivalDeadline) {
50      TrackingId trackingId = bookingService.bookNewCargo(
51        new UnLocode(origin), 
52        new UnLocode(destination),
53        arrivalDeadline
54      );
55      return trackingId.idString();
56    }
57  
58    @Override
59    public CargoRoutingDTO loadCargoForRouting(String trackingId) {
60      final Cargo cargo = cargoRepository.find(new TrackingId(trackingId));
61      final CargoRoutingDTOAssembler assembler = new CargoRoutingDTOAssembler();
62      return assembler.toDTO(cargo);
63    }
64  
65    @Override
66    public void assignCargoToRoute(String trackingIdStr, RouteCandidateDTO routeCandidateDTO) {
67      final Itinerary itinerary = new ItineraryCandidateDTOAssembler().fromDTO(routeCandidateDTO, voyageRepository, locationRepository);
68      final TrackingId trackingId = new TrackingId(trackingIdStr);
69  
70      bookingService.assignCargoToRoute(itinerary, trackingId);
71    }
72  
73    @Override
74    public void changeDestination(String trackingId, String destinationUnLocode) throws RemoteException {
75      bookingService.changeDestination(new TrackingId(trackingId), new UnLocode(destinationUnLocode));
76    }
77  
78    @Override
79    public List<CargoRoutingDTO> listAllCargos() {
80      final List<Cargo> cargoList = cargoRepository.findAll();
81      final List<CargoRoutingDTO> dtoList = new ArrayList<CargoRoutingDTO>(cargoList.size());
82      final CargoRoutingDTOAssembler assembler = new CargoRoutingDTOAssembler();
83      for (Cargo cargo : cargoList) {
84        dtoList.add(assembler.toDTO(cargo));
85      }
86      return dtoList;
87    }
88  
89    @Override
90    public List<RouteCandidateDTO> requestPossibleRoutesForCargo(String trackingId) throws RemoteException {
91      final List<Itinerary> itineraries = bookingService.requestPossibleRoutesForCargo(new TrackingId(trackingId));
92  
93      final List<RouteCandidateDTO> routeCandidates = new ArrayList<RouteCandidateDTO>(itineraries.size());
94      final ItineraryCandidateDTOAssembler dtoAssembler = new ItineraryCandidateDTOAssembler();
95      for (Itinerary itinerary : itineraries) {
96        routeCandidates.add(dtoAssembler.toDTO(itinerary));
97      }
98  
99      return routeCandidates;
100   }
101 
102   public void setBookingService(BookingService bookingService) {
103     this.bookingService = bookingService;
104   }
105 
106   public void setLocationRepository(LocationRepository locationRepository) {
107     this.locationRepository = locationRepository;
108   }
109 
110   public void setCargoRepository(CargoRepository cargoRepository) {
111     this.cargoRepository = cargoRepository;
112   }
113 
114   public void setVoyageRepository(VoyageRepository voyageRepository) {
115     this.voyageRepository = voyageRepository;
116   }
117 }