KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > html > HtmlFormWrapper


1 /*
2  * $Id: HtmlFormWrapper.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.html;
25
26 import java.io.IOException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.UtilHttp;
35 import org.ofbiz.base.util.UtilValidate;
36 import org.ofbiz.base.util.collections.MapStack;
37 import org.ofbiz.widget.form.FormFactory;
38 import org.ofbiz.widget.form.FormStringRenderer;
39 import org.ofbiz.widget.form.ModelForm;
40
41 import org.xml.sax.SAXException JavaDoc;
42
43
44 /**
45  * Widget Library - HTML Form Wrapper class - makes it easy to do the setup and render of a form
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5462 $
49  * @since 2.2
50  */

51 public class HtmlFormWrapper {
52     
53     public static final String JavaDoc module = HtmlFormWrapper.class.getName();
54     
55     protected String JavaDoc resourceName;
56     protected String JavaDoc formName;
57     protected HttpServletRequest JavaDoc request;
58     protected HttpServletResponse JavaDoc response;
59     protected ModelForm modelForm;
60     protected FormStringRenderer renderer;
61     protected Map JavaDoc context;
62
63     protected HtmlFormWrapper() {}
64
65     public HtmlFormWrapper(String JavaDoc resourceName, String JavaDoc formName, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
66             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
67         this.resourceName = resourceName;
68         this.formName = formName;
69         this.request = request;
70         this.response = response;
71         
72         this.modelForm = FormFactory.getFormFromWebappContext(resourceName, formName, request);
73
74         this.renderer = new HtmlFormRenderer(request, response);
75         
76         this.context = new HashMap JavaDoc();
77         Map JavaDoc parameterMap = UtilHttp.getParameterMap(request);
78         context.put("parameters", parameterMap);
79         
80         //make sure the locale is in the context
81
context.put("locale", UtilHttp.getLocale(request));
82         
83         // if there was an error message, this is an error
84
if (UtilValidate.isNotEmpty((String JavaDoc) request.getAttribute("_ERROR_MESSAGE_"))) {
85             context.put("isError", Boolean.TRUE);
86         } else {
87             context.put("isError", Boolean.FALSE);
88         }
89         
90         // if a parameter was passed saying this is an error, it is an error
91
if ("true".equals((String JavaDoc) parameterMap.get("isError"))) {
92             context.put("isError", Boolean.TRUE);
93         }
94         
95         Map JavaDoc uiLabelMap = (Map JavaDoc) request.getAttribute("uiLabelMap");
96         if (uiLabelMap != null && uiLabelMap.size() > 0 && context.get("uiLabelMap") == null) {
97             Debug.logInfo("Got uiLabelMap: " + uiLabelMap, module);
98             context.put("uiLabelMap", uiLabelMap);
99         }
100     }
101     
102     public String JavaDoc renderFormString(Object JavaDoc contextStack) {
103         if (contextStack instanceof MapStack) {
104             return renderFormString((MapStack) contextStack);
105         } else {
106             Debug.logWarning("WARNING: call renderFormString with a non-MapStack: " + (contextStack == null ? "null" : contextStack.getClass().getName()), module);
107             return renderFormString();
108         }
109     }
110     public String JavaDoc renderFormString(MapStack contextStack) {
111         // create a new context with the current context on the bottom
112
contextStack.push(this.context);
113         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
114         modelForm.renderFormString(buffer, contextStack, renderer);
115         contextStack.pop();
116         return buffer.toString();
117     }
118     public String JavaDoc renderFormString() {
119         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
120         modelForm.renderFormString(buffer, context, renderer);
121         return buffer.toString();
122     }
123
124     /**
125      * Tells the form library whether this is a response to an error or not.
126      * Defaults on initialization according to the presense of an errorMessage
127      * in the request or if an isError parameter was passed to the page with
128      * the value "true". If true then the prefilled values will come from the
129      * parameters Map instead of the value Map.
130      */

131     public void setIsError(boolean isError) {
132         this.context.put("isError", new Boolean JavaDoc(isError));
133     }
134     
135     public boolean getIsError() {
136         Boolean JavaDoc isErrorBoolean = (Boolean JavaDoc) this.context.get("isError");
137         if (isErrorBoolean == null) {
138             return false;
139         } else {
140             return isErrorBoolean.booleanValue();
141         }
142     }
143     
144     /**
145      * The "useRequestParameters" value in the form context tells the form library
146      * to use the request parameters to fill in values instead of the value map.
147      * This is generally used when it is an empty form to pre-set inital values.
148      * This is automatically set to false for list and multi forms. For related
149      * functionality see the setIsError method.
150      *
151      * @param useRequestParameters
152      */

153     public void setUseRequestParameters(boolean useRequestParameters) {
154         this.context.put("useRequestParameters", new Boolean JavaDoc(useRequestParameters));
155     }
156     
157     public boolean getUseRequestParameters() {
158         Boolean JavaDoc useRequestParametersBoolean = (Boolean JavaDoc) this.context.get("useRequestParameters");
159         if (useRequestParametersBoolean == null) {
160             return false;
161         } else {
162             return useRequestParametersBoolean.booleanValue();
163         }
164     }
165     
166     public void setFormOverrideName(String JavaDoc formName) {
167         this.context.put("formName", formName);
168     }
169     
170     public void putInContext(String JavaDoc name, Object JavaDoc value) {
171         this.context.put(name, value);
172     }
173     
174     public Object JavaDoc getFromContext(String JavaDoc name) {
175         return this.context.get(name);
176     }
177     
178     public ModelForm getModelForm() {
179         return modelForm;
180     }
181
182     public FormStringRenderer getRenderer() {
183         return renderer;
184     }
185
186     public void setRenderer(FormStringRenderer renderer) {
187         this.renderer = renderer;
188     }
189 }
190
Popular Tags