001: /*
002: * $Id: HtmlFormWrapper.java,v 1.4 2004/02/28 21:03:24 jonesde Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.content.widget.html;
025:
026: import java.io.IOException;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.xml.parsers.ParserConfigurationException;
033:
034: import org.ofbiz.base.util.UtilHttp;
035: import org.ofbiz.base.util.UtilValidate;
036: import org.ofbiz.content.widget.form.FormFactory;
037: import org.ofbiz.content.widget.form.FormStringRenderer;
038: import org.ofbiz.content.widget.form.ModelForm;
039: import org.xml.sax.SAXException;
040:
041: /**
042: * Widget Library - HTML Form Wrapper class - makes it easy to do the setup and render of a form
043: *
044: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
045: * @version $Revision: 1.4 $
046: * @since 2.2
047: */
048: public class HtmlFormWrapper {
049:
050: public static final String module = HtmlFormWrapper.class.getName();
051:
052: protected String resourceName;
053: protected String formName;
054: protected HttpServletRequest request;
055: protected HttpServletResponse response;
056: protected ModelForm modelForm;
057: protected FormStringRenderer renderer;
058: protected Map context;
059:
060: protected HtmlFormWrapper() {
061: }
062:
063: public HtmlFormWrapper(String resourceName, String formName,
064: HttpServletRequest request, HttpServletResponse response)
065: throws IOException, SAXException,
066: ParserConfigurationException {
067: this .resourceName = resourceName;
068: this .formName = formName;
069: this .request = request;
070: this .response = response;
071:
072: this .modelForm = FormFactory.getFormFromWebappContext(
073: resourceName, formName, request);
074:
075: this .renderer = new HtmlFormRenderer(request, response);
076:
077: this .context = new HashMap();
078: Map parameterMap = UtilHttp.getParameterMap(request);
079: context.put("parameters", parameterMap);
080:
081: //make sure the locale is in the context
082: context.put("locale", UtilHttp.getLocale(request));
083:
084: // if there was an error message, this is an error
085: if (UtilValidate.isNotEmpty((String) request
086: .getAttribute("_ERROR_MESSAGE_"))) {
087: context.put("isError", Boolean.TRUE);
088: } else {
089: context.put("isError", Boolean.FALSE);
090: }
091:
092: // if a parameter was passed saying this is an error, it is an error
093: if ("true".equals((String) parameterMap.get("isError"))) {
094: context.put("isError", Boolean.TRUE);
095: }
096: }
097:
098: public String renderFormString() {
099: StringBuffer buffer = new StringBuffer();
100: modelForm.renderFormString(buffer, context, renderer);
101: return buffer.toString();
102: }
103:
104: /**
105: * Tells the form library whether this is a response to an error or not.
106: * Defaults on initialization according to the presense of an errorMessage
107: * in the request or if an isError parameter was passed to the page with
108: * the value "true". If true then the prefilled values will come from the
109: * parameters Map instead of the value Map.
110: */
111: public void setIsError(boolean isError) {
112: this .context.put("isError", new Boolean(isError));
113: }
114:
115: public boolean getIsError() {
116: Boolean isErrorBoolean = (Boolean) this .context.get("isError");
117: if (isErrorBoolean == null) {
118: return false;
119: } else {
120: return isErrorBoolean.booleanValue();
121: }
122: }
123:
124: /**
125: * The "useRequestParameters" value in the form context tells the form library
126: * to use the request parameters to fill in values instead of the value map.
127: * This is generally used when it is an empty form to pre-set inital values.
128: * This is automatically set to false for list and multi forms. For related
129: * functionality see the setIsError method.
130: *
131: * @param useRequestParameters
132: */
133: public void setUseRequestParameters(boolean useRequestParameters) {
134: this .context.put("useRequestParameters", new Boolean(
135: useRequestParameters));
136: }
137:
138: public boolean getUseRequestParameters() {
139: Boolean useRequestParametersBoolean = (Boolean) this .context
140: .get("useRequestParameters");
141: if (useRequestParametersBoolean == null) {
142: return false;
143: } else {
144: return useRequestParametersBoolean.booleanValue();
145: }
146: }
147:
148: public void setFormOverrideName(String formName) {
149: this .context.put("formName", formName);
150: }
151:
152: public void putInContext(String name, Object value) {
153: this .context.put(name, value);
154: }
155:
156: public Object getFromContext(String name) {
157: return this .context.get(name);
158: }
159:
160: public ModelForm getModelForm() {
161: return modelForm;
162: }
163:
164: public FormStringRenderer getRenderer() {
165: return renderer;
166: }
167:
168: public void setRenderer(FormStringRenderer renderer) {
169: this.renderer = renderer;
170: }
171: }
|