Skip to content

Commit

Permalink
Feat: Updating (org_settings) work
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 21, 2026
1 parent 6cf34de commit efd31df
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 104 deletions.
Binary file modified docs/SqlDatabase/ER-DiagramFile.mwb
Binary file not shown.
Binary file modified docs/SqlDatabase/ER-DiagramFile.mwb.bak
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ntnu.systemutvikling.team6.controller.components;

import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class CategoryTagController extends BaseController{
@FXML private Button button;
private static final String[][] TAG_COLORS = {
{"#E5F0D7", "#6E8A5C"}, // green
{"#D7E8F0", "#5C7A8A"}, // blue
{"#F0E5D7", "#8A6E5C"}, // orange
{"#EFD7F0", "#8A5C8A"}, // purple
{"#F0D7D7", "#8A5C5C"}, // red
{"#D7F0EE", "#5C8A87"}, // teal
{"#F0EDD7", "#8A845C"}, // yellow
};

private String[] getRandomTagColor() {
return TAG_COLORS[(int) (Math.random() * TAG_COLORS.length)];
}

private String category;

@Override
protected void authTokenisSet() {

}

public void setCategory(String category) {
this.category = category;

button.setText(category);
String[] colors = getRandomTagColor();
button.setStyle(
"-fx-background-color: " + colors[0] + ";" +
"-fx-text-fill: " + colors[1] + ";" +
"-fx-background-radius: 20;" +
"-fx-font-size: 11px;" +
"-fx-padding: 4 10 4 10;"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ private void switchToLoginPage(ActionEvent event) {
@FXML
private void switchToCharityUserPage(ActionEvent event) {
System.out.println("Click!");
LoaderScene.LoadScene("loginSite", event, null, null, authToken);
LoaderScene.LoadScene("profile_org_settings", event, null, null, authToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ntnu.systemutvikling.team6.controller.profileCharity;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import ntnu.systemutvikling.team6.controller.components.*;
import ntnu.systemutvikling.team6.database.DAO.CharityUserDAO;
import ntnu.systemutvikling.team6.database.DAO.UserDAO;
import ntnu.systemutvikling.team6.database.DatabaseConnection;
import ntnu.systemutvikling.team6.database.Readers.UserSelect;
import ntnu.systemutvikling.team6.models.Charity;
import ntnu.systemutvikling.team6.models.Donation;
import ntnu.systemutvikling.team6.models.registry.DonationRegistry;
import ntnu.systemutvikling.team6.models.user.User;
import ntnu.systemutvikling.team6.security.PasswordHasher;

import java.io.IOException;
import java.util.Base64;
import java.util.List;

public class profileOrgSettingsController extends BaseController {
@FXML private NavbarController navbarController;
@FXML private Label charityNameLabel;
@FXML private TextField organizationNameField;
@FXML private HBox tagContainer;

@Override
protected void authTokenisSet() {
if (!isLoggedin() || authToken.isCharityUser() == null){
showAlert(Alert.AlertType.ERROR, "Not logged inn or dont have privileges", "You need to be logged inn an account with Charity User priviliges.");
Platform.runLater(() -> {
Stage stage = (Stage) Stage.getWindows().stream()
.filter(w -> w.isShowing())
.findFirst()
.orElse(null);
if (stage != null) {
LoaderScene.LoadScene("loginSite", stage, null, null, authToken);
}
});
return;
}
navbarController.setAuthToken(authToken);
populateFields();
}

private void populateFields(){
Charity usersCharity = authToken.isCharityUser();
charityNameLabel.setText(usersCharity.getName());
organizationNameField.setText(usersCharity.getName());

// Tags
List<String> categories = usersCharity.getCategory();
displayCategories(categories);

}

private void displayCategories(List<String> categories) {
tagContainer.getChildren().clear();
if(categories.isEmpty()){
Label empty = new Label("No categories");
empty.setStyle("-fx-text-fill: #777777; -fx-font-size: 14;");
tagContainer.getChildren().add(empty);
return;
}

for (String category : categories) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/components/categoryTag.fxml"));
Parent card = loader.load();
CategoryTagController cardController = loader.getController();
cardController.setCategory(category);
cardController.setAuthToken(authToken);
tagContainer.getChildren().add(card);
} catch (IOException e) {
throw new RuntimeException("Could not load organization card.", e);
}
}
}

@FXML
private void handleNewName(ActionEvent event){
String organizationNameFieldText = organizationNameField.getText();


if (organizationNameFieldText.isBlank()) {
showAlert(Alert.AlertType.ERROR, "Empty input", "Please fill out all fields");
return;
}

boolean updateSuccess;
DatabaseConnection conn = new DatabaseConnection();
CharityUserDAO userDataObject = new CharityUserDAO(conn);
Charity savedCharity = authToken.isCharityUser();
Charity minimalCharityWithJustNewName = new Charity(savedCharity.getUUID().toString(), savedCharity.getOrg_number(), organizationNameFieldText, null, savedCharity.getStatus(), savedCharity.getPreApproved(),null, null, null, null);
try {
updateSuccess = userDataObject.updateCharityVanityName(minimalCharityWithJustNewName);
} catch (Exception e) {
e.printStackTrace();
showAlert(Alert.AlertType.ERROR, "Unexpected Error", "Unexpected error ocurred");
return;
}
if (updateSuccess) {
showAlert(
Alert.AlertType.INFORMATION,
"Settings updated",
"You have successfully changed your settings");
authToken.isCharityUser().setName(organizationNameFieldText);
LoaderScene.LoadScene("profile_user_settings", event, null, null, authToken);
} else {
System.out.println("Something went wrong when updating Settings");
}

}

@FXML
private void switchToPaymentsPage(ActionEvent event){
LoaderScene.LoadScene("profile_org_Payments", event, null, null, authToken);
}

@FXML
private void switchToFeedbackPage(ActionEvent event){
LoaderScene.LoadScene("profile_org_", event, null, null, authToken);
}

@FXML
private void switchToEditPage(ActionEvent event){
LoaderScene.LoadScene("profile_user_inbox", event, null, null, authToken);
}


@FXML
private void handleLogout(ActionEvent event){
authToken.logout();
showAlert(Alert.AlertType.INFORMATION, "Logging out", "Logging out...");
LoaderScene.LoadScene("FrontPage", event, null, null, authToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ntnu.systemutvikling.team6.database.DAO;

import ntnu.systemutvikling.team6.database.DatabaseConnection;
import ntnu.systemutvikling.team6.models.Charity;
import ntnu.systemutvikling.team6.models.user.Settings;
import ntnu.systemutvikling.team6.models.user.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CharityUserDAO {

private DatabaseConnection connection;

public CharityUserDAO(DatabaseConnection connection) {
this.connection = connection;
}
public boolean updateCharityVanityName(Charity charity){
Connection conn = null;
String sql = """
UPDATE CharityVanity SET
charity_name = ?
WHERE UUID_charity = ?;
""";
try {
conn = connection.getMySqlConnection();
PreparedStatement ps = conn.prepareStatement(sql);

System.out.println(charity.getUUID().toString());
ps.setString(1, charity.getName());
ps.setString(2, charity.getUUID().toString());

return ps.executeUpdate() > 0;

} catch (SQLException e) {
e.printStackTrace();
System.out.println("Something went wrong when updating ah");
return false;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean isEmailTaken(String email){
public Charity getUserCharityUser(String uuid){
if (uuid == null || uuid.isBlank()) {
throw new IllegalArgumentException(
"Email cannot be null or blank," + " and must contain '@' and '.'");
"UUID cannot be null or blank");
}
Charity currentCharity = null;
Connection conn = null;
Expand All @@ -81,7 +81,7 @@ public Charity getUserCharityUser(String uuid){
INNER JOIN CharityVanity cv ON cv.UUID_charity = c.UUID_charities
LEFT JOIN Charity_Categories cc ON cc.Charities_UUID_charities = c.UUID_charities
LEFT JOIN Categories cat ON cat.category_id = cc.Categories_category_id
WHERE CharityUserId = ?;
WHERE CharityUserId = ?
""";
PreparedStatement stmt = conn.prepareStatement(sql_query);
stmt.setString(1, uuid);
Expand All @@ -108,7 +108,7 @@ public Charity getUserCharityUser(String uuid){
}

String categoryName = rs.getString("category");
if (categoryName != null & !currentCharity.getCategory().contains(categoryName)) {
if (categoryName != null && !currentCharity.getCategory().contains(categoryName)) {
currentCharity.getCategory().add(categoryName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ public byte[] getLogoBlob() {
return this.logoBlob;
}

/** Setter for new name */
public void setName(String name) {
this.name = name;
}

/** Setter for verification status. This one sets the charity as verified. */
public void setVerified() {
this.status = "approved";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox alignment="CENTER" spacing="8" xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ntnu.systemutvikling.team6.controller.components.CategoryTagController">
<children>
<Button fx:id="Button" style="-fx-background-color: #E5F0D7; -fx-text-fill: #6E8A5C; -fx-font-size: 16px;" text="Non-profit" />
</children>
</VBox>
Loading

0 comments on commit efd31df

Please sign in to comment.