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.Field; 30 import org.dbforms.config.FieldValue; 31 import org.dbforms.config.FieldValues; 32 import org.dbforms.config.Table; 33 34 import org.dbforms.util.Util; 35 36 import java.io.UnsupportedEncodingException ; 37 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import javax.servlet.jsp.JspException ; 41 42 43 44 81 public class DbLinkURLTag extends DbBaseHandlerTag 82 implements javax.servlet.jsp.tagext.TryCatchFinally { 83 private static Log logCat = LogFactory.getLog(DbLinkURLTag.class 84 .getName()); private transient FieldValues positionFv; 87 91 private String childField; 92 93 private String href; 95 private String keyToDestPos; 96 private String keyToKeyToDestPos; 97 98 102 private String parentField; 103 private String position; 104 private String singleRow = "false"; 105 private String tableName; 106 107 112 public void setChildField(String childField) { 113 this.childField = childField; 114 } 115 116 117 122 public String getChildField() { 123 return childField; 124 } 125 126 127 132 public void setDestPos(String position) { 133 this.position = position; 134 } 135 136 137 142 public String getDestPos() { 143 return position; 144 } 145 146 147 152 public void setHref(String href) { 153 this.href = href; 154 } 155 156 157 162 public String getHref() { 163 return href; 164 } 165 166 167 172 public void setKeyToDestPos(String keyToDestPos) { 173 this.keyToDestPos = keyToDestPos; 174 } 175 176 177 182 public String getKeyToDestPos() { 183 return keyToDestPos; 184 } 185 186 187 192 public void setKeyToKeyToDestPos(String keyToKeyToDestPos) { 193 this.keyToKeyToDestPos = keyToKeyToDestPos; 194 } 195 196 197 202 public String getKeyToKeyToDestPos() { 203 return keyToKeyToDestPos; 204 } 205 206 207 212 public void setParentField(String parentField) { 213 this.parentField = parentField; 214 } 215 216 217 222 public String getParentField() { 223 return parentField; 224 } 225 226 227 232 public void setPosition(String position) { 233 this.position = position; 234 } 235 236 237 242 public String getPosition() { 243 return position; 244 } 245 246 247 252 public void setSingleRow(String string) { 253 singleRow = string; 254 } 255 256 257 262 public String getSingleRow() { 263 return singleRow; 264 } 265 266 267 272 public Table getTable() { 273 if (!Util.isNull(tableName)) { 274 return getConfig() 275 .getTableByName(tableName); 276 } else if (getParentForm() != null) { 278 return getParentForm() 279 .getTable(); 280 } else { 281 throw new IllegalArgumentException ("no table specified. either you define expliclty the attribute \"tableName\" or you put this tag inside a db:form!"); 282 } 283 } 284 285 286 291 public void setTableName(String tableName) { 292 this.tableName = tableName; 293 } 294 295 296 302 public void addPositionPart(Field field, 303 String value) { 304 if (positionFv == null) { 305 positionFv = new FieldValues(); 306 } 307 308 FieldValue fv = new FieldValue(field, value); 310 positionFv.put(fv); 311 } 312 313 314 322 public int doBodyEndTag() throws javax.servlet.jsp.JspException { 323 return SKIP_BODY; 324 } 325 326 327 330 public void doCatch(Throwable t) throws Throwable { 331 throw t; 332 } 333 334 335 343 public int doEndTag() throws javax.servlet.jsp.JspException { 344 try { 345 HttpServletResponse response = (HttpServletResponse ) pageContext 346 .getResponse(); 347 348 String s = makeUrl(); 349 s = response.encodeURL(s); 350 pageContext.getOut() 351 .write(s); 352 } catch (java.io.IOException ioe) { 353 throw new JspException ("IO Error: " + ioe.getMessage()); 354 } catch (Exception e) { 355 throw new JspException ("Error: " + e.getMessage()); 356 } 357 358 return EVAL_PAGE; 359 } 360 361 362 365 public void doFinally() { 366 logCat.info("doFinally called"); 367 position = null; 368 369 if (positionFv != null) { 370 positionFv.clear(); 371 } 372 373 positionFv = null; 374 href = null; 375 tableName = null; 376 position = null; 377 keyToDestPos = null; 378 keyToKeyToDestPos = null; 379 singleRow = "false"; 380 381 super.doFinally(); 382 } 383 384 385 394 public int doStartTag() throws javax.servlet.jsp.JspException { 395 if (Util.isNull(getPosition())) { 397 return EVAL_BODY_BUFFERED; } else { 399 return SKIP_BODY; } 401 } 402 403 404 411 protected String makeUrl() throws UnsupportedEncodingException { 412 if (this.position == null) { 415 if (positionFv != null) { position = getTable() 417 .getKeyPositionString(positionFv); 418 } 419 } 420 421 StringBuffer tagBuf = new StringBuffer (200); 423 HttpServletRequest request = (HttpServletRequest ) pageContext 424 .getRequest(); 425 String contextPath = request.getContextPath(); 426 tagBuf.append(contextPath); 427 428 if (!contextPath.endsWith("/")) { 430 tagBuf.append("/"); 431 } 432 433 tagBuf.append("servlet/control?"); 434 435 String tagName = "ac_goto"; 436 tagBuf.append(getDataTag(tagName, "x", "t")); 437 438 tagName = "data" + tagName + "_x"; 439 tagBuf.append(getDataTag(tagName, "fu", href)); 440 441 tagBuf.append(getDataTag(tagName, "destTable", getTable().getName())); 445 446 tagBuf.append(getDataTag(tagName, "destPos", 450 Util.encode(position, 451 pageContext.getRequest().getCharacterEncoding()))); 452 453 tagBuf.append(getDataTag(tagName, "keyToDestPos", 455 Util.encode(keyToDestPos, 456 pageContext.getRequest().getCharacterEncoding()))); 457 tagBuf.append(getDataTag(tagName, "keyToKeyDestPos", 458 Util.encode(keyToKeyToDestPos, 459 pageContext.getRequest().getCharacterEncoding()))); 460 461 if (getTable() != getParentForm() 463 .getTable()) { 464 tagBuf.append(getDataTag(tagName, "srcTable", 465 getParentForm().getTable().getName())); 466 tagBuf.append(getDataTag(tagName, "childField", 467 Util.encode(childField, 468 pageContext.getRequest().getCharacterEncoding()))); 469 tagBuf.append(getDataTag(tagName, "parentField", 470 Util.encode(parentField, 471 pageContext.getRequest().getCharacterEncoding()))); 472 } 473 474 tagBuf.append(getDataTag(tagName, "singleRow", getSingleRow())); 475 476 String s = tagBuf.toString(); 477 s = s.substring(0, s.length() - 1); 478 479 return s; 480 } 481 482 483 private String getDataTag(String primaryTagName, 484 String dataKey, 485 String dataValue) { 486 String s = ""; 487 488 if (!Util.isNull(dataValue)) { 489 StringBuffer tagBuf = new StringBuffer (); 490 tagBuf.append(primaryTagName); 491 tagBuf.append("_"); 492 tagBuf.append(dataKey); 493 tagBuf.append("="); 494 tagBuf.append(dataValue); 495 tagBuf.append("&"); 496 s = tagBuf.toString(); 497 } 498 499 return s; 500 } 501 } 502 | Popular Tags |