1 23 24 package org.dbforms.taglib; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.config.FieldValue; 30 31 import org.dbforms.util.MessageResources; 32 import org.dbforms.util.ParseUtil; 33 import org.dbforms.util.Util; 34 35 import java.io.IOException ; 36 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.Locale ; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.jsp.JspException ; 43 import javax.servlet.jsp.JspWriter ; 44 45 46 47 148 public class DbFilterTag extends DbBaseHandlerTag 149 implements javax.servlet.jsp.tagext.TryCatchFinally { 150 151 protected static final String FLT_COND = "_cond_"; 152 153 154 protected static final String FLT_PREFIX = "filter_"; 155 156 157 protected static final String FLT_SEL = "_sel"; 158 159 160 protected static final String FLT_SET = "_set"; 161 162 163 protected static final String FLT_VALUE = "_value_"; 164 165 166 protected static final String FLT_VALUETYPE = "_valuetype_"; 167 168 169 protected static final String FLT_SEARCHALGO = "_searchalgo_"; 170 private static Log logCat = LogFactory.getLog(DbFilterTag.class 171 .getName()); 172 173 174 private ArrayList conds; 175 176 177 private String disabledCaption; 178 179 180 private String filterName; 181 182 183 private String setCaption; 184 185 186 private String size; 187 188 189 private String unsetCaption; 190 191 199 public static String getSqlFilter(HttpServletRequest request, 200 int tableId) { 201 int conditionId = getCurrentCondition(request, tableId); 202 203 if (conditionId > -1) { 204 return DbFilterConditionTag.getSqlFilter(request, tableId, conditionId); 206 } 207 208 return null; 209 } 210 211 212 221 public static FieldValue[] getSqlFilterParams(HttpServletRequest request, 222 int tableId) { 223 int conditionId = getCurrentCondition(request, tableId); 224 225 if (conditionId > -1) { 226 return DbFilterConditionTag.getSqlFilterParams(request, tableId, 228 conditionId); 229 } 230 231 return null; 232 } 233 234 235 240 public void setDisabledCaption(String string) { 241 disabledCaption = string; 242 } 243 244 245 250 public void setSetCaption(String string) { 251 setCaption = string; 252 } 253 254 255 260 public void setSize(String string) { 261 size = string; 262 } 263 264 265 270 public void setUnsetCaption(String string) { 271 unsetCaption = string; 272 } 273 274 275 278 public void doCatch(Throwable t) throws Throwable { 279 throw t; 280 } 281 282 283 289 public int doEndTag() throws JspException { 290 int currentCondId = getCurrentCondition((HttpServletRequest ) pageContext 292 .getRequest(), getTableId()); 293 DbFilterConditionTag currentCond = null; 294 295 if (currentCondId > -1) { 296 currentCond = new DbFilterConditionTag(); 297 298 currentCond.setState(pageContext, this, 300 (DbFilterConditionTag.State) conds.get(currentCondId)); 301 } 302 303 StringBuffer buf = render(currentCond); 304 305 try { 306 if (bodyContent != null) { 309 bodyContent.clearBody(); 310 } 311 312 JspWriter out = pageContext.getOut(); 313 out.write(buf.toString()); 314 } catch (IOException e) { 315 throw new JspException (e.getMessage()); 316 } 317 318 return SKIP_BODY; 319 } 320 321 322 327 public void doFinally() { 328 conds = null; 329 disabledCaption = null; 330 filterName = null; 331 setCaption = null; 332 size = null; 333 unsetCaption = null; 334 super.doFinally(); 335 } 336 337 338 343 public int doStartTag() throws JspException { 344 init(); 345 346 return EVAL_BODY_INCLUDE; 347 } 348 349 350 357 protected static String getFilterName(int tableId) { 358 return FLT_PREFIX + tableId; 359 } 360 361 362 367 protected String getFilterName() { 368 return filterName; 369 } 370 371 372 377 protected int getTableId() { 378 return getParentForm() 379 .getTable() 380 .getId(); 381 } 382 383 384 391 protected int addCondition(DbFilterConditionTag condition) { 392 conds.add(condition.getState()); 393 394 return conds.size() - 1; 395 } 396 397 398 406 private static int getCurrentCondition(HttpServletRequest request, 407 int tableId) { 408 int curCondId = -1; 409 410 String param = ParseUtil.getParameter(request, 412 getFilterName(tableId) + FLT_SEL); 413 414 if (!Util.isNull(param)) { 415 try { 417 curCondId = Integer.parseInt(param); 418 } catch (NumberFormatException e) { 419 logCat.error("getCurrentCondition", e); 420 } 421 } 422 423 logCat.debug("setting current filter: " + curCondId); 424 425 return curCondId; 426 } 427 428 429 432 private void init() { 433 conds = new ArrayList (); 434 filterName = getFilterName(getTableId()); 435 436 if (size == null) { 437 size = "1"; 438 } 439 440 if (disabledCaption == null) { 441 disabledCaption = ""; 442 } 443 444 if (setCaption == null) { 445 setCaption = "set"; 446 } 447 448 if (unsetCaption == null) { 449 unsetCaption = "unset"; 450 } 451 } 452 453 454 463 private StringBuffer render(DbFilterConditionTag currentCond) 464 throws JspException { 465 StringBuffer buf = new StringBuffer (); 466 467 buf.append("\n<select name=\"" + filterName + FLT_SEL + "\" class=\"" 469 + getStyleClass() + "\" size=\"" + size 470 + "\" onchange=\"document.dbform.submit()\" >\n"); 471 472 int cnt = 0; 473 buf.append("\t<option value=\"-1\" >" + disabledCaption + "</option>\n"); 474 475 DbFilterConditionTag cond = new DbFilterConditionTag(); 477 Locale locale = MessageResources.getLocale((HttpServletRequest ) pageContext 478 .getRequest()); 479 480 for (Iterator i = conds.iterator(); i.hasNext();) { 481 cond.setState(this.pageContext, this, 483 (DbFilterConditionTag.State) i.next()); 484 485 String selected = ((currentCond != null) && currentCond.equals(cond)) 487 ? "selected" 488 : ""; 489 490 String label = cond.getLabel(); 493 494 if ((label != null) && getParentForm() 495 .hasCaptionResourceSet()) { 496 try { 497 String message = MessageResources.getMessage(label, locale); 498 499 if (message != null) { 500 label = message; 501 } 502 } catch (Exception e) { 503 logCat.debug("setCaption(" + label + ") Exception : " 504 + e.getMessage()); 505 } 506 } 507 508 buf.append("\t<option value=\"" + cnt + "\" " + selected + ">" + label 510 + "</option>\n"); 511 cnt++; 512 } 513 514 buf.append("</select>\n"); 515 516 if (currentCond != null) { 517 buf.append(currentCond.render()); 519 520 if (!Util.isNull(setCaption)) { 521 DbBaseHandlerFactory btn = new DbBaseHandlerFactory(this.pageContext, 522 this, 523 DbNavReloadButtonTag.class); 524 ((DbNavReloadButtonTag) btn.getTag()).setCaption(setCaption); 525 ((DbNavReloadButtonTag) btn.getTag()).setForceReload("true"); 526 buf.append(btn.render()); 527 } 528 529 if (!Util.isNull(unsetCaption)) { 530 DbBaseHandlerFactory btn = new DbBaseHandlerFactory(this.pageContext, 531 this, 532 DbNavReloadButtonTag.class); 533 ((DbNavReloadButtonTag) btn.getTag()).setCaption(unsetCaption); 534 ((DbNavReloadButtonTag) btn.getTag()).setOnClick("document.dbform." 535 + filterName 536 + FLT_SEL 537 + ".selectedIndex = -1;"); 538 ((DbNavReloadButtonTag) btn.getTag()).setForceReload("true"); 539 buf.append(btn.render()); 540 } 541 } 542 543 return buf; 544 } 545 } 546 | Popular Tags |