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
23
24
25
26
27
28
29
30
31
32
33
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 }