1 16 17 package org.apache.cocoon.forms.flow.java; 18 19 import java.util.Locale ; 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 ; 35 36 43 public class FormInstance extends AbstractContinuable { 44 45 private Form form; 46 private Binding binding; 47 private Locale locale; 48 49 52 public FormInstance(String 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 } catch (Exception 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 78 public FormInstance(String definitionFile, String bindingFile) { 79 this(definitionFile); 80 createBinding(bindingFile); 81 } 82 83 86 public FormInstance(Element 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 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 108 public Widget getChild(String name) { 109 if (name == null) { 110 return this.form; 111 } else { 112 return this.form.getChild(name); 113 } 114 } 115 116 public String getSubmitId() { 117 118 Widget widget = this.form.getSubmitWidget(); 119 return widget == null ? null : widget.getId(); 121 } 122 123 128 131 132 137 140 141 public void show(String uri) { 142 show(uri, new VarMap()); 143 } 144 145 163 public void show(String uri, Object 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 175 boolean finished = false; 176 177 do { 178 sendPageAndWait(uri, bizData); 179 180 FormContext formContext = new FormContext(getRequest(), locale); 181 182 FlowHelper.setContextObject(this.getObjectModel(), bizData); 185 186 finished = this.form.process(formContext); 187 188 } while(!finished); 189 } 190 228 229 public void createBinding(String 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 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 object) { 249 if (this.binding == null) 250 throw new Error ("Binding not configured for this form."); 251 252 try { 253 this.binding.loadFormFromModel(this.form, object); 254 } catch (Exception e) { 255 throw new CascadingRuntimeException("Could not load form from model", e); 256 } 257 } 258 259 public void save(Object object) { 260 if (this.binding == null) 261 throw new Error ("Binding not configured for this form."); 262 263 try { 264 this.binding.saveFormToModel(this.form, object); 265 } catch (Exception e) { 266 throw new CascadingRuntimeException("Could not save form into model", e); 267 } 268 } 269 270 public void setAttribute(String name, Object value) { 271 this.form.setAttribute(name, value); 272 } 273 274 public Object getAttribute(String name) { 275 return this.form.getAttribute(name); 276 } 277 278 public void removeAttribute(String name) { 279 this.form.removeAttribute(name); 280 } 281 } 282 | Popular Tags |