1 23 24 package org.dbforms.taglib; 25 26 import org.dbforms.config.FieldValue; 27 28 import org.dbforms.util.ParseUtil; 29 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.jsp.JspException ; 35 import javax.servlet.jsp.PageContext ; 36 import javax.servlet.jsp.tagext.TryCatchFinally ; 37 38 39 40 50 public class DbFilterConditionTag extends TagSupportWithScriptHandler 51 implements TryCatchFinally { 52 53 54 private transient State state; 55 56 59 public DbFilterConditionTag() { 60 super(); 61 state = new State(); 62 } 63 64 69 public void setLabel(String string) { 70 state.label = string; 71 } 72 73 74 79 public String getLabel() { 80 return state.label; 81 } 82 83 84 89 public int doAfterBody() throws JspException { 90 if (this.bodyContent != null) { 91 state.filterCondition = bodyContent.getString() 92 .trim(); 93 } 94 95 return SKIP_BODY; 96 } 97 98 99 102 public void doCatch(Throwable t) throws Throwable { 103 throw t; 104 } 105 106 107 112 public void doFinally() { 113 state = new State(); 114 } 115 116 117 123 public int doStartTag() throws JspException { 124 init(); 125 126 String sel = ParseUtil.getParameter((HttpServletRequest ) pageContext 127 .getRequest(), 128 ((DbFilterTag) getParent()) 129 .getFilterName() 130 + DbFilterTag.FLT_SEL); 131 132 if (sel != null) { 134 int selId = Integer.parseInt(sel); 135 136 if (selId == state.conditionId) { 137 return EVAL_BODY_BUFFERED; 138 } 139 } 140 141 return SKIP_BODY; 142 } 143 144 145 150 public boolean equals(Object obj) { 151 return (obj instanceof DbFilterConditionTag) 152 && (state.conditionId == ((DbFilterConditionTag) obj).state.conditionId); 153 } 154 155 156 161 public int hashCode() { 162 return state.conditionId; 163 } 164 165 166 174 protected static String getConditionName(int tableId, 175 int conditionId) { 176 return DbFilterTag.getFilterName(tableId) + DbFilterTag.FLT_COND 177 + conditionId; 178 } 179 180 181 190 protected static String getSqlFilter(HttpServletRequest request, 191 int tableId, 192 int conditionId) { 193 return ParseUtil.getParameter(request, 194 DbFilterTag.getFilterName(tableId) 195 + DbFilterTag.FLT_COND + conditionId); 196 } 197 198 199 208 protected static FieldValue[] getSqlFilterParams(HttpServletRequest request, 209 int tableId, 210 int conditionId) { 211 return DbFilterValueTag.readValuesFromRequest(request, tableId, 212 conditionId); 213 } 214 215 216 217 222 protected String getConditionName() { 223 return getConditionName(((DbFilterTag) getParent()).getTableId(), 224 state.conditionId); 225 } 226 227 228 235 protected void setState(PageContext pg, 236 DbFilterTag parent, 237 State state) { 238 setParent(parent); 239 setPageContext(pg); 240 this.state = state; 241 } 242 243 244 249 protected State getState() { 250 return state; 251 } 252 253 254 262 protected int addValue(DbFilterValueTag value) { 263 state.values.add(value.getState()); 264 265 return state.values.size() - 1; 266 } 267 268 269 274 protected StringBuffer render() throws JspException { 275 StringBuffer buf = new StringBuffer (); 276 buf.append("<input type=\"hidden\" name=\"" 277 + ((DbFilterTag) getParent()).getFilterName() 278 + DbFilterTag.FLT_COND + state.conditionId + "\" value=\"" 279 + state.filterCondition + "\" />\n"); 280 281 DbFilterValueTag value = new DbFilterValueTag(); 282 283 for (Iterator i = state.values.iterator(); i.hasNext();) { 284 value.setState(this.pageContext, this, 285 (DbFilterValueTag.State) i.next()); 286 buf.append(value.render()); 287 } 288 289 return buf; 290 } 291 292 293 296 private void init() { 297 state.values = new ArrayList (); 299 300 state.conditionId = ((DbFilterTag) getParent()).addCondition(this); 302 state.filterCondition = null; 303 304 if (state.label == null) { 305 state.label = Integer.toString(state.conditionId); 306 } 307 } 308 309 317 protected static class State { 318 319 protected ArrayList values = null; 320 321 322 protected String filterCondition = null; 323 324 325 protected String label = null; 326 327 328 protected int conditionId = -1; 329 } 330 } 331 | Popular Tags |