Skip to content

Commit

Permalink
Feat: APIScraper can will update the database
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Mar 11, 2026
1 parent 0e9df82 commit 6c509f0
Showing 1 changed file with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package ntnu.sytemutvikling.team6.scraper;

import com.google.gson.Gson;
import ntnu.sytemutvikling.team6.DAO.APICharityData;
import ntnu.sytemutvikling.team6.database.DatabaseConnection;

import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Fetches JSON information from the IK API and parses the JSON into a list of {@link APICharityData} objects.
* AND can update the database.
*/

public class APICharityScraper {
private final HttpClient client;
private final HttpRequest request;
private final DatabaseConnection connection;

/**
* Constructs a new APICharityScraper object.
Expand All @@ -32,7 +37,8 @@ public APICharityScraper(HttpClient client) throws URISyntaxException {
.uri(new URI("https://app.innsamlingskontrollen.no/api/public/v1/all"))
.GET()
.build();
}
this.connection = new DatabaseConnection();
}

/**
* Checks if the http request returns an 'OK' response.
Expand Down Expand Up @@ -89,5 +95,106 @@ public List<APICharityData> parseJSON(String JSONData) {
.filter(c-> !"obs".equalsIgnoreCase(c.getStatus()))
.toList();
}
/**
* 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) {
Connection conn = null;
try {
conn = connection.getMySqlConnection();
conn.setAutoCommit(false);
// Inserts objects values into charities
String sql_query = """
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)
""";

try (PreparedStatement s = conn.prepareStatement(sql_query)) {
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();
}


// Temporary table for parity check
sql_query = "CREATE TEMPORARY TABLE temp (org_number VARCHAR(100) PRIMARY KEY)";
try (PreparedStatement temp_create = conn.prepareStatement(sql_query)) {
temp_create.execute();
}

String sql_update_temp_table = "INSERT INTO temp (org_number) VALUES (?)";
try (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 not found in the list from charities table
sql_query = """
DELETE FROM charities c
WHERE NOT EXISTS (
SELECT 1
FROM temp t
WHERE c.org_number = t.org_number
)
""";

try (PreparedStatement delete_old_records = conn.prepareStatement(sql_query)){
delete_old_records.execute();
}

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();
}
}
}

}
}

0 comments on commit 6c509f0

Please sign in to comment.