Skip to content

Commit

Permalink
refactor[DbWrapper&ParameterValidator]: move parameter validation in …
Browse files Browse the repository at this point in the history
…exports to ParameterValidator class
  • Loading branch information
Lucy Ciara Herud-Thomassen authored and Lucy Ciara Herud-Thomassen committed Apr 9, 2026
1 parent c3b0555 commit ab64cda
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
45 changes: 7 additions & 38 deletions src/main/java/edu/group5/app/model/wrapper/DbWrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.group5.app.model.wrapper;

import java.lang.reflect.Parameter;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
Expand All @@ -15,6 +16,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.group5.app.utils.ParameterValidator;

/**
* A class for wrapping the database.
*/
Expand Down Expand Up @@ -137,30 +140,11 @@ public List<Object[]> importUsers() {
*/
public int exportUsers(List<Object[]> data) throws IllegalArgumentException {
this.importUsers();

if (data == null) {
throw new IllegalArgumentException("data can't be null");
}

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");
}
}
Set<Object> ids = new HashSet<>();
if (data.stream().anyMatch(i -> !ids.add(i[0]))) {
throw new IllegalArgumentException("data can't contain duplicate rows");
}
ParameterValidator.exportChecker(data, "data", this.users);


PreparedStatement ps = null;
Expand Down Expand Up @@ -258,26 +242,11 @@ private List<Object[]> importDonations(int user_id, boolean all) {
*/
public int exportDonations(List<Object[]> data) throws IllegalArgumentException {
this.fetchAllDonations();

if (data == null) {
throw new IllegalArgumentException("data can't be null");
}

if (data.isEmpty()) {
return 0;
}
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");
}
if (this.donations.size() > 0 && (int) data.getLast()[0] <= (int) this.donations.getLast()[0]) {
throw new IllegalArgumentException("data can't contain existing rows");
}
Set<Object> ids = new HashSet<>();
if (data.stream().anyMatch(i -> !ids.add(i[0]))) {
throw new IllegalArgumentException("data can't contain duplicate rows");
}
ParameterValidator.exportChecker(data, "data", this.donations);

PreparedStatement ps = null;
int rowsAffected = 0;
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/edu/group5/app/utils/ParameterValidator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package edu.group5.app.utils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* ParameterValidator is a utility class that provides static methods for validating various types
Expand Down Expand Up @@ -82,4 +86,43 @@ private static final void nullCheck(Object variable, String variableName)
throw new IllegalArgumentException(String.format("%s can't be null", variableName));
}
}

/**
* A method for checking if the data to be exported is valid.
*
* @param data The data to export.
* @param dataName The name of the variable of the data to export.
* @param oldData The existing data to compare to for checking row existence.
* @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 static final void exportChecker(
List<Object[]> data, String dataName, List<Object[]> oldData
) throws IllegalArgumentException {
if (data == null) {
throw new IllegalArgumentException(String.format("%s can't be null", dataName));
}
// TODO: change to check length for every row.
if (data.get(0).length != 6) {
throw new IllegalArgumentException(
String.format("%s's arrays must have a length of 6", dataName)
);
}
if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) {
throw new IllegalArgumentException(
String.format("One or more rows in %s contains null values", dataName)
);
}
// TODO: change how existing rows are checked.
if (oldData.size() > 0) {
if ((int) data.getLast()[0] <= (int) oldData.getLast()[0]) {
throw new IllegalArgumentException("data can't contain existing rows");
}
}
Set<Object> ids = new HashSet<>();
if (data.stream().anyMatch(i -> !ids.add(i[0]))) {
throw new IllegalArgumentException("data can't contain duplicate rows");
}
}
}

0 comments on commit ab64cda

Please sign in to comment.