Skip to content

Commit

Permalink
Updated DatabaseManager
Browse files Browse the repository at this point in the history
Added JavaDocs and printStackTrace to createCharitiesTable method.
  • Loading branch information
roaraf committed Mar 4, 2026
1 parent e99d6c7 commit c6ee1c9
Showing 1 changed file with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,34 @@
import java.sql.*;
import java.util.List;

/**
* Manages creation and synchronization of database tables in a MySQL database.
* <p>
* This class is responsible for establishing connections using environment variables
* and maintaining the {@code charities} table based on data retrieved from the IK API.
* </p>
*/

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.
* <p>
* Required environment variables:
* <ul>
* <li>{@code HMH_DB_URL}</li>
* <li>{@code HMH_DB_USERNAME}</li>
* <li>{@code HMH_DB_PASSWORD}</li>
* </ul>
* </p>
*
* @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");
Expand All @@ -28,8 +50,15 @@ public DatabaseManager() {
}
}

/**
* Creates the {@code charities} table if it does not already exist.
* <p>
* The table structure is based on fields from {@link APICharityData}.
* </p>
* @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,
Expand All @@ -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.
* <p>
* Summary of function:
* <ul>
* <li>Inserts new records</li>
* <li>Updates existing records using {@code ON DUPLICATE KEY UPDATE}</li>
* <li>Removes database records that are not present in the provided list</li>
* </ul>
* A temporary table is used to determine which records should be deleted.
* </p>
*
* @param charities a list of ApiCharityDaya objects
* @throws RuntimeException if a {@link SQLException} occurs during database synchronization
*/

public void updateCharities(List<APICharityData> charities) {
// Inserts values in APICharityData to table

try (Connection conn = DriverManager.getConnection(databaseURL, username, password))
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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 (
Expand All @@ -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.");
}
Expand Down

0 comments on commit c6ee1c9

Please sign in to comment.