-
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.
Fix: Moved ApiToDatabaseService thing to service folder as it fits, b…
…ut doesnt work
- Loading branch information
AdrianBalunan
committed
Apr 12, 2026
1 parent
02fbe15
commit 0b6f9d2
Showing
2 changed files
with
140 additions
and
139 deletions.
There are no files selected for viewing
139 changes: 0 additions & 139 deletions
139
...lpapplication/src/main/java/ntnu/systemutvikling/team6/database/APIToDatabaseService.java
This file was deleted.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
...elpapplication/src/main/java/ntnu/systemutvikling/team6/service/APIToDatabaseService.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,140 @@ | ||
| package ntnu.systemutvikling.team6.service; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import ntnu.systemutvikling.team6.database.DatabaseConnection; | ||
| import ntnu.systemutvikling.team6.models.Charity; | ||
|
|
||
| public class APIToDatabaseService { | ||
| private final DatabaseConnection connection; | ||
|
|
||
| /** | ||
| * Contractor for DatabaseManager. It uses a DatabaseConnection object that contains a connection | ||
| * credentials. | ||
| * | ||
| * @param connection | ||
| */ | ||
| public APIToDatabaseService(DatabaseConnection connection) { | ||
| this.connection = 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 | ||
| * 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. | ||
| * | ||
| * @param charities | ||
| */ | ||
| public void addAPIDataToTable(List<Charity> charities) { | ||
| Connection conn = null; | ||
| 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) | ||
| """; | ||
|
|
||
| try (PreparedStatement ps = conn.prepareStatement(sql_query)) { | ||
| for (Charity charity : charities) { | ||
| if (charity.getUUID() == null) { | ||
| ps.setString(1, UUID.randomUUID().toString()); | ||
| } else { | ||
| ps.setString(1, charity.getUUID().toString()); | ||
| } | ||
|
|
||
| ps.setString(2, charity.getOrg_number().replaceAll("\\s", "")); | ||
| ps.setString(3, charity.getName()); | ||
| ps.setString(4, "Blank until webscraber works"); | ||
| ps.setString(5, charity.getDescription()); | ||
| ps.setBoolean(6, charity.getPreApproved()); // Description is the link | ||
| ps.setString(7, charity.getStatus()); | ||
|
|
||
| ps.addBatch(); | ||
| } | ||
| ps.executeBatch(); | ||
| } | ||
|
|
||
| // -- Intergerty Check: | ||
| String createTemp = | ||
| """ | ||
| CREATE TEMPORARY TABLE temp_api_charities ( | ||
| org_number VARCHAR(255) PRIMARY KEY | ||
| ) | ||
| """; | ||
|
|
||
| try (PreparedStatement ps = conn.prepareStatement(createTemp)) { | ||
| ps.execute(); | ||
| } | ||
|
|
||
| String insertTemp = "INSERT IGNORE INTO temp_api_charities (org_number) VALUES (?)"; | ||
|
|
||
| try (PreparedStatement ps = conn.prepareStatement(insertTemp)) { | ||
|
|
||
| for (Charity charity : charities) { | ||
| ps.setString(1, charity.getOrg_number().replaceAll("\\s", "")); | ||
| ps.addBatch(); | ||
| } | ||
|
|
||
| ps.executeBatch(); | ||
| } | ||
|
|
||
| String deleteSql = | ||
| """ | ||
| DELETE FROM Charities c | ||
| WHERE NOT EXISTS ( | ||
| SELECT 1 FROM temp_api_charities t | ||
| WHERE t.org_number = c.org_number | ||
| ) | ||
| AND NOT EXISTS ( | ||
| SELECT 1 FROM Donations d WHERE d.charity_id = c.UUID_charities | ||
| ) | ||
| AND NOT EXISTS ( | ||
| SELECT 1 FROM Feedback f WHERE f.charity_id = c.UUID_charities | ||
| ) | ||
| AND NOT EXISTS ( | ||
| SELECT 1 FROM CharityUsers cu WHERE cu.Charities_UUID_charities = c.UUID_charities | ||
| ); | ||
| """; | ||
|
|
||
| try (PreparedStatement ps = conn.prepareStatement(deleteSql)) { | ||
| ps.executeUpdate(); | ||
| } | ||
|
|
||
| conn.commit(); | ||
|
|
||
| } catch (SQLException e) { | ||
| if (conn != null) { | ||
| try { | ||
| conn.rollback(); | ||
| } catch (SQLException ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| } | ||
| e.printStackTrace(); | ||
|
|
||
| throw new RuntimeException("ERROR: Something went wrong during updating charities table."); | ||
| } finally { | ||
| if (conn != null) { | ||
| try { | ||
| conn.setAutoCommit(true); | ||
| conn.close(); | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |