KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > flow > java > FormInstance


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.apache.cocoon.forms.flow.java;
18
19 import java.util.Locale JavaDoc;
20
21 import org.apache.avalon.framework.CascadingRuntimeException;
22 import org.apache.cocoon.components.flow.FlowHelper;
23 import org.apache.cocoon.components.flow.java.AbstractContinuable;
24 import org.apache.cocoon.components.flow.java.VarMap;
25 import org.apache.cocoon.forms.FormContext;
26 import org.apache.cocoon.forms.FormManager;
27 import org.apache.cocoon.forms.binding.Binding;
28 import org.apache.cocoon.forms.binding.BindingManager;
29 import org.apache.cocoon.forms.formmodel.Form;
30 import org.apache.cocoon.forms.formmodel.Widget;
31 import org.apache.cocoon.forms.transformation.FormsPipelineConfig;
32 import org.apache.excalibur.source.Source;
33 import org.apache.excalibur.source.SourceResolver;
34 import org.w3c.dom.Element JavaDoc;
35
36 /**
37  * Implementation of the Cocoon Forms/Java Flow integration.
38  *
39  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
40  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
41  * @version CVS $Id: FormInstance.java 329817 2005-10-31 13:19:17Z sylvain $
42  */

43 public class FormInstance extends AbstractContinuable {
44
45     private Form form;
46     private Binding binding;
47     private Locale JavaDoc locale;
48   
49     /**
50      * Create a form, given the URI of its definition file
51      */

52     public FormInstance(String JavaDoc uri) {
53         FormManager formMgr = null;
54         SourceResolver resolver = null;
55         Source src = null;
56         try {
57             formMgr = (FormManager)getComponent(FormManager.ROLE);
58             resolver = (SourceResolver)getComponent(SourceResolver.ROLE);
59             src = resolver.resolveURI(uri);
60             this.form = formMgr.createForm(src);
61             this.binding = null;
62             // this.validator = null;
63
// TODO : do we keep this ?
64
// this.formWidget = new Widget(this.form); could not create instance
65
} catch (Exception JavaDoc e) {
66             throw new CascadingRuntimeException("Could not create form instance", e);
67         } finally {
68             releaseComponent(formMgr);
69             if (src != null) resolver.release(src);
70             releaseComponent(resolver);
71         }
72     }
73
74     /**
75      * Create a form, given the URI of its definition file, the
76      * binding file.
77      */

78     public FormInstance(String JavaDoc definitionFile, String JavaDoc bindingFile) {
79         this(definitionFile);
80         createBinding(bindingFile);
81     }
82
83     /**
84      * Create a form of an fd:form element in the form of a org.w3c.dom.Element
85      */

86     public FormInstance(Element JavaDoc formDefinition) {
87         FormManager formMgr = null;
88         try {
89             formMgr = (FormManager)getComponent(FormManager.ROLE);
90             this.form = formMgr.createForm(formDefinition);
91             this.binding = null;
92         } catch (Exception JavaDoc e) {
93             throw new CascadingRuntimeException("Could not create form instance", e);
94         } finally {
95             releaseComponent(formMgr);
96         }
97     }
98
99     public Widget getModel() {
100         return this.form;
101     }
102
103     /**
104      * Get a Widget (the java object) from the form.
105      * If <code>name</code> is undefined, the form widget itself is returned.
106      * Otherwise, the form's child widget of name <code>name</code> is returned.
107      */

108     public Widget getChild(String JavaDoc name) {
109         if (name == null) {
110             return this.form;
111         } else {
112             return this.form.getChild(name);
113         }
114     }
115
116         public String JavaDoc getSubmitId() {
117             
118             Widget widget = this.form.getSubmitWidget();
119         // Can be null on "normal" submit
120
return widget == null ? null : widget.getId();
121         }
122
123     /**
124      * Sets the point in your script that will be returned to when the form is
125      * redisplayed. If setBookmark() is not called, this is implicitly set to
126      * the beginning of showForm().
127      */

128 /* public WebContinuation setBookmark() {
129           return (this.local_.webContinuation = cocoon.createWebContinuation());
130     }*/

131
132     /**
133      * Returns the bookmark continuation associated with this form, or undefined
134      * if setBookmark() has not been called.
135      *
136      */

137 /* public WebContinuation getBookmark() {
138         return this.local_.webContinuation;
139     } */

140
141     public void show(String JavaDoc uri) {
142         show(uri, new VarMap());
143     }
144     
145     /**
146      * Manages the display of a form and its validation.
147      *
148      * This uses some additionnal propertied on the form object :
149      * - "locale" : the form locale (default locale is used if not set)
150      * - "validator" : additional validation function. This function receives
151      * the form object as parameter and should return a boolean indicating
152      * if the form handling is finished (true) or if the form should be
153      * redisplayed again (false)
154      *
155      * On return, the calling code can check some properties to know the form result :
156      * - "isValid" : true if the form was sucessfully validated
157      * - "submitId" : the id of the widget that triggered the form submit (can be null)
158      *
159      * @param uri the page uri (like in cocoon.sendPageAndWait())
160      * @param bizData some business data for the view (like in cocoon.sendPageAndWait()).
161      * The "{FormsPipelineConfig.CFORMSKEY}" and "locale" properties are added to this object.
162      */

163     public void show(String JavaDoc uri, Object JavaDoc bizData) {
164
165         if (bizData==null) bizData = new VarMap();
166         ((VarMap)bizData).add(FormsPipelineConfig.CFORMSKEY, this.form);
167
168         if (this.locale == null)
169             this.locale = java.util.Locale.getDefault();
170         ((VarMap)bizData).add("locale", this.locale);
171     
172         // Keep the first continuation that will be created as the result of this function
173
//var result = null;
174

175         boolean finished = false;
176
177         do {
178             sendPageAndWait(uri, bizData);
179         
180             FormContext formContext = new FormContext(getRequest(), locale);
181
182             // Prematurely add the bizData as a request attribute so that event listeners can use it
183
// (the same is done by cocoon.sendPage())
184
FlowHelper.setContextObject(this.getObjectModel(), bizData);
185
186             finished = this.form.process(formContext);
187         
188         } while(!finished);
189     }
190     /*
191     /**
192      * Manages the display of a form and its validation.
193      * @param uri the page uri (like in cocoon.sendPageAndWait())
194      * @param fun optional function which will be executed after pipeline
195      * processing. Useful for releasing resources needed during pipeline
196      * processing but which should not become part of the continuation
197      * @param ttl Time to live (in milliseconds) for the continuation
198      * created
199      * @return The web continuation associated with submitting this form
200      *
201     public showForm(String uri, Object fun, ttl) {
202         if (!this.getBookmark()) {
203             this.setBookmark();
204         }
205         FormContext formContext = FormsFlowHelper.getFormContext(cocoon, this.locale);
206         // this is needed by the FormTemplateTransformer:
207         //var javaWidget = this.formWidget_.unwrap();;
208         //this.formWidget_["CocoonFormsInstance"] = javaWidget;
209         getRequest().setAttribute(Packages.org.apache.cocoon.forms.transformation.CFORMSKEY, this.formWidget);
210         WebContinuation wk = sendPageAndWait(uri, this.formWidget, fun, ttl);
211         var formContext = new FormContext(cocoon.request, javaWidget.getLocale());
212         var userErrors = 0;
213         this.formWidget_.validationErrorListener = function(widget, error) {
214             if (error != null) {
215                 userErrors++;
216             }
217         }
218         var finished = javaWidget.process(formContext);
219         if (this.onValidate) {
220             this.onValidate(this);
221         }
222         if (!finished || userErrors > 0) {
223             cocoon.continuation = this.local_.webContinuation;
224             this.local_.webContinuation.continuation(this.local_.webContinuation);
225         }
226         return wk;
227     }*/

228
229     public void createBinding(String JavaDoc bindingURI) {
230         BindingManager bindingManager = null;
231         Source source = null;
232         SourceResolver resolver = null;
233         try {
234             bindingManager = (BindingManager)getComponent(BindingManager.ROLE);
235             resolver = (SourceResolver)getComponent(SourceResolver.ROLE);
236             source = resolver.resolveURI(bindingURI);
237             this.binding = bindingManager.createBinding(source);
238         } catch (Exception JavaDoc e) {
239             throw new CascadingRuntimeException("Could not create binding", e);
240         } finally {
241             if (source != null)
242                 resolver.release(source);
243             releaseComponent(bindingManager);
244             releaseComponent(resolver);
245         }
246     }
247
248     public void load(Object JavaDoc object) {
249         if (this.binding == null)
250             throw new Error JavaDoc("Binding not configured for this form.");
251
252         try {
253             this.binding.loadFormFromModel(this.form, object);
254         } catch (Exception JavaDoc e) {
255             throw new CascadingRuntimeException("Could not load form from model", e);
256         }
257     }
258
259     public void save(Object JavaDoc object) {
260         if (this.binding == null)
261             throw new Error JavaDoc("Binding not configured for this form.");
262
263         try {
264             this.binding.saveFormToModel(this.form, object);
265         } catch (Exception JavaDoc e) {
266             throw new CascadingRuntimeException("Could not save form into model", e);
267         }
268     }
269
270     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
271         this.form.setAttribute(name, value);
272     }
273
274     public Object JavaDoc getAttribute(String JavaDoc name) {
275         return this.form.getAttribute(name);
276     }
277
278     public void removeAttribute(String JavaDoc name) {
279         this.form.removeAttribute(name);
280     }
281 }
282
Popular Tags