diff --git a/docs/SqlDatabase/ER-DiagramFile.mwb b/docs/SqlDatabase/ER-DiagramFile.mwb index ee90f5e..15edab1 100644 Binary files a/docs/SqlDatabase/ER-DiagramFile.mwb and b/docs/SqlDatabase/ER-DiagramFile.mwb differ diff --git a/docs/SqlDatabase/ER-DiagramFile.mwb.bak b/docs/SqlDatabase/ER-DiagramFile.mwb.bak index 64d9533..c348da3 100644 Binary files a/docs/SqlDatabase/ER-DiagramFile.mwb.bak and b/docs/SqlDatabase/ER-DiagramFile.mwb.bak differ diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java index fc3588d..a4818f3 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java @@ -49,6 +49,7 @@ public void init() { try { HttpClient https = HttpClient.newHttpClient(); APICharityScraper scraper = new APICharityScraper(https); + DatabaseConnection conn = new DatabaseConnection(); APIToDatabaseService db = new APIToDatabaseService(conn); diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java index 62de2e8..4000b25 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java @@ -57,18 +57,14 @@ public void createTables() { -- ----------------------------------------------------- -- Table `HelpMeHelp`.`Charities` -- ----------------------------------------------------- - CREATE TABLE IF NOT EXISTS `apbaluna`.`Charities` ( - `UUID_charities` CHAR(36) NOT NULL, - `org_number` VARCHAR(255) NOT NULL, - `charity_name` VARCHAR(255) NOT NULL, - `charity_description` VARCHAR(255) NOT NULL, - `charity_link` VARCHAR(255) NOT NULL, - `pre_approved` TINYINT NOT NULL, - `status` VARCHAR(255) NOT NULL, - PRIMARY KEY (`UUID_charities`), - UNIQUE INDEX `org_number_UNIQUE` (`org_number` ASC) VISIBLE) - ENGINE = InnoDB; - + CREATE TABLE IF NOT EXISTS `apbaluna`.`Charities` ( + `UUID_charities` CHAR(36) NOT NULL, + `org_number` VARCHAR(255) NOT NULL, + `pre_approved` TINYINT NOT NULL, + `status` VARCHAR(255) NOT NULL, + PRIMARY KEY (`UUID_charities`), + UNIQUE INDEX `org_number_UNIQUE` (`org_number` ASC) VISIBLE) + ENGINE = InnoDB; """; String donationsTable = """ @@ -118,14 +114,14 @@ PRIMARY KEY (`UUID_User`)) -- Table `apbaluna`.`Settings` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `apbaluna`.`Settings` ( - `User_UUID_User` CHAR(36) NOT NULL, + `UUID_user` CHAR(36) NOT NULL, `isAnonymous` TINYINT NOT NULL, `language` VARCHAR(45) NOT NULL, `lightmode` TINYINT NOT NULL, PRIMARY KEY (`User_UUID_User`), - INDEX `fk_Settings_User1_idx` (`User_UUID_User` ASC) VISIBLE, + INDEX `fk_Settings_User1_idx` (`UUID_user` ASC) VISIBLE, CONSTRAINT `fk_Settings_User1` - FOREIGN KEY (`User_UUID_User`) + FOREIGN KEY (`UUID_user`) REFERENCES `apbaluna`.`User` (`UUID_User`) ON DELETE NO ACTION ON UPDATE NO ACTION) @@ -253,6 +249,17 @@ FOREIGN KEY (`User_UUID_User`) ON UPDATE NO ACTION) ENGINE = InnoDB; """; + String charityVanityTable = + """ + CREATE TABLE IF NOT EXISTS `apbaluna`.`Charities` ( + `UUID_charities` CHAR(36) NOT NULL, + `org_number` VARCHAR(255) NOT NULL, + `pre_approved` TINYINT NOT NULL, + `status` VARCHAR(255) NOT NULL, + PRIMARY KEY (`UUID_charities`), + UNIQUE INDEX `org_number_UNIQUE` (`org_number` ASC) VISIBLE) + ENGINE = InnoDB; + """; try (Connection conn = connection.getMySqlConnection(); Statement s = conn.createStatement()) { @@ -266,6 +273,7 @@ FOREIGN KEY (`User_UUID_User`) s.execute(categoriesTable); s.execute(charityCategoriesTable); s.execute(charityUserTable); + s.execute(charityVanityTable); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("Error creating table."); diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/APIToDatabaseService.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/APIToDatabaseService.java index ac7b918..1c4efc8 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/APIToDatabaseService.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/APIToDatabaseService.java @@ -12,7 +12,7 @@ public class APIToDatabaseService { private final DatabaseConnection connection; /** - * Contractor for DatabaseManager. It uses a DatabaseConnection object that contains a connection + * Contractor for APIToDatabaseService. It uses a DatabaseConnection object that contains a connection * credentials. * * @param connection @@ -23,29 +23,33 @@ public APIToDatabaseService(DatabaseConnection connection) { /** * This method is used to verify the integrity of the data in the {@code charities} table and to - * update it based on the data retrieved from the IK API. The param charities are retrieved from + * update it based on the data retrieved from the IK API and the charity's URL. + * The param charities are retrieved from * the IK API through the APICharityData class. Called in initialize method in * HmHApplication.java, which is the main class of the application, to ensure that the data is up - * to date when the application starts. Uses a a temp table to ensure that the data in the - * database is consistent with the data from the API. + * to date when the application starts. Uses a temp table to ensure that the data in the database + * is consistent with the data from the API. + *

Uses a URLScraper object to get data not contained in the API, and static methods from + * LogoDownloader to get the charity's logo as a blob.

* - * @param charities + * @param charities a list of {@code Charity} objects to add to the database */ public void addAPIDataToTable(List charities) { Connection conn = null; + int charityCounter = 0; try { conn = connection.getMySqlConnection(); conn.setAutoCommit(false); String sql_query = """ - INSERT INTO Charities (UUID_charities, org_number, charity_name, charity_description, charity_link, pre_approved, status) - VALUES (?, ?, ?, ?, ?, ?, ?) - ON DUPLICATE KEY UPDATE - charity_name = VALUES(charity_name), - charity_link = VALUES(charity_link), - pre_approved = VALUES(pre_approved), - status = VALUES(status) - """; + INSERT INTO Charities (UUID_charities, org_number, charity_name, charity_description, charity_link, pre_approved, status) + VALUES (?, ?, ?, ?, ?, ?, ?) + ON DUPLICATE KEY UPDATE + charity_name = VALUES(charity_name), + charity_link = VALUES(charity_link), + pre_approved = VALUES(pre_approved), + status = VALUES(status) + """; try (PreparedStatement ps = conn.prepareStatement(sql_query)) { for (Charity charity : charities) {