1 package dinamica; 2 3 import java.util.HashMap ; 4 import javax.naming.*; 5 import javax.sql.DataSource ; 6 7 8 19 public class GenericTransaction extends AbstractModule 20 { 21 22 23 private HashMap _publish = new HashMap (); 24 25 30 protected void publish(String key, Recordset data) throws Throwable 31 { 32 _publish.put(key, data); 33 34 35 data.setID(key); 36 Recordset info = data.getRecordsetInfo(); 37 38 39 String infoID = key + ".metadata"; 40 _publish.put(infoID, info); 41 42 } 43 44 59 public int service(Recordset inputParams) throws Throwable 60 { 61 int rc = createRecordsets(inputParams); 62 63 if (inputParams!=null) 64 _publish.put("_request", inputParams); 65 66 return rc; 67 68 } 69 70 78 protected int createRecordsets(Recordset inputParams) throws Throwable 79 { 80 81 int rc = 0; 82 83 84 Db db = getDb(); 85 86 87 Recordset rs = _config.getRecordsets(); 88 Recordset rs1 = null; 89 90 91 while (rs.next()) 92 { 93 94 95 String id = (String )rs.getValue("id"); 96 String source = (String )rs.getValue("source"); 97 String scope = (String )rs.getValue("scope"); 98 String onempty = (String )rs.getValue("onempty"); 99 String maxRows = (String )rs.getValue("maxrows"); 100 int limit = 0; 101 if (maxRows!=null) 102 limit = Integer.parseInt(maxRows); 103 String dataSrc = rs.getString("datasource"); 104 String params = rs.getString("params"); 105 106 107 if (source.equals("sql")) 108 { 109 110 String sqlFile = getResource(id); 111 TemplateEngine t = new TemplateEngine(_ctx, _req, sqlFile); 112 if (params==null) 113 sqlFile = t.getSql(inputParams); 114 else { 115 Recordset rsParams = getRecordset(params); 117 if (rsParams.getRecordCount()>0) 118 rsParams.first(); 119 else 120 throw new Throwable ("The recordset [" + params + "] used to replace SQL template values is empty."); 121 sqlFile = t.getSql(rsParams); 122 } 123 124 if (dataSrc==null) 126 { 127 if (limit>0) 128 rs1 = db.get(sqlFile, limit); 129 else 130 rs1 = db.get(sqlFile); 131 } 132 else 133 { 134 rs1 = dbGet(dataSrc, sqlFile, limit); 135 } 136 137 if (onempty!=null) 138 { 139 if (rs1.getRecordCount()==0) 140 rc = Integer.parseInt(onempty); 141 } 142 } 143 else if (source.equals("session")) 144 { 145 rs1 = (Recordset)getSession().getAttribute(id); 146 147 if (rs1==null) 149 throw new Throwable ("Recordset [" + id + "] not found in Session attribute, maybe the application was reloaded, destroying the session."); 150 } 151 else if (source.equals("request")) 152 { 153 rs1 = (Recordset)_req.getAttribute(id); 154 } 155 else if (source.equals("textfile")) 156 { 157 rs1 = this.getRsFromFlatFile(id); 158 } 159 else if (source.equals("class")) 160 { 161 IRecordsetProvider rsProv = (IRecordsetProvider)getObject(id); 162 rs1 = rsProv.getRecordset(inputParams); 163 } 164 else 165 { 166 throw new Throwable ("Invalid recordset source in config.xml (" + _config.path + "). Source attribute values can be sql, session, textfile or request only."); 167 } 168 169 170 _publish.put(id, rs1); 171 172 173 rs1.setID(id); 174 Recordset info = rs1.getRecordsetInfo(); 175 176 177 String infoID = id + ".metadata"; 178 _publish.put(infoID, info); 179 180 181 if (scope.equals("session")) 182 { 183 getSession().setAttribute(id, rs1); 184 } 185 else if (scope.equals("request")) 186 { 187 _req.setAttribute(id, rs1); 188 } 189 else if (!scope.equals("transaction")) 190 { 191 throw new Throwable ("Invalid recordset scope in config.xml (" + _config.path + "). Scope attribute values can be transaction, session or request only."); 192 } 193 194 } 195 196 return rc; 197 } 198 199 206 public Recordset getRecordset(String id) throws Throwable 207 { 208 if (_publish.containsKey(id)) 209 { 210 return (Recordset)_publish.get(id); 211 } 212 else 213 { 214 throw new Throwable ("Invalid recordset ID: " + id); 215 } 216 } 217 218 227 protected String getSQL(String sql, Recordset rs) throws Throwable 228 { 229 230 TemplateEngine t = new TemplateEngine(_ctx,_req, sql); 231 return t.getSql(rs); 232 233 } 234 235 260 protected GenericTransaction getObject(String className) throws Throwable 261 { 262 263 GenericTransaction t = null; 264 265 266 t = (GenericTransaction) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance(); 267 t.init(_ctx, _req, _res); 268 t.setConfig(_config); 269 t.setConnection(_conn); 270 271 272 t.setLogWriter(_pw); 273 274 return t; 275 276 } 277 278 286 public Recordset getChartInfoRecordset() throws Throwable 287 { 288 289 Recordset rs = new Recordset(); 290 rs.append("chart-plugin", java.sql.Types.VARCHAR); 291 rs.append("title", java.sql.Types.VARCHAR); 292 rs.append("title-x", java.sql.Types.VARCHAR); 293 rs.append("title-y", java.sql.Types.VARCHAR); 294 rs.append("column-x", java.sql.Types.VARCHAR); 295 rs.append("column-y", java.sql.Types.VARCHAR); 296 rs.append("title-series", java.sql.Types.VARCHAR); 297 rs.append("width", java.sql.Types.INTEGER); 298 rs.append("height", java.sql.Types.INTEGER); 299 rs.append("data", java.sql.Types.VARCHAR); 300 rs.append("dateformat", java.sql.Types.VARCHAR); 301 302 rs.append("session", java.sql.Types.VARCHAR); rs.append("image-id", java.sql.Types.VARCHAR); 306 rs.append("color", java.sql.Types.VARCHAR); 309 return rs; 310 } 311 312 331 protected DataSource getDataSource(String name) throws Throwable 332 { 333 334 String jndiPrefix = ""; 336 if (getContext()!=null) 337 jndiPrefix = getContext().getInitParameter("jndi-prefix"); 338 else 339 jndiPrefix = "java:comp/env/"; 340 341 if (jndiPrefix==null) 342 jndiPrefix=""; 343 344 DataSource ds = Jndi.getDataSource(jndiPrefix + name); 345 if (ds==null) 346 throw new Throwable ("Can't get datasource: " + name); 347 348 return ds; 349 350 } 351 352 370 protected DataSource getDataSource() throws Throwable 371 { 372 373 String jndiPrefix = null; 375 String name = null; 376 377 if (getContext()!=null) 378 { 379 380 if (getConfig().transDataSource!=null) 381 name = getConfig().transDataSource; 382 else 383 name = getContext().getInitParameter("def-datasource"); 384 385 jndiPrefix = getContext().getInitParameter("jndi-prefix"); 386 387 } 388 else 389 throw new Throwable ("This method can't return a datasource if servlet the context is null."); 390 391 if (jndiPrefix==null) 392 jndiPrefix=""; 393 394 DataSource ds = Jndi.getDataSource(jndiPrefix + name); 395 if (ds==null) 396 throw new Throwable ("Can't get datasource: " + name); 397 398 return ds; 399 400 } 401 402 406 protected String getEnvEntry(String name) throws Throwable 407 { 408 409 Context env = (Context) new InitialContext().lookup("java:comp/env"); 410 String v = (String ) env.lookup(name); 411 return v; 412 413 } 414 415 421 public HashMap getData() 422 { 423 return _publish; 424 } 425 426 435 protected Recordset dbGet(String DataSourceName, String sql, int limit) throws Throwable 436 { 437 java.sql.Connection conn = getDataSource(DataSourceName).getConnection(); 438 try 439 { 440 Db db = new Db(conn); 441 if (limit>0) 442 return db.get(sql, limit); 443 else 444 return db.get(sql); 445 } 446 catch (Throwable e) 447 { 448 throw e; 449 } 450 finally 451 { 452 if (conn!=null) 453 conn.close(); 454 } 455 } 456 457 465 protected Recordset dbGet(String DataSourceName, String sql) throws Throwable 466 { 467 return dbGet(DataSourceName, sql, 0); 468 } 469 470 480 protected Recordset getRsFromFlatFile(String path) throws Throwable 481 { 482 Recordset rs = new Recordset(); 483 String data = getResource(path); 484 String rows[] = StringUtil.split(data, "\r\n"); 485 String listseparator = "\t"; 486 487 if (rows[0].indexOf(",") != -1) 489 listseparator = ","; 490 else 491 if (rows[0].indexOf(";") != -1) 492 listseparator = ";"; 493 494 String fields[] = StringUtil.split(rows[0], listseparator); 495 String names[] = StringUtil.split(rows[1], listseparator); 496 boolean is_blank_row = false; 497 498 if (fields.length!=names.length) 499 throw new Throwable ("Row #2 (column names) does not match the right number of columns."); 500 501 for (int i=0;i<fields.length;i++) 502 { 503 if (fields[i].toLowerCase().equals("varchar")) 504 rs.append(names[i], java.sql.Types.VARCHAR); 505 else if (fields[i].toLowerCase().equals("date")) 506 rs.append(names[i], java.sql.Types.DATE); 507 else if (fields[i].toLowerCase().equals("integer")) 508 rs.append(names[i], java.sql.Types.INTEGER); 509 else if (fields[i].toLowerCase().equals("double")) 510 rs.append(names[i], java.sql.Types.DOUBLE); 511 else { 512 throw new Throwable ("Invalid column type [" + fields[i] +"]. Valid column types are: varchar, date, integer and double."); 513 } 514 515 } 516 517 for (int i=2;i<rows.length;i++) 518 { 519 if(rows[i].equals("")) 521 continue; 522 523 if(!is_blank_row) 525 rs.addNew(); 526 527 is_blank_row = true; 529 530 String value[] = StringUtil.split(rows[i], listseparator); 531 532 535 for (int j=0;j<Math.min(fields.length,value.length);j++) 536 { 537 value[j] = getSQL(value[j],null); 539 540 if(value[j].equals("")||value[j].toLowerCase().equals("null")) 542 { 543 rs.setValue(names[j], null); 544 } 545 else 546 { 547 is_blank_row = false; 548 if (fields[j].toLowerCase().equals("varchar")) 549 rs.setValue(names[j], value[j]); 550 else if (fields[j].toLowerCase().equals("date")) 551 { 552 if(value[j].indexOf("@") != -1) { 554 String date[] = StringUtil.split(value[j], "@"); 555 rs.setValue(names[j], StringUtil.getDateObject(date[0],date[1])); 556 } 557 else 558 { 559 rs.setValue(names[j], StringUtil.getDateObject(value[j],"yyyy-MM-dd")); 560 } 561 } 562 else if (fields[j].toLowerCase().equals("integer")) 563 rs.setValue(names[j], new Integer (value[j])); 564 else if (fields[j].toLowerCase().equals("double")) 565 rs.setValue(names[j], new Double (value[j])); 566 } 567 } 568 } 569 570 if(is_blank_row) 572 rs.delete(rs.getRecordNumber()); 573 574 return rs; 575 } 576 577 } 578 | Popular Tags |