KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > form > ATag


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.form;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import javax.servlet.jsp.JspException JavaDoc;
14
15 import org.apache.log4j.Logger;
16
17 import com.inversoft.beans.BeanException;
18 import com.inversoft.verge.mvc.MVCException;
19 import com.inversoft.verge.mvc.config.BaseFormConfig;
20 import com.inversoft.verge.mvc.controller.form.FormMVCMetaData;
21 import com.inversoft.verge.mvc.controller.form.FormURLTools;
22 import com.inversoft.verge.mvc.model.ModelResolution;
23 import com.inversoft.verge.mvc.model.form.FormMetaData;
24 import com.inversoft.verge.mvc.view.jsp.JspTools;
25 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
26 import com.inversoft.verge.util.WebBean;
27
28
29 /**
30  * This class is an anchor tag for the Form-Based MVC system
31  * that allows a link to be an action into the MVC and in turn
32  * into the ActionFlow system.
33  *
34  * @author Brian Pontarelli
35  */

36 public class ATag extends com.inversoft.verge.mvc.view.jsp.html.ATag {
37
38     /**
39      * This classes logger
40      */

41     private static final Logger logger = Logger.getLogger(ATag.class);
42
43     private String JavaDoc action;
44     protected String JavaDoc localAction;
45     private String JavaDoc form;
46     protected String JavaDoc localForm;
47     private String JavaDoc category;
48     protected String JavaDoc localCategory;
49
50
51     /**
52      * Retrieves the tags action attribute
53      *
54      * @return The tags action attribute
55      */

56     public String JavaDoc getAction() {
57         return action;
58     }
59
60     /**
61      * Populates the tags action attribute
62      *
63      * @param action The tags action attribute
64      */

65     public void setAction(String JavaDoc action) {
66         this.action = action;
67     }
68
69     /**
70      * Retrieves the tag's form attribute
71      *
72      * @return Returns the tag's form attribute
73      */

74     public String JavaDoc getForm() {
75         return form;
76     }
77
78     /**
79      * Populates the tag's form attribute
80      *
81      * @param form The value of the tag's form attribute
82      */

83     public void setForm(String JavaDoc form) {
84         this.form = form;
85     }
86
87     /**
88      * Retrieves the tags category attribute
89      *
90      * @return Returns the tags category attribute
91      */

92     public String JavaDoc getCategory() {
93         return category;
94     }
95
96     /**
97      * Populates the tags category attribute
98      *
99      * @param category The value of the tags category attribute
100      */

101     public void setCategory(String JavaDoc category) {
102         this.category = category;
103     }
104
105     /**
106      * Expands the attributes that require it.
107      */

108     protected void initialize() throws JspException JavaDoc {
109         super.initialize();
110
111         localAction = action;
112         localForm = form;
113         localCategory = category;
114
115         if (!JspTools.JSP_20) {
116             localCategory = (String JavaDoc) JspTools.expand("category", category,
117                 String JavaDoc.class, this, pageContext);
118         }
119
120         // Stores all the form beans into the page context so that they are available
121
// within this anchor tag.
122
BaseFormConfig config = null;
123         if (localForm != null) {
124             FormMVCMetaData metaData = new FormMVCMetaData(localForm, localAction);
125             try {
126                 config = metaData.findFormConfig(pageContext.getRequest());
127             } catch (MVCException mvce) {
128                 logger.error(mvce);
129                 throw new JspException JavaDoc("Invalid form name: " + localForm, mvce);
130             }
131
132             // Store all the model objects into the page context
133
Iterator JavaDoc iter = config.getFormBeans().iterator();
134             WebBean webBean;
135             Object JavaDoc bean;
136             FormMetaData md;
137             ModelResolution modelResolution;
138             String JavaDoc key;
139
140             while (iter.hasNext()) {
141                 webBean = (WebBean) iter.next();
142                 key = webBean.getID();
143
144                 try {
145                     bean = webBean.getInstance(pageContext);
146                 } catch (BeanException be) {
147                     logger.error(be);
148                     throw new JspException JavaDoc(be);
149                 }
150
151                 pageContext.setAttribute(key, bean);
152
153                 // Store the ModelResolution for the model tags
154
md = new FormMetaData(key, null);
155                 modelResolution = new ModelResolution(bean, md);
156                 ModelResolutionRegistry.store(key, modelResolution, pageContext);
157             }
158         }
159     }
160
161     /**
162      * Does the work of rendering the anchor tag using the MVC action as href,
163      * which overrides the href passed in, the parameters map and the rest of the
164      * attributes and such.
165      *
166      * @param buf The StringBuffer to append the anchor tag to
167      * @param href The action of the anchor tag
168      * @throws JspException If there was a problem generating the URLs for the
169      * anchor tag
170      */

171     protected void createATag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
172             String JavaDoc href, Map JavaDoc parameters)
173     throws JspException JavaDoc {
174
175         // Call the super implementation but use our URL and the parameters with
176
// the MVC extras in there
177
String JavaDoc url = FormURLTools.generateURL(localForm, localAction);
178         super.createATag(buf, id, name, url, parameters);
179     }
180 }
Popular Tags