KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > actionflow > 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.actionflow;
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.actionflow.ActionFlowMetaData;
21 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowURLTools;
22 import com.inversoft.verge.mvc.model.ModelResolution;
23 import com.inversoft.verge.mvc.model.actionflow.ActionFlowModelMetaData;
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 ActionFlow system that
31  * 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 entry;
48     protected String JavaDoc localEntry;
49     private String JavaDoc namespace;
50     protected String JavaDoc localNamespace;
51     private String JavaDoc category;
52     protected String JavaDoc localCategory;
53
54
55     /**
56      * Retrieves the tags action attribute
57      *
58      * @return The tags action attribute
59      */

60     public String JavaDoc getAction() {
61         return action;
62     }
63
64     /**
65      * Populates the tags action attribute
66      *
67      * @param action The tags action attribute
68      */

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

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

87     public void setForm(String JavaDoc form) {
88         this.form = form;
89     }
90
91     /**
92      * Retrieves the tag's entry attribute
93      *
94      * @return Returns the tag's entry attribute
95      */

96     public String JavaDoc getEntry() {
97         return entry;
98     }
99
100     /**
101      * Populates the tag's entry attribute
102      *
103      * @param entry The value of the tag's entry attribute
104      */

105     public void setEntry(String JavaDoc entry) {
106         this.entry = entry;
107     }
108
109     /**
110      * Retrieves the tag's namespace attribute
111      *
112      * @return Returns the tag's namespace attribute
113      */

114     public String JavaDoc getNamespace() {
115         return namespace;
116     }
117
118     /**
119      * Populates the tag's namespace attribute
120      *
121      * @param namespace The value of the tag's namespace attribute
122      */

123     public void setNamespace(String JavaDoc namespace) {
124         this.namespace = namespace;
125     }
126
127     /**
128      * Retrieves the tags category attribute
129      *
130      * @return Returns the tags category attribute
131      */

132     public String JavaDoc getCategory() {
133         return category;
134     }
135
136     /**
137      * Populates the tags category attribute
138      *
139      * @param category The value of the tags category attribute
140      */

141     public void setCategory(String JavaDoc category) {
142         this.category = category;
143     }
144
145     /**
146      * Expand all the variables that need it
147      */

148     protected void initialize() throws JspException JavaDoc {
149         super.initialize();
150
151         localForm = form;
152         localAction = action;
153         localNamespace = namespace;
154         localEntry = entry;
155         localCategory = category;
156
157         if (!JspTools.JSP_20) {
158             localAction = (String JavaDoc) JspTools.expand("action", action, String JavaDoc.class,
159                 this, pageContext);
160             localNamespace = (String JavaDoc) JspTools.expand("namespace", namespace, String JavaDoc.class,
161                 this, pageContext);
162             localEntry = (String JavaDoc) JspTools.expand("entry", entry, String JavaDoc.class,
163                 this, pageContext);
164             localCategory = (String JavaDoc) JspTools.expand("category", category, String JavaDoc.class,
165                 this, pageContext);
166         }
167
168         // Stores all the form beans into the page context so that they are available
169
// within this anchor tag.
170
if (form != null) {
171
172             // Validate that the form configuration is valid and fetch the form config
173
ActionFlowMetaData metaData = new ActionFlowMetaData(localNamespace, localForm,
174                 localEntry, localAction, null);
175             BaseFormConfig config = null;
176             try {
177                 config = metaData.findFormConfig(pageContext.getRequest());
178             } catch (MVCException mvce) {
179                 logger.error(mvce);
180                 throw new JspException JavaDoc("Invalid form name: " + localForm, mvce);
181             }
182
183             // Store all the model objects into the page context
184
Iterator JavaDoc iter = config.getFormBeans().iterator();
185             WebBean webBean;
186             Object JavaDoc bean;
187             ActionFlowModelMetaData md;
188             ModelResolution modelResolution;
189             String JavaDoc key;
190
191             while (iter.hasNext()) {
192                 webBean = (WebBean) iter.next();
193                 key = webBean.getID();
194
195                 try {
196                     bean = webBean.getInstance(pageContext);
197                 } catch (BeanException be) {
198                     logger.error(be);
199                     throw new JspException JavaDoc(be);
200                 }
201
202                 pageContext.setAttribute(key, bean);
203
204                 // Store the ModelResolution for the model tags
205
md = new ActionFlowModelMetaData(key, null);
206                 modelResolution = new ModelResolution(bean, md);
207                 ModelResolutionRegistry.store(key, modelResolution, pageContext);
208             }
209         }
210     }
211
212     /**
213      * Does the work of rendering the anchor tag using the MVC action as href,
214      * which overrides the href passed in, the parameters map and the rest of the
215      * attributes and such.
216      *
217      * @param buf The StringBuffer to append the anchor tag to
218      * @param href The action of the anchor tag
219      * @throws JspException If there was a problem generating the URLs for the
220      * anchor tag
221      */

222     protected void createATag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
223             String JavaDoc href, Map JavaDoc parameters)
224     throws JspException JavaDoc {
225
226         // Get the MVC servlet URL path
227
String JavaDoc url = ActionFlowURLTools.generateURL(localNamespace, localForm,
228             localAction, localEntry);
229
230         // Call the super implementation but use our URL and the parameters with
231
// the MVC extras in there
232
super.createATag(buf, id, name, url, parameters);
233     }
234 }
Popular Tags