1 package org.objectweb.petals.demo.icare.web; 2 3 import java.beans.PropertyEditorSupport ; 4 import java.text.NumberFormat ; 5 import java.text.SimpleDateFormat ; 6 import java.util.Date ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 import org.objectweb.petals.demo.icare.model.Car; 14 import org.objectweb.petals.demo.icare.model.Reservation; 15 import org.objectweb.petals.demo.icare.service.CarService; 16 import org.springframework.beans.propertyeditors.CustomDateEditor; 17 import org.springframework.beans.propertyeditors.CustomNumberEditor; 18 import org.springframework.validation.BindException; 19 import org.springframework.web.bind.RequestUtils; 20 import org.springframework.web.bind.ServletRequestDataBinder; 21 import org.springframework.web.servlet.ModelAndView; 22 import org.springframework.web.servlet.mvc.SimpleFormController; 23 24 32 public class MakeReservationController extends SimpleFormController{ 33 34 private CarService carService; 35 36 public CarService getCarService() { 37 return carService; 38 } 39 40 public void setCarService(CarService carService) { 41 this.carService = carService; 42 } 43 44 public MakeReservationController() { 45 setSessionForm(true); 46 setBindOnNewForm(true); 47 setValidateOnBinding(true); 48 } 49 50 @Override 51 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { 52 NumberFormat nf = NumberFormat.getNumberInstance(); 55 binder.registerCustomEditor(Long .class, null, 56 new CustomNumberEditor(Long .class, nf, true)); 57 58 binder.registerCustomEditor(null, "car", new PropertyEditorSupport () { 59 60 @Override 61 public void setAsText(String text) throws IllegalArgumentException { 62 Integer id = new Integer (text); 63 Car car = carService.findCarById(id); 64 setValue(car); 65 } 66 67 }); 68 69 SimpleDateFormat dateFormat = new SimpleDateFormat ("dd/MM/yy"); 70 dateFormat.setLenient(false); 71 binder.registerCustomEditor(Date .class, new CustomDateEditor(dateFormat, true)); 72 73 super.initBinder(request, binder); 74 } 75 76 public ModelAndView processFormSubmission(HttpServletRequest request, 77 HttpServletResponse response, Object command, BindException errors) 78 throws Exception { 79 if (request.getParameter("cancel") != null) { 80 return new ModelAndView(getSuccessView()); 81 } 82 83 return super.processFormSubmission(request, response, command, errors); 84 } 85 86 @SuppressWarnings ("unchecked") 87 protected Map referenceData(HttpServletRequest request) throws Exception { 88 Map refData = new HashMap (); 89 refData.put("cars", carService.getAllCars()); 90 return refData; 91 } 92 93 protected Object formBackingObject(HttpServletRequest request) 94 throws Exception { 95 long reservationId = RequestUtils.getIntParameter(request, "id", -1); 96 Reservation reservation = null; 97 if (reservationId == -1) { 98 reservation = new Reservation(); 99 } else { 100 reservation = carService.findReservationById(reservationId); 101 } 102 return reservation; 103 } 104 105 protected ModelAndView onSubmit(HttpServletRequest request, 106 HttpServletResponse response, Object command, BindException errors) 107 throws Exception { 108 109 Reservation reservation = (Reservation) command; 110 if (request.getParameter("delete") != null) { 111 } else { 113 carService.storeReservation(reservation); 114 } 115 return new ModelAndView(getSuccessView()); 116 } 117 } 118 | Popular Tags |