| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
package org.melati.poem; |
| 46 | |
|
| 47 | |
import java.io.ByteArrayOutputStream; |
| 48 | |
import java.io.PrintStream; |
| 49 | |
import java.text.DateFormat; |
| 50 | |
import java.util.Enumeration; |
| 51 | |
import java.util.Map; |
| 52 | |
import java.util.Vector; |
| 53 | |
|
| 54 | |
import org.melati.poem.transaction.Transaction; |
| 55 | |
import org.melati.poem.transaction.Transactioned; |
| 56 | |
import org.melati.poem.util.FlattenedEnumeration; |
| 57 | |
import org.melati.poem.util.MappedEnumeration; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public class JdbcPersistent extends Transactioned implements Persistent, Cloneable { |
| 68 | |
private Table<?> table; |
| 69 | |
private Integer troid; |
| 70 | |
private AccessToken clearedToken; |
| 71 | 8357 | private boolean |
| 72 | |
knownCanRead = false, knownCanWrite = false, knownCanDelete = false; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | 8357 | private boolean dirty = false; |
| 85 | |
|
| 86 | |
private static final int NONEXISTENT = 0, EXISTENT = 1, DELETED = 2; |
| 87 | 8357 | private int status = NONEXISTENT; |
| 88 | |
|
| 89 | 8357 | private Object[] extras = null; |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | 8339 | public JdbcPersistent() { |
| 94 | 8339 | } |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public JdbcPersistent(JdbcTable<?> table, Integer troid) { |
| 102 | 16 | super(table.getDatabase()); |
| 103 | 16 | this.table = table; |
| 104 | 16 | this.troid = troid; |
| 105 | 16 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public JdbcPersistent(String tableName, String troidString) { |
| 113 | 2 | super(PoemThread.database()); |
| 114 | 2 | this.table = PoemThread.database().getTable(tableName); |
| 115 | 2 | this.troid = new Integer(troidString); |
| 116 | 2 | } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
final void setStatusNonexistent() { |
| 125 | 7320 | status = NONEXISTENT; |
| 126 | 7320 | } |
| 127 | |
|
| 128 | |
final void setStatusExistent() { |
| 129 | 7090 | status = EXISTENT; |
| 130 | 7090 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public final boolean statusNonexistent() { |
| 137 | 6011 | return status == NONEXISTENT; |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public final boolean statusExistent() { |
| 145 | 120785 | return status == EXISTENT; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
private void assertNotFloating() { |
| 158 | 17544 | if (troid == null) |
| 159 | 14 | throw new InvalidOperationOnFloatingPersistentPoemException(this); |
| 160 | 17530 | } |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
private void assertNotDeleted() { |
| 166 | 34511 | if (status == DELETED) |
| 167 | 14 | throw new RowDisappearedPoemException(this); |
| 168 | 34497 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
protected void load(Transaction transaction) { |
| 177 | 827 | if (troid == null) |
| 178 | 0 | throw new InvalidOperationOnFloatingPersistentPoemException(this); |
| 179 | |
|
| 180 | 827 | table.load((PoemTransaction)transaction, this); |
| 181 | |
|
| 182 | 827 | } |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
protected boolean upToDate(Transaction transaction) { |
| 193 | 827 | return valid; |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
protected void writeDown(Transaction transaction) { |
| 205 | 3236 | if (status != DELETED) { |
| 206 | 2346 | assertNotFloating(); |
| 207 | 2346 | table.writeDown((PoemTransaction)transaction, this); |
| 208 | |
|
| 209 | |
} |
| 210 | 3236 | } |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
protected void writeLock(Transaction transaction) { |
| 217 | 119567 | if (troid != null) { |
| 218 | 1471 | super.writeLock(transaction); |
| 219 | 1469 | assertNotDeleted(); |
| 220 | 1456 | dirty = true; |
| 221 | 1456 | table.notifyTouched((PoemTransaction)transaction, this); |
| 222 | |
} |
| 223 | 119552 | } |
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
protected void readLock(Transaction transaction) { |
| 229 | 52133 | if (troid != null) { |
| 230 | 33040 | super.readLock(transaction); |
| 231 | 33040 | assertNotDeleted(); |
| 232 | |
} |
| 233 | 52132 | } |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
protected void commit(Transaction transaction) { |
| 243 | |
|
| 244 | 570 | assertNotFloating(); |
| 245 | 570 | super.commit(transaction); |
| 246 | |
|
| 247 | 570 | } |
| 248 | |
|
| 249 | |
protected void rollback(Transaction transaction) { |
| 250 | |
|
| 251 | 9 | assertNotFloating(); |
| 252 | 9 | if (status == DELETED) |
| 253 | 8 | status = EXISTENT; |
| 254 | 9 | super.rollback(transaction); |
| 255 | |
|
| 256 | 9 | } |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
public void makePersistent() { |
| 269 | 159 | getTable().create(this); |
| 270 | 150 | } |
| 271 | |
|
| 272 | |
|
| 273 | |
synchronized Object[] extras() { |
| 274 | 420 | if (extras == null) |
| 275 | 46 | extras = new Object[table.extrasCount()]; |
| 276 | 374 | else if (extras.length < table.extrasCount() ) { |
| 277 | 0 | Object[] newExtras = new Object[table.extrasCount()]; |
| 278 | 0 | System.arraycopy(extras, 0, newExtras, 0, extras.length); |
| 279 | 0 | extras = newExtras; |
| 280 | |
} |
| 281 | 420 | return extras; |
| 282 | |
} |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
public final Table<?> getTable() { |
| 289 | 149328 | return table; |
| 290 | |
} |
| 291 | |
|
| 292 | |
synchronized void setTable(JdbcTable<?> table, Integer troid) { |
| 293 | 15896 | setTransactionPool(table.getDatabase()); |
| 294 | 15896 | this.table = table; |
| 295 | 15896 | this.troid = troid; |
| 296 | 15896 | } |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
public final Database getDatabase() { |
| 304 | 5960 | return table.getDatabase(); |
| 305 | |
} |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
public final Integer troid() { |
| 320 | 46080 | return troid; |
| 321 | |
} |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
public final Integer getTroid() throws AccessPoemException { |
| 339 | 43 | assertCanRead(); |
| 340 | 43 | return troid(); |
| 341 | |
} |
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
protected void existenceLock(SessionToken sessionToken) { |
| 350 | 12721 | super.readLock(sessionToken.transaction); |
| 351 | 12721 | } |
| 352 | |
|
| 353 | |
protected void readLock(SessionToken sessionToken) |
| 354 | |
throws AccessPoemException { |
| 355 | 46128 | assertCanRead(sessionToken.accessToken); |
| 356 | 46127 | readLock(sessionToken.transaction); |
| 357 | 46126 | } |
| 358 | |
|
| 359 | |
protected void writeLock(SessionToken sessionToken) |
| 360 | |
throws AccessPoemException { |
| 361 | 119321 | if (troid != null) |
| 362 | 1225 | assertCanWrite(sessionToken.accessToken); |
| 363 | 119320 | writeLock(sessionToken.transaction); |
| 364 | 119319 | } |
| 365 | |
|
| 366 | |
protected void deleteLock(SessionToken sessionToken) |
| 367 | |
throws AccessPoemException { |
| 368 | 247 | if (troid != null) |
| 369 | 247 | assertCanDelete(sessionToken.accessToken); |
| 370 | 247 | writeLock(sessionToken.transaction); |
| 371 | 233 | } |
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
public void existenceLock() { |
| 378 | 6111 | existenceLock(PoemThread.sessionToken()); |
| 379 | 6111 | } |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
protected void readLock() throws AccessPoemException { |
| 386 | 46128 | readLock(PoemThread.sessionToken()); |
| 387 | 46126 | } |
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
protected void writeLock() throws AccessPoemException { |
| 394 | 119321 | writeLock(PoemThread.sessionToken()); |
| 395 | 119319 | } |
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
protected Capability getCanRead() { |
| 413 | 321 | return null; |
| 414 | |
} |
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
public void assertCanRead(AccessToken token) |
| 422 | |
throws AccessPoemException { |
| 423 | |
|
| 424 | 399 | if (!(clearedToken == token && knownCanRead) && troid != null) { |
| 425 | 325 | Capability canRead = getCanRead(); |
| 426 | 325 | if (canRead == null) |
| 427 | 322 | canRead = getTable().getDefaultCanRead(); |
| 428 | 325 | if (canRead != null) { |
| 429 | 9 | if (!token.givesCapability(canRead)) |
| 430 | 4 | throw new ReadPersistentAccessPoemException(this, token, canRead); |
| 431 | 5 | if (clearedToken != token) { |
| 432 | 5 | knownCanWrite = false; |
| 433 | 5 | knownCanDelete = false; |
| 434 | |
} |
| 435 | 5 | clearedToken = token; |
| 436 | 5 | knownCanRead = true; |
| 437 | |
} |
| 438 | |
} |
| 439 | 395 | } |
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
public final void assertCanRead() throws AccessPoemException { |
| 446 | 54 | assertCanRead(PoemThread.accessToken()); |
| 447 | 52 | } |
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
public final boolean getReadable() { |
| 455 | |
try { |
| 456 | 2 | assertCanRead(); |
| 457 | 1 | return true; |
| 458 | |
} |
| 459 | 1 | catch (AccessPoemException e) { |
| 460 | 1 | return false; |
| 461 | |
} |
| 462 | |
} |
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
protected Capability getCanWrite() { |
| 479 | 921 | return null; |
| 480 | |
} |
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
public void assertCanWrite(AccessToken token) |
| 488 | |
throws AccessPoemException { |
| 489 | |
|
| 490 | 1235 | if (!(clearedToken == token && knownCanWrite) && troid != null) { |
| 491 | 922 | Capability canWrite = getCanWrite(); |
| 492 | 922 | if (canWrite == null) |
| 493 | 921 | canWrite = getTable().getDefaultCanWrite(); |
| 494 | 922 | if (canWrite != null) { |
| 495 | 161 | if (!token.givesCapability(canWrite)) |
| 496 | 5 | throw new WritePersistentAccessPoemException(this, token, canWrite); |
| 497 | 156 | if (clearedToken != token) { |
| 498 | 154 | knownCanRead = false; |
| 499 | 154 | knownCanDelete = false; |
| 500 | |
} |
| 501 | 156 | clearedToken = token; |
| 502 | 156 | knownCanWrite = true; |
| 503 | |
} |
| 504 | |
} |
| 505 | 1230 | } |
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
public final void assertCanWrite() throws AccessPoemException { |
| 512 | 4 | assertCanWrite(PoemThread.accessToken()); |
| 513 | 2 | } |
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
|
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | |
|
| 529 | |
protected Capability getCanDelete() { |
| 530 | 122 | return null; |
| 531 | |
} |
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
|
| 537 | |
|
| 538 | |
public void assertCanDelete(AccessToken token) |
| 539 | |
throws AccessPoemException { |
| 540 | |
|
| 541 | 255 | if (!(clearedToken == token && knownCanDelete) && troid != null) { |
| 542 | 126 | Capability canDelete = getCanDelete(); |
| 543 | 126 | if (canDelete == null) |
| 544 | 122 | canDelete = getTable().getDefaultCanDelete(); |
| 545 | 126 | if (canDelete != null) { |
| 546 | 122 | if (!token.givesCapability(canDelete)) |
| 547 | 4 | throw new DeletePersistentAccessPoemException(this, token, canDelete); |
| 548 | 118 | if (clearedToken != token) { |
| 549 | 96 | knownCanRead = false; |
| 550 | 96 | knownCanWrite = false; |
| 551 | |
} |
| 552 | 118 | clearedToken = token; |
| 553 | 118 | knownCanDelete = true; |
| 554 | |
} |
| 555 | |
} |
| 556 | 251 | } |
| 557 | |
|
| 558 | |
|
| 559 | |
|
| 560 | |
|
| 561 | |
|
| 562 | |
public final void assertCanDelete() throws AccessPoemException { |
| 563 | 4 | assertCanDelete(PoemThread.accessToken()); |
| 564 | 2 | } |
| 565 | |
|
| 566 | |
|
| 567 | |
|
| 568 | |
|
| 569 | |
|
| 570 | |
|
| 571 | |
|
| 572 | |
|
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | |
|
| 577 | |
protected Capability getCanSelect() { |
| 578 | 0 | return null; |
| 579 | |
} |
| 580 | |
|
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
public void assertCanCreate(AccessToken token) { |
| 587 | 6021 | Capability canCreate = getTable().getCanCreate(); |
| 588 | 6021 | if (canCreate != null && !token.givesCapability(canCreate)) |
| 589 | 4 | throw new CreationAccessPoemException(getTable(), token, canCreate); |
| 590 | 6017 | } |
| 591 | |
|
| 592 | |
|
| 593 | |
|
| 594 | |
|
| 595 | |
|
| 596 | |
public final void assertCanCreate() throws AccessPoemException { |
| 597 | 4 | assertCanCreate(PoemThread.accessToken()); |
| 598 | 2 | } |
| 599 | |
|
| 600 | |
|
| 601 | |
|
| 602 | |
|
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
|
| 609 | |
|
| 610 | |
|
| 611 | |
|
| 612 | |
|
| 613 | |
|
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
public Object getRaw(String name) |
| 618 | |
throws NoSuchColumnPoemException, AccessPoemException { |
| 619 | 57 | return getTable().getColumn(name).getRaw(this); |
| 620 | |
} |
| 621 | |
|
| 622 | |
|
| 623 | |
|
| 624 | |
|
| 625 | |
|
| 626 | |
|
| 627 | |
public final String getRawString(String name) |
| 628 | |
throws AccessPoemException, NoSuchColumnPoemException { |
| 629 | 22 | Column<?> column = getTable().getColumn(name); |
| 630 | 22 | return column.getType().stringOfRaw(column.getRaw(this)); |
| 631 | |
} |
| 632 | |
|
| 633 | |
|
| 634 | |
|
| 635 | |
|
| 636 | |
|
| 637 | |
|
| 638 | |
public void setRaw(String name, Object raw) |
| 639 | |
throws NoSuchColumnPoemException, AccessPoemException, |
| 640 | |
ValidationPoemException { |
| 641 | 6 | getTable().getColumn(name).setRaw(this, raw); |
| 642 | 6 | } |
| 643 | |
|
| 644 | |
|
| 645 | |
|
| 646 | |
|
| 647 | |
|
| 648 | |
|
| 649 | |
public final void setRawString(String name, String string) |
| 650 | |
throws NoSuchColumnPoemException, AccessPoemException, |
| 651 | |
ParsingPoemException, ValidationPoemException { |
| 652 | 4 | Column<?> column = getTable().getColumn(name); |
| 653 | 4 | column.setRaw(this, column.getType().rawOfString(string)); |
| 654 | 4 | } |
| 655 | |
|
| 656 | |
|
| 657 | |
|
| 658 | |
|
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | |
|
| 663 | |
|
| 664 | |
|
| 665 | |
|
| 666 | |
|
| 667 | |
public Object getCooked(String name) |
| 668 | |
throws NoSuchColumnPoemException, AccessPoemException { |
| 669 | 62 | return getTable().getColumn(name).getCooked(this); |
| 670 | |
} |
| 671 | |
|
| 672 | |
|
| 673 | |
|
| 674 | |
|
| 675 | |
|
| 676 | |
|
| 677 | |
public final String getCookedString(String name, PoemLocale locale, |
| 678 | |
int style) |
| 679 | |
throws NoSuchColumnPoemException, AccessPoemException { |
| 680 | 8 | Column<?> column = getTable().getColumn(name); |
| 681 | 8 | return column.getType().stringOfCooked(column.getCooked(this), |
| 682 | |
locale, style); |
| 683 | |
} |
| 684 | |
|
| 685 | |
|
| 686 | |
|
| 687 | |
|
| 688 | |
|
| 689 | |
|
| 690 | |
public void setCooked(String name, Object cooked) |
| 691 | |
throws NoSuchColumnPoemException, ValidationPoemException, |
| 692 | |
AccessPoemException { |
| 693 | 67 | getTable().getColumn(name).setCooked(this, cooked); |
| 694 | 67 | } |
| 695 | |
|
| 696 | |
|
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
|
| 701 | |
|
| 702 | |
|
| 703 | |
|
| 704 | |
|
| 705 | |
|
| 706 | |
public final Field<?> getField(String name) |
| 707 | |
throws NoSuchColumnPoemException, AccessPoemException { |
| 708 | 8 | return getTable().getColumn(name).asField(this); |
| 709 | |
} |
| 710 | |
|
| 711 | |
|
| 712 | |
|
| 713 | |
|
| 714 | |
|
| 715 | |
public Enumeration<Field<?>> fieldsOfColumns(Enumeration<Column<?>> columns) { |
| 716 | 28 | final JdbcPersistent _this = this; |
| 717 | 28 | return |
| 718 | 168 | new MappedEnumeration<Field<?>, Column<?>>(columns) { |
| 719 | |
public Field<?> mapped(Column<?> column) { |
| 720 | 140 | return column.asField(_this); |
| 721 | |
} |
| 722 | |
}; |
| 723 | |
} |
| 724 | |
|
| 725 | |
|
| 726 | |
|
| 727 | |
|
| 728 | |
|
| 729 | |
|
| 730 | |
public Enumeration<Field<?>> getFields() { |
| 731 | 4 | return fieldsOfColumns(getTable().columns()); |
| 732 | |
} |
| 733 | |
|
| 734 | |
|
| 735 | |
|
| 736 | |
|
| 737 | |
|
| 738 | |
|
| 739 | |
public Enumeration<Field<?>> getRecordDisplayFields() { |
| 740 | 16 | return fieldsOfColumns(getTable().getRecordDisplayColumns()); |
| 741 | |
} |
| 742 | |
|
| 743 | |
|
| 744 | |
|
| 745 | |
|
| 746 | |
|
| 747 | |
public Enumeration<Field<?>> getDetailDisplayFields() { |
| 748 | 2 | return fieldsOfColumns(getTable().getDetailDisplayColumns()); |
| 749 | |
} |
| 750 | |
|
| 751 | |
|
| 752 | |
|
| 753 | |
|
| 754 | |
|
| 755 | |
public Enumeration<Field<?>> getSummaryDisplayFields() { |
| 756 | 2 | return fieldsOfColumns(getTable().getSummaryDisplayColumns()); |
| 757 | |
} |
| 758 | |
|
| 759 | |
|
| 760 | |
|
| 761 | |
|
| 762 | |
|
| 763 | |
public Enumeration<Field<?>> getSearchCriterionFields() { |
| 764 | 2 | return fieldsOfColumns(getTable().getSearchCriterionColumns()); |
| 765 | |
} |
| 766 | |
|
| 767 | |
|
| 768 | |
|
| 769 | |
|
| 770 | |
|
| 771 | |
public Field<?> getPrimaryDisplayField() { |
| 772 | 1 | return getTable().displayColumn().asField(this); |
| 773 | |
} |
| 774 | |
|
| 775 | |
|
| 776 | |
|
| 777 | |
|
| 778 | |
|
| 779 | |
|
| 780 | |
|
| 781 | |
|
| 782 | |
|
| 783 | |
|
| 784 | |
|
| 785 | |
public void delete(Map<Column<?>, IntegrityFix> columnToIntegrityFix) { |
| 786 | |
|
| 787 | 134 | assertNotFloating(); |
| 788 | |
|
| 789 | 126 | deleteLock(PoemThread.sessionToken()); |
| 790 | |
|
| 791 | 115 | Enumeration<Column<?>> columns = getDatabase().referencesTo(getTable()); |
| 792 | 115 | Vector<Enumeration<Persistent>> refEnumerations = new Vector<Enumeration<Persistent>>(); |
| 793 | |
|
| 794 | 291 | while (columns.hasMoreElements()) { |
| 795 | 176 | Column<?> column = columns.nextElement(); |
| 796 | |
|
| 797 | |
IntegrityFix fix; |
| 798 | |
try { |
| 799 | 176 | fix = columnToIntegrityFix == null ? |
| 800 | 0 | null : columnToIntegrityFix.get(column); |
| 801 | |
} |
| 802 | 0 | catch (ClassCastException e) { |
| 803 | 0 | throw new AppBugPoemException( |
| 804 | |
"columnToIntegrityFix argument to Persistent.deleteAndCommit " + |
| 805 | |
"is meant to be a Map from Column to IntegrityFix", |
| 806 | |
e); |
| 807 | 176 | } |
| 808 | |
|
| 809 | 176 | if (fix == null) |
| 810 | 176 | fix = column.getIntegrityFix(); |
| 811 | |
|
| 812 | 176 | if (column.getType() instanceof ReferencePoemType) |
| 813 | 338 | refEnumerations.addElement( |
| 814 | 169 | fix.referencesTo(this, column, column.selectionWhereEq(troid()), |
| 815 | |
columnToIntegrityFix)); |
| 816 | 7 | else if (column.getType() instanceof StringKeyReferencePoemType) { |
| 817 | 7 | getDatabase().log("Found a StringKeyReferencePoemType " + getName()); |
| 818 | 7 | String keyName = ((StringKeyReferencePoemType)column.getType()).targetKeyName(); |
| 819 | 7 | String keyValue = (String)getRaw(keyName); |
| 820 | 7 | if (keyValue != null) |
| 821 | 0 | refEnumerations.addElement( |
| 822 | 0 | fix.referencesTo(this, column, column.selectionWhereEq(keyValue), |
| 823 | |
columnToIntegrityFix)); |
| 824 | |
|
| 825 | 7 | } else |
| 826 | 0 | throw new UnexpectedExceptionPoemException( |
| 827 | |
"Only expecting Reference or StringKeyReferences"); |
| 828 | 176 | } |
| 829 | |
|
| 830 | 115 | Enumeration<Persistent> refs = new FlattenedEnumeration<Persistent>(refEnumerations.elements()); |
| 831 | |
|
| 832 | 115 | if (refs.hasMoreElements()) |
| 833 | 1 | throw new DeletionIntegrityPoemException(this, refs); |
| 834 | |
|
| 835 | 114 | delete_unsafe(); |
| 836 | 114 | } |
| 837 | |
|
| 838 | |
|
| 839 | |
|
| 840 | |
|
| 841 | |
|
| 842 | |
public void delete_unsafe() { |
| 843 | 123 | assertNotFloating(); |
| 844 | 121 | SessionToken sessionToken = PoemThread.sessionToken(); |
| 845 | 121 | deleteLock(sessionToken); |
| 846 | |
try { |
| 847 | 118 | status = DELETED; |
| 848 | 118 | table.delete(troid(), sessionToken.transaction); |
| 849 | 0 | } catch (PoemException e) { |
| 850 | 0 | status = EXISTENT; |
| 851 | 0 | throw e; |
| 852 | 118 | } |
| 853 | 118 | } |
| 854 | |
|
| 855 | |
|
| 856 | |
|
| 857 | |
|
| 858 | |
|
| 859 | |
|
| 860 | |
public final void delete() { |
| 861 | 118 | delete(null); |
| 862 | 106 | } |
| 863 | |
|
| 864 | |
|
| 865 | |
|
| 866 | |
|
| 867 | |
|
| 868 | |
public void deleteAndCommit(Map<Column<?>, IntegrityFix> integrityFixOfColumn) |
| 869 | |
throws AccessPoemException, DeletionIntegrityPoemException { |
| 870 | |
|
| 871 | 13 | getDatabase().beginExclusiveLock(); |
| 872 | |
try { |
| 873 | 13 | delete(integrityFixOfColumn); |
| 874 | 5 | PoemThread.commit(); |
| 875 | |
} |
| 876 | 8 | catch (RuntimeException e) { |
| 877 | 8 | PoemThread.rollback(); |
| 878 | 8 | throw e; |
| 879 | |
} |
| 880 | |
finally { |
| 881 | 13 | getDatabase().endExclusiveLock(); |
| 882 | 5 | } |
| 883 | 5 | } |
| 884 | |
|
| 885 | |
|
| 886 | |
|
| 887 | |
|
| 888 | |
|
| 889 | |
public final void deleteAndCommit() |
| 890 | |
throws AccessPoemException, DeletionIntegrityPoemException { |
| 891 | 7 | deleteAndCommit(null); |
| 892 | 3 | } |
| 893 | |
|
| 894 | |
|
| 895 | |
|
| 896 | |
|
| 897 | |
|
| 898 | |
public Persistent duplicated() throws AccessPoemException { |
| 899 | 4 | assertNotFloating(); |
| 900 | 2 | assertNotDeleted(); |
| 901 | 2 | return (JdbcPersistent)clone(); |
| 902 | |
} |
| 903 | |
|
| 904 | |
|
| 905 | |
|
| 906 | |
|
| 907 | |
|
| 908 | |
public Persistent duplicatedFloating() throws AccessPoemException { |
| 909 | 4 | return (JdbcPersistent)clone(); |
| 910 | |
} |
| 911 | |
|
| 912 | |
|
| 913 | |
|
| 914 | |
|
| 915 | |
|
| 916 | |
|
| 917 | |
|
| 918 | |
|
| 919 | |
|
| 920 | |
|
| 921 | |
|
| 922 | |
|
| 923 | |
|
| 924 | |
public String toString() { |
| 925 | 57 | if (getTable() == null) { |
| 926 | 2 | return "null/" + troid(); |
| 927 | |
} |
| 928 | 55 | return getTable().getName() + "/" + troid(); |
| 929 | |
} |
| 930 | |
|
| 931 | |
|
| 932 | |
|
| 933 | |
|
| 934 | |
|
| 935 | |
public String displayString(PoemLocale locale, int style) |
| 936 | |
throws AccessPoemException { |
| 937 | 59 | Column<?> displayColumn = getTable().displayColumn(); |
| 938 | 59 | if (displayColumn.isTroidColumn() && this.troid == null) |
| 939 | 0 | return "null"; |
| 940 | |
else |
| 941 | 59 | return displayColumn.getType().stringOfCooked(displayColumn.getCooked(this), |
| 942 | |
locale, style); |
| 943 | |
} |
| 944 | |
|
| 945 | |
|
| 946 | |
|
| 947 | |
|
| 948 | |
|
| 949 | |
public String displayString(PoemLocale locale) |
| 950 | |
throws AccessPoemException { |
| 951 | 2 | return displayString(locale, DateFormat.MEDIUM); |
| 952 | |
} |
| 953 | |
|
| 954 | |
|
| 955 | |
|
| 956 | |
|
| 957 | |
public String displayString() |
| 958 | |
throws AccessPoemException { |
| 959 | 3 | return displayString(PoemLocale.HERE, DateFormat.MEDIUM); |
| 960 | |
} |
| 961 | |
|
| 962 | |
|
| 963 | |
|
| 964 | |
|
| 965 | |
|
| 966 | |
|
| 967 | |
|
| 968 | |
|
| 969 | |
|
| 970 | |
|
| 971 | |
|
| 972 | |
public final int hashCode() { |
| 973 | 4 | if (troid == null) |
| 974 | 2 | throw new InvalidOperationOnFloatingPersistentPoemException(this); |
| 975 | |
|
| 976 | 2 | return getTable().hashCode() + troid().intValue(); |
| 977 | |
} |
| 978 | |
|
| 979 | |
|
| 980 | |
|
| 981 | |
|
| 982 | |
|
| 983 | |
public final boolean equals(Object object) { |
| 984 | 32 | if (object == null || !(object instanceof Persistent)) |
| 985 | 4 | return false; |
| 986 | |
else { |
| 987 | 28 | JdbcPersistent other = (JdbcPersistent)object; |
| 988 | 28 | return other.troid() == troid() && |
| 989 | 20 | other.getTable() == getTable(); |
| 990 | |
} |
| 991 | |
} |
| 992 | |
|
| 993 | |
|
| 994 | |
|
| 995 | |
|
| 996 | |
|
| 997 | |
public synchronized void invalidate() { |
| 998 | 14358 | assertNotFloating(); |
| 999 | 14356 | super.invalidate(); |
| 1000 | 14356 | extras = null; |
| 1001 | 14356 | } |
| 1002 | |
|
| 1003 | |
|
| 1004 | |
|
| 1005 | |
|
| 1006 | |
|
| 1007 | |
protected Object clone() { |
| 1008 | |
|
| 1009 | 6 | assertCanRead(); |
| 1010 | |
JdbcPersistent it; |
| 1011 | |
try { |
| 1012 | 6 | it = (JdbcPersistent)super.clone(); |
| 1013 | |
} |
| 1014 | 0 | catch (CloneNotSupportedException e) { |
| 1015 | 0 | throw new UnexpectedExceptionPoemException(e, "Object no longer supports clone."); |
| 1016 | 6 | } |
| 1017 | |
|
| 1018 | 6 | it.extras = (Object[])extras().clone(); |
| 1019 | 6 | it.reset(); |
| 1020 | 6 | it.troid = null; |
| 1021 | 6 | it.status = NONEXISTENT; |
| 1022 | |
|
| 1023 | 6 | return it; |
| 1024 | |
} |
| 1025 | |
|
| 1026 | |
|
| 1027 | |
|
| 1028 | |
|
| 1029 | |
|
| 1030 | |
public String dump() { |
| 1031 | 6 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 1032 | 6 | PrintStream ps = new PrintStream(baos); |
| 1033 | 6 | dump(ps); |
| 1034 | 4 | return baos.toString(); |
| 1035 | |
} |
| 1036 | |
|
| 1037 | |
|
| 1038 | |
|
| 1039 | |
|
| 1040 | |
|
| 1041 | |
public void dump(PrintStream p) { |
| 1042 | 14 | p.println(getTable().getName() + "/" + troid()); |
| 1043 | 14 | for (Enumeration<Field<?>> f = getRecordDisplayFields(); f.hasMoreElements();) { |
| 1044 | 58 | p.print(" "); |
| 1045 | 58 | ((Field<?>)f.nextElement()).dump(p); |
| 1046 | 54 | p.println(); |
| 1047 | |
} |
| 1048 | 10 | } |
| 1049 | |
|
| 1050 | |
|
| 1051 | |
|
| 1052 | |
|
| 1053 | |
|
| 1054 | |
public void postWrite() { |
| 1055 | 6524 | } |
| 1056 | |
|
| 1057 | |
|
| 1058 | |
|
| 1059 | |
|
| 1060 | |
|
| 1061 | |
public void postInsert() { |
| 1062 | 6010 | } |
| 1063 | |
|
| 1064 | |
|
| 1065 | |
|
| 1066 | |
|
| 1067 | |
|
| 1068 | |
public void postModify() { |
| 1069 | 516 | } |
| 1070 | |
|
| 1071 | |
|
| 1072 | |
|
| 1073 | |
|
| 1074 | |
|
| 1075 | |
public void preEdit() { |
| 1076 | 2 | } |
| 1077 | |
|
| 1078 | |
|
| 1079 | |
|
| 1080 | |
|
| 1081 | |
|
| 1082 | |
public void postEdit(boolean creating) { |
| 1083 | 4 | } |
| 1084 | |
|
| 1085 | |
|
| 1086 | |
|
| 1087 | |
|
| 1088 | |
|
| 1089 | |
|
| 1090 | |
|
| 1091 | |
|
| 1092 | |
|
| 1093 | |
|
| 1094 | |
|
| 1095 | |
|
| 1096 | |
|
| 1097 | |
|
| 1098 | |
|
| 1099 | |
protected String countMatchSQL(boolean includeDeleted, |
| 1100 | |
boolean excludeUnselectable) { |
| 1101 | 2 | return getTable().countSQL( |
| 1102 | 1 | fromClause(), |
| 1103 | 1 | getTable().whereClause(this), |
| 1104 | |
includeDeleted, excludeUnselectable); |
| 1105 | |
} |
| 1106 | |
|
| 1107 | |
|
| 1108 | |
|
| 1109 | |
|
| 1110 | |
|
| 1111 | |
|
| 1112 | |
|
| 1113 | |
|
| 1114 | |
|
| 1115 | |
|
| 1116 | |
|
| 1117 | |
|
| 1118 | |
protected String fromClause() { |
| 1119 | 18 | return getTable().quotedName(); |
| 1120 | |
} |
| 1121 | |
|
| 1122 | |
public Treeable[] getChildren() { |
| 1123 | 0 | Enumeration<Persistent> refs = getDatabase().referencesTo(this); |
| 1124 | 0 | Vector<Persistent> v = new Vector<Persistent>(); |
| 1125 | 0 | while (refs.hasMoreElements()) |
| 1126 | 0 | v.addElement(refs.nextElement()); |
| 1127 | |
Treeable[] kids; |
| 1128 | 0 | synchronized (v) { |
| 1129 | 0 | kids = new Treeable[v.size()]; |
| 1130 | 0 | v.copyInto(kids); |
| 1131 | 0 | } |
| 1132 | |
|
| 1133 | 0 | return kids; |
| 1134 | |
} |
| 1135 | |
|
| 1136 | |
|
| 1137 | |
|
| 1138 | |
|
| 1139 | |
|
| 1140 | |
|
| 1141 | |
public String getName() { |
| 1142 | 0 | return displayString(); |
| 1143 | |
} |
| 1144 | |
|
| 1145 | |
|
| 1146 | |
|
| 1147 | |
|
| 1148 | |
public boolean isDirty() { |
| 1149 | 8353 | return dirty; |
| 1150 | |
} |
| 1151 | |
|
| 1152 | |
|
| 1153 | |
|
| 1154 | |
|
| 1155 | |
public void setDirty(boolean dirty) { |
| 1156 | 14924 | this.dirty = dirty; |
| 1157 | 14924 | } |
| 1158 | |
|
| 1159 | |
} |