01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.sample.petclinic.web;
18:
19: import java.text.SimpleDateFormat;
20: import java.util.Date;
21:
22: import javax.servlet.http.HttpServletRequest;
23: import javax.servlet.http.HttpServletResponse;
24:
25: import org.compass.sample.petclinic.Clinic;
26: import org.springframework.beans.propertyeditors.CustomDateEditor;
27: import org.springframework.validation.BindException;
28: import org.springframework.web.bind.ServletRequestDataBinder;
29: import org.springframework.web.servlet.ModelAndView;
30: import org.springframework.web.servlet.mvc.SimpleFormController;
31:
32: /**
33: * JavaBean abstract base class for petclinic-aware form controllers. Provides
34: * convenience methods for subclasses.
35: *
36: * @author Ken Krebs
37: */
38: public abstract class AbstractClinicForm extends SimpleFormController {
39:
40: private Clinic clinic;
41:
42: public void setClinic(Clinic clinic) {
43: this .clinic = clinic;
44: }
45:
46: protected Clinic getClinic() {
47: return this .clinic;
48: }
49:
50: public void afterPropertiesSet() {
51: if (this .clinic == null) {
52: throw new IllegalArgumentException("'clinic' is required");
53: }
54: }
55:
56: /**
57: * Set up a custom property editor for the application's date format.
58: */
59: protected void initBinder(HttpServletRequest request,
60: ServletRequestDataBinder binder) {
61: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
62: dateFormat.setLenient(false);
63: binder.registerCustomEditor(Date.class, new CustomDateEditor(
64: dateFormat, false));
65: }
66:
67: /**
68: * Method disallows duplicate form submission. Typically used to prevent
69: * duplicate insertion of entities into the datastore. Shows a new form with
70: * an error message.
71: */
72: protected ModelAndView disallowDuplicateFormSubmission(
73: HttpServletRequest request, HttpServletResponse response)
74: throws Exception {
75: BindException errors = getErrorsForNewForm(request);
76: errors.reject("duplicateFormSubmission",
77: "Duplicate form submission");
78: return showForm(request, response, errors);
79: }
80: }
|