Skip to content

Commit

Permalink
Update[ParameterValidator]: Update codebase to use ParameterValidator…
Browse files Browse the repository at this point in the history
… class where it suits the best
  • Loading branch information
Fredrik Marjoni committed Apr 16, 2026
1 parent 2d3df56 commit b91eecd
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 109 deletions.
9 changes: 8 additions & 1 deletion src/main/java/edu/group5/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class App extends Application {
NavigationController nav;

private Logger logger;
static int MAX_ReTRIES = 3;

@Override
public void init() {
Expand All @@ -44,8 +45,14 @@ public void init() {
this.dbWrapper = new DbWrapper(false);
OrgApiWrapper orgApiWrapper = new OrgApiWrapper("https://app.innsamlingskontrollen.no/api/public/v1/all");

while (!dbWrapper.connect()) {
int retries = 0;
while (!dbWrapper.connect() && retries < MAX_ReTRIES) {
this.logger.warning("Failed to connect to database");
retries++;
}
if (retries == MAX_ReTRIES) {
this.logger.severe("Failed to connect to database after " + MAX_ReTRIES + " attempts");
throw new RuntimeException("Failed to connect to database");
}

// Load data from database
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/edu/group5/app/control/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.group5.app.model.AppState;
import edu.group5.app.model.user.User;
import edu.group5.app.model.user.UserService;
import edu.group5.app.utils.ParameterValidator;
import edu.group5.app.view.loginpage.LoginPageView;
import edu.group5.app.view.loginpage.SignUpPageView;
import javafx.scene.control.Alert;
Expand Down Expand Up @@ -31,6 +32,9 @@ public class AuthController {
private final UserService userService;

public AuthController(AppState appState, NavigationController nav, UserService userService) {
ParameterValidator.objectChecker(appState, "AppState");
ParameterValidator.objectChecker(nav, "NavigationController");
ParameterValidator.objectChecker(userService, "UserService");
this.appState = appState;
this.nav = nav;
this.userService = userService;
Expand Down Expand Up @@ -69,7 +73,7 @@ public void handleSignUp(SignUpPageView view, String firstName, String lastName,
// Clears password char array after creating a hash.
String hashedPassword = encoder.encode(new String(passwordChars));
for (int i = 0; i < passwordChars.length; i++) {
passwordChars[0] = '0';
passwordChars[i] = '\u0000';
}

Alert privacyPolicy = new Alert(Alert.AlertType.CONFIRMATION);
Expand All @@ -86,9 +90,8 @@ public void handleSignUp(SignUpPageView view, String firstName, String lastName,

if (success) {
User user = userService.getUserByEmail(email);

appState.setCurrentUser(user);
nav.showHomePage();
appState.setCurrentUser(user);
nav.showHomePage();
} else {
view.showError("Registration failed. Email may already be in use.");
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/edu/group5/app/control/DonationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import edu.group5.app.model.organization.Organization;
import edu.group5.app.model.user.Customer;
import edu.group5.app.model.user.User;
import edu.group5.app.utils.ParameterValidator;

import java.math.BigDecimal;
import java.util.HashSet;
Expand Down Expand Up @@ -34,6 +35,9 @@ public class DonationController {
private final DonationService service;

public DonationController(AppState appState, NavigationController nav, DonationService service) {
ParameterValidator.objectChecker(appState, "AppState");
ParameterValidator.objectChecker(nav, "NavigationController");
ParameterValidator.objectChecker(service, "DonationService");
this.appState = appState;
this.nav = nav;
this.service = service;
Expand All @@ -46,6 +50,7 @@ public DonationController(AppState appState, NavigationController nav, DonationS
* @return a map of donations.
*/
public Map<Integer, Donation> getUserDonations(int userId) {
ParameterValidator.intChecker(userId, "User ID");
return service.getDonationRepository().filterByUser(userId);
}

Expand All @@ -56,7 +61,11 @@ public Map<Integer, Donation> getUserDonations(int userId) {
* @return a set of organization IDs
*/
public Set<Integer> getUniqueOrganizationIDs() {
Map<Integer, Donation> userDonations = getUserDonations(appState.getCurrentUser().getUserId());
User currentUser = appState.getCurrentUser();
if (currentUser == null) {
throw new IllegalStateException("No user logged in");
}
Map<Integer, Donation> userDonations = getUserDonations(currentUser.getUserId());

Set<Integer> uniqueOrganizations = new HashSet<>();
for (Donation donation : userDonations.values()) {
Expand Down Expand Up @@ -146,19 +155,26 @@ private void handleDonate() {
);

if (success) {
System.out.println("Donation created: " + amount + " kr to " + currentOrg.name() + ", with payment method: " + paymentMethod);
System.out.println("Donation created: "
+ amount + " kr to " + currentOrg.name()
+ ", with payment method: " + paymentMethod);
} else {
System.err.println("Failed to create donation");
}

// Clear donation session state
appState.setCurrentDonationAmount(null);
// Clear org
appState.setCurrentOrganization(null);
// Clear payment method
appState.setCurrentPaymentMethod(null);

// Navigate to payment complete
nav.showPaymentCompletePage();
}

private void showError(String message) {
ParameterValidator.stringChecker(message, "message");
Alert errorAlert = new Alert(Alert.AlertType.WARNING);
errorAlert.setTitle("Donation Error");
errorAlert.setHeaderText("Cannot Process Donation");
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/edu/group5/app/control/NavigationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.group5.app.model.donation.DonationService;
import edu.group5.app.model.organization.OrganizationService;
import edu.group5.app.model.user.UserService;
import edu.group5.app.utils.ParameterValidator;
import edu.group5.app.view.Header;
import edu.group5.app.view.causespage.CausesPageView;
import edu.group5.app.view.donationpage.DonationPageView;
Expand All @@ -30,7 +31,14 @@ public class NavigationController {
private final DonationController donationController;
private final OrganizationController organizationController;

public NavigationController(BorderPane root, AppState appState, UserService userService, DonationService donationService, OrganizationService organizationService) {
public NavigationController(BorderPane root, AppState appState, UserService userService,
DonationService donationService, OrganizationService organizationService) {
ParameterValidator.objectChecker(root, "Root BorderPane");
ParameterValidator.objectChecker(appState, "AppState");
ParameterValidator.objectChecker(userService, "UserService");
ParameterValidator.objectChecker(donationService, "DonationService");
ParameterValidator.objectChecker(organizationService, "OrganizationService");

this.root = root;
this.header = new Header(this);
this.loginHeader = new LoginHeader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import edu.group5.app.model.organization.Organization;
import edu.group5.app.model.organization.OrganizationService;
import edu.group5.app.utils.ParameterValidator;
import tools.jackson.databind.deser.bean.CreatorCandidate.Param;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand All @@ -13,10 +15,12 @@ public class OrganizationController {
private final OrganizationService service;

public OrganizationController(OrganizationService service) {
ParameterValidator.objectChecker(service, "OrganizationService");
this.service = service;
}

public Organization getOrganizationById(int orgId) {
ParameterValidator.intChecker(orgId, "Organization ID");
return service.findByOrgNumber(orgId);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/edu/group5/app/model/DBRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.HashMap;
import java.util.Map;

import edu.group5.app.utils.ParameterValidator;

import java.util.List;

/**
Expand All @@ -22,6 +25,7 @@ public abstract class DBRepository<K, V> extends Repository<K, V> {
* @param content the HashMap used to store repository entities
*/
protected DBRepository(Map<K, V> content) {
ParameterValidator.objectChecker(content, "Content");
super(content);
this.contentLock = new HashMap<>();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/edu/group5/app/model/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Map;

import edu.group5.app.utils.ParameterValidator;

/**
* Represents a repository.
*/
Expand All @@ -13,6 +15,7 @@ public abstract class Repository<K, V> {
* @param content the underlying data structure used to store entities
*/
protected Repository(Map<K, V> content) {
ParameterValidator.objectChecker(content, "content");
this.content = content;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.group5.app.model.donation;

import edu.group5.app.model.DBRepository;
import edu.group5.app.utils.ParameterValidator;

import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -32,9 +33,7 @@ public class DonationRepository extends DBRepository<Integer, Donation> {
*/
public DonationRepository(List<Object[]> rows) {
super(new HashMap<>());
if (rows == null) {
throw new IllegalArgumentException("The list of rows cannot be null");
}
ParameterValidator.objectChecker(rows, "List of donation rows");
this.content = new HashMap<>();
for (Object[] row : rows) {
if (row == null || row.length != 6) {
Expand Down Expand Up @@ -88,9 +87,7 @@ public List<Object[]> export() {
* @throws IllegalArgumentException if the donationId is not positive
*/
public Donation getDonationById(int donationId) {
if (donationId <= 0) {
throw new IllegalArgumentException("Donation ID must be positive");
}
ParameterValidator.intChecker(donationId, "Donation ID");
return content.get(donationId);
}

Expand All @@ -102,7 +99,7 @@ public Donation getDonationById(int donationId) {
*/
public int getNextDonationId() {
return content.keySet().stream().max(Integer::compareTo).orElse(0) + 1;
} /* TODO change this when data database is introduced */
}

public Map<Integer, Donation> getAllDonations() {
return new HashMap<>(content);
Expand All @@ -122,9 +119,7 @@ public Map<Integer, Donation> getAllDonations() {
*/
@Override
public boolean addContent(Donation donation) {
if (donation == null) {
throw new IllegalArgumentException("Donation cannot be null");
}
ParameterValidator.objectChecker(donation, "Donation");
if (content.containsKey(donation.donationId())) {
return false;
}
Expand Down Expand Up @@ -180,9 +175,7 @@ public HashMap<Integer, Donation> sortByAmount() {
* @throws IllegalArgumentException if the orgNumber is not positive
*/
public HashMap<Integer, Donation> filterByOrganization(int orgNumber) {
if (orgNumber <= 0) {
throw new IllegalArgumentException("Organization number must be positive");
}
ParameterValidator.intChecker(orgNumber, "Organization number");
return content.entrySet()
.stream()
.filter(entry -> entry.getValue().organizationId() == orgNumber)
Expand All @@ -201,9 +194,7 @@ public HashMap<Integer, Donation> filterByOrganization(int orgNumber) {
* @throws IllegalArgumentException if the userId is not positive
*/
public HashMap<Integer, Donation> filterByUser(int userId) {
if (userId <= 0) {
throw new IllegalArgumentException("User ID must be positive");
}
ParameterValidator.intChecker(userId, "User ID");
return content.entrySet().stream()
.filter(entry -> entry.getValue().userId() == userId)
.collect(Collectors.toMap(
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/edu/group5/app/model/donation/DonationService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package edu.group5.app.model.donation;
import java.time.Instant;

import java.math.BigDecimal;
import java.sql.Timestamp;
import edu.group5.app.model.organization.Organization;
import edu.group5.app.model.organization.OrganizationRepository;
import edu.group5.app.model.user.Customer;
import edu.group5.app.utils.ParameterValidator;

/**
* DonationService class provides functionality for handling donations in the system.
Expand All @@ -27,12 +27,8 @@ public class DonationService {
*/
public DonationService(DonationRepository donationRepository,
OrganizationRepository organizationRepository) {
if (donationRepository == null) {
throw new IllegalArgumentException("DonationRepository cannot be null");
}
if (organizationRepository == null) {
throw new IllegalArgumentException("OrganizationRepository cannot be null");
}
ParameterValidator.objectChecker(donationRepository, "DonationRepository");
ParameterValidator.objectChecker(organizationRepository, "OrganizationRepository");
this.donationRepository = donationRepository;
this.organizationRepository = organizationRepository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Objects;

import edu.group5.app.utils.ParameterValidator;

/**
* Represents an organization.
*
Expand Down Expand Up @@ -45,9 +47,7 @@ public record Organization(
*/
public Organization(int orgNumber, String name, boolean trusted, String websiteUrl, boolean isPreApproved,
String description, String logoUrl) {
if (orgNumber < 0) {
throw new IllegalArgumentException("orgNumber cannot be negative");
}
ParameterValidator.intChecker(orgNumber, "Organization number");
this.orgNumber = orgNumber;
this.name = Objects.requireNonNull(name, "name cannot be null");
this.trusted = trusted;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.group5.app.model.organization;

import edu.group5.app.model.Repository;
import edu.group5.app.utils.ParameterValidator;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;

Expand Down Expand Up @@ -30,12 +31,8 @@ public class OrganizationRepository extends Repository<Integer, Organization> {
public OrganizationRepository(Object[] input, OrganizationScraper scraper) {
super(new HashMap<>());
this.grandMap = new HashMap<>();
if (input == null) {
throw new IllegalArgumentException("The input cannot be null");
}
if (scraper == null) {
throw new IllegalArgumentException("The scraper cannot be null");
}
ParameterValidator.objectChecker(input, "Input data");
ParameterValidator.objectChecker(scraper, "Scraper");
this.scraper = scraper;

ObjectMapper mapper = new ObjectMapper();
Expand All @@ -44,6 +41,11 @@ public OrganizationRepository(Object[] input, OrganizationScraper scraper) {
HashMap<String, Object> contentMap =
mapper.convertValue(obj, new TypeReference<HashMap<String, Object>>() {});

Object orgNumberObj = contentMap.get("org_number");
if (orgNumberObj == null) {
System.err.println("Skipping organization: missing org_number");
continue;
}
String orgNumberStr = ((String) contentMap.get("org_number")).replaceAll("\\s", "");
int orgNumber = Integer.parseInt(orgNumberStr);
String name = (String) contentMap.get("name");
Expand Down Expand Up @@ -101,9 +103,7 @@ public Map<Integer, Organization> getTrustedOrganizations() {
* @throws IllegalArgumentException if the organization number is not a positive integer
*/
public Organization findByOrgNumber(int orgNumber) {
if (orgNumber <= 0) {
throw new IllegalArgumentException("The Organization number must be a positive integer");
}
ParameterValidator.intChecker(orgNumber, "Organization number");
return grandMap.get(orgNumber);
}

Expand All @@ -114,9 +114,7 @@ public Organization findByOrgNumber(int orgNumber) {
* @throws IllegalArgumentException if the name is null or empty
*/
public Organization findByOrgName(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("The name cannot be null");
}
ParameterValidator.stringChecker(name, "Organization name");
return grandMap.values().stream()
.filter(org -> org.name().equalsIgnoreCase(name))
.findFirst()
Expand Down
Loading

0 comments on commit b91eecd

Please sign in to comment.