diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java index a4818f3..a78be83 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/HmHApplication.java @@ -9,7 +9,7 @@ import ntnu.systemutvikling.team6.database.DatabaseSetup; import ntnu.systemutvikling.team6.models.Charity; import ntnu.systemutvikling.team6.models.registry.CharityRegistry; -import ntnu.systemutvikling.team6.scraper.APICharityScraper; +import ntnu.systemutvikling.team6.scraper.scraperComponents.APICharityScraper; import ntnu.systemutvikling.team6.service.APIToDatabaseService; public class HmHApplication extends Application { diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseManager.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseManager.java deleted file mode 100644 index 4f6d3ae..0000000 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseManager.java +++ /dev/null @@ -1,384 +0,0 @@ -package ntnu.systemutvikling.team6.database; - -import java.sql.*; -import java.util.*; - -import ntnu.systemutvikling.team6.models.Charity; -import ntnu.systemutvikling.team6.models.registry.CharityRegistry; -import ntnu.systemutvikling.team6.models.Donation; -import ntnu.systemutvikling.team6.models.registry.DonationRegistry; -import ntnu.systemutvikling.team6.scraper.APICharityData; -import ntnu.systemutvikling.team6.scraper.LogoDownloader; -import ntnu.systemutvikling.team6.scraper.URLCharityScraper; - -/** - * Manages the Database with MySQL tables and JDBC. - * - *
This class is responsible for creating the tables needed for the application, if not done - * already and maintaining the {@code charities} table based on data retrieved from the IK API. It - * is also responsible for retrieving the data from the database and sending it to the application - * through the CharityRegistry and DonationRegistry. It is used by the FrontpageController to - * retrieve the data needed to display the charities - */ -public class DatabaseManager { - private final DatabaseConnection connection; - - /** - * Contractor for DatabaseManager. It uses a DatabaseConnection object that contains a connection - * credentials. - */ - public DatabaseManager() { - this.connection = new DatabaseConnection(); - } - - /** - * Connection test for the Database. Does a simple SELECT SQL query and returns either true og and - * Exception if failed - * - * @return true if Sucsedd or SQLExepction if failed - */ - public boolean testConnection() { - try (Connection conn = connection.getMySqlConnection(); - Statement stmt = conn.createStatement()) { - - ResultSet rs = stmt.executeQuery("SELECT 1"); - - if (rs.next()) { - System.out.println("Database connection verified."); - return true; - } - - } catch (SQLException e) { - System.out.println("Database connection failed."); - e.printStackTrace(); - } - - return false; - } - - /** - * Creates the {@code Charities} and {@code Donations} tables if they do not already exist. - * - *
The table structure for Charities is based on fields from {@link APICharityData}. - * - * @throws RuntimeException if a {@link SQLException} occurs while creating the table - */ - public void createTables() { - String sql_query1 = - """ - -- ----------------------------------------------------- - -- Table `HelpMeHelp`.`Charities` - -- ----------------------------------------------------- - CREATE TABLE IF NOT EXISTS Charities ( - UUID_charities CHAR(36) PRIMARY KEY, - org_number VARCHAR(255) NOT NULL, - charity_name VARCHAR(255) NOT NULL, - charity_link VARCHAR(255) NOT NULL, - pre_approved TINYINT NOT NULL, - status VARCHAR(255) NOT NULL, - description TEXT, - logoURL TEXT, - categories TEXT, - key_values TEXT, - logoBlob MEDIUMBLOB, - UNIQUE KEY unique_org_number (org_number) - ) ENGINE=InnoDB; - - - """; - String sql_query2 = - """ - -- ----------------------------------------------------- - -- Table `HelpMeHelp`.`Donations` - -- ----------------------------------------------------- - CREATE TABLE IF NOT EXISTS Donations ( - `UUID_Donations` CHAR(36) NOT NULL, - `amount` DECIMAL NOT NULL, - `date` DATE NOT NULL, - `Charities_UUID_charities` CHAR(36) NOT NULL, - PRIMARY KEY (`UUID_Donations`), - INDEX `fk_Donations_Charities_idx` (`Charities_UUID_charities` ASC) VISIBLE, - CONSTRAINT `fk_Donations_Charities` - FOREIGN KEY (`Charities_UUID_charities`) - REFERENCES Charities (`UUID_charities`) - ON DELETE CASCADE - ON UPDATE CASCADE) - ENGINE = InnoDB; - """; - - try (Connection conn = connection.getMySqlConnection(); - Statement s = conn.createStatement()) { - - s.execute(sql_query1); - s.execute(sql_query2); - } catch (SQLException e) { - e.printStackTrace(); - throw new RuntimeException("Error creating table."); - } - } - - /** - * 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 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 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 a list of {@code Charity} objects to add to the database - */ - public void addAPIDataToTable(ListThe query performs a LEFT JOIN between the {@code Charities}, {@code Feedback}, and {@code - * User} tables. Each unique charity is added once to the registry; any feedback rows found for + *
The query performs a LEFT JOIN between the {@code Charities}, {@code Feedback}, {@code + * User}, {@code CharityVanity}, and {@code category(s)} tables. Each unique charity is added once to the registry; any feedback rows found for * that charity are appended to its feedback list. * - *
Note: charities with no feedback are still included in the result due to the LEFT JOIN. + *
Note: charities with no feedback and categories are still included in the result due to the LEFT JOIN.
*
* @return a {@link CharityRegistry} containing all charities found in the database, each
* populated with its associated {@link Feedback} objects (if any)
@@ -52,12 +55,17 @@ public CharityRegistry getCharitiesFromDB() {
String sql_query =
"""
SELECT
- c.UUID_charities, c.org_number, c.charity_name, c.charity_link, c.pre_approved, c.status,
+ c.UUID_charities, c.org_number, c.pre_approved, c.status,
f.UUID_feedback, f.feedback_comment, f.feedback_date, f.isAnonymous, f.charity_id, f.user_id,
+ cv.charity_name, cv.charity_link, cv.description, cv.logoURL, cv.key_values, cv.logoBLOB,
u.UUID_user, u.user_name, u.user_email, u.user_password, u.role
+ cat.category
FROM Charities c
LEFT JOIN Feedback f ON f.charity_id = c.UUID_charities
LEFT JOIN User u ON f.user_id = u.UUID_user
+ LEFT JOIN Charity_Categories cc ON cc.Charities_UUID_charities = c.UUID.charities
+ LEFT JOIN Cateegories cat ON cat.category_id = cc.Categories_category_id
+ INNER JOIN CharityVanity cv ON cv.UUID_charity = c.UUID_charities;
""";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql_query);
@@ -65,6 +73,8 @@ public CharityRegistry getCharitiesFromDB() {
Charity currentCharity = null;
String lastCharity = null;
+ Set