KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > demo > icare > web > MakeReservationController


1 package org.objectweb.petals.demo.icare.web;
2
3 import java.beans.PropertyEditorSupport JavaDoc;
4 import java.text.NumberFormat JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
25  * @author Cristian Spiescu
26  * @author Imad Zarzouri
27  * @author Omar Senhaji Rhazi
28  * @author Anass Ouazzani Chahdi
29  *
30  * @since 21.01.2007
31  */

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 JavaDoc
51     protected void initBinder(HttpServletRequest JavaDoc request, ServletRequestDataBinder binder) throws Exception JavaDoc {
52         //binder.registerCustomEditor(null, "category", new PropertyEditorSupport());
53
// convert java.lang.Long
54
NumberFormat JavaDoc nf = NumberFormat.getNumberInstance();
55         binder.registerCustomEditor(Long JavaDoc.class, null,
56                 new CustomNumberEditor(Long JavaDoc.class, nf, true));
57         
58         binder.registerCustomEditor(null, "car", new PropertyEditorSupport JavaDoc() {
59
60             @Override JavaDoc
61             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
62                 Integer JavaDoc id = new Integer JavaDoc(text);
63                 Car car = carService.findCarById(id);
64                 setValue(car);
65             }
66             
67         });
68
69         SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("dd/MM/yy");
70         dateFormat.setLenient(false);
71         binder.registerCustomEditor(Date JavaDoc.class, new CustomDateEditor(dateFormat, true));
72         
73         super.initBinder(request, binder);
74     }
75
76     public ModelAndView processFormSubmission(HttpServletRequest JavaDoc request,
77             HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
78             throws Exception JavaDoc {
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 JavaDoc("unchecked")
87     protected Map JavaDoc referenceData(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
88         Map JavaDoc refData = new HashMap JavaDoc();
89         refData.put("cars", carService.getAllCars());
90         return refData;
91     }
92     
93     protected Object JavaDoc formBackingObject(HttpServletRequest JavaDoc request)
94             throws Exception JavaDoc {
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 JavaDoc request,
106             HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
107             throws Exception JavaDoc {
108
109         Reservation reservation = (Reservation) command;
110         if (request.getParameter("delete") != null) {
111             //carService.deleteCar(car.getId());
112
} else {
113             carService.storeReservation(reservation);
114         }
115         return new ModelAndView(getSuccessView());
116     }
117 }
118
Popular Tags