-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working manager class for handling creation, insertion, and deletion of tables and records. Currently, supports creating charities table, updating the table with values from the IK API, and doing a parity check, removing values that doesn't exist in the IK API.
- Loading branch information
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package ntnu.sytemutvikling.team6.models; | ||
|
|
||
| import java.sql.*; | ||
| import java.util.List; | ||
|
|
||
| public class DatabaseManager { | ||
| // Database environment is MySQL | ||
| private final String databaseURL; | ||
| private final String username; | ||
| private final String password; | ||
|
|
||
| // Values stored in system environment variables for security (using ntnu's phpmyadmin for this project) | ||
| public DatabaseManager() { | ||
| this.databaseURL = System.getenv("HMH_DB_URL"); | ||
| this.username = System.getenv("HMH_DB_USERNAME"); | ||
| this.password = System.getenv("HMH_DB_PASSWORD"); | ||
|
|
||
| if (this.databaseURL == null || this.databaseURL.isBlank()) { | ||
| throw new IllegalStateException("Database environment variable URL has not been set"); | ||
| } | ||
|
|
||
| if (this.username == null || this.username.isBlank()) { | ||
| throw new IllegalStateException("Username environment variable has not been set"); | ||
| } | ||
|
|
||
| if (this.password == null || this.password.isBlank()) { | ||
| throw new IllegalStateException("Password environment variable has not been set"); | ||
| } | ||
| } | ||
|
|
||
| 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, | ||
| name VARCHAR(255), | ||
| status VARCHAR(50), | ||
| url VARCHAR(255), | ||
| is_pre_approved BOOLEAN | ||
| )"""; | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(databaseURL, username, password); | ||
| Statement s = conn.createStatement()) { | ||
|
|
||
| s.execute(sql_create_main_table); | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Error creating tables."); | ||
| } | ||
| } | ||
|
|
||
| public void updateCharities(List<APICharityData> charities) { | ||
| // Inserts values in APICharityData to table | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(databaseURL, username, password)) | ||
| { | ||
| // Inserts objects values into charities | ||
| String sql_update = """ | ||
| INSERT INTO charities (org_number, name, status, url, is_pre_approved) | ||
| VALUES (?, ?, ?, ?, ?) | ||
| ON DUPLICATE KEY UPDATE | ||
| name = VALUES(name), | ||
| status = VALUES(status), | ||
| url = VALUES(url), | ||
| is_pre_approved = VALUES(is_pre_approved) | ||
| """; | ||
|
|
||
| PreparedStatement s = conn.prepareStatement(sql_update); | ||
|
|
||
| for (APICharityData charity : charities) { | ||
| s.setString(1, charity.getOrg_number().replaceAll("\\s", "")); | ||
| s.setString(2, charity.getName()); | ||
| s.setString(3, charity.getStatus()); | ||
| s.setString(4, charity.getUrl()); | ||
| s.setBoolean(5, charity.getIs_pre_approved()); | ||
|
|
||
| s.addBatch(); | ||
| } | ||
| s.executeBatch(); | ||
|
|
||
| // Removed temp table used 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) { | ||
| temp_update.setString(1, charity.getOrg_number().replaceAll("\\s", "")); | ||
| temp_update.addBatch(); | ||
| } | ||
| 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. | ||
| String sql_delete_old_records = """ | ||
| DELETE FROM charities c | ||
| WHERE NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM temp t | ||
| WHERE c.org_number = t.org_number | ||
| ) | ||
| """; | ||
| PreparedStatement delete_old_records = conn.prepareStatement(sql_delete_old_records); | ||
| delete_old_records.execute(); | ||
|
|
||
| // Deletes temporary table | ||
| String sql_delete_temp_table = "DROP TABLE IF EXISTS temp"; | ||
| PreparedStatement delete_temp_table = conn.prepareStatement(sql_delete_temp_table); | ||
| delete_temp_table.execute(); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } catch (SQLException e) { | ||
| // prints whole sql error message | ||
| e.printStackTrace(); | ||
| throw new RuntimeException("ERROR: Something went wrong during updating charities table."); | ||
| } | ||
|
|
||
| } | ||
| } |