diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java index 6d91982..a4e2ce8 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java @@ -3,12 +3,34 @@ import java.sql.*; import java.util.List; +/** + * Manages creation and synchronization of database tables in a MySQL database. + *

+ * This class is responsible for establishing connections using environment variables + * and maintaining the {@code charities} table based on data retrieved from the IK API. + *

+ */ + public class DatabaseManager { - // Database environment is MySQL private final String databaseURL; private final String username; private final String password; + /** + Constructs a new {@code DatabaseManager} using database credentials + * retrieved from system environment variables. + *

+ * Required environment variables: + *

+ *

+ * + * @throws IllegalStateException if either databaseURL, username, or password is {@code null} or blank + */ + // Values stored in system environment variables for security (using ntnu's phpmyadmin for this project) public DatabaseManager() { this.databaseURL = System.getenv("HMH_DB_URL"); @@ -28,8 +50,15 @@ public DatabaseManager() { } } + /** + * Creates the {@code charities} table if it does not already exist. + *

+ * The table structure is based on fields from {@link APICharityData}. + *

+ * @throws RuntimeException if a {@link SQLException} occurs while creating the table + */ + public void createCharitiesTable() { - // Creates table if it doesn't exist String sql_create_main_table = """ CREATE TABLE IF NOT EXISTS charities ( org_number VARCHAR(100) PRIMARY KEY, @@ -44,12 +73,28 @@ url VARCHAR(255), s.execute(sql_create_main_table); } catch (SQLException e) { + e.printStackTrace(); throw new RuntimeException("Error creating tables."); } } + /** + * Synchronizes the {@code charities} table with the provided list of APICharityData. + *

+ * Summary of function: + *

+ * A temporary table is used to determine which records should be deleted. + *

+ * + * @param charities a list of ApiCharityDaya objects + * @throws RuntimeException if a {@link SQLException} occurs during database synchronization + */ + public void updateCharities(List charities) { - // Inserts values in APICharityData to table try (Connection conn = DriverManager.getConnection(databaseURL, username, password)) { @@ -77,17 +122,15 @@ INSERT INTO charities (org_number, name, status, url, is_pre_approved) } s.executeBatch(); - // Removed temp table used for parity check + // Temporary table for parity check String sql_drop_temp_table = "DROP TABLE IF EXISTS temp"; PreparedStatement drop_temp_table = conn.prepareStatement(sql_drop_temp_table); drop_temp_table.execute(); - // Creates a temp table used for parity check String sql_create_temp_table = "CREATE TABLE temp (org_number VARCHAR(100) PRIMARY KEY)"; PreparedStatement temp_create = conn.prepareStatement(sql_create_temp_table); temp_create.execute(); - // Inserts org_number into the temp table String sql_update_temp_table = "INSERT INTO temp (org_number) VALUES (?)"; PreparedStatement temp_update = conn.prepareStatement(sql_update_temp_table); for (APICharityData charity : charities) { @@ -96,8 +139,7 @@ INSERT INTO charities (org_number, name, status, url, is_pre_approved) } temp_update.executeBatch(); - // Removes records from main charities table, that don't exist in the temp table. - // This makes it so if any organization is removed by IK, it will also be removed from the database. + // Removes records not found in the list from charities table String sql_delete_old_records = """ DELETE FROM charities c WHERE NOT EXISTS ( @@ -118,7 +160,6 @@ WHERE NOT EXISTS ( } catch (SQLException e) { - // prints whole sql error message e.printStackTrace(); throw new RuntimeException("ERROR: Something went wrong during updating charities table."); }