01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.acting;
18:
19: import org.apache.avalon.framework.parameters.Parameters;
20: import org.apache.cocoon.environment.Redirector;
21: import org.apache.cocoon.environment.SourceResolver;
22: import org.apache.cocoon.environment.Request;
23: import org.apache.cocoon.environment.ObjectModelHelper;
24: import org.apache.cocoon.forms.FormContext;
25: import org.apache.cocoon.forms.event.FormHandler;
26: import org.apache.cocoon.forms.formmodel.Form;
27: import org.apache.cocoon.i18n.I18nUtils;
28: import org.apache.cocoon.components.LifecycleHelper;
29: import org.apache.excalibur.source.Source;
30:
31: import java.util.Map;
32: import java.util.Collections;
33: import java.util.Locale;
34:
35: /**
36: * An action that will create a form instance, let it handle the current request (and
37: * do validation), and will return not-null if validation was successfully or null when
38: * validation failed. In both cases, the created form instance is stored in a request attribute,
39: * so that it can be picked up later on by other components.
40: *
41: * <p>Required parameters:
42: * <ul>
43: * <li><strong>form-definition</strong>: filename (URL) pointing to the form definition file
44: * <li><strong>attribute-name</strong>: name of the request attribute in which the form instance should be stored
45: * </ul>
46: *
47: * @version $Id: HandleFormSubmitAction.java 449149 2006-09-23 03:58:05Z crossley $
48: */
49: public class HandleFormSubmitAction extends AbstractFormsAction {
50:
51: public Map act(Redirector redirector, SourceResolver resolver,
52: Map objectModel, String src, Parameters parameters)
53: throws Exception {
54: String formSource = parameters.getParameter("form-definition");
55: String formAttribute = parameters
56: .getParameter("attribute-name");
57: String formHandlerClassName = parameters.getParameter(
58: "formhandler", null);
59:
60: Locale locale = I18nUtils.parseLocale(parameters.getParameter(
61: "locale", null), Locale.getDefault());
62:
63: Source source = resolver.resolveURI(formSource);
64: try {
65: Form form = formManager.createForm(source);
66:
67: Request request = ObjectModelHelper.getRequest(objectModel);
68: FormHandler formHandler = null;
69:
70: if (formHandlerClassName != null) {
71: // TODO cache these classes
72: Class clazz = Class.forName(formHandlerClassName);
73: formHandler = (FormHandler) clazz.newInstance();
74: LifecycleHelper.setupComponent(formHandler, null, null,
75: manager, null);
76: form.setFormHandler(formHandler);
77: }
78:
79: FormContext formContext = new FormContext(request, locale);
80:
81: boolean finished = form.process(formContext);
82: request.setAttribute(formAttribute, form);
83:
84: if (finished) {
85: return Collections.EMPTY_MAP;
86: }
87: return null;
88: } finally {
89: resolver.release(source);
90: }
91: }
92: }
|