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.woody.acting;
18:
19: import org.apache.avalon.framework.thread.ThreadSafe;
20: import org.apache.avalon.framework.parameters.Parameters;
21: import org.apache.cocoon.acting.Action;
22: import org.apache.cocoon.environment.Redirector;
23: import org.apache.cocoon.environment.SourceResolver;
24: import org.apache.cocoon.environment.Request;
25: import org.apache.cocoon.environment.ObjectModelHelper;
26: import org.apache.cocoon.woody.FormContext;
27: import org.apache.cocoon.woody.event.FormHandler;
28: import org.apache.cocoon.woody.formmodel.Form;
29: import org.apache.cocoon.i18n.I18nUtils;
30: import org.apache.cocoon.components.LifecycleHelper;
31: import org.apache.excalibur.source.Source;
32:
33: import java.util.Map;
34: import java.util.Collections;
35: import java.util.Locale;
36:
37: /**
38: * An action that will create a form instance, let it handle the current request (and
39: * do validation), and will return not-null if validation was successfully or null when
40: * validation failed. In both cases, the created form instance is stored in a request attribute,
41: * so that it can be picked up later on by other components.
42: *
43: * <p>Required parameters:
44: * <ul>
45: * <li><strong>form-definition</strong>: filename (URL) pointing to the form definition file
46: * <li><strong>attribute-name</strong>: name of the request attribute in which the form instance should be stored
47: * </ul>
48: *
49: * @version $Id: HandleFormSubmitAction.java 433543 2006-08-22 06:22:54Z crossley $
50: */
51: public class HandleFormSubmitAction extends AbstractWoodyAction
52: implements Action, ThreadSafe {
53:
54: public Map act(Redirector redirector, SourceResolver resolver,
55: Map objectModel, String src, Parameters parameters)
56: throws Exception {
57: String formSource = parameters.getParameter("form-definition");
58: String formAttribute = parameters
59: .getParameter("attribute-name");
60: String formHandlerClassName = parameters.getParameter(
61: "formhandler", null);
62:
63: Locale locale = Locale.getDefault();
64: String localeStr = parameters.getParameter("locale", null);
65: if (localeStr != null)
66: locale = I18nUtils.parseLocale(localeStr, locale);
67:
68: Source source = resolver.resolveURI(formSource);
69: try {
70: Form form = formManager.createForm(source);
71:
72: Request request = ObjectModelHelper.getRequest(objectModel);
73: FormHandler formHandler = null;
74:
75: if (formHandlerClassName != null) {
76: // TODO cache these classes
77: Class clazz = Class.forName(formHandlerClassName);
78: formHandler = (FormHandler) clazz.newInstance();
79: LifecycleHelper.setupComponent(formHandler, null, null,
80: manager, null, null);
81: form.setFormHandler(formHandler);
82: }
83:
84: FormContext formContext = new FormContext(request, locale);
85:
86: boolean finished = form.process(formContext);
87: request.setAttribute(formAttribute, form);
88:
89: if (finished)
90: return Collections.EMPTY_MAP;
91: else
92: return null;
93: } finally {
94: resolver.release(source);
95: }
96: }
97: }
|