Skip to content

Commit

Permalink
Merge pull request #39 from SorinPopteanu/add-factories
Browse files Browse the repository at this point in the history
Add factories
  • Loading branch information
SorinPopteanu authored Mar 27, 2024
2 parents 140c50d + 4792afc commit 94e3ecb
Show file tree
Hide file tree
Showing 29 changed files with 907 additions and 303 deletions.
6 changes: 5 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
opens org.chainoptim.desktop.core.user.model to com.fasterxml.jackson.databind;

// - Organization
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;
Expand Down Expand Up @@ -115,6 +114,9 @@
// - Fallback Manager
opens org.chainoptim.desktop.shared.fallback to javafx.fxml, com.google.guice;

// - Confirm Dialogs
opens org.chainoptim.desktop.shared.confirmdialog.model to com.fasterxml.jackson.databind;

// - Utils
opens org.chainoptim.desktop.shared.util.resourceloader to com.google.guice;

Expand All @@ -123,5 +125,7 @@
opens org.chainoptim.desktop.shared.search.controller to com.google.guice, javafx.fxml;

exports org.chainoptim.desktop;
opens org.chainoptim.desktop.core.organization.controller to com.fasterxml.jackson.databind, com.google.guice, javafx.fxml;
opens org.chainoptim.desktop.shared.confirmdialog.controller to com.fasterxml.jackson.databind, com.google.guice, javafx.fxml;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package org.chainoptim.desktop.core.organization.controller;

import org.chainoptim.desktop.core.organization.model.CustomRole;
import org.chainoptim.desktop.core.organization.model.FeaturePermissions;
import org.chainoptim.desktop.core.organization.model.Permissions;
import org.chainoptim.desktop.core.user.model.User;
import org.chainoptim.desktop.core.user.service.UserService;
import org.chainoptim.desktop.shared.confirmdialog.controller.GenericConfirmDialogActionListener;
import org.chainoptim.desktop.shared.util.DataReceiver;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.util.Pair;
import lombok.Setter;

import java.util.List;

public class OrganizationAssignRoleController implements DataReceiver<Pair<User, List<CustomRole>>> {

// State
private User user;
private List<CustomRole> customRoles;
private CustomRole selectedRole;
private boolean hasRenderedWarning;

// Listeners
@Setter
private GenericConfirmDialogActionListener<Pair<String, Integer>> actionListener; // userId, customRoleId

// FXML
@FXML
private Label titleLabel;
@FXML
private VBox rolesVBox;
@FXML
private TextFlow messageTextFlowContainer;
@FXML
private VBox permissionsVBox;
@FXML
private Button confirmButton;
@FXML
private Button cancelButton;

@Override
public void setData(Pair<User, List<CustomRole>> data) {
user = data.getKey();
customRoles = data.getValue();
System.out.println("Receiving data: " + user + " " + customRoles);

initializeUI();
}

private void initializeUI() {
if (customRoles == null) return;

titleLabel.setText("Assign role to " + user.getUsername());

for (CustomRole customRole : customRoles) {
Button roleName = new Button(customRole.getName());
roleName.setMaxWidth(Double.MAX_VALUE);
roleName.getStyleClass().add("assign-role-element");
roleName.setOnAction(this::selectRole);
rolesVBox.getChildren().add(roleName);
}
}

private void selectRole(ActionEvent event) {
Button source = (Button) event.getSource();
selectedRole = customRoles.get(rolesVBox.getChildren().indexOf(source));
source.getStyleClass().clear();
source.getStyleClass().add("assign-role-selected-element");

// Deselect other roles
for (int i = 0; i < rolesVBox.getChildren().size(); i++) {
if (i != rolesVBox.getChildren().indexOf(source)) {
rolesVBox.getChildren().get(i).getStyleClass().clear();
rolesVBox.getChildren().get(i).getStyleClass().add("assign-role-element");
}
}

// Render permissions to be granted
renderPermissionsVBox();

// On first selection, render warning message
if (hasRenderedWarning) return;
String message = "Are you sure you want to assign this role to " + user.getUsername() + "?"
+ " This will override the basic role " + user.getRole().toString()
+ " and grant them the following permissions:";
messageTextFlowContainer.getChildren().clear();
messageTextFlowContainer.getChildren().add(new Text(message));
hasRenderedWarning = true;
}

private void renderPermissionsVBox() {
if (selectedRole == null || selectedRole.getPermissions() == null) return;

permissionsVBox.getChildren().clear();

renderFeaturePermissions(selectedRole.getPermissions().getProducts(), "products");
renderFeaturePermissions(selectedRole.getPermissions().getFactories(), "factories");
renderFeaturePermissions(selectedRole.getPermissions().getWarehouses(), "warehouses");
renderFeaturePermissions(selectedRole.getPermissions().getSuppliers(), "suppliers");
renderFeaturePermissions(selectedRole.getPermissions().getClients(), "clients");
}

private void renderFeaturePermissions(FeaturePermissions featurePermissions, String featureName) {
if (featurePermissions == null) return;

String permissionText = "To ";
if (Boolean.TRUE.equals(featurePermissions.getCanRead())) {
permissionText += "Read, ";
}
if (Boolean.TRUE.equals(featurePermissions.getCanCreate())) {
permissionText += "Create, ";
}
if (Boolean.TRUE.equals(featurePermissions.getCanUpdate())) {
permissionText += "Update, ";
}
if (Boolean.TRUE.equals(featurePermissions.getCanDelete())) {
permissionText += "Delete, ";
}
permissionText = permissionText.substring(0, permissionText.length() - 2); // Remove trailing comma and space

if (permissionText.length() > 1) {
permissionText += " " + featureName;

Label featureLabel = new Label(permissionText);
featureLabel.getStyleClass().add("general-label");
permissionsVBox.getChildren().add(featureLabel);
}
}

@FXML
private void onConfirmButtonClicked() {
if (selectedRole == null) return;

Pair<String, Integer> data = new Pair<>(user.getId(), selectedRole.getId());
actionListener.onConfirmAction(data);
}

@FXML
private void onCancelButtonClicked() {
actionListener.onCancelAction();
}
}
Loading

0 comments on commit 94e3ecb

Please sign in to comment.