View Javadoc

1   package se.citerus.dddsample.interfaces.booking.facade.internal.assembler;
2   
3   import se.citerus.dddsample.domain.model.cargo.Itinerary;
4   import se.citerus.dddsample.domain.model.cargo.Leg;
5   import se.citerus.dddsample.domain.model.location.Location;
6   import se.citerus.dddsample.domain.model.location.LocationRepository;
7   import se.citerus.dddsample.domain.model.location.UnLocode;
8   import se.citerus.dddsample.domain.model.voyage.Voyage;
9   import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
10  import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
11  import se.citerus.dddsample.interfaces.booking.facade.dto.LegDTO;
12  import se.citerus.dddsample.interfaces.booking.facade.dto.RouteCandidateDTO;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /**
18   * Assembler class for the ItineraryCandidateDTO.
19   */
20  public class ItineraryCandidateDTOAssembler {
21  
22    /**
23     * @param itinerary itinerary
24     * @return A route candidate DTO
25     */
26    public RouteCandidateDTO toDTO(final Itinerary itinerary) {
27      final List<LegDTO> legDTOs = new ArrayList<LegDTO>(itinerary.legs().size());
28      for (Leg leg : itinerary.legs()) {
29        legDTOs.add(toLegDTO(leg));
30      }
31      return new RouteCandidateDTO(legDTOs);
32    }
33  
34    /**
35     * @param leg leg
36     * @return A leg DTO
37     */
38    protected LegDTO toLegDTO(final Leg leg) {
39      final VoyageNumber voyageNumber = leg.voyage().voyageNumber();
40      final UnLocode from = leg.loadLocation().unLocode();
41      final UnLocode to = leg.unloadLocation().unLocode();
42      return new LegDTO(voyageNumber.idString(), from.idString(), to.idString(), leg.loadTime(), leg.unloadTime());
43    }
44  
45    /**
46     * @param routeCandidateDTO route candidate DTO
47     * @param voyageRepository voyage repository
48     * @param locationRepository location repository
49     * @return An itinerary
50     */
51    public Itinerary fromDTO(final RouteCandidateDTO routeCandidateDTO,
52                             final VoyageRepository voyageRepository,
53                             final LocationRepository locationRepository) {
54      final List<Leg> legs = new ArrayList<Leg>(routeCandidateDTO.getLegs().size());
55      for (LegDTO legDTO : routeCandidateDTO.getLegs()) {
56        final VoyageNumber voyageNumber = new VoyageNumber(legDTO.getVoyageNumber());
57        final Voyage voyage = voyageRepository.find(voyageNumber);
58        final Location from = locationRepository.find(new UnLocode(legDTO.getFrom()));
59        final Location to = locationRepository.find(new UnLocode(legDTO.getTo()));
60        legs.add(new Leg(voyage, from, to, legDTO.getLoadTime(), legDTO.getUnloadTime()));
61      }
62      return new Itinerary(legs);
63    }
64  }