diff --git a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java index 073fd81..2357e87 100644 --- a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java +++ b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java @@ -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:"; @@ -24,6 +27,11 @@ public class DbWrapper { private List 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'"; @@ -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); @@ -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(); @@ -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 importUsers() { PreparedStatement ps = null; ResultSet results = null; @@ -93,7 +125,17 @@ public List importUsers() { return this.users; } - public int exportUsers(List 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 data) throws IllegalArgumentException { this.importUsers(); if (data == null) { @@ -102,12 +144,14 @@ public int exportUsers(List 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"); @@ -123,7 +167,13 @@ public int exportUsers(List 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]); @@ -144,10 +194,21 @@ public int exportUsers(List data) { return rowsAffected; } + /** + * Imports all donations. + * + * @return A List of Object arrays for each donation in the database. + */ public List 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 importDonations(int user_id) { return this.importDonations(user_id, false); } @@ -155,7 +216,7 @@ public List importDonations(int user_id) { private List importDonations(int user_id, boolean all) { PreparedStatement ps = null; ResultSet results = null; - try{ + try { if (all) { ps = this.connection.prepareStatement("SELECT * FROM donations"); } else { @@ -185,7 +246,17 @@ private List importDonations(int user_id, boolean all) { return this.donations; } - public int exportDonations(List 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 data) throws IllegalArgumentException { this.fetchAllDonations(); if (data == null) { @@ -212,7 +283,13 @@ public int exportDonations(List 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++) {