Skip to content

Commit

Permalink
refactor[wrapper]&feat[DbWrapper]: move wrapper classes to its own fo…
Browse files Browse the repository at this point in the history
…lder, and add a class for database communication
  • Loading branch information
Lucy Ciara Herud-Thomassen authored and Lucy Ciara Herud-Thomassen committed Mar 12, 2026
1 parent 1c56f3c commit bdd2284
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 65 deletions.
29 changes: 0 additions & 29 deletions src/main/java/edu/group5/app/control/DbWrapper.java

This file was deleted.

113 changes: 113 additions & 0 deletions src/main/java/edu/group5/app/control/wrapper/DbWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package edu.group5.app.control.wrapper;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class DbWrapper {
protected Connection connection;
private static final String CONNECTION_TYPE = "jdbc:h2:";
private static final String DB_SCRIPT = "INIT=RUNSCRIPT FROM 'classpath:";
private String connectionString;

public DbWrapper(boolean test) {
if (test) {
this.connectionString = CONNECTION_TYPE + "mem:test;" + DB_SCRIPT + "test_init.sql'";
} else {
this.connectionString = CONNECTION_TYPE + "file:./help-me-help;" + DB_SCRIPT + "init.sql'";
}
}

public boolean connect() throws SQLException {
try {
this.connection = DriverManager.getConnection(this.connectionString);
} catch (SQLException SQLe) {
System.out.println(SQLe.getMessage());
}
return connection.isValid(0);
}

public List<Object[]> importUser() throws SQLException {
PreparedStatement ps = this.connection.prepareStatement("SELECT * FROM user");
ResultSet results = ps.executeQuery();
List<Object[]> data = new ArrayList<Object[]>();
while (results.next()) {
data.add(
new Object[] {
results.getInt("user_id"),
results.getString("role"),
results.getString("first_name"),
results.getString("last_name"),
results.getString("email"),
results.getString("password_hash")
});
}
return data;
}

public int exportUser(List<Object[]> data) throws SQLException {
if (data == null) {
throw new IllegalArgumentException("data can't be null");
}
if (data.get(0).length != 6) {
throw new IllegalArgumentException("data's arrays must have a length of 6");
}
int rowsAffected = 0;
PreparedStatement ps = this.connection.prepareStatement(
"INSERT INTO user (user_id, role, first_name, last_name, email, password_hash) VALUES (?, ?, ?, ?, ?, ?)");
for (Object[] row : data) {
ps.setInt(1, (int) row[0]);
for (int i = 1; i < row.length; i++) {
ps.setString(i + 1, (String) row[i]);
}
rowsAffected = ps.executeUpdate();
}
return rowsAffected;
}

public List<Object[]> importDonations(int user_id) throws SQLException {
PreparedStatement ps = this.connection.prepareStatement("SELECT * FROM donations WHERE user_id = ?");
ps.setInt(1, user_id);
ResultSet results = ps.executeQuery();
List<Object[]> data = new ArrayList<Object[]>();
while (results.next()) {
data.add(
new Object[] {
results.getInt(1),
results.getInt(2),
results.getInt(3),
results.getBigDecimal(4),
results.getTimestamp(5),
results.getString(6)
});
}
return data;
}

public int exportDonations(List<Object[]> data) throws SQLException {
if (data == null) {
throw new IllegalArgumentException("data can't be null");
}
if (data.get(0).length == 6) {
throw new IllegalArgumentException("data's arrays must have a length of 6");
}
int rowsAffected = 0;
PreparedStatement ps = this.connection.prepareStatement(
"INSERT INTO donations (donation_id, user_id, organization_id, amount, dating, payment_method) VALUES (?, ?, ?, ?, ?, ?)");
for (Object[] row : data) {
for (int i = 0; i < 3; i++) {
ps.setInt(i + 1, (int) row[i]);
}
ps.setBigDecimal(4, (BigDecimal) row[3]);
ps.setTimestamp(5, (Timestamp) row[4]);
rowsAffected = ps.executeUpdate();
}
return rowsAffected;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.group5.app.control;
package edu.group5.app.control.wrapper;

import java.io.IOException;
import java.net.URI;
Expand All @@ -17,7 +17,8 @@ public class OrgApiWrapper extends Wrapper {
private HttpRequest request;

/**
* The constructor, which takes a url String and constructs a URI and HttpRequest object from it.
* The constructor, which takes a url String and constructs a URI and
* HttpRequest object from it.
* If the url is invalid, it will throw a fitting exception.
*
* @param urlString A string of the URL that's being connected to.
Expand All @@ -44,11 +45,13 @@ public OrgApiWrapper(String urlString) {
/**
* A method for importing data from the wrapped API.
*
* @return Returns a boolean, which indicates if the import was successful. Will be False if, for
* example, there is no internet connection.
* @return Returns a boolean, which indicates if the import was successful. Will
* be False if, for
* example, there is no internet connection.
*
* @throws InterruptedException This exception is thrown whenever the program is interrupted, like
* by ctrl + c.
* @throws InterruptedException This exception is thrown whenever the program is
* interrupted, like
* by ctrl + c.
*/
@Override
public boolean importData() throws InterruptedException {
Expand All @@ -67,7 +70,8 @@ public boolean importData() throws InterruptedException {
/**
* A method for accessing the imported data.
*
* @return Returns an array with HashMaps, which is how data is structured in the API.
* @return Returns an array with HashMaps, which is how data is structured in
* the API.
*/
@Override
public Object[] getData() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.group5.app.control;
package edu.group5.app.control.wrapper;

/**
* An abstract class for all Wrappers of datasets.
Expand All @@ -9,13 +9,16 @@ protected Wrapper() {
}

/**
* An abstract method for importing data from the dataset that child methods wrap.
* An abstract method for importing data from the dataset that child methods
* wrap.
*
* @return Returns a boolean, which indicates if the import was successful. Will be False if, for
* example, there is no internet connection.
* @return Returns a boolean, which indicates if the import was successful. Will
* be False if, for
* example, there is no internet connection.
*
* @throws InterruptedException This exception is thrown whenever the program is interrupted, like
* by ctrl + c.
* @throws InterruptedException This exception is thrown whenever the program is
* interrupted, like
* by ctrl + c.
*/
public abstract boolean importData() throws InterruptedException;

Expand Down
23 changes: 12 additions & 11 deletions src/main/resources/init.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
CREATE TABLE IF NOT EXISTS
users (
user_id int PRIMARY KEY,
first_name varchar(20),
last_name varchar(20),
email varchar(20),
password_hash varchar(72)
user_id INT PRIMARY KEY,
role VARCHAR(20),
first_name VARCHAR(20),
last_name VARCHAR(20),
email VARCHAR(20),
password_hash VARCHAR(72)
);

CREATE TABLE IF NOT EXISTS
donations (
donation_id int PRIMARY KEY,
user_id int NOT NULL,
organization_id int NOT NULL,
amount double NOT NULL,
dating timestamp NOT NULL,
payment_method varchar(20),
donation_id INT PRIMARY KEY,
user_id INT NOT NULL,
organization_id INT NOT NULL,
amount DECIMAL NOT NULL,
dating TIMESTAMP NOT NULL,
payment_method VARCHAR(20),
CONSTRAINT fk_user
FOREIGN KEY (user_id)
REFERENCES users(user_id)
Expand Down
25 changes: 13 additions & 12 deletions src/main/resources/test_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ DROP TABLE IF EXISTS users, donations;

CREATE TABLE IF NOT EXISTS
users (
user_id int PRIMARY KEY,
first_name varchar(20),
last_name varchar(20),
email varchar(20),
password_hash varchar(72)
user_id INT PRIMARY KEY,
role VARCHAR(20),
first_name VARCHAR(20),
last_name VARCHAR(20),
email VARCHAR(20),
password_hash VARCHAR(72)
);

CREATE TABLE IF NOT EXISTS
donations (
donation_id int PRIMARY KEY,
user_id int NOT NULL,
organization_id int NOT NULL,
amount double NOT NULL,
dating timestamp NOT NULL,
payment_method varchar(20),
donation_id INT PRIMARY KEY,
user_id INT NOT NULL,
organization_id INT NOT NULL,
amount DECIMAL NOT NULL,
dating TIMESTAMP NOT NULL,
payment_method VARCHAR(20),
CONSTRAINT fk_user
FOREIGN KEY (user_id)
REFERENCES users(user_id)
);
);;

0 comments on commit bdd2284

Please sign in to comment.