1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: ValidationController.java,v 1.5 2002/09/05 15:41:51 ludovicc Exp $
37 */
38 package samples.servlet.xml.validation;
39
40
41 import java.util.Iterator;
42 import java.util.LinkedList;
43 import java.util.List;
44 import org.scopemvc.controller.basic.BasicController;
45 import org.scopemvc.controller.basic.ViewContext;
46 import org.scopemvc.controller.servlet.ScopeServlet;
47 import org.scopemvc.core.Control;
48 import org.scopemvc.core.ControlException;
49 import org.scopemvc.util.Debug;
50 import org.scopemvc.view.servlet.ServletView;
51 import org.scopemvc.view.servlet.ValidationFailure;
52
53 /***
54 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
55 * @created 05 September 2002
56 * @version $Revision: 1.5 $ $Date: 2002/09/05 15:41:51 $
57 */
58 public class ValidationController extends BasicController {
59
60 /***
61 * A Control ID.
62 */
63 public final static String SUBMIT = "submit";
64
65
66 /***
67 * Constructor for the ValidationController object
68 */
69 public ValidationController() {
70 setModel(new ValidationModel());
71
72 ServletView viewContainer = new ServletView();
73 viewContainer.addPage(new ValidationView1());
74 viewContainer.addPage(new ValidationView2());
75 setView(viewContainer);
76 }
77
78
79 /***
80 * If user interacts with a view parented by this Controller but there's no
81 * Control in the request then the Controller is started with this method:
82 * this is the default behaviour in the absence of a Control.
83 */
84 public void startup() {
85 ((ServletView) getView()).setVisible(ValidationView1.ID);
86 showView();
87 }
88
89
90 /***
91 * Respond to SUBMIT.
92 *
93 * @param inControl TODO: Describe the Parameter
94 * @throws ControlException TODO: Describe the Exception
95 */
96 protected void doHandleControl(Control inControl) throws ControlException {
97 if (inControl.matchesID(SUBMIT)) {
98 doSubmit();
99 }
100 }
101
102
103 /***
104 * On SUBMIT, switch over to the second View (which will bind to this
105 * Controller's model) and show it. If errors occurred during population of
106 * model with data from the view then add a list of the failures to the
107 * model and reshow the first page.
108 */
109 protected void doSubmit() {
110 List failures = (List) ViewContext.getViewContext().
111 getProperty(ScopeServlet.VALIDATION_FAILURES);
112 if (failures != null) {
113 List failureList = new LinkedList();
114 for (Iterator i = failures.iterator(); i.hasNext(); ) {
115 Object o = i.next();
116 if (Debug.ON) {
117 Debug.assertTrue(o instanceof ValidationFailure);
118 }
119 ValidationFailure failure = (ValidationFailure) o;
120
121 StringBuffer message = new StringBuffer();
122 message.append("Failed to set '");
123 message.append(failure.getProperty());
124 message.append("' to '");
125 message.append(failure.getValue());
126 message.append("' because ");
127 message.append(failure.getException().getLocalizedMessage());
128 failureList.add(message.toString());
129 }
130 ((ValidationModel) getModel()).setValidationFailures(failureList);
131 ((ServletView) getView()).setVisible(ValidationView1.ID);
132 showView();
133 } else {
134 ((ValidationModel) getModel()).setValidationFailures(null);
135 ((ServletView) getView()).setVisible(ValidationView2.ID);
136 showView();
137 }
138 }
139 }
This page was automatically generated by Maven