stephan 2004/03/31 05:32:57
Modified: src/blocks/javaflow/java/org/apache/cocoon/samples/flow/java FormFlow.java PersistenceFlow.java Added: src/blocks/javaflow/java/org/apache/cocoon/forms/flow/java FormInstance.java Removed: src/blocks/javaflow/java/org/apache/cocoon/forms/flow/java AbstractFormFlow.java Log: Using the same OO structure for Forms. Revision Changes Path 1.1 cocoon-2.1/src/blocks/javaflow/java/org/apache/cocoon/forms/flow/java/FormInstance.java Index: FormInstance.java =================================================================== /* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.cocoon.forms.flow.java; import java.util.Locale; import org.apache.cocoon.components.flow.FlowHelper; import org.apache.cocoon.components.flow.java.*; import org.apache.cocoon.forms.flow.javascript.FormsFlowHelper; import org.apache.cocoon.forms.*; import org.apache.cocoon.forms.binding.*; import org.apache.cocoon.forms.formmodel.*; import org.apache.cocoon.forms.transformation.FormsPipelineConfig; import org.apache.excalibur.source.*; import org.w3c.dom.Element; /** * Implementation of the Cocoon Forms/Java Flow integration. * * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a> * @author <a href="mailto:[EMAIL PROTECTED]">Stephan Michels</a> * @version CVS $Id: FormInstance.java,v 1.1 2004/03/31 13:32:57 stephan Exp $ */ public class FormInstance extends AbstractCocoonFlow { private Form form; private Binding binding; private Widget formWidget; private Locale locale; private boolean isValid; private String submitId; private Object validator; // Used? /** * Create a form, given the URI of its definition file */ public FormInstance(String uri) { FormManager formMgr = null; SourceResolver resolver = null; Source src = null; try { formMgr = (FormManager)getComponent(FormManager.ROLE); resolver = (SourceResolver)getComponent(SourceResolver.ROLE); src = resolver.resolveURI(uri); this.form = formMgr.createForm(src); this.binding = null; // this.validator = null; // TODO : do we keep this ? // this.formWidget = new Widget(this.form); could not create instance } catch (Exception e) { throw new RuntimeException(e.toString()); } finally { releaseComponent(formMgr); if (src != null) resolver.release(src); releaseComponent(resolver); } } /** * Create a form, given the URI of its definition file, the * binding file. */ public FormInstance(String definitionFile, String bindingFile) { this(definitionFile); createBinding(bindingFile); } /** * Create a form of an fd:form element in the form of a org.w3c.dom.Element */ public FormInstance(Element formDefinition) { FormManager formMgr = null; SourceResolver resolver = null; Source src = null; try { formMgr = (FormManager)getComponent(FormManager.ROLE); resolver = (SourceResolver)getComponent(SourceResolver.ROLE); Form form = formMgr.createForm(formDefinition); this.binding = null; // this.formWidget = new Widget(form); could not create instance // this.local = cocoon.createPageLocal(); PageLocal ? } catch (Exception e) { throw new RuntimeException(e.toString()); } finally { releaseComponent(formMgr); if (src != null) resolver.release(src); releaseComponent(resolver); } } public Widget getModel() { return this.formWidget; } /** * Get a Widget (the java object) from the form. * If <code>name</code> is undefined, the form widget itself is returned. * Otherwise, the form's child widget of name <code>name</code> is returned. */ public Widget getWidget(String name) { if (name == null) { return this.form/*Widget*/; } else { return this.form/*Widget*/.getWidget(name); } } /** * Sets the point in your script that will be returned to when the form is * redisplayed. If setBookmark() is not called, this is implicitly set to * the beginning of showForm(). */ /* public WebContinuation setBookmark() { return (this.local_.webContinuation = cocoon.createWebContinuation()); }*/ /** * Returns the bookmark continuation associated with this form, or undefined * if setBookmark() has not been called. * */ /* public WebContinuation getBookmark() { return this.local_.webContinuation; } */ public void show(String uri) { show(uri, new VarMap()); } /** * Manages the display of a form and its validation. * * This uses some additionnal propertied on the form object : * - "locale" : the form locale (default locale is used if not set) * - "validator" : additional validation function. This function receives * the form object as parameter and should return a boolean indicating * if the form handling is finished (true) or if the form should be * redisplayed again (false) * * On return, the calling code can check some properties to know the form result : * - "isValid" : true if the form was sucessfully validated * - "submitId" : the id of the widget that triggered the form submit (can be null) * * @parameter uri the page uri (like in cocoon.sendPageAndWait()) * @parameter bizdata some business data for the view (like in cocoon.sendPageAndWait()). * The "{FormsPipelineConfig.CFORMSKEY}" and "locale" properties are added to this object. */ public void show(String uri, Object bizData) { if (bizData==null) bizData = new VarMap(); ((VarMap)bizData).add(FormsPipelineConfig.CFORMSKEY, this.form); if (this.locale == null) this.locale = java.util.Locale.getDefault(); ((VarMap)bizData).add("locale", this.locale); // Keep the first continuation that will be created as the result of this function //var result = null; boolean finished = false; this.isValid = false; do { sendPageAndWait(uri, bizData); FormContext formContext = new FormContext(getRequest(), locale); // Prematurely add the bizData as a request attribute so that event listeners can use it // (the same is done by cocoon.sendPage()) getRequest().setAttribute(FlowHelper.CONTEXT_OBJECT, bizData); finished = this.form.process(formContext); // Additional flow-level validation if (finished) { if (this.validator == null) { this.isValid = this.form.isValid(); } else { this.isValid = this.form.isValid() /*& this.validator(this.form, bizData)*/; } finished = this.isValid; } // FIXME: Theoretically, we should clone the form widget (this.form) to ensure it keeps its // value with the continuation. We don't do it since there should me not much pratical consequences // except a sudden change of repeaters whose size changed from a continuation to another. } while(!finished); Widget widget = this.form.getSubmitWidget(); // Can be null on "normal" submit this.submitId = widget == null ? null : widget.getId(); } /** * Manages the display of a form and its validation. * @parameter uri the page uri (like in cocoon.sendPageAndWait()) * @parameter fun optional function which will be executed after pipeline * processing. Useful for releasing resources needed during pipeline * processing but which should not become part of the continuation * @parameter ttl Time to live (in milliseconds) for the continuation * created * @returns The web continuation associated with submitting this form */ /* public showForm(String uri, Object fun, ttl) { if (!this.getBookmark()) { this.setBookmark(); } FormContext formContext = FormsFlowHelper.getFormContext(cocoon, this.locale); // this is needed by the FormTemplateTransformer: //var javaWidget = this.formWidget_.unwrap();; //this.formWidget_["CocoonFormsInstance"] = javaWidget; getRequest().setAttribute(Packages.org.apache.cocoon.forms.transformation.CFORMSKEY, this.formWidget); WebContinuation wk = sendPageAndWait(uri, this.formWidget, fun, ttl); var formContext = new FormContext(cocoon.request, javaWidget.getLocale()); var userErrors = 0; this.formWidget_.validationErrorListener = function(widget, error) { if (error != null) { userErrors++; } } var finished = javaWidget.process(formContext); if (this.onValidate) { this.onValidate(this); } if (!finished || userErrors > 0) { cocoon.continuation = this.local_.webContinuation; this.local_.webContinuation.continuation(this.local_.webContinuation); } return wk; }*/ public void createBinding(String bindingURI) { BindingManager bindingManager = null; Source source = null; SourceResolver resolver = null; try { bindingManager = (BindingManager)getComponent(BindingManager.ROLE); resolver = (SourceResolver)getComponent(SourceResolver.ROLE); source = resolver.resolveURI(bindingURI); this.binding = bindingManager.createBinding(source); } catch (Exception e) { throw new RuntimeException(e.toString()); } finally { if (source != null) resolver.release(source); releaseComponent(bindingManager); releaseComponent(resolver); } } public void load(Object object) { if (this.binding == null) throw new Error("Binding not configured for this form."); try { this.binding.loadFormFromModel(this.form, object); } catch (Exception e) { throw new RuntimeException(e.toString()); } } public void save(Object object) { if (this.binding == null) throw new Error("Binding not configured for this form."); try { this.binding.saveFormToModel(this.form, object); } catch (Exception e) { throw new RuntimeException(e.toString()); } } public void setAttribute(String name, Object value) { this.form.setAttribute(name, value); } public Object getAttribute(String name) { return this.form.getAttribute(name); } public void removeAttribute(String name) { this.form.removeAttribute(name); } } 1.3 +7 -9 cocoon-2.1/src/blocks/javaflow/java/org/apache/cocoon/samples/flow/java/FormFlow.java Index: FormFlow.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/javaflow/java/org/apache/cocoon/samples/flow/java/FormFlow.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- FormFlow.java 29 Mar 2004 20:56:14 -0000 1.2 +++ FormFlow.java 31 Mar 2004 13:32:57 -0000 1.3 @@ -23,11 +23,11 @@ import java.util.Date; -public class FormFlow extends AbstractFormFlow { +public class FormFlow extends AbstractCocoonFlow { public void doEditForm1() { - Form form = loadForm("forms/form1.xml"); + FormInstance form = new FormInstance("forms/form1.xml"); Field birthDate = (Field) form.getWidget("birthdate"); birthDate.setValue(new Date()); @@ -41,8 +41,7 @@ field = (Field) repeater.getWidget(1, "firstname"); field.setValue("Lucien"); - getRequest().setAttribute("form1", form); - showForm(form, "form/form1"); + form.show("form/form1"); sendPage("page/form1-result", new VarMap().add("email", ((Field)form.getWidget("email")).getValue()) .add("somebool", ((BooleanField)form.getWidget("somebool")).getValue()) @@ -65,11 +64,10 @@ contact.setFirstName("Hermann"); bean.addContact(contact); - Form form = loadForm("forms/form2.xml"); - Binding binding = loadBinding("forms/form2-binding.xml"); - binding.loadFormFromModel(form, bean); - showForm(form, "form/form2"); - binding.saveFormToModel(form, bean); + FormInstance form = new FormInstance("forms/form2.xml", "forms/form2-binding.xml"); + form.load(bean); + form.show("form/form2"); + form.save(bean); sendPage("page/form2-result", new VarMap().add("form2bean", bean)); } 1.6 +12 -12 cocoon-2.1/src/blocks/javaflow/java/org/apache/cocoon/samples/flow/java/PersistenceFlow.java Index: PersistenceFlow.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/javaflow/java/org/apache/cocoon/samples/flow/java/PersistenceFlow.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- PersistenceFlow.java 31 Mar 2004 10:20:00 -0000 1.5 +++ PersistenceFlow.java 31 Mar 2004 13:32:57 -0000 1.6 @@ -21,7 +21,7 @@ import org.apache.cocoon.components.flow.java.*; import org.apache.cocoon.forms.binding.*; -import org.apache.cocoon.forms.flow.java.AbstractFormFlow; +import org.apache.cocoon.forms.flow.java.*; import org.apache.cocoon.forms.formmodel.Form; import org.apache.cocoon.ojb.broker.components.PBFactory; import org.apache.cocoon.ojb.samples.EmployeeDAO; @@ -29,7 +29,7 @@ import org.apache.ojb.broker.*; import org.apache.ojb.broker.query.*; -public class PersistenceFlow extends AbstractFormFlow { +public class PersistenceFlow extends AbstractCocoonFlow { private transient PersistenceBroker broker = null; @@ -46,15 +46,15 @@ // Fill some initial data to the bean employee.setId(1); // Load form descriptor - Form form = loadForm("forms/employee.xml"); + FormInstance form = new FormInstance("forms/employee.xml"); // Load form binding - Binding binding = loadBinding("forms/employee-binding.xml"); + form.createBinding("forms/employee-binding.xml"); // Load the Bean to the form - binding.loadFormFromModel(form, employee); + form.load(employee); // Let Cocoon Forms handle the form - showForm(form, "form/employee"); + form.show("form/employee"); // Update the Bean based on user input - binding.saveFormToModel(form, employee); + form.save(employee); // Update Bean in Database broker.store(employee); // Send response to the user @@ -77,15 +77,15 @@ // Load bean based on the given PrimaryKey employee = (Employee) broker.getObjectByIdentity(new Identity(employee, broker)); // Load form descriptor - Form form = loadForm("forms/employee.xml"); + FormInstance form = new FormInstance("forms/employee.xml"); // Load form binding - Binding binding = loadBinding("forms/employee-binding.xml"); + form.createBinding("forms/employee-binding.xml"); // Load the Bean to the form - binding.loadFormFromModel(form, employee); + form.load(employee); // Let Cocoon Forms handle the form - showForm(form, "form/employee"); + form.show("form/employee"); // Update the Bean based on user input - binding.saveFormToModel(form, employee); + form.save(employee); // Update Bean in Database broker.store(employee);