1 23 package org.dbforms.taglib; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.dbforms.config.DbFormsConfig; 28 import org.dbforms.config.DbFormsConfigRegistry; 29 import org.dbforms.config.ResultSetVector; 30 31 import org.dbforms.util.IEscaper; 32 import org.dbforms.util.IFormatEmbeddedData; 33 import org.dbforms.util.KeyValuePair; 34 import org.dbforms.util.MessageResources; 35 import org.dbforms.util.ReflectionUtil; 36 import org.dbforms.util.SqlUtil; 37 import org.dbforms.util.Util; 38 39 import java.sql.Connection ; 40 import java.sql.SQLException ; 41 42 import java.util.List ; 43 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.jsp.JspException ; 46 import javax.servlet.jsp.PageContext ; 47 48 49 50 56 public abstract class EmbeddedData extends DbBaseHandlerTag 57 implements javax.servlet.jsp.tagext.TryCatchFinally , StaticDataAddInterface { 58 private static Log logCat = LogFactory.getLog(EmbeddedData.class 59 .getName()); 60 private IFormatEmbeddedData printfFormat; 61 private List data; 62 private String dbConnectionName; 63 private String disableCache = "false"; 64 private String format; 65 private String formatClass; 66 private String name; 67 68 73 public void setDbConnectionName(String name) { 74 dbConnectionName = name; 75 } 76 77 78 83 public String getDbConnectionName() { 84 return dbConnectionName; 85 } 86 87 88 94 public void setDisableCache(java.lang.String newDisableCache) { 95 disableCache = newDisableCache; 96 } 97 98 99 105 public java.lang.String getDisableCache() { 106 return disableCache; 107 } 108 109 110 115 public IEscaper getEscaper() { 116 DataContainer parent = ((DataContainer) getParent()); 117 IEscaper res = parent.getEscaper(); 118 119 return res; 120 } 121 122 123 128 public void setFormat(java.lang.String format) { 129 this.format = format; 130 } 131 132 133 138 public void setFormatClass(String formatClass) { 139 this.formatClass = formatClass; 140 } 141 142 143 148 public String getFormatClass() { 149 return formatClass; 150 } 151 152 153 162 public void setName(String name) { 163 this.name = name; 164 } 165 166 167 172 public String getName() { 173 return name; 174 } 175 176 177 182 public void addElement(KeyValuePair pair) { 183 data.add(pair); 184 } 185 186 187 190 public void doCatch(Throwable t) throws Throwable { 191 throw t; 192 } 193 194 195 198 public void doFinally() { 199 name = null; 200 dbConnectionName = null; 201 format = null; 202 printfFormat = null; 203 formatClass = null; 204 disableCache = "false"; 205 } 206 207 208 216 public int doStartTag() throws JspException { 217 225 printfFormat = null; 226 227 if (!Util.isNull(format) || !Util.isNull(getFormatClass())) { 228 if (Util.isNull(getFormatClass())) { 229 setFormatClass(getConfig().getDefaultFormatterClass()); 230 } 231 232 if (Util.isNull(format)) { 233 setFormat("%s"); 234 } 235 236 if (format.indexOf('%') < 0) { 238 StringBuffer newFormat = new StringBuffer (); 239 240 for (int j = 0; j < format.length(); j++) { 241 if (format.charAt(j) == 's') { 242 newFormat.append("%s"); 243 } else { 244 newFormat.append(format.charAt(j)); 245 } 246 } 247 248 format = newFormat.toString(); 249 250 } 252 253 try { 254 printfFormat = (IFormatEmbeddedData) ReflectionUtil.newInstance(getFormatClass()); 255 printfFormat.setLocale(MessageResources.getLocale( 256 (HttpServletRequest ) pageContext.getRequest())); 257 printfFormat.setFormat(format); 258 } catch (Exception e) { 259 logCat.error("cannot create the new printfFormat [" 260 + getFormatClass() + "]", e); 261 } 262 } 263 264 int result = SKIP_BODY; 265 data = null; 266 267 if (useCache()) { 270 data = (List ) pageContext.getAttribute(name, PageContext.PAGE_SCOPE); 271 } 272 273 if (data == null) { 275 result = EVAL_BODY_BUFFERED; 276 logCat.info("generating Embeddeddata " + name); 277 278 DbFormsConfig config = null; 282 283 try { 284 config = DbFormsConfigRegistry.instance().lookup(); 285 } catch (Exception e) { 286 logCat.error(e); 287 throw new JspException (e); 288 } 289 290 Connection con = null; 291 292 try { 293 con = config.getConnection(dbConnectionName); 294 } catch (Exception e) { 295 throw new JspException (e); 296 } 297 298 try { 299 data = fetchData(con); 300 301 pageContext.setAttribute(name, data, PageContext.PAGE_SCOPE); 304 305 } catch (SQLException sqle) { 307 throw new JspException ("Database error in EmbeddedData.fetchData " 308 + sqle.toString()); 309 } finally { 310 SqlUtil.closeConnection(con); 311 } 312 } else { 313 logCat.info(" Embeddeddata " + name + " already generated"); 314 } 315 316 ((DataContainer) getParent()).setEmbeddedData(data); 317 318 return result; 320 } 321 322 323 331 protected abstract List fetchData(Connection con) throws SQLException ; 332 333 334 345 protected List formatEmbeddedResultRows(ResultSetVector rsv) { 346 List result = new java.util.Vector (); 347 boolean resultSuccessFullyFormated = false; 348 349 if (printfFormat != null) { 350 try { 351 rsv.moveFirst(); 352 353 for (int i = 0; i < rsv.size(); i++) { 354 355 String [] currentRow = rsv.getCurrentRow(); 356 String htKey = currentRow[0]; 357 358 Object [] objs = rsv.getCurrentRowAsObjects(); 359 360 Object [] objs2 = new Object [objs.length - 1]; 361 362 for (int j = 0; j < objs2.length; j++) { 363 if ((objs[j] instanceof String ) || (objs[j] instanceof Byte ) 364 || (objs[j] instanceof java.lang.Integer ) 365 || (objs[j] instanceof Short ) 366 || (objs[j] instanceof Float ) 367 || (objs[j] instanceof Long ) 368 || (objs[j] instanceof Double )) { 369 objs2[j] = objs[(j + 1)]; 370 } else { 371 objs2[j] = currentRow[j + 1]; 372 373 } 375 } 376 377 String htValue = printfFormat.sprintf(objs2); 378 result.add(new KeyValuePair(htKey, htValue)); 379 rsv.moveNext(); 380 } 381 382 resultSuccessFullyFormated = true; 383 } catch (IllegalArgumentException ex) { 384 logCat.error("Could not format result using format '" + format 385 + "', error message is " + ex.getMessage()); 386 logCat.error( 387 "Using fallback method of comma separated list instead"); 388 result = new java.util.Vector (); 389 } catch (NullPointerException npe) { 391 logCat.error("Could not format result using format '" + format 392 + "', error message is " + npe.getMessage()); 393 logCat.error( 394 "Using fallback method of comma separated list instead"); 395 result = new java.util.Vector (); 396 } 397 } 398 399 if (!resultSuccessFullyFormated) { 401 rsv.moveFirst(); 402 403 for (int i = 0; i < rsv.size(); i++) { 404 405 String [] currentRow = rsv.getCurrentRow(); 406 String htKey = currentRow[0]; 407 StringBuffer htValueBuf = new StringBuffer (); 408 409 for (int j = 1; j < currentRow.length; j++) { 410 htValueBuf.append(currentRow[j]); 411 412 if (j < (currentRow.length - 1)) { 413 htValueBuf.append(", "); 414 } 415 } 416 417 String htValue = htValueBuf.toString(); 419 result.add(new KeyValuePair(htKey, htValue)); 420 rsv.moveNext(); 421 } 422 } 423 424 return result; 425 } 426 427 428 433 protected boolean useCache() { 434 return !(Util.getTrue(this.getDisableCache())); 435 } 436 } 437 | Popular Tags |