View Javadoc

1   package se.citerus.dddsample.interfaces.booking.web;
2   
3   import org.springframework.beans.propertyeditors.CustomDateEditor;
4   import org.springframework.web.bind.ServletRequestDataBinder;
5   import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
6   import se.citerus.dddsample.interfaces.booking.facade.BookingServiceFacade;
7   import se.citerus.dddsample.interfaces.booking.facade.dto.CargoRoutingDTO;
8   import se.citerus.dddsample.interfaces.booking.facade.dto.LegDTO;
9   import se.citerus.dddsample.interfaces.booking.facade.dto.LocationDTO;
10  import se.citerus.dddsample.interfaces.booking.facade.dto.RouteCandidateDTO;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  import java.text.SimpleDateFormat;
15  import java.util.*;
16  
17  /**
18   * Handles cargo booking and routing. Operates against a dedicated remoting service facade,
19   * and could easily be rewritten as a thick Swing client. Completely separated from the domain layer,
20   * unlike the tracking user interface.
21   * <p/>
22   * In order to successfully keep the domain model shielded from user interface considerations,
23   * this approach is generally preferred to the one taken in the tracking controller. However,
24   * there is never any one perfect solution for all situations, so we've chosen to demonstrate
25   * two polarized ways to build user interfaces.   
26   *
27   * @see se.citerus.dddsample.interfaces.tracking.CargoTrackingController
28   */
29  public final class CargoAdminController extends MultiActionController {
30  
31    private BookingServiceFacade bookingServiceFacade;
32  
33    @Override
34    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
35      super.initBinder(request, binder);
36      binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm"), false));
37    }
38  
39    public Map registrationForm(HttpServletRequest request, HttpServletResponse response) throws Exception {
40      Map<String, Object> map = new HashMap<String, Object>();
41      List<LocationDTO> dtoList = bookingServiceFacade.listShippingLocations();
42  
43      List<String> unLocodeStrings = new ArrayList<String>();
44  
45      for (LocationDTO dto : dtoList) {
46        unLocodeStrings.add(dto.getUnLocode());
47      }
48  
49      map.put("unlocodes", unLocodeStrings);
50      map.put("locations", dtoList);
51      return map;
52    }
53  
54    public void register(HttpServletRequest request, HttpServletResponse response,
55                         RegistrationCommand command) throws Exception {
56      Date arrivalDeadline = new SimpleDateFormat("M/dd/yyyy").parse(command.getArrivalDeadline());
57      String trackingId = bookingServiceFacade.bookNewCargo(
58        command.getOriginUnlocode(), command.getDestinationUnlocode(), arrivalDeadline
59      );
60      response.sendRedirect("show.html?trackingId=" + trackingId);
61    }
62  
63    public Map list(HttpServletRequest request, HttpServletResponse response) throws Exception {
64      Map<String, Object> map = new HashMap<String, Object>();
65      List<CargoRoutingDTO> cargoList = bookingServiceFacade.listAllCargos();
66  
67      map.put("cargoList", cargoList);
68      return map;
69    }
70  
71    public Map show(HttpServletRequest request, HttpServletResponse response) throws Exception {
72      Map<String, Object> map = new HashMap<String, Object>();
73      String trackingId = request.getParameter("trackingId");
74      CargoRoutingDTO dto = bookingServiceFacade.loadCargoForRouting(trackingId);
75      map.put("cargo", dto);
76      return map;
77    }
78  
79    public Map selectItinerary(HttpServletRequest request, HttpServletResponse response) throws Exception {
80      Map<String, Object> map = new HashMap<String, Object>();
81      String trackingId = request.getParameter("trackingId");
82  
83      List<RouteCandidateDTO> routeCandidates = bookingServiceFacade.requestPossibleRoutesForCargo(trackingId);
84      map.put("routeCandidates", routeCandidates);
85  
86      CargoRoutingDTO cargoDTO = bookingServiceFacade.loadCargoForRouting(trackingId);
87      map.put("cargo", cargoDTO);
88  
89      return map;
90    }
91  
92    public void assignItinerary(HttpServletRequest request, HttpServletResponse response, RouteAssignmentCommand command) throws Exception {
93      List<LegDTO> legDTOs = new ArrayList<LegDTO>(command.getLegs().size());
94      for (RouteAssignmentCommand.LegCommand leg : command.getLegs()) {
95        legDTOs.add(new LegDTO(
96          leg.getVoyageNumber(),
97          leg.getFromUnLocode(),
98          leg.getToUnLocode(),
99          leg.getFromDate(),
100         leg.getToDate())
101       );
102     }
103 
104     RouteCandidateDTO selectedRoute = new RouteCandidateDTO(legDTOs);
105 
106     bookingServiceFacade.assignCargoToRoute(command.getTrackingId(), selectedRoute);
107 
108     response.sendRedirect("show.html?trackingId=" + command.getTrackingId());
109     //response.sendRedirect("list.html");
110   }
111 
112   public Map pickNewDestination(HttpServletRequest request, HttpServletResponse response) throws Exception {
113     Map<String, Object> map = new HashMap<String, Object>();
114 
115     List<LocationDTO> locations = bookingServiceFacade.listShippingLocations();
116     map.put("locations", locations);
117 
118     String trackingId = request.getParameter("trackingId");
119     CargoRoutingDTO cargo = bookingServiceFacade.loadCargoForRouting(trackingId);
120     map.put("cargo", cargo);
121 
122     return map;
123   }
124 
125   public void changeDestination(HttpServletRequest request, HttpServletResponse response) throws Exception {
126     String trackingId = request.getParameter("trackingId");
127     String unLocode = request.getParameter("unlocode");
128     bookingServiceFacade.changeDestination(trackingId, unLocode);
129     response.sendRedirect("show.html?trackingId=" + trackingId);
130   }
131 
132   public void setBookingServiceFacade(BookingServiceFacade bookingServiceFacade) {
133     this.bookingServiceFacade = bookingServiceFacade;
134   }
135 }