diff --git a/src/main/java/se/citerus/dddsample/interfaces/InterfacesApplicationContext.java b/src/main/java/se/citerus/dddsample/interfaces/InterfacesApplicationContext.java index 43d9c4ac..7e4e61f1 100644 --- a/src/main/java/se/citerus/dddsample/interfaces/InterfacesApplicationContext.java +++ b/src/main/java/se/citerus/dddsample/interfaces/InterfacesApplicationContext.java @@ -22,8 +22,6 @@ import se.citerus.dddsample.domain.model.handling.HandlingEventRepository; import se.citerus.dddsample.domain.model.location.LocationRepository; import se.citerus.dddsample.domain.model.voyage.VoyageRepository; -import se.citerus.dddsample.interfaces.booking.facade.BookingServiceFacade; -import se.citerus.dddsample.interfaces.booking.facade.internal.BookingServiceFacadeImpl; import se.citerus.dddsample.interfaces.booking.web.CargoAdminController; import se.citerus.dddsample.interfaces.handling.HandlingReportParser; import se.citerus.dddsample.interfaces.handling.file.UploadDirectoryScanner; @@ -77,13 +75,8 @@ public TrackCommandValidator trackCommandValidator() { } @Bean - public CargoAdminController cargoAdminController(BookingServiceFacade bookingServiceFacade) { - return new CargoAdminController(bookingServiceFacade); - } - - @Bean - public BookingServiceFacade bookingServiceFacade(BookingService bookingService, LocationRepository locationRepository, CargoRepository cargoRepository, VoyageRepository voyageRepository) { - return new BookingServiceFacadeImpl(bookingService, locationRepository, cargoRepository, voyageRepository); + public CargoAdminController cargoAdminController(LocationRepository locationRepository, BookingService bookingService, CargoRepository cargoRepository, VoyageRepository voyageRepository) { + return new CargoAdminController(locationRepository, bookingService, cargoRepository, voyageRepository); } @Bean diff --git a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/BookingServiceFacade.java b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/BookingServiceFacade.java deleted file mode 100644 index fb33cd79..00000000 --- a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/BookingServiceFacade.java +++ /dev/null @@ -1,31 +0,0 @@ -package se.citerus.dddsample.interfaces.booking.facade; - -import java.rmi.RemoteException; -import java.util.Date; -import java.util.List; - -import se.citerus.dddsample.interfaces.booking.facade.dto.CargoRoutingDTO; -import se.citerus.dddsample.interfaces.booking.facade.dto.LocationDTO; -import se.citerus.dddsample.interfaces.booking.facade.dto.RouteCandidateDTO; - -/** - * This facade shields the domain layer - model, services, repositories - - * from concerns about such things as the user interface. - */ -public interface BookingServiceFacade { - - String bookNewCargo(String origin, String destination, Date arrivalDeadline) throws RemoteException; - - CargoRoutingDTO loadCargoForRouting(String trackingId) throws RemoteException; - - void assignCargoToRoute(String trackingId, RouteCandidateDTO route) throws RemoteException; - - void changeDestination(String trackingId, String destinationUnLocode) throws RemoteException; - - List requestPossibleRoutesForCargo(String trackingId) throws RemoteException; - - List listShippingLocations() throws RemoteException; - - List listAllCargos() throws RemoteException; - -} diff --git a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/BookingServiceFacadeImpl.java b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/BookingServiceFacadeImpl.java deleted file mode 100644 index 87ce50a7..00000000 --- a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/BookingServiceFacadeImpl.java +++ /dev/null @@ -1,108 +0,0 @@ -package se.citerus.dddsample.interfaces.booking.facade.internal; - -import se.citerus.dddsample.application.BookingService; -import se.citerus.dddsample.domain.model.cargo.Cargo; -import se.citerus.dddsample.domain.model.cargo.CargoRepository; -import se.citerus.dddsample.domain.model.cargo.Itinerary; -import se.citerus.dddsample.domain.model.cargo.TrackingId; -import se.citerus.dddsample.domain.model.location.Location; -import se.citerus.dddsample.domain.model.location.LocationRepository; -import se.citerus.dddsample.domain.model.location.UnLocode; -import se.citerus.dddsample.domain.model.voyage.VoyageRepository; -import se.citerus.dddsample.interfaces.booking.facade.BookingServiceFacade; -import se.citerus.dddsample.interfaces.booking.facade.dto.CargoRoutingDTO; -import se.citerus.dddsample.interfaces.booking.facade.dto.LocationDTO; -import se.citerus.dddsample.interfaces.booking.facade.dto.RouteCandidateDTO; -import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.CargoRoutingDTOAssembler; -import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.ItineraryCandidateDTOAssembler; -import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.LocationDTOAssembler; - -import java.rmi.RemoteException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This implementation has additional support from the infrastructure, for exposing as an RMI - * service and for keeping the OR-mapper unit-of-work open during DTO assembly, - * analogous to the view rendering for web interfaces. - * - */ -public class BookingServiceFacadeImpl implements BookingServiceFacade { - - private BookingService bookingService; - private LocationRepository locationRepository; - private CargoRepository cargoRepository; - private VoyageRepository voyageRepository; - - public BookingServiceFacadeImpl(BookingService bookingService, LocationRepository locationRepository, CargoRepository cargoRepository, VoyageRepository voyageRepository) { - this.bookingService = bookingService; - this.locationRepository = locationRepository; - this.cargoRepository = cargoRepository; - this.voyageRepository = voyageRepository; - } - - @Override - public List listShippingLocations() { - final List allLocations = locationRepository.findAll(); - final LocationDTOAssembler assembler = new LocationDTOAssembler(); - return assembler.toDTOList(allLocations); - } - - @Override - public String bookNewCargo(String origin, String destination, Date arrivalDeadline) { - TrackingId trackingId = bookingService.bookNewCargo( - new UnLocode(origin), - new UnLocode(destination), - arrivalDeadline - ); - return trackingId.idString(); - } - - @Override - public CargoRoutingDTO loadCargoForRouting(String trackingId) { - final Cargo cargo = cargoRepository.find(new TrackingId(trackingId)); - final CargoRoutingDTOAssembler assembler = new CargoRoutingDTOAssembler(); - return assembler.toDTO(cargo); - } - - @Override - public void assignCargoToRoute(String trackingIdStr, RouteCandidateDTO routeCandidateDTO) { - final Itinerary itinerary = new ItineraryCandidateDTOAssembler().fromDTO(routeCandidateDTO, voyageRepository, locationRepository); - final TrackingId trackingId = new TrackingId(trackingIdStr); - - bookingService.assignCargoToRoute(itinerary, trackingId); - } - - @Override - public void changeDestination(String trackingId, String destinationUnLocode) throws RemoteException { - bookingService.changeDestination(new TrackingId(trackingId), new UnLocode(destinationUnLocode)); - } - - @Override - public List listAllCargos() { - final List cargoList = cargoRepository.findAll(); - final List dtoList = new ArrayList(cargoList.size()); - final CargoRoutingDTOAssembler assembler = new CargoRoutingDTOAssembler(); - for (Cargo cargo : cargoList) { - dtoList.add(assembler.toDTO(cargo)); - } - return dtoList; - } - - @Override - public List requestPossibleRoutesForCargo(String trackingId) throws RemoteException { - final List itineraries = bookingService.requestPossibleRoutesForCargo(new TrackingId(trackingId)); - - final List routeCandidates = new ArrayList(itineraries.size()); - final ItineraryCandidateDTOAssembler dtoAssembler = new ItineraryCandidateDTOAssembler(); - for (Itinerary itinerary : itineraries) { - routeCandidates.add(dtoAssembler.toDTO(itinerary)); - } - - return routeCandidates; - } -} diff --git a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/CargoRoutingDTOAssembler.java b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/CargoRoutingDTOAssembler.java index 8cd3235d..ca0cfdd1 100644 --- a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/CargoRoutingDTOAssembler.java +++ b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/CargoRoutingDTOAssembler.java @@ -15,7 +15,7 @@ public class CargoRoutingDTOAssembler { * @param cargo cargo * @return A cargo routing DTO */ - public CargoRoutingDTO toDTO(final Cargo cargo) { + public static CargoRoutingDTO toDTO(final Cargo cargo) { final CargoRoutingDTO dto = new CargoRoutingDTO( cargo.trackingId().idString(), cargo.origin().unLocode().idString(), diff --git a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/ItineraryCandidateDTOAssembler.java b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/ItineraryCandidateDTOAssembler.java index faff42f4..f010e303 100644 --- a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/ItineraryCandidateDTOAssembler.java +++ b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/ItineraryCandidateDTOAssembler.java @@ -23,8 +23,8 @@ public class ItineraryCandidateDTOAssembler { * @param itinerary itinerary * @return A route candidate DTO */ - public RouteCandidateDTO toDTO(final Itinerary itinerary) { - final List legDTOs = new ArrayList(itinerary.legs().size()); + public static RouteCandidateDTO toDTO(final Itinerary itinerary) { + final List legDTOs = new ArrayList<>(itinerary.legs().size()); for (Leg leg : itinerary.legs()) { legDTOs.add(toLegDTO(leg)); } @@ -35,7 +35,7 @@ public RouteCandidateDTO toDTO(final Itinerary itinerary) { * @param leg leg * @return A leg DTO */ - protected LegDTO toLegDTO(final Leg leg) { + protected static LegDTO toLegDTO(final Leg leg) { final VoyageNumber voyageNumber = leg.voyage().voyageNumber(); final UnLocode from = leg.loadLocation().unLocode(); final UnLocode to = leg.unloadLocation().unLocode(); @@ -48,10 +48,10 @@ protected LegDTO toLegDTO(final Leg leg) { * @param locationRepository location repository * @return An itinerary */ - public Itinerary fromDTO(final RouteCandidateDTO routeCandidateDTO, + public static Itinerary fromDTO(final RouteCandidateDTO routeCandidateDTO, final VoyageRepository voyageRepository, final LocationRepository locationRepository) { - final List legs = new ArrayList(routeCandidateDTO.getLegs().size()); + final List legs = new ArrayList<>(routeCandidateDTO.getLegs().size()); for (LegDTO legDTO : routeCandidateDTO.getLegs()) { final VoyageNumber voyageNumber = new VoyageNumber(legDTO.getVoyageNumber()); final Voyage voyage = voyageRepository.find(voyageNumber); diff --git a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/LocationDTOAssembler.java b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/LocationDTOAssembler.java index af2b4c2b..d17a80fa 100644 --- a/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/LocationDTOAssembler.java +++ b/src/main/java/se/citerus/dddsample/interfaces/booking/facade/internal/assembler/LocationDTOAssembler.java @@ -8,12 +8,12 @@ public class LocationDTOAssembler { - public LocationDTO toDTO(Location location) { + public static LocationDTO toDTO(Location location) { return new LocationDTO(location.unLocode().idString(), location.name()); } - public List toDTOList(List allLocations) { - final List dtoList = new ArrayList(allLocations.size()); + public static List toDTOList(List allLocations) { + final List dtoList = new ArrayList<>(allLocations.size()); for (Location location : allLocations) { dtoList.add(toDTO(location)); } diff --git a/src/main/java/se/citerus/dddsample/interfaces/booking/web/CargoAdminController.java b/src/main/java/se/citerus/dddsample/interfaces/booking/web/CargoAdminController.java index eb8f5e4d..58d262df 100644 --- a/src/main/java/se/citerus/dddsample/interfaces/booking/web/CargoAdminController.java +++ b/src/main/java/se/citerus/dddsample/interfaces/booking/web/CargoAdminController.java @@ -6,11 +6,22 @@ import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import se.citerus.dddsample.interfaces.booking.facade.BookingServiceFacade; +import se.citerus.dddsample.application.BookingService; +import se.citerus.dddsample.domain.model.cargo.Cargo; +import se.citerus.dddsample.domain.model.cargo.CargoRepository; +import se.citerus.dddsample.domain.model.cargo.Itinerary; +import se.citerus.dddsample.domain.model.cargo.TrackingId; +import se.citerus.dddsample.domain.model.location.Location; +import se.citerus.dddsample.domain.model.location.LocationRepository; +import se.citerus.dddsample.domain.model.location.UnLocode; +import se.citerus.dddsample.domain.model.voyage.VoyageRepository; import se.citerus.dddsample.interfaces.booking.facade.dto.CargoRoutingDTO; import se.citerus.dddsample.interfaces.booking.facade.dto.LegDTO; import se.citerus.dddsample.interfaces.booking.facade.dto.LocationDTO; import se.citerus.dddsample.interfaces.booking.facade.dto.RouteCandidateDTO; +import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.CargoRoutingDTOAssembler; +import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.ItineraryCandidateDTOAssembler; +import se.citerus.dddsample.interfaces.booking.facade.internal.assembler.LocationDTOAssembler; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,10 +47,16 @@ @RequestMapping("/admin") public final class CargoAdminController { - private BookingServiceFacade bookingServiceFacade; + private final LocationRepository locationRepository; + private final BookingService bookingService; + private final CargoRepository cargoRepository; + private final VoyageRepository voyageRepository; - public CargoAdminController(BookingServiceFacade bookingServiceFacade) { - this.bookingServiceFacade = bookingServiceFacade; + public CargoAdminController(LocationRepository locationRepository, BookingService bookingService, CargoRepository cargoRepository, VoyageRepository voyageRepository) { + this.locationRepository = locationRepository; + this.bookingService = bookingService; + this.cargoRepository = cargoRepository; + this.voyageRepository = voyageRepository; } @InitBinder @@ -49,9 +66,9 @@ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder b @RequestMapping("/registration") public String registration(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception { - List dtoList = bookingServiceFacade.listShippingLocations(); + List dtoList = listAllCargo(); - List unLocodeStrings = new ArrayList(); + List unLocodeStrings = new ArrayList<>(); for (LocationDTO dto : dtoList) { unLocodeStrings.add(dto.getUnLocode()); @@ -66,24 +83,30 @@ public String registration(HttpServletRequest request, HttpServletResponse respo public void register(HttpServletRequest request, HttpServletResponse response, RegistrationCommand command) throws Exception { Date arrivalDeadline = new SimpleDateFormat("dd/MM/yyyy").parse(command.getArrivalDeadline()); - String trackingId = bookingServiceFacade.bookNewCargo( - command.getOriginUnlocode(), command.getDestinationUnlocode(), arrivalDeadline + TrackingId trackingId = bookingService.bookNewCargo( + new UnLocode(command.getOriginUnlocode()), + new UnLocode(command.getDestinationUnlocode()), + arrivalDeadline ); - response.sendRedirect("show?trackingId=" + trackingId); + response.sendRedirect("show?trackingId=" + trackingId.idString()); } @RequestMapping("/list") public String list(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception { - List cargoList = bookingServiceFacade.listAllCargos(); + final List cargoList = cargoRepository.findAll(); + final List dtoList = new ArrayList<>(cargoList.size()); + for (Cargo cargo : cargoList) { + dtoList.add(CargoRoutingDTOAssembler.toDTO(cargo)); + } - model.put("cargoList", cargoList); + model.put("cargoList", dtoList); return "admin/list"; } @RequestMapping("/show") public String show(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception { String trackingId = request.getParameter("trackingId"); - CargoRoutingDTO dto = bookingServiceFacade.loadCargoForRouting(trackingId); + CargoRoutingDTO dto = loadCargoForRouting(trackingId); model.put("cargo", dto); return "admin/show"; } @@ -92,10 +115,15 @@ public String show(HttpServletRequest request, HttpServletResponse response, Map public String selectItinerary(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception { String trackingId = request.getParameter("trackingId"); - List routeCandidates = bookingServiceFacade.requestPossibleRoutesForCargo(trackingId); + final List itineraries = bookingService.requestPossibleRoutesForCargo(new TrackingId(trackingId)); + + final List routeCandidates = new ArrayList<>(itineraries.size()); + for (Itinerary itinerary : itineraries) { + routeCandidates.add(ItineraryCandidateDTOAssembler.toDTO(itinerary)); + } model.put("routeCandidates", routeCandidates); - CargoRoutingDTO cargoDTO = bookingServiceFacade.loadCargoForRouting(trackingId); + CargoRoutingDTO cargoDTO = loadCargoForRouting(trackingId); model.put("cargo", cargoDTO); return "admin/selectItinerary"; @@ -103,7 +131,7 @@ public String selectItinerary(HttpServletRequest request, HttpServletResponse re @RequestMapping(value = "/assignItinerary", method = RequestMethod.POST) public void assignItinerary(HttpServletRequest request, HttpServletResponse response, RouteAssignmentCommand command) throws Exception { - List legDTOs = new ArrayList(command.getLegs().size()); + List legDTOs = new ArrayList<>(command.getLegs().size()); for (RouteAssignmentCommand.LegCommand leg : command.getLegs()) { legDTOs.add(new LegDTO( leg.getVoyageNumber(), @@ -116,18 +144,21 @@ public void assignItinerary(HttpServletRequest request, HttpServletResponse resp RouteCandidateDTO selectedRoute = new RouteCandidateDTO(legDTOs); - bookingServiceFacade.assignCargoToRoute(command.getTrackingId(), selectedRoute); + final Itinerary itinerary = ItineraryCandidateDTOAssembler.fromDTO(selectedRoute, voyageRepository, locationRepository); + final TrackingId trackingId = new TrackingId(command.getTrackingId()); + + bookingService.assignCargoToRoute(itinerary, trackingId); response.sendRedirect("show?trackingId=" + command.getTrackingId()); } @RequestMapping(value = "/pickNewDestination") public String pickNewDestination(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception { - List locations = bookingServiceFacade.listShippingLocations(); + List locations = listAllCargo(); model.put("locations", locations); String trackingId = request.getParameter("trackingId"); - CargoRoutingDTO cargo = bookingServiceFacade.loadCargoForRouting(trackingId); + CargoRoutingDTO cargo = loadCargoForRouting(trackingId); model.put("cargo", cargo); return "admin/pickNewDestination"; @@ -137,7 +168,17 @@ public String pickNewDestination(HttpServletRequest request, HttpServletResponse public void changeDestination(HttpServletRequest request, HttpServletResponse response) throws Exception { String trackingId = request.getParameter("trackingId"); String unLocode = request.getParameter("unlocode"); - bookingServiceFacade.changeDestination(trackingId, unLocode); + bookingService.changeDestination(new TrackingId(trackingId), new UnLocode(unLocode)); response.sendRedirect("show?trackingId=" + trackingId); } + + private CargoRoutingDTO loadCargoForRouting(String trackingId) { + final Cargo cargo = cargoRepository.find(new TrackingId(trackingId)); + return CargoRoutingDTOAssembler.toDTO(cargo); + } + + private List listAllCargo() { + final List allLocations = locationRepository.findAll(); + return LocationDTOAssembler.toDTOList(allLocations); + } }