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.util.HashMap;
20: import java.util.Map;
21:
22: import javax.servlet.ServletException;
23: import javax.servlet.http.HttpServletRequest;
24: import javax.servlet.http.HttpServletResponse;
25:
26: import org.compass.sample.petclinic.Owner;
27: import org.compass.sample.petclinic.Pet;
28: import org.compass.sample.petclinic.PetType;
29: import org.compass.sample.petclinic.util.EntityUtils;
30: import org.springframework.web.bind.RequestUtils;
31: import org.springframework.web.servlet.ModelAndView;
32:
33: /**
34: * JavaBean form controller that is used to add a new <code>Pet</code> to the
35: * system.
36: *
37: * @author Ken Krebs
38: */
39: public class AddPetForm extends AbstractClinicForm {
40:
41: public AddPetForm() {
42: // need a session to hold the formBackingObject
43: setSessionForm(true);
44: }
45:
46: protected Map referenceData(HttpServletRequest request)
47: throws ServletException {
48: Map refData = new HashMap();
49: refData.put("types", getClinic().getPetTypes());
50: return refData;
51: }
52:
53: protected Object formBackingObject(HttpServletRequest request)
54: throws ServletException {
55: Owner owner = getClinic().loadOwner(
56: RequestUtils
57: .getRequiredIntParameter(request, "ownerId"));
58: Pet pet = new Pet();
59: owner.addPet(pet);
60: return pet;
61: }
62:
63: protected void onBind(HttpServletRequest request, Object command) {
64: Pet pet = (Pet) command;
65: int typeId = Integer.parseInt(request.getParameter("typeId"));
66: pet.setType((PetType) EntityUtils.getById(getClinic()
67: .getPetTypes(), PetType.class, typeId));
68: }
69:
70: /** Method inserts a new Pet */
71: protected ModelAndView onSubmit(Object command)
72: throws ServletException {
73: Pet pet = (Pet) command;
74: // delegate the insert to the Business layer
75: getClinic().storePet(pet);
76: return new ModelAndView(getSuccessView(), "ownerId", pet
77: .getOwner().getId());
78: }
79:
80: protected ModelAndView handleInvalidSubmit(
81: HttpServletRequest request, HttpServletResponse response)
82: throws Exception {
83: return disallowDuplicateFormSubmission(request, response);
84: }
85: }
|