Skip to content

Commit

Permalink
Merge pull request #38 from SorinPopteanu/add-factories
Browse files Browse the repository at this point in the history
Add Organization roles
  • Loading branch information
SorinPopteanu authored Mar 26, 2024
2 parents 33f3878 + c0fd016 commit 46275db
Show file tree
Hide file tree
Showing 23 changed files with 1,060 additions and 170 deletions.
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
opens org.chainoptim.desktop.core.organization.controller to javafx.fxml, com.google.guice;
opens org.chainoptim.desktop.core.organization.service to com.google.guice;
opens org.chainoptim.desktop.core.organization.model to com.fasterxml.jackson.databind;
opens org.chainoptim.desktop.core.organization.dto to com.fasterxml.jackson.databind;

// Features
// - Product
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ public void initialize(URL location, ResourceBundle resources) {
}

private void fetchAndSetUser(String username) {
try {
userService.getUserByUsername(username)
.thenAcceptAsync(userOptional -> userOptional.ifPresentOrElse(this::updateCurrentUser,
() -> Platform.runLater(() -> System.err.println("User not found."))))
.exceptionally(ex -> {
Platform.runLater(() -> System.err.println("Failed to load user: " + ex.getMessage()));
return null;
});
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Error encoding username: " + e.getMessage());
}
userService.getUserByUsername(username)
.thenAcceptAsync(userOptional -> userOptional.ifPresentOrElse(this::updateCurrentUser,
() -> Platform.runLater(() -> System.err.println("User not found."))))
.exceptionally(ex -> {
Platform.runLater(() -> System.err.println("Failed to load user: " + ex.getMessage()));
return null;
});
}

private void updateCurrentUser(User user) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.chainoptim.desktop.core.organization.controller;

import org.chainoptim.desktop.core.organization.model.ConfirmDeleteDialogActionListener;
import org.chainoptim.desktop.core.organization.model.ConfirmUpdateDialogActionListener;
import org.chainoptim.desktop.core.organization.model.CustomRole;
import org.chainoptim.desktop.core.user.model.User;
import org.chainoptim.desktop.core.user.service.UserService;
import org.chainoptim.desktop.shared.util.DataReceiver;
import com.google.inject.Inject;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import lombok.Setter;

import java.util.List;
import java.util.Optional;

public class ConfirmCustomRoleDeleteController implements DataReceiver<Integer> {

private final UserService userService;

private Integer customRoleId;

@Setter
private ConfirmDeleteDialogActionListener actionListener;

@FXML
private VBox usersVBox;
@FXML
private Button confirmButton;
@FXML
private Button cancelButton;

@Inject
public ConfirmCustomRoleDeleteController(UserService userService) {
this.userService = userService;
}

@Override
public void setData(Integer customRoleId) {
this.customRoleId = customRoleId;

userService.getUsersByCustomRoleId(customRoleId)
.thenApply(this::handleUsersResponse)
.exceptionally(this::handleUsersException);
}

private Optional<List<User>> handleUsersResponse(Optional<List<User>> users) {
if (users.isEmpty()) {
return Optional.empty();
}
Platform.runLater(() -> {
List<User> userList = users.get();
usersVBox.getChildren().clear();

for (User user : userList) {
// Put names into VBox
Label usernameLabel = new Label(user.getUsername());
usersVBox.getChildren().add(usernameLabel);
}
});
return users;
}

private Optional<List<User>> handleUsersException(Throwable ex) {
ex.printStackTrace();
return Optional.empty();
}

@FXML
private void onConfirmButtonClicked() {
actionListener.onConfirmCustomRoleDelete(customRoleId);
}

@FXML
private void onCancelButtonClicked() {
actionListener.onCancelCustomRoleDelete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.chainoptim.desktop.core.organization.controller;

import org.chainoptim.desktop.core.organization.model.ConfirmUpdateDialogActionListener;
import org.chainoptim.desktop.core.organization.model.CustomRole;
import org.chainoptim.desktop.core.user.model.User;
import org.chainoptim.desktop.core.user.service.UserService;
import org.chainoptim.desktop.shared.util.DataReceiver;
import com.google.inject.Inject;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import lombok.Setter;

import java.util.List;
import java.util.Optional;

public class ConfirmCustomRoleUpdateController implements DataReceiver<CustomRole> {

private final UserService userService;

private CustomRole customRole;

@Setter
private ConfirmUpdateDialogActionListener confirmUpdateDialogActionListener;

@FXML
private VBox usersVBox;
@FXML
private Button confirmButton;
@FXML
private Button cancelButton;

@Inject
public ConfirmCustomRoleUpdateController(UserService userService) {
this.userService = userService;
}

@Override
public void setData(CustomRole data) {
this.customRole = data;
System.out.println("Custom role: " + customRole.getName());

userService.getUsersByCustomRoleId(customRole.getId())
.thenApply(this::handleUsersResponse)
.exceptionally(this::handleUsersException);
}

private Optional<List<User>> handleUsersResponse(Optional<List<User>> users) {
if (users.isEmpty()) {
System.out.println("No users found for custom role: " + customRole.getName());
return Optional.empty();
}
Platform.runLater(() -> {
List<User> userList = users.get();
for (User user : userList) {
// Put names into VBox
Label usernameLabel = new Label(user.getUsername());
usersVBox.getChildren().add(usernameLabel);
}
});
return users;
}

private Optional<List<User>> handleUsersException(Throwable ex) {
ex.printStackTrace();
return Optional.empty();
}

@FXML
private void onConfirmButtonClicked() {
confirmUpdateDialogActionListener.onConfirmCustomRoleUpdate(customRole);
}

@FXML
private void onCancelButtonClicked() {
confirmUpdateDialogActionListener.onCancelCustomRoleUpdate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ private Optional<Organization> handleOrganizationException(Throwable ex) {
}

@FXML
private void handleChangePlan() {

loadTabContent(subscriptionPlanTab, "/org/chainoptim/desktop/core/organization/OrganizationSubscriptionPlanView.fxml", this.organization);
private void handleEditOrganization() {
System.out.println("Edit organization clicked");
}
}
Loading

0 comments on commit 46275db

Please sign in to comment.