KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > actionflow > FormTag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.actionflow;
8
9
10 import java.util.Iterator JavaDoc;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13
14 import org.apache.log4j.Logger;
15
16 import com.inversoft.beans.BeanException;
17 import com.inversoft.util.StringTools;
18 import com.inversoft.verge.mvc.MVCException;
19 import com.inversoft.verge.mvc.MVCRequest;
20 import com.inversoft.verge.mvc.config.BaseFormConfig;
21 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowMetaData;
22 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowURLTools;
23 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigStruct;
24 import com.inversoft.verge.mvc.model.ModelResolution;
25 import com.inversoft.verge.mvc.model.actionflow.ActionFlowModelMetaData;
26 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
27 import com.inversoft.verge.mvc.view.ViewConstants;
28 import com.inversoft.verge.mvc.view.jsp.JspTools;
29 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
30 import com.inversoft.verge.util.WebBean;
31
32
33 /**
34  * This class is the form based MVC form tag.
35  *
36  * @author Brian Pontarelli
37  */

38 public class FormTag extends com.inversoft.verge.mvc.view.jsp.html.FormTag {
39
40     /**
41      * This classes logger
42      */

43     private static final Logger logger = Logger.getLogger(FormTag.class);
44
45     private String JavaDoc form;
46     protected String JavaDoc localForm;
47     private String JavaDoc namespace;
48     protected String JavaDoc localNamespace;
49     private String JavaDoc var;
50     private ActionFlowMetaData metaData;
51     private BaseFormConfig config;
52     private boolean hasForm = false;
53
54
55     /**
56      * Retrieves the tag's form attribute
57      *
58      * @return The tag's form attribute
59      */

60     public String JavaDoc getForm() {
61         return form;
62     }
63
64     /**
65      * Populates the tag's form attribute
66      *
67      * @param form The tag's form attribute
68      */

69     public void setForm(String JavaDoc form) {
70         this.form = form;
71     }
72
73     /**
74      * Retrieves the tag's namespace attribute
75      *
76      * @return The tag's namespace attribute
77      */

78     public String JavaDoc getNamespace() {
79         return namespace;
80     }
81
82     /**
83      * Populates the tag's namespace attribute
84      *
85      * @param namespace The tag's namespace attribute
86      */

87     public void setNamespace(String JavaDoc namespace) {
88         this.namespace = namespace;
89     }
90
91     /**
92      * Retrieves the tags var attribute
93      *
94      * @return The tags var attribute
95      */

96     public String JavaDoc getVar() {
97         return var;
98     }
99
100     /**
101      * Populates the tags var attribute
102      *
103      * @param var The tags var attribute
104      */

105     public void setVar(String JavaDoc var) {
106         this.var = var;
107     }
108
109     /**
110      * Returns the config of this form
111      *
112      * @return The config of this form
113      */

114     public BaseFormConfig getConfig() {
115         return config;
116     }
117
118     /**
119      * Expands any of the variables that need it.
120      */

121     protected void initialize() throws JspException JavaDoc {
122         super.initialize();
123
124         localForm = form;
125         localNamespace = namespace;
126
127         if (!JspTools.JSP_20) {
128             localForm = (String JavaDoc) JspTools.expand("form", form, String JavaDoc.class, this,
129                 pageContext);
130             localNamespace = (String JavaDoc) JspTools.expand("namespace", namespace,
131                 String JavaDoc.class, this, pageContext);
132         }
133     }
134
135     /**
136      * Outputs the opening form tag with the name, method and attributes given
137      * supplied on the JSP page. This is done via the {@link
138      * #createForm(StringBuffer, String, String, String, String) createForm()}
139      * method, which is called by the parent implementation of doStartTag.
140      *
141      * @return Always returns EVAL_BODY_INCLUDE
142      * @throws JspException If there was a problem outputting the HTML
143      */

144     public int doStartTag() throws JspException JavaDoc {
145
146         initialize();
147
148         // If the user specified a form, set it up
149
hasForm = (localForm != null);
150         if (hasForm) {
151             if (localNamespace == null) {
152                 throw new JspException JavaDoc("<actionflow:form> form and namespace " +
153                     "attributes required");
154             }
155
156             setupForm();
157         }
158
159         return super.doStartTag();
160     }
161
162     /**
163      * Sets up the form variables and objects needed for the model tags.
164      *
165      * @throws JspException If there were any problems
166      */

167     void setupForm() throws JspException JavaDoc {
168         metaData = new ActionFlowMetaData(localNamespace, localForm);
169
170         try {
171             config = metaData.findFormConfig(pageContext.getRequest());
172         } catch (MVCException mvce) {
173             logger.error(mvce);
174             throw new JspException JavaDoc("Invalid form name: " + localForm, mvce);
175         }
176
177         // If there is no name of the form, use the configuration name
178
if (localName == null) {
179             localName = localForm;
180         }
181
182         // Store the FormConfig in the MVCRequest so that the model tags can use
183
// it
184
MVCRequest mvcRequest = JspTools.getMVCRequest(pageContext);
185         ActionFlowConfigStruct struct = new ActionFlowConfigStruct(config, metaData);
186         mvcRequest.setConfiguration(struct);
187
188         // The contract that only a form with a single form bean and the var
189
// attribute specified is verified in the TEI class. Here we just grab
190
// all the form beans and stuff them into the page context and the MVC
191
// stores
192
boolean hasVar = !StringTools.isEmpty(var);
193         Iterator JavaDoc iter = config.getFormBeans().iterator();
194         WebBean webBean;
195         Object JavaDoc bean;
196         String JavaDoc id;
197         ActionFlowModelMetaData md;
198         ModelResolution modelResolution;
199
200         while (iter.hasNext()) {
201             webBean = (WebBean) iter.next();
202
203             try {
204                 bean = webBean.getInstance(pageContext);
205             } catch (BeanException be) {
206                 logger.error(be);
207                 throw new JspException JavaDoc(be);
208             }
209
210             // Uses the var name if specified. Since var is only used with a
211
// single form bean form, this is okay
212
if (hasVar) {
213                 id = var;
214             } else {
215                 id = webBean.getID();
216             }
217             pageContext.setAttribute(id, bean);
218
219             // Store the ModelResolution for the model tags
220
md = new ActionFlowModelMetaData(webBean.getID(), null);
221             modelResolution = new ModelResolution(bean, md);
222             ModelResolutionRegistry.store(id, modelResolution, pageContext);
223         }
224     }
225
226     /**
227      * Renders the start of the HTML form tag and appends it to the StringBuffer
228      * given. This uses the MVC servlet URL from the ServletContext parameter and
229      * also optionally appends the ActionFlow form hidden parameters. This also
230      * always appends the JavaScript hidden parameters.
231      *
232      * @param buf The StringBuffer to append to
233      * @param url Ignored completely
234      * @param name The name of the form
235      * @param method The submission method of the form
236      * @throws JspException If the context parameter was missing
237      */

238     protected void createForm(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name, String JavaDoc url,
239             String JavaDoc method)
240     throws JspException JavaDoc {
241
242         // If there is no name of the form, use the configuration name
243
if (super.getName() == null) {
244             name = localForm;
245         }
246
247         // Get the MVC servlet URL path
248
String JavaDoc localURL = ActionFlowURLTools.generateURL(localNamespace, localForm,
249             null, null);
250
251         super.createForm(buf, id, name, localURL, method);
252
253         HtmlViewToolkit.appendHiddenTag(buf, ViewConstants.MVC_HIDDEN_1, null);
254         HtmlViewToolkit.appendHiddenTag(buf, ViewConstants.MVC_HIDDEN_2, null);
255     }
256 }
Popular Tags