KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > param > ParamLinkTag


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.param;
14
15 import java.io.IOException JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
23
24 import org.apache.log4j.Logger;
25
26 import com.tonbeller.tbutils.res.Resources;
27 import com.tonbeller.wcf.controller.Controller;
28 import com.tonbeller.wcf.controller.RequestContext;
29 import com.tonbeller.wcf.controller.RequestListener;
30 import com.tonbeller.wcf.expr.ExprUtils;
31 import com.tonbeller.wcf.token.RequestToken;
32 import com.tonbeller.wcf.utils.DomUtils;
33
34 /**
35  * produces an <a HREF="..." > hyperlink.
36  * Stores a list of SessionParam instances. If the user clicks on
37  * the hyperlink, all these parameters will be added to the SessionParamPool.
38  *
39  * @author av
40  */

41 public class ParamLinkTag extends TagSupport JavaDoc {
42   String JavaDoc page;
43
44   String JavaDoc displayName;
45   String JavaDoc displayValue;
46   String JavaDoc paramName;
47   String JavaDoc sqlValue;
48   String JavaDoc mdxValue;
49   String JavaDoc textValue;
50   String JavaDoc target; // equivalent to the HTML target
51

52   // shall we generate a token to disallow browser-back button?
53
boolean token;
54
55   /**
56    * null if no anchors are rendered, i.e. only text is displayed
57    * @see ParamLinkGroupTag#isRenderActions()
58    */

59   ClickHandler clickHandler;
60
61   /**
62    * Actions may be added to the link. They are executed
63    * when the user clicks on the link.
64    *
65    * @author av
66    */

67   public interface Action {
68     void execute(RequestContext context) throws Exception JavaDoc;
69   }
70
71   /**
72    * invokes a Method with the signature
73    * <p>
74    * methodName(RequestContext context) throws Exception;
75    *
76    * @author av
77    */

78   public static class InvokeMethodAction implements Action {
79     private String JavaDoc methodName;
80     private String JavaDoc targetExpr;
81
82     InvokeMethodAction(String JavaDoc targetExpr, String JavaDoc methodName) {
83       this.targetExpr = targetExpr;
84       this.methodName = methodName;
85     }
86
87     public void execute(RequestContext context) throws Exception JavaDoc {
88       Object JavaDoc target = context.getModelReference(targetExpr);
89       Class JavaDoc c = target.getClass();
90       Method JavaDoc m = c.getMethod(methodName, new Class JavaDoc[] { RequestContext.class});
91       m.invoke(target, new Object JavaDoc[] { context});
92     }
93   }
94
95   private static Logger logger = Logger.getLogger(ParamLinkTag.class);
96
97   static class ClickHandler implements RequestListener {
98     private List JavaDoc params = new ArrayList JavaDoc();
99     private List JavaDoc actions = new ArrayList JavaDoc();
100     private String JavaDoc page;
101
102     ClickHandler(String JavaDoc page) {
103       this.page = page;
104     }
105
106     public void addParam(SessionParam param) {
107       params.add(param);
108     }
109
110     public void addAction(Action action) {
111       actions.add(action);
112     }
113
114     public void request(RequestContext context) throws Exception JavaDoc {
115       // set parameters
116
SessionParamPool pool = SessionParamPool.instance(context.getSession());
117       for (Iterator JavaDoc it = params.iterator(); it.hasNext();) {
118         SessionParam p = (SessionParam) it.next();
119         if (logger.isInfoEnabled()) {
120           logger.info("setting parameter: " + p);
121         }
122         pool.setParam(p);
123       }
124       // execute actions
125
for (Iterator JavaDoc it = actions.iterator(); it.hasNext();) {
126         Action a = (Action) it.next();
127         a.execute(context);
128       }
129       // forward to next page
130
if (page != null)
131         Controller.instance(context.getSession()).setNextView(page);
132     }
133   }
134
135   ParamLinkGroupTag groupTag;
136
137   public int doStartTag() throws JspException JavaDoc {
138     try {
139       clickHandler = null;
140       groupTag = (ParamLinkGroupTag) findAncestorWithClass(this, ParamLinkGroupTag.class);
141       if (groupTag == null)
142         throw new JspException JavaDoc("ParamLinkTag must be used inside a ParamLinkGroupTag");
143       // hide hyperlinks in excel/PDF modes
144
if (!groupTag.isRenderActions())
145         return EVAL_BODY_INCLUDE;
146
147       String JavaDoc id = DomUtils.randomId();
148       clickHandler = new ClickHandler(evalString(page));
149       groupTag.getDispatcher().addRequestListener(groupTag.getId(), id, clickHandler);
150
151       Resources res = Resources.instance(ParamLinkTag.class);
152       String JavaDoc htmlTarget = "";
153       if (getTarget() != null) {
154         htmlTarget = " " + res.getString("paramLinkTag.targetTag", getTarget());
155       }
156
157       String JavaDoc href = "?" + groupTag.getId() + "=" + id;
158       if (token) {
159         RequestToken rt = RequestToken.instance(pageContext.getSession());
160         href = rt.appendHttpParameter(href);
161       }
162
163       Object JavaDoc[] args = { href, htmlTarget};
164       String JavaDoc s = res.getString("paramLinkTag.startTag", args);
165       pageContext.getOut().print(s);
166
167       if (paramName != null)
168         addParam(paramName, displayName, displayValue, sqlValue, mdxValue, textValue);
169
170     } catch (IOException JavaDoc e) {
171       logger.error(null, e);
172     }
173
174     return EVAL_BODY_INCLUDE;
175   }
176
177   /**
178    * creates a SessionParam from the arg list and adds it to the list of parameters
179    * @param paramName
180    * @param displayName
181    * @param displayValue
182    * @param sqlValue
183    * @param mdxValue
184    * @param textValue
185    */

186   public void addParam(String JavaDoc paramName, String JavaDoc displayName, String JavaDoc displayValue, String JavaDoc sqlValue,
187       String JavaDoc mdxValue, String JavaDoc textValue) {
188
189     if (paramName == null)
190       throw new IllegalArgumentException JavaDoc("parameter name is null");
191
192     SessionParam p = new SessionParam();
193     p.setName(paramName);
194     p.setDisplayName(evalString(displayName));
195     p.setDisplayValue(evalString(displayValue));
196     p.setTextValue(evalString(textValue));
197     p.setMdxValue(mdxValue);
198     p.setSqlValue(eval(sqlValue));
199     addParam(p);
200   }
201
202   /**
203    * @param p
204    */

205   public void addParam(SessionParam p) {
206     if (clickHandler != null)
207       clickHandler.addParam(p);
208   }
209
210   public void addAction(Action a) {
211     if (clickHandler != null)
212       clickHandler.addAction(a);
213   }
214
215   public int doEndTag() throws JspException JavaDoc {
216     if (!groupTag.isRenderActions())
217       return super.doEndTag();
218     try {
219       Resources res = Resources.instance(ParamLinkTag.class);
220       String JavaDoc s = res.getString("paramLinkTag.endTag");
221       pageContext.getOut().print(s);
222     } catch (IOException JavaDoc e) {
223       logger.error(null, e);
224     }
225     clickHandler = null;
226     return super.doEndTag();
227   }
228
229   private Object JavaDoc eval(String JavaDoc expr) {
230     if (expr == null)
231       return null;
232     if (ExprUtils.isExpression(expr))
233       return ExprUtils.getModelReference(pageContext, expr);
234     return expr;
235   }
236
237   private String JavaDoc evalString(String JavaDoc expr) {
238     if (expr == null)
239       return null;
240     if (ExprUtils.isExpression(expr))
241       return String.valueOf(ExprUtils.getModelReference(pageContext, expr));
242     return expr;
243   }
244
245   public void setDisplayName(String JavaDoc displayName) {
246     this.displayName = displayName;
247   }
248
249   public void setDisplayValue(String JavaDoc displayValue) {
250     this.displayValue = displayValue;
251   }
252
253   public void setMdxValue(String JavaDoc mdxValue) {
254     this.mdxValue = mdxValue;
255   }
256
257   public void setParamName(String JavaDoc paramName) {
258     this.paramName = paramName;
259   }
260
261   public void setSqlValue(String JavaDoc sqlValue) {
262     this.sqlValue = sqlValue;
263   }
264
265   public void setPage(String JavaDoc page) {
266     this.page = page;
267   }
268
269   public String JavaDoc getTextValue() {
270     return textValue;
271   }
272
273   public void setTextValue(String JavaDoc textValue) {
274     this.textValue = textValue;
275   }
276
277   public void setTarget(String JavaDoc target) {
278     this.target = target;
279   }
280
281   public String JavaDoc getTarget() {
282     return target;
283   }
284
285   public void setToken(boolean token) {
286     this.token = token;
287   }
288
289 }
Popular Tags