1 13 package com.tonbeller.wcf.param; 14 15 import java.io.IOException ; 16 import java.lang.reflect.Method ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.tagext.TagSupport ; 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 41 public class ParamLinkTag extends TagSupport { 42 String page; 43 44 String displayName; 45 String displayValue; 46 String paramName; 47 String sqlValue; 48 String mdxValue; 49 String textValue; 50 String target; 52 boolean token; 54 55 59 ClickHandler clickHandler; 60 61 67 public interface Action { 68 void execute(RequestContext context) throws Exception ; 69 } 70 71 78 public static class InvokeMethodAction implements Action { 79 private String methodName; 80 private String targetExpr; 81 82 InvokeMethodAction(String targetExpr, String methodName) { 83 this.targetExpr = targetExpr; 84 this.methodName = methodName; 85 } 86 87 public void execute(RequestContext context) throws Exception { 88 Object target = context.getModelReference(targetExpr); 89 Class c = target.getClass(); 90 Method m = c.getMethod(methodName, new Class [] { RequestContext.class}); 91 m.invoke(target, new Object [] { context}); 92 } 93 } 94 95 private static Logger logger = Logger.getLogger(ParamLinkTag.class); 96 97 static class ClickHandler implements RequestListener { 98 private List params = new ArrayList (); 99 private List actions = new ArrayList (); 100 private String page; 101 102 ClickHandler(String 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 { 115 SessionParamPool pool = SessionParamPool.instance(context.getSession()); 117 for (Iterator 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 for (Iterator it = actions.iterator(); it.hasNext();) { 126 Action a = (Action) it.next(); 127 a.execute(context); 128 } 129 if (page != null) 131 Controller.instance(context.getSession()).setNextView(page); 132 } 133 } 134 135 ParamLinkGroupTag groupTag; 136 137 public int doStartTag() throws JspException { 138 try { 139 clickHandler = null; 140 groupTag = (ParamLinkGroupTag) findAncestorWithClass(this, ParamLinkGroupTag.class); 141 if (groupTag == null) 142 throw new JspException ("ParamLinkTag must be used inside a ParamLinkGroupTag"); 143 if (!groupTag.isRenderActions()) 145 return EVAL_BODY_INCLUDE; 146 147 String 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 htmlTarget = ""; 153 if (getTarget() != null) { 154 htmlTarget = " " + res.getString("paramLinkTag.targetTag", getTarget()); 155 } 156 157 String href = "?" + groupTag.getId() + "=" + id; 158 if (token) { 159 RequestToken rt = RequestToken.instance(pageContext.getSession()); 160 href = rt.appendHttpParameter(href); 161 } 162 163 Object [] args = { href, htmlTarget}; 164 String 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 e) { 171 logger.error(null, e); 172 } 173 174 return EVAL_BODY_INCLUDE; 175 } 176 177 186 public void addParam(String paramName, String displayName, String displayValue, String sqlValue, 187 String mdxValue, String textValue) { 188 189 if (paramName == null) 190 throw new IllegalArgumentException ("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 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 { 216 if (!groupTag.isRenderActions()) 217 return super.doEndTag(); 218 try { 219 Resources res = Resources.instance(ParamLinkTag.class); 220 String s = res.getString("paramLinkTag.endTag"); 221 pageContext.getOut().print(s); 222 } catch (IOException e) { 223 logger.error(null, e); 224 } 225 clickHandler = null; 226 return super.doEndTag(); 227 } 228 229 private Object eval(String 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 evalString(String 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 displayName) { 246 this.displayName = displayName; 247 } 248 249 public void setDisplayValue(String displayValue) { 250 this.displayValue = displayValue; 251 } 252 253 public void setMdxValue(String mdxValue) { 254 this.mdxValue = mdxValue; 255 } 256 257 public void setParamName(String paramName) { 258 this.paramName = paramName; 259 } 260 261 public void setSqlValue(String sqlValue) { 262 this.sqlValue = sqlValue; 263 } 264 265 public void setPage(String page) { 266 this.page = page; 267 } 268 269 public String getTextValue() { 270 return textValue; 271 } 272 273 public void setTextValue(String textValue) { 274 this.textValue = textValue; 275 } 276 277 public void setTarget(String target) { 278 this.target = target; 279 } 280 281 public String getTarget() { 282 return target; 283 } 284 285 public void setToken(boolean token) { 286 this.token = token; 287 } 288 289 } | Popular Tags |