View Javadoc

1   package se.citerus.dddsample.interfaces.tracking;
2   
3   import org.springframework.context.MessageSource;
4   import org.springframework.validation.BindException;
5   import org.springframework.web.servlet.ModelAndView;
6   import org.springframework.web.servlet.mvc.SimpleFormController;
7   import org.springframework.web.servlet.support.RequestContextUtils;
8   import se.citerus.dddsample.domain.model.cargo.Cargo;
9   import se.citerus.dddsample.domain.model.cargo.CargoRepository;
10  import se.citerus.dddsample.domain.model.cargo.TrackingId;
11  import se.citerus.dddsample.domain.model.handling.HandlingEvent;
12  import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
13  
14  import javax.servlet.http.HttpServletRequest;
15  import javax.servlet.http.HttpServletResponse;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Locale;
19  import java.util.Map;
20  
21  /**
22   * Controller for tracking cargo. This interface sits immediately on top of the
23   * domain layer, unlike the booking interface which has a a remote facade and supporting
24   * DTOs in between.
25   * <p/>
26   * An adapter class, designed for the tracking use case, is used to wrap the domain model
27   * to make it easier to work with in a web page rendering context. We do not want to apply
28   * view rendering constraints to the design of our domain model, and the adapter
29   * helps us shield the domain model classes. 
30   * <p/>
31   *
32   * @eee se.citerus.dddsample.application.web.CargoTrackingViewAdapter
33   * @see se.citerus.dddsample.interfaces.booking.web.CargoAdminController
34   *
35   */
36  public final class CargoTrackingController extends SimpleFormController {
37  
38    private CargoRepository cargoRepository;
39    private HandlingEventRepository handlingEventRepository;
40  
41    public CargoTrackingController() {
42      setCommandClass(TrackCommand.class);
43    }
44  
45    @Override
46    protected ModelAndView onSubmit(final HttpServletRequest request, final HttpServletResponse response,
47                                    final Object command, final BindException errors) throws Exception {
48  
49      final TrackCommand trackCommand = (TrackCommand) command;
50      final String trackingIdString = trackCommand.getTrackingId();
51  
52      final TrackingId trackingId = new TrackingId(trackingIdString);
53      final Cargo cargo = cargoRepository.find(trackingId);
54  
55      final Map<String, CargoTrackingViewAdapter> model = new HashMap<String, CargoTrackingViewAdapter>();
56      if (cargo != null) {
57        final MessageSource messageSource = getApplicationContext();
58        final Locale locale = RequestContextUtils.getLocale(request);
59        final List<HandlingEvent> handlingEvents = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
60        model.put("cargo", new CargoTrackingViewAdapter(cargo, messageSource, locale, handlingEvents));
61      } else {
62        errors.rejectValue("trackingId", "cargo.unknown_id", new Object[]{trackCommand.getTrackingId()}, "Unknown tracking id");
63      }
64      return showForm(request, response, errors, model);
65    }
66  
67    public void setCargoRepository(CargoRepository cargoRepository) {
68      this.cargoRepository = cargoRepository;
69    }
70  
71    public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
72      this.handlingEventRepository = handlingEventRepository;
73    }
74  
75  }