Skip to content

Commit

Permalink
Feat: Added JavaDoc to CharitySelect and Test file
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 12, 2026
1 parent 2bc2a18 commit 8d3c6f1
Show file tree
Hide file tree
Showing 2 changed files with 425 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,126 +1,168 @@
package ntnu.systemutvikling.team6.database.Readers;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import ntnu.systemutvikling.team6.database.DatabaseConnection;
import ntnu.systemutvikling.team6.models.Charity;
import ntnu.systemutvikling.team6.models.CharityRegistry;
import ntnu.systemutvikling.team6.models.Feedback;
import ntnu.systemutvikling.team6.models.user.User;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;

/**
* Data access class responsible for reading charity-related data from the database.
*
* <p>Provides methods to retrieve all charities (with their associated feedback and users) as well
* as feedback entries for a specific charity by UUID.
*
* <p>All queries are executed against a MySQL database via a {@link DatabaseConnection}.
*/
public class CharitySelect {
private final DatabaseConnection connection;
private final DatabaseConnection connection;

public CharitySelect(DatabaseConnection connection){
this.connection = connection;
}
/**
* Constructs a new {@code CharitySelect} with the given database connection.
*
* @param connection the {@link DatabaseConnection} to use for executing queries; must not be
* {@code null}
*/
public CharitySelect(DatabaseConnection connection) {
this.connection = connection;
}

public CharityRegistry getCharitiesFromDB() {
CharityRegistry registry = null;
Connection conn = null;
try {
conn = connection.getMySqlConnection();
String sql_query =
"""
SELECT
/**
* Retrieves all charities from the database, including their associated feedback and the users
* who submitted each piece of feedback.
*
* <p>The 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
* that charity are appended to its feedback list.
*
* <p>Note: charities with no feedback 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)
* @throws RuntimeException if a {@link SQLException} occurs while executing the query
*/
public CharityRegistry getCharitiesFromDB() {
CharityRegistry registry = null;
Connection conn = null;
try {
conn = connection.getMySqlConnection();
String sql_query =
"""
SELECT
c.UUID_charities, c.org_number, c.charity_name, c.charity_link, c.pre_approved, c.status,
f.UUID_feedback, f.feedback_comment, f.feedback_date, f.isAnonymous, f.charity_id, f.user_id,
u.UUID_user, u.user_name, u.user_email, u.user_password, u.role
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
""";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql_query);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql_query);

Charity currentCharity = null;
String lastCharity = null;
Charity currentCharity = null;
String lastCharity = null;

registry = new CharityRegistry();
while (rs.next()) {
String currentId = rs.getString("UUID_charities");
registry = new CharityRegistry();
while (rs.next()) {
String currentId = rs.getString("UUID_charities");

if (lastCharity == null || !currentId.equals(lastCharity)) {
currentCharity =
new Charity(
rs.getString("UUID_charities"),
rs.getString("org_number"),
rs.getString("charity_link"),
rs.getString("charity_name"),
rs.getBoolean("pre_approved"),
rs.getString("status")
);
registry.addCharity(currentCharity);
lastCharity = currentId;
}
String feedbackId = rs.getString("UUID_feedback");
if (feedbackId != null){
User userWithNoSettingsAndInbox = new User(
rs.getString("UUID_User"),
rs.getString("user_name"),
rs.getString("user_email"),
rs.getString("user_password"),
rs.getString("role")
);
if (lastCharity == null || !currentId.equals(lastCharity)) {
currentCharity =
new Charity(
rs.getString("UUID_charities"),
rs.getString("org_number"),
rs.getString("charity_link"),
rs.getString("charity_name"),
rs.getBoolean("pre_approved"),
rs.getString("status"));
registry.addCharity(currentCharity);
lastCharity = currentId;
}
String feedbackId = rs.getString("UUID_feedback");
if (feedbackId != null) {
User userWithNoSettingsAndInbox =
new User(
rs.getString("UUID_User"),
rs.getString("user_name"),
rs.getString("user_email"),
rs.getString("user_password"),
rs.getString("role"));

Feedback feedback = new Feedback(
rs.getString("UUID_feedback"),
userWithNoSettingsAndInbox,
rs.getString("feedback_comment"),
LocalDate.parse(rs.getString("feedback_date"))
);
Feedback feedback =
new Feedback(
rs.getString("UUID_feedback"),
userWithNoSettingsAndInbox,
rs.getString("feedback_comment"),
LocalDate.parse(rs.getString("feedback_date")));

currentCharity.getFeedbacks().add(feedback);
}
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR: Something went wrong during updating charities table.");
currentCharity.getFeedbacks().add(feedback);
}
return registry;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR: Something went wrong during updating charities table.");
}
public ArrayList<Feedback> getFeedbackforCharityUUID(String charity_uuid) {
ArrayList<Feedback> Feedbacks = new ArrayList<>();
Connection conn = null;
try {
conn = connection.getMySqlConnection();
String sql_query =
"""
return registry;
}

/**
* A helper function that retrieves all feedback entries associated with a specific charity,
* identified by its UUID. Currently, has no use.
*
* <p>Each {@link Feedback} object is populated with the associated {@link User} (without settings
* or inbox data). The query uses a LEFT JOIN between the {@code Feedback} and {@code User}
* tables, filtered by {@code charity_id}.
*
* @param charity_uuid the UUID of the charity whose feedback should be retrieved; must not be
* {@code null}
* @return an {@link ArrayList} of {@link Feedback} objects for the given charity; returns an
* empty list if no feedback exists for that charity
* @throws RuntimeException if any exception occurs while executing the query, wrapping the
* original cause
*/
public ArrayList<Feedback> getFeedbackforCharityUUID(String charity_uuid) {
ArrayList<Feedback> Feedbacks = new ArrayList<>();
Connection conn = null;
try {
conn = connection.getMySqlConnection();
String sql_query =
"""
SELECT
f.UUID_feedback, f.feedback_comment, f.feedback_date, f.isAnonymous, f.charity_id, f.user_id,
u.UUID_user, u.user_name, u.user_email, u.user_password, u.role
u.UUID_user, u.user_name, u.user_email, u.user_password, u.role
FROM Feedback f
LEFT JOIN User u ON f.user_id = u.UUID_user
LEFT JOIN User u ON f.user_id = u.UUID_user
WHERE f.charity_id = ?;
""";
PreparedStatement stmt = conn.prepareStatement(sql_query);
stmt.setString(1, charity_uuid);
ResultSet rs = stmt.executeQuery();
PreparedStatement stmt = conn.prepareStatement(sql_query);
stmt.setString(1, charity_uuid);
ResultSet rs = stmt.executeQuery();

while (rs.next()){
User userWithNoSettingsAndInbox = new User(
rs.getString("UUID_User"),
rs.getString("user_name"),
rs.getString("user_email"),
rs.getString("user_password"),
rs.getString("role")
);
Feedback feedback = new Feedback(
rs.getString("UUID_feedback"),
userWithNoSettingsAndInbox,
rs.getString("feedback_comment"),
LocalDate.parse(rs.getString("feedback_date"))
);
Feedbacks.add(feedback);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
conn = null;
}
return Feedbacks;
while (rs.next()) {
User userWithNoSettingsAndInbox =
new User(
rs.getString("UUID_User"),
rs.getString("user_name"),
rs.getString("user_email"),
rs.getString("user_password"),
rs.getString("role"));
Feedback feedback =
new Feedback(
rs.getString("UUID_feedback"),
userWithNoSettingsAndInbox,
rs.getString("feedback_comment"),
LocalDate.parse(rs.getString("feedback_date")));
Feedbacks.add(feedback);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
conn = null;
}
return Feedbacks;
}
}
Loading

0 comments on commit 8d3c6f1

Please sign in to comment.