|
1 | 1 | package db; |
2 | 2 |
|
3 | 3 | import connections.Connection; |
| 4 | +import exceptions.ExceptionHandler; |
4 | 5 | import exceptions.ExceptionRecord; |
5 | 6 |
|
6 | | -import java.sql.PreparedStatement; |
7 | | -import java.sql.Timestamp; |
| 7 | +import java.sql.*; |
| 8 | + |
| 9 | +import static java.sql.DriverManager.getConnection; |
8 | 10 |
|
9 | 11 | /** |
10 | 12 | * Static store methods — one per N21 table. |
@@ -437,8 +439,162 @@ public static void createCommunicatorTables() |
437 | 439 | catch (Exception e) { fail("communicator_tables", e); } |
438 | 440 | } |
439 | 441 |
|
440 | | - public static void storeChatMessage(final long FROM, final long TO, |
441 | | - final String MESSAGE, final String TYPE) |
| 442 | + // --------------------------------------------------------------------- |
| 443 | + // TABLE CREATION |
| 444 | + // --------------------------------------------------------------------- |
| 445 | + |
| 446 | + public static void createWhiteAuditorTables() { |
| 447 | + try (Connection c = getConnection()) { |
| 448 | + |
| 449 | + ((java.sql.Connection) c).createStatement().execute(""" |
| 450 | + CREATE TABLE IF NOT EXISTS wat_tasks ( |
| 451 | + id BIGINT AUTO_INCREMENT PRIMARY KEY, |
| 452 | + from_national_id BIGINT NOT NULL, |
| 453 | + to_national_id BIGINT NOT NULL, |
| 454 | + type VARCHAR(32) NOT NULL, |
| 455 | + filename VARCHAR(255), |
| 456 | + size INT, |
| 457 | + payload LONGTEXT, |
| 458 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 459 | + ) |
| 460 | + """); |
| 461 | + |
| 462 | + } catch (Exception e) { ExceptionHandler.dispatch(e); } |
| 463 | + } |
| 464 | + |
| 465 | + // --------------------------------------------------------------------- |
| 466 | + // STORE FILE |
| 467 | + // --------------------------------------------------------------------- |
| 468 | + |
| 469 | + public static void storeAssignedFile(long fromId, long toId, String filename, String base64) throws Exception |
| 470 | + { |
| 471 | + try (Connection c = getConnection(); |
| 472 | + PreparedStatement ps = ((java.sql.Connection) c).prepareStatement(""" |
| 473 | + INSERT INTO wat_tasks (from_national_id, to_national_id, type, filename, payload) |
| 474 | + VALUES (?, ?, 'file', ?, ?) |
| 475 | + """)) { |
| 476 | + |
| 477 | + ps.setLong(1, fromId); |
| 478 | + ps.setLong(2, toId); |
| 479 | + ps.setString(3, filename); |
| 480 | + ps.setString(4, base64); |
| 481 | + ps.executeUpdate(); |
| 482 | + |
| 483 | + } catch (Exception e) { ExceptionHandler.dispatch(e); } |
| 484 | + } |
| 485 | + |
| 486 | + // --------------------------------------------------------------------- |
| 487 | + // STORE BITS |
| 488 | + // --------------------------------------------------------------------- |
| 489 | + |
| 490 | + public static void storeAssignedBits(long fromId, long toId, int size, String base64) { |
| 491 | + try (Connection c = getConnection(); |
| 492 | + PreparedStatement ps = ((java.sql.Connection) c).prepareStatement( |
| 493 | + """ |
| 494 | + INSERT INTO wat_tasks (from_national_id, to_national_id, type, size, payload) |
| 495 | + VALUES (?, ?, 'bits', ?, ?) |
| 496 | + """ |
| 497 | + )) { |
| 498 | + |
| 499 | + ps.setLong(1, fromId); |
| 500 | + ps.setLong(2, toId); |
| 501 | + ps.setInt(3, size); |
| 502 | + ps.setString(4, base64); |
| 503 | + ps.executeUpdate(); |
| 504 | + } catch (SQLException ex) |
| 505 | + { |
| 506 | + throw new RuntimeException(ex); |
| 507 | + } catch (Exception e) |
| 508 | + { |
| 509 | + throw new RuntimeException(e); |
| 510 | + } |
| 511 | + } |
| 512 | + |
| 513 | + // --------------------------------------------------------------------- |
| 514 | + // STORE SIGNATORY |
| 515 | + // --------------------------------------------------------------------- |
| 516 | + |
| 517 | + public static void storeAssignedSignatory(long fromId, long toId, String symbol) { |
| 518 | + try (Connection c = getConnection(); |
| 519 | + PreparedStatement ps = ((java.sql.Connection) c).prepareStatement(""" |
| 520 | + INSERT INTO wat_tasks (from_national_id, to_national_id, type, payload) |
| 521 | + VALUES (?, ?, 'signatory', ?) |
| 522 | + """)) { |
| 523 | + |
| 524 | + ps.setLong(1, fromId); |
| 525 | + ps.setLong(2, toId); |
| 526 | + ps.setString(3, symbol); |
| 527 | + ps.executeUpdate(); |
| 528 | + |
| 529 | + } catch (Exception e) { ExceptionHandler.dispatch(e); } |
| 530 | + } |
| 531 | + |
| 532 | + // --------------------------------------------------------------------- |
| 533 | + // LIST TASKS FOR USER |
| 534 | + // --------------------------------------------------------------------- |
| 535 | + |
| 536 | + public static ResultSet loadTasksFor(long toId) { |
| 537 | + try { |
| 538 | + Connection c = getConnection(); |
| 539 | + PreparedStatement ps = ((java.sql.Connection) c).prepareStatement(""" |
| 540 | + SELECT id, type, from_national_id, created_at |
| 541 | + FROM wat_tasks |
| 542 | + WHERE to_national_id = ? |
| 543 | + ORDER BY created_at DESC |
| 544 | + """); |
| 545 | + ps.setLong(1, toId); |
| 546 | + return ps.executeQuery(); |
| 547 | + |
| 548 | + } catch (Exception e) { ExceptionHandler.dispatch(e); return null; } |
| 549 | + } |
| 550 | + |
| 551 | + // --------------------------------------------------------------------- |
| 552 | + // GET SINGLE TASK |
| 553 | + // --------------------------------------------------------------------- |
| 554 | + |
| 555 | + public static ResultSet loadTask(long taskId) { |
| 556 | + try { |
| 557 | + Connection c = getConnection(); |
| 558 | + PreparedStatement ps = ((java.sql.Connection) c).prepareStatement(""" |
| 559 | + SELECT * |
| 560 | + FROM wat_tasks |
| 561 | + WHERE id = ? |
| 562 | + """); |
| 563 | + ps.setLong(1, taskId); |
| 564 | + return ps.executeQuery(); |
| 565 | + |
| 566 | + } catch (Exception e) { ExceptionHandler.dispatch(e); return null; } |
| 567 | + } |
| 568 | + |
| 569 | + // --------------------------------------------------------------------- |
| 570 | + // DELETE TASK |
| 571 | + // --------------------------------------------------------------------- |
| 572 | + |
| 573 | + public static void deleteTask(long taskId) { |
| 574 | + try (Connection c = getConnection(); |
| 575 | + PreparedStatement ps = ((java.sql.Connection) c).prepareStatement(""" |
| 576 | + DELETE FROM wat_tasks WHERE id = ? |
| 577 | + """)) { |
| 578 | + |
| 579 | + ps.setLong(1, taskId); |
| 580 | + ps.executeUpdate(); |
| 581 | + |
| 582 | + } catch (Exception e) { ExceptionHandler.dispatch(e); } |
| 583 | + } |
| 584 | + |
| 585 | + |
| 586 | + public static Connection getConnection() throws Exception |
| 587 | + { |
| 588 | + String url = "jdbc:mysql://localhost:3306/n21db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&characterEncoding=UTF-8"; |
| 589 | + String user = "root"; // change if needed |
| 590 | + String pass = "password"; // change if needed |
| 591 | + |
| 592 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 593 | + |
| 594 | + return (Connection) DriverManager.getConnection(url, user, pass); |
| 595 | + } |
| 596 | + |
| 597 | + public static void storeChatMessage(final long FROM, final long TO, final String MESSAGE, final String TYPE) |
442 | 598 | { |
443 | 599 | if (dbOk()) |
444 | 600 | { |
|
0 commit comments