1 23 package org.dbforms.event.datalist.dao; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.dbforms.config.DbEventInterceptorData; 28 import org.dbforms.config.DbFormsConfigRegistry; 29 import org.dbforms.config.Field; 30 import org.dbforms.config.FieldTypes; 31 import org.dbforms.config.FieldValue; 32 import org.dbforms.config.FieldValues; 33 import org.dbforms.config.ResultSetVector; 34 import org.dbforms.config.Table; 35 36 import org.dbforms.util.FileHolder; 37 import org.dbforms.util.Util; 38 39 import java.io.File ; 40 import java.io.IOException ; 41 42 import java.sql.Connection ; 43 import java.sql.SQLException ; 44 45 import java.util.Iterator ; 46 47 48 49 54 public abstract class DataSource { 55 56 private static Log logCat = LogFactory.getLog(DataSource.class); 57 private Table table; 58 59 62 public DataSource() { 63 } 64 65 75 public abstract void setSelect(FieldValue[] filterConstraint, 76 FieldValue[] orderConstraint, String sqlFilter, 77 FieldValue[] sqlFilterParams); 78 79 80 96 public ResultSetVector getCurrent(DbEventInterceptorData interceptorData, 97 String position, int count) throws SQLException { 98 try { 99 open(); 100 101 int start = findStartRow(position); 102 103 if (count == 0) { 104 count = size() - start; 105 } 106 107 return getResultSetVector(interceptorData, start, count); 108 } catch (Exception e) { 109 logCat.error("getCurrent", e); 110 close(); 111 112 return new ResultSetVector(); 113 } 114 } 115 116 117 126 public ResultSetVector getFirst(DbEventInterceptorData interceptorData, 127 int count) throws SQLException { 128 try { 129 open(); 130 131 if (count == 0) { 132 count = size(); 133 } 134 135 return getResultSetVector(interceptorData, 0, count); 136 } catch (Exception e) { 137 logCat.error("getFirst", e); 138 close(); 139 140 return new ResultSetVector(); 141 } 142 } 143 144 145 154 public ResultSetVector getLast(DbEventInterceptorData interceptorData, 155 int count) throws SQLException { 156 try { 157 open(); 158 159 return getResultSetVector(interceptorData, size() - 1, -count); 160 } catch (Exception e) { 161 logCat.error("getLast", e); 162 close(); 163 164 return new ResultSetVector(); 165 } 166 } 167 168 169 185 public ResultSetVector getNext(DbEventInterceptorData interceptorData, 186 String position, int count) throws SQLException { 187 try { 188 open(); 189 190 int start = findStartRow(position) + 1; 191 192 if (count == 0) { 193 count = size() - start; 194 } 195 196 return getResultSetVector(interceptorData, start, count); 197 } catch (Exception e) { 198 logCat.error("getNext", e); 199 close(); 200 201 return new ResultSetVector(); 202 } 203 } 204 205 206 222 public ResultSetVector getPrev(DbEventInterceptorData interceptorData, 223 String position, int count) throws SQLException { 224 try { 225 open(); 226 227 int start = findStartRow(position) - 1; 228 229 if (count == 0) { 230 count = start; 231 } 232 233 return getResultSetVector(interceptorData, start, -count); 234 } catch (Exception e) { 235 logCat.error("getPrev", e); 236 close(); 237 238 return new ResultSetVector(); 239 } 240 } 241 242 243 252 public void setSelect(String tableList, String whereClause) 253 throws SQLException { 254 throw new SQLException ("Free form select not implemented"); 255 } 256 257 258 263 public Table getTable() { 264 return table; 265 } 266 267 268 282 public void doDelete(DbEventInterceptorData interceptorData, 283 String keyValuesStr) throws SQLException { 284 } 285 286 287 295 public void doInsert(DbEventInterceptorData interceptorData, 296 FieldValues fieldValues) throws SQLException { 297 } 298 299 300 315 public void doUpdate(DbEventInterceptorData interceptorData, 316 FieldValues fieldValues, String keyValuesStr) throws SQLException { 317 } 318 319 320 329 protected void setConnection(Connection con, String dbConnectionName) { 330 } 331 332 333 342 protected abstract Object [] getRow(int i) throws SQLException ; 343 344 345 348 protected abstract void close(); 349 350 351 366 protected abstract int findStartRow(String startRow) 367 throws SQLException ; 368 369 370 381 protected abstract boolean hasMore(int i) throws SQLException ; 382 383 384 389 protected abstract void open() throws SQLException ; 390 391 392 399 protected abstract int size() throws SQLException ; 400 401 402 405 protected void setTable(Table table) { 406 this.table = table; 407 } 408 409 410 417 protected void deleteBlobFilesFromDisk(FieldValues fieldValues) 418 throws SQLException { 419 Iterator iter = fieldValues.keys(); 420 421 while (iter.hasNext()) { 422 String fieldName = (String ) iter.next(); 423 Field curField = table.getFieldByName(fieldName); 424 425 if (curField != null) { 426 int fieldType = curField.getType(); 427 428 String directory = null; 429 430 try { 431 directory = Util.replaceRealPath(curField.getDirectory(), 432 DbFormsConfigRegistry.instance().lookup().getRealPath()); 433 } catch (Exception e) { 434 logCat.error("deleteBlobFilesFromDisk", e); 435 throw new SQLException (e.getMessage()); 436 } 437 438 if (fieldType == FieldTypes.DISKBLOB) { 439 String fileName = fieldValues.get(fieldName).getFieldValue() 440 .trim(); 441 442 if (!Util.isNull(fileName)) { 444 File file = new File (directory, fileName); 446 447 if (file.exists()) { 448 file.delete(); 449 logCat.info("deleted file " + fileName + " from dir " 450 + directory); 451 } else { 452 logCat.info("delete of file " + fileName + " from dir " 453 + directory + " failed because file not found"); 454 } 455 } 456 } 457 } 458 } 459 } 460 461 462 470 protected void saveBlobFilesToDisk(FieldValues fieldValues) 471 throws SQLException { 472 Iterator iter = fieldValues.keys(); 473 474 while (iter.hasNext()) { 475 String fieldName = (String ) iter.next(); 476 Field curField = table.getFieldByName(fieldName); 477 478 if (curField != null) { 479 int fieldType = curField.getType(); 480 481 if (fieldType == FieldTypes.DISKBLOB) { 482 String directory = curField.getDirectory(); 483 484 if (directory == null) { 486 throw new IllegalArgumentException ( 487 "directory-attribute needed for fields of type DISKBLOB"); 488 } 489 490 try { 491 directory = Util.replaceRealPath(directory, 492 DbFormsConfigRegistry.instance().lookup().getRealPath()); 493 } catch (Exception e) { 494 logCat.error("saveBlobFilesToDisk", e); 495 } 496 497 File dir = new File (directory); 499 500 if (!dir.isDirectory()) { 502 throw new IllegalArgumentException ("Not a directory: " 503 + directory); 504 } 505 506 if (!dir.canWrite()) { 508 throw new IllegalArgumentException ("Not writable: " 509 + directory); 510 } 511 512 FileHolder fileHolder = fieldValues.get(fieldName).getFileHolder(); 514 515 if (fileHolder != null) { 516 try { 517 fileHolder.writeBufferToFile(dir); 518 519 logCat.info("fin + closedy"); 521 } catch (IOException ioe) { 522 logCat.error("saveBlobFilesToDisk", ioe); 524 throw new SQLException ("could not store file '" 525 + fileHolder.getFileName() + "' to dir '" + directory 526 + "'"); 527 } 528 } else { 529 logCat.info("uh! empty fileHolder"); 530 } 531 } 532 } 533 } 534 } 535 536 537 547 protected ResultSetVector getResultSetVector( 548 DbEventInterceptorData interceptorData, int startRow, int count) 549 throws SQLException { 550 ResultSetVector result = null; 551 result = new ResultSetVector(table); 552 553 int begin = 0; 554 int ende = 0; 555 556 Object [] row; 557 558 if (count > 0) { 559 begin = startRow; 560 561 for (ende = begin; ende < (startRow + count); ende++) { 562 row = getRow(ende); 563 564 if (row == null) { 565 break; 566 } 567 568 result.addRow(interceptorData, row); 569 } 570 } else if (count < 0) { 571 begin = startRow + count + 1; 572 573 if (begin < 0) { 574 begin = 0; 575 } 576 577 for (ende = begin; ende <= startRow; ende++) { 578 row = getRow(ende); 579 580 if (row == null) { 581 break; 582 } 583 584 result.addRow(interceptorData, row); 585 } 586 } 587 588 result.setFirstPage(!(begin > 0)); 589 result.setLastPage(!hasMore(ende)); 590 591 return result; 592 } 593 } 594 | Popular Tags |