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.Collection;
20:
21: import javax.servlet.http.HttpServletRequest;
22: import javax.servlet.http.HttpServletResponse;
23:
24: import org.compass.sample.petclinic.Owner;
25: import org.springframework.validation.BindException;
26: import org.springframework.web.servlet.ModelAndView;
27:
28: /**
29: * JavaBean Form controller that is used to search for <code>Owner</code>s by
30: * last name.
31: *
32: * @author Ken Krebs
33: */
34: public class FindOwnersForm extends AbstractClinicForm {
35:
36: private String selectView;
37:
38: /** Creates a new instance of FindOwnersForm */
39: public FindOwnersForm() {
40: // OK to start with a blank command object
41: setCommandClass(Owner.class);
42: }
43:
44: /**
45: * Set the name of the view that should be used for selection display.
46: */
47: public void setSelectView(String selectView) {
48: this .selectView = selectView;
49: }
50:
51: protected void initApplicationContext() {
52: super .initApplicationContext();
53: if (this .selectView == null) {
54: throw new IllegalArgumentException("selectView isn't set");
55: }
56: }
57:
58: /**
59: * Method used to search for owners renders View depending on how many are
60: * found
61: */
62: protected ModelAndView onSubmit(HttpServletRequest request,
63: HttpServletResponse response, Object command,
64: BindException errors) throws Exception {
65: Owner owner = (Owner) command;
66: // find owners by last name
67: Collection results = getClinic()
68: .findOwners(owner.getLastName());
69: if (results.size() < 1) {
70: // no owners found
71: errors.rejectValue("lastName", "notFound", null,
72: "not found");
73: return showForm(request, response, errors);
74: }
75:
76: if (results.size() > 1) {
77: // multiple owners found
78: return new ModelAndView(this .selectView, "selections",
79: results);
80: }
81:
82: // 1 owner found
83: owner = (Owner) results.iterator().next();
84: return new ModelAndView(getSuccessView(), "ownerId", owner
85: .getId());
86: }
87: }
|