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:
25: import org.compass.sample.petclinic.Pet;
26: import org.compass.sample.petclinic.PetType;
27: import org.compass.sample.petclinic.util.EntityUtils;
28: import org.springframework.web.bind.RequestUtils;
29: import org.springframework.web.servlet.ModelAndView;
30:
31: /**
32: * JavaBean Form controller that is used to edit an existing <code>Pet</code>.
33: *
34: * @author Ken Krebs
35: */
36: public class EditPetForm extends AbstractClinicForm {
37:
38: public EditPetForm() {
39: // need a session to hold the formBackingObject
40: setSessionForm(true);
41: // initialize the form from the formBackingObject
42: setBindOnNewForm(true);
43: }
44:
45: protected Map referenceData(HttpServletRequest request)
46: throws ServletException {
47: Map refData = new HashMap();
48: refData.put("types", getClinic().getPetTypes());
49: return refData;
50: }
51:
52: protected Object formBackingObject(HttpServletRequest request)
53: throws ServletException {
54: // get the Pet referred to by id in the request
55: return getClinic().loadPet(
56: RequestUtils.getRequiredIntParameter(request, "petId"));
57: }
58:
59: protected void onBind(HttpServletRequest request, Object command)
60: throws ServletException {
61: Pet pet = (Pet) command;
62: int typeId = RequestUtils.getRequiredIntParameter(request,
63: "typeId");
64: pet.setType((PetType) EntityUtils.getById(getClinic()
65: .getPetTypes(), PetType.class, typeId));
66: }
67:
68: /** Method updates an existing Pet */
69: protected ModelAndView onSubmit(Object command)
70: throws ServletException {
71: Pet pet = (Pet) command;
72: // delegate the update to the business layer
73: getClinic().storePet(pet);
74: return new ModelAndView(getSuccessView(), "ownerId", pet
75: .getOwner().getId());
76: }
77: }
|