diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CharityPageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CharityPageController.java index 6fcea04..af15da4 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CharityPageController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CharityPageController.java @@ -4,12 +4,8 @@ import javafx.scene.layout.FlowPane; public class CharityPageController { - @FXML private FlowPane charityPage; - - @FXML - public void initialize() { - - } - + @FXML private FlowPane charityPage; + @FXML + public void initialize() {} } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/Charity.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/Charity.java index 25f1453..4cdf03a 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/Charity.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/Charity.java @@ -34,45 +34,43 @@ public class Charity { /* List that contains the charity's Feedbacks */ private List feedbacks; + + /** - * Contructor for creating a new charity. The charity is unverified by default. + * Contructor for creating a new charity. Taylored to match data given from Api. Other attributes + * will just be initialized as empty * * @param org_number matches from innsamlingkontrollen * @param name matches from innsamlingkontrollen * @param is_pre_approved name matches from innsamlingkontrollen * @param status name matches from innsamlingkontrollen - * @param description no coreleation to innsamlingskontrollen - * @param category no coreleation to innsamlingskontrollen - * - */ - public Charity(String org_number, String name, String description, String category, boolean is_pre_approved, String status) { + public Charity( + String org_number, String link, String name, boolean is_pre_approved, String status) { this.UUID = java.util.UUID.randomUUID(); - this.org_number = org_number == null ? "" : org_number.replaceAll("\\s", ""); + this.org_number = org_number.replaceAll("\\s", ""); this.name = name; - this.description = description; + this.description = "Les mer her: " + link; this.is_pre_approved = is_pre_approved; this.status = status; this.feedbacks = new ArrayList<>(); - this.category = category; + this.category = ""; } - /** - * Contructor for creating a new charity. Taylored to match data given from Api. - * Other attributes will just be initialized as empty + * Contructor for creating a new charity. Taylored to match data given from DATABASE. Other attributes + * will just be initialized as empty * * @param org_number matches from innsamlingkontrollen * @param name matches from innsamlingkontrollen * @param is_pre_approved name matches from innsamlingkontrollen * @param status name matches from innsamlingkontrollen - * - */ - public Charity(String org_number, String link, String name, boolean is_pre_approved, String status) { - this.UUID = java.util.UUID.randomUUID(); + public Charity(String uuid, + String org_number, String link, String name, boolean is_pre_approved, String status) { + this.UUID = UUID.fromString(uuid); this.org_number = org_number.replaceAll("\\s", ""); this.name = name; - this.description = "Les mer her: " + link; + this.description = link; this.is_pre_approved = is_pre_approved; this.status = status; this.feedbacks = new ArrayList<>(); @@ -83,6 +81,7 @@ public Charity(String org_number, String link, String name, boolean is_pre_appro public UUID getUUID() { return UUID; } + public String getOrg_number() { return org_number; } @@ -91,7 +90,9 @@ public String getStatus() { return status; } - public boolean getPreApproved(){return is_pre_approved;} + public boolean getPreApproved() { + return is_pre_approved; + } public List getFeedbacks() { return feedbacks; @@ -118,5 +119,4 @@ public void setVerified() { public void setUnverified() { this.status = "Veto"; } - } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/CharityRegistry.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/CharityRegistry.java index a7fd080..b829a41 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/CharityRegistry.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/models/CharityRegistry.java @@ -17,9 +17,11 @@ public Optional findCharityByOrgnumber(String org_number) { if (org_number == null) { throw new IllegalArgumentException("CharityId can not be null."); } - return charities.stream().filter(charity -> org_number.equals(charity.getOrg_number())).findFirst(); + return charities.stream() + .filter(charity -> org_number.equals(charity.getOrg_number())) + .findFirst(); } - + public Optional findCharityByUUID(UUID uuid) { if (uuid == null) { throw new IllegalArgumentException("CharityId can not be null."); @@ -40,6 +42,7 @@ public boolean removeCharity(String org_number) { } return charities.removeIf(charity -> org_number.equals(charity.getOrg_number())); } + public boolean removeCharityUUID(UUID uuid) { if (uuid == null) { throw new IllegalArgumentException("CharityId can not be null."); diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityData.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityData.java index f773542..1b6a930 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityData.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityData.java @@ -3,92 +3,86 @@ import ntnu.systemutvikling.team6.database.DatabaseManager; /** - * Represents data parsed from the IK API JSON response. - * Instances are immutable; to update any value, a new object must be created. - *

- * Receives data directly from {@link APICharityScraper} or {@link IKOrganizationScraper}. - *

- *

- * {@code org_number} should be a unique number, as it is used as a primary key - * in {@link DatabaseManager}. - *

+ * Represents data parsed from the IK API JSON response. Instances are immutable; to update any + * value, a new object must be created. + * + *

Receives data directly from {@link APICharityScraper} or {@link IKOrganizationScraper}. + * + *

{@code org_number} should be a unique number, as it is used as a primary key in {@link + * DatabaseManager}. */ - public class APICharityData { - private final String org_number; - private final String name; - private final String status; - private final String url; - private final boolean is_pre_approved; - - /** - * Constructs a new APICharityData object. - * @param org_number a unique number that identifies the organization - * @param name the name of the organization - * @param status {@code approved} for approved organizations, - * {@code obs} for non-approved organizations - * @param url the URL for more info about the organization on the IK domain - * @param is_pre_approved whether the organization was pre-approved - */ - - public APICharityData(String org_number, String name, String status, String url, boolean is_pre_approved) { - if (org_number == null || org_number.isBlank()) { - throw new IllegalArgumentException("ERROR: Org number cannot be null or blank"); - } - this.org_number = org_number.replaceAll("\\s", ""); - this.name = name; - this.status = status; - this.url = url; - this.is_pre_approved = is_pre_approved; - } - - /** - * Returns the organization number. Must not be {@code null} or blank. - * - * @return the organization number - */ - - public String getOrg_number() { - return this.org_number; - } - - /** - * Returns the name of the organization. Whitespace removed. - * - * @return the name of the organization - */ + private final String org_number; + private final String name; + private final String status; + private final String url; + private final boolean is_pre_approved; - public String getName() { - return name; + /** + * Constructs a new APICharityData object. + * + * @param org_number a unique number that identifies the organization + * @param name the name of the organization + * @param status {@code approved} for approved organizations, {@code obs} for non-approved + * organizations + * @param url the URL for more info about the organization on the IK domain + * @param is_pre_approved whether the organization was pre-approved + */ + public APICharityData( + String org_number, String name, String status, String url, boolean is_pre_approved) { + if (org_number == null || org_number.isBlank()) { + throw new IllegalArgumentException("ERROR: Org number cannot be null or blank"); } + this.org_number = org_number.replaceAll("\\s", ""); + this.name = name; + this.status = status; + this.url = url; + this.is_pre_approved = is_pre_approved; + } - /** - * Returns whether the organization is approved or not - * @return the approved status of the organization - */ - - public String getStatus() { - return status; - } + /** + * Returns the organization number. Must not be {@code null} or blank. + * + * @return the organization number + */ + public String getOrg_number() { + return this.org_number; + } - /** - * Returns the URL of the organizations information page on IK - * - * @return the URL for more info about the organization - */ + /** + * Returns the name of the organization. Whitespace removed. + * + * @return the name of the organization + */ + public String getName() { + return name; + } - public String getUrl() { - return url; - } + /** + * Returns whether the organization is approved or not + * + * @return the approved status of the organization + */ + public String getStatus() { + return status; + } - /** - * Returns whether the organization was pre-approved. - * - * @return {@code true} if organization was pre-approved
- * {@code false} otherwise - */ + /** + * Returns the URL of the organizations information page on IK + * + * @return the URL for more info about the organization + */ + public String getUrl() { + return url; + } - public boolean getIs_pre_approved() { - return this.is_pre_approved; - } + /** + * Returns whether the organization was pre-approved. + * + * @return {@code true} if organization was pre-approved
+ * {@code false} otherwise + */ + public boolean getIs_pre_approved() { + return this.is_pre_approved; + } } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityScraper.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityScraper.java index 7a92cee..9f71537 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityScraper.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/APICharityScraper.java @@ -1,132 +1,121 @@ package ntnu.systemutvikling.team6.scraper; import com.google.gson.Gson; -import ntnu.systemutvikling.team6.database.DatabaseConnection; -import ntnu.systemutvikling.team6.models.Charity; -import ntnu.systemutvikling.team6.models.CharityRegistry; - 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; +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.CharityRegistry; /** - * Fetches JSON information from the IK API and parses the JSON into a list of {@link APICharityData} objects. - * AND can update the database. + * 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 static String API_url = "https://app.innsamlingskontrollen.no/api/public/v1/all"; - - /** - * Constructs a new APICharityScraper object. - * @param client the client responsible for making the http connection. - * @throws URISyntaxException if the API URL is malformed or violates URI syntax rules - */ - - public APICharityScraper(HttpClient client) throws URISyntaxException { - this.client = client; - this.request = HttpRequest.newBuilder() - .uri(new URI(API_url)) - .GET() - .build(); + private final HttpClient client; + private final HttpRequest request; + private static final String API_url = "https://app.innsamlingskontrollen.no/api/public/v1/all"; + + /** + * Constructs a new APICharityScraper object. + * + * @param client the client responsible for making the http connection. + * @throws URISyntaxException if the API URL is malformed or violates URI syntax rules + */ + public APICharityScraper(HttpClient client) throws URISyntaxException { + this.client = client; + this.request = HttpRequest.newBuilder().uri(new URI(API_url)).GET().build(); + } + + /** + * Checks if the http request returns an 'OK' response. + * + * @return {@code true} if connection was successful + * @throws IOException if an I/O error occurs while sending or receiving the HTTP reques + * @throws InterruptedException if the operation is interrupted while waiting for the response + * @throws RuntimeException if the connection is unsuccessful + */ + public boolean checkConnection() throws IOException, InterruptedException { + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + if (response.statusCode() == 200) { + return true; } - - /** - * Checks if the http request returns an 'OK' response. - * - * @return {@code true} if connection was successful - * @throws IOException if an I/O error occurs while sending or receiving the HTTP reques - * @throws InterruptedException if the operation is interrupted while waiting for the response - * @throws RuntimeException if the connection is unsuccessful - */ - - public boolean checkConnection() throws IOException, InterruptedException { - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - - if (response.statusCode() == 200) { - return true; - } - throw new RuntimeException("Connection failed: HTTP error code: " + response.statusCode() + - "\nResponse text: " + response.body() - ); + throw new RuntimeException( + "Connection failed: HTTP error code: " + + response.statusCode() + + "\nResponse text: " + + response.body()); + } + + /** + * Fetches the JSON data from the IK API and stores it in a String. + * + * @return a String of the JSON values in IK API + * @throws IOException if an I/O error occurs while sending or receiving the HTTP request + * @throws InterruptedException if the operation is interrupted while waiting for the response + */ + public String getJSONData() throws IOException, InterruptedException { + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + return response.body(); + } + + /** + * Parses the JSON data using gson and creates a list of APICharityData objects. + * + * @param JSONData the {@code String} of JSON data to be parsed + * @return a list of APICharityDaya objects + * @throws com.google.gson.JsonSyntaxException if the provided JSON is not valid + */ + // public List parseJSON(String JSONData) { + public CharityRegistry parseJSON(String JSONData) { + Gson gson = new Gson(); + APICharityData[] charityData = gson.fromJson(JSONData, APICharityData[].class); + + if (charityData == null) { + // return new ArrayList<>(); + return new CharityRegistry(); } - /** - * Fetches the JSON data from the IK API and stores it in a String. - * - * @return a String of the JSON values in IK API - * @throws IOException if an I/O error occurs while sending or receiving the HTTP request - * @throws InterruptedException if the operation is interrupted while waiting for the response - */ - - public String getJSONData() throws IOException, InterruptedException { - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - - return response.body(); - } + // Filters out "obs" status due to non-unique org_number values + /* + return Arrays.stream(charityData) + .filter(c-> !"obs".equalsIgnoreCase(c.getStatus())) + .toList(); - /** - * Parses the JSON data using gson and creates a list of APICharityData objects. - * - * @param JSONData the {@code String} of JSON data to be parsed - * @return a list of APICharityDaya objects - * @throws com.google.gson.JsonSyntaxException if the provided JSON is not valid */ - //public List parseJSON(String JSONData) { - public CharityRegistry parseJSON(String JSONData) { - Gson gson = new Gson(); - APICharityData[] charityData = gson.fromJson(JSONData, APICharityData[].class); - - if (charityData == null) { - //return new ArrayList<>(); - return new CharityRegistry(); - } - - // Filters out "obs" status due to non-unique org_number values - /* - return Arrays.stream(charityData) - .filter(c-> !"obs".equalsIgnoreCase(c.getStatus())) - .toList(); - */ - - // Alternate using predefiend Charity class - CharityRegistry charityRegistry = new CharityRegistry(); - for (APICharityData apiCharityData : charityData){ - Charity charity = new Charity( - apiCharityData.getOrg_number(), - apiCharityData.getUrl(), - apiCharityData.getName(), - apiCharityData.getIs_pre_approved(), - apiCharityData.getStatus() - ); - charityRegistry.addCharity(charity); - } - return charityRegistry; + // Alternate using predefiend Charity class + CharityRegistry charityRegistry = new CharityRegistry(); + for (APICharityData apiCharityData : charityData) { + Charity charity = + new Charity( + apiCharityData.getOrg_number(), + apiCharityData.getUrl(), + apiCharityData.getName(), + apiCharityData.getIs_pre_approved(), + apiCharityData.getStatus()); + charityRegistry.addCharity(charity); } - /** - * Synchronizes the {@code charities} table with the provided list of APICharityData. - *

- * Summary of function: - *

    - *
  • Inserts new records
  • - *
  • Updates existing records using {@code ON DUPLICATE KEY UPDATE}
  • - *
  • Removes database records that are not present in the provided list
  • - *
- * A temporary table is used to determine which records should be deleted. - *

- * - * @param charities a list of ApiCharityDaya objects - * @throws RuntimeException if a {@link SQLException} occurs during database synchronization - */ + return charityRegistry; + } + /** + * Synchronizes the {@code charities} table with the provided list of APICharityData. + * + *

Summary of function: + * + *

    + *
  • Inserts new records + *
  • Updates existing records using {@code ON DUPLICATE KEY UPDATE} + *
  • Removes database records that are not present in the provided list + *
+ * + * A temporary table is used to determine which records should be deleted. + * + * @param charities a list of ApiCharityDaya objects + * @throws RuntimeException if a {@link SQLException} occurs during database synchronization + */ }