Skip to content

Commit

Permalink
style[DbWrapper]: add javadocs and split lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Ciara Herud-Thomassen authored and Lucy Ciara Herud-Thomassen committed Apr 9, 2026
1 parent ff2e953 commit 1960fce
Showing 1 changed file with 82 additions and 5 deletions.
87 changes: 82 additions & 5 deletions src/main/java/edu/group5/app/model/wrapper/DbWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* A class for wrapping the database.
*/
public class DbWrapper {
protected Connection connection;
private static final String CONNECTION_TYPE = "jdbc:h2:";
Expand All @@ -24,6 +27,11 @@ public class DbWrapper {
private List<Object[]> donations;
private Logger logger = Logger.getLogger(DbWrapper.class.getName());

/**
* The constructor, which constructs a String for connecting to the database.
*
* @param test Whether to construct the connection String for testing (in-memory) or not.
*/
public DbWrapper(boolean test) {
if (test) {
this.connectionString = CONNECTION_TYPE + "mem:test;" + DB_SCRIPT + "test_init.sql'";
Expand All @@ -33,6 +41,11 @@ public DbWrapper(boolean test) {
this.logger.info("connectionString constructed");
}

/**
* Connects to the database, and returns the result, logging failures.
*
* @return True if successful, false if not.
*/
public boolean connect() {
try {
this.connection = DriverManager.getConnection(this.connectionString);
Expand All @@ -49,7 +62,13 @@ public boolean connect() {
}
}

/**
* Disconnects the database connection, logging failures.
*
* @return True if successful, false if not.
*/
public boolean disconnect() {
// We are not interested in whether it fails to close, as we check its closed status later.
try{ this.connection.close(); } catch (Exception e) {};
try {
return this.connection.isClosed();
Expand All @@ -59,12 +78,25 @@ public boolean disconnect() {
}
}

/**
* Closes queries and results.
*
* @param results The ResultSet to close, can be null.
* @param ps The PreparedStatement to close, can be null.
*/
private void close(ResultSet results, PreparedStatement ps) {
// This method can take null arguments, so an exception is expected.
try { results.close(); } catch (Exception e) {}
try { ps.close(); } catch (Exception e) {}
this.logger.info("results and ps closed");
}

/**
* Gets all users from the database.
*
* @return The users from the database returned as a List of Object arrays, where each Object
* array represents a user and a row in the users table in the database.
*/
public List<Object[]> importUsers() {
PreparedStatement ps = null;
ResultSet results = null;
Expand Down Expand Up @@ -93,7 +125,17 @@ public List<Object[]> importUsers() {
return this.users;
}

public int exportUsers(List<Object[]> data) {
/**
* Puts new users into the database.
*
* @param data The new users to put into the database. Each Object array in the List is a new
* user to add as a row.
* @return The number of rows affected in the transaction.
* @throws IllegalArgumentException This exception is thrown when data is null, its rows are not
* of length 6, any of the rows are null, any of the rows are duplicates or existing rows in
* the database, or any of the values in the rows can't be cast to the correct data-types.
*/
public int exportUsers(List<Object[]> data) throws IllegalArgumentException {
this.importUsers();

if (data == null) {
Expand All @@ -102,12 +144,14 @@ public int exportUsers(List<Object[]> data) {
if (data.isEmpty()) {
return 0;
}
// TODO: change to check length for every row.
if (data.get(0).length != 6) {
throw new IllegalArgumentException("data's arrays must have a length of 6");
}
if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) {
throw new IllegalArgumentException("One or more rows in data contains null values");
}
// TODO: change how existing rows are checked.
if (this.users.size() > 0) {
if ((int) data.getLast()[0] <= (int) this.users.getLast()[0]) {
throw new IllegalArgumentException("data can't contain existing rows");
Expand All @@ -123,7 +167,13 @@ public int exportUsers(List<Object[]> data) {
int rowsAffected = 0;
try {
ps = this.connection.prepareStatement(
"INSERT INTO users (user_id, role, first_name, last_name, email, password_hash) VALUES (?, ?, ?, ?, ?, ?)");
"""
INSERT INTO users
(user_id, role, first_name, last_name, email, password_hash)
VALUES
(?, ?, ?, ?, ?, ?)
"""
);
for (Object[] row : data) {
try {
ps.setInt(1, (int) row[0]);
Expand All @@ -144,18 +194,29 @@ public int exportUsers(List<Object[]> data) {
return rowsAffected;
}

/**
* Imports all donations.
*
* @return A List of Object arrays for each donation in the database.
*/
public List<Object[]> fetchAllDonations() {
return this.importDonations(0, true);
}

/**
* Imports the donations of a specific user based on a given user_id.
*
* @param user_id The id of the user to get the donations of.
* @return A List of Object arrays for each donation in the database.
*/
public List<Object[]> importDonations(int user_id) {
return this.importDonations(user_id, false);
}

private List<Object[]> importDonations(int user_id, boolean all) {
PreparedStatement ps = null;
ResultSet results = null;
try{
try {
if (all) {
ps = this.connection.prepareStatement("SELECT * FROM donations");
} else {
Expand Down Expand Up @@ -185,7 +246,17 @@ private List<Object[]> importDonations(int user_id, boolean all) {
return this.donations;
}

public int exportDonations(List<Object[]> data) {
/**
* Puts new donations into the database.
*
* @param data The new donation to put into the database. Each Object array in the List is a new
* donations to add as a row.
* @return The number of rows affected in the transaction.
* @throws IllegalArgumentException This exception is thrown when data is null, its rows are not
* of length 6, any of the rows are null, any of the rows are duplicates or existing rows in
* the database, or any of the values in the rows can't be cast to the correct data-types.
*/
public int exportDonations(List<Object[]> data) throws IllegalArgumentException {
this.fetchAllDonations();

if (data == null) {
Expand All @@ -212,7 +283,13 @@ public int exportDonations(List<Object[]> data) {
int rowsAffected = 0;
try {
ps = this.connection.prepareStatement(
"INSERT INTO donations (donation_id, user_id, organization_id, amount, dating, payment_method) VALUES (?, (SELECT user_id FROM users WHERE user_id = ?), ?, ?, ?, ?)");
"""
INSERT INTO donations
(donation_id, user_id, organization_id, amount, dating, payment_method)
VALUES
(?, (SELECT user_id FROM users WHERE user_id = ?), ?, ?, ?, ?)
"""
);
for (Object[] row : data) {
try {
for (int i = 0; i < 3; i++) {
Expand Down

0 comments on commit 1960fce

Please sign in to comment.