0

I made a function in my program which alllows employees to login in an application I made. The code for that looks like this:

LoginController:

//Login for employee
@FXML
private TextField textUsername;

@FXML
private PasswordField textPassword;

@FXML
private ChoiceBox<String> EmpSelect;

Stage dialogStage = new Stage();
Scene scene;

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

public LoginController() {
    connection = sqlDatabaseConnection.connectdb();
}

//Login for employee
@FXML
private void handleButtonAction(ActionEvent event) {
    String username = textUsername.getText().toString();
    String password = textPassword.getText().toString();
    String function = EmpSelect.getValue().toString();
    String sql = "SELECT * FROM Employee WHERE username = ? and password = ? and function = ?";

    try {
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        preparedStatement.setString(3, function);
        resultSet = preparedStatement.executeQuery();

        if (!resultSet.next()) {
            infoBox("Enter Correct Username and Password", "Failed", null);
        } else {

            if ("Employee".equals(function)) {
                infoBox("Login Successfull", "Success", null);
                FXMLDocumentController controller = new FXMLDocumentController();
                controller.newAnchorpane("WorkerHomescreen", paneLogin);
            } else if ("Manager".equals(function)) {
                infoBox("Login Successfull", "Success", null);
                FXMLDocumentController controller = new FXMLDocumentController();
                controller.newAnchorpane("WorkerHomescreen", paneLogin);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void infoBox(String infoMessage, String titleBar, String headerMessage) {
    Alert alert = new Alert(AlertType.INFORMATION);
    alert.setTitle(titleBar);
    alert.setHeaderText(headerMessage);
    alert.setContentText(infoMessage);
    alert.showAndWait();
}

With this piece of code employees are able to login. This works all fine and dandy. As you can see with this code it checks in the choiceBox if you want to login as a employee or a manager, and than it redirects you to the correct page.

The thing is that if you login as a manager you will be able to see all the buttons and labels. And when you login as a employee you will be able to see only a few buttons and labels.

I want this to happen when the page loads after the login. And seeing as I want to login to the page called: WorkerHomescreen.fxml I thought it would be handy to put some code in this class.

WorkerHomescreen class:

Connection connection = null;
public WorkerHomescreenController() {
    connection = sqlDatabaseConnection.connectdb();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    String Check = "SELECT * FROM Employee WHERE function = 'employee'";
    but4.setVisible(false);
}

So basically with this piece of code I want to make it so that the query checks if you logged in as a employee. And if that is the case the button called "but4" will become invisible. I tried this and it doesn't work. It hides the button even if you are logged in as a manager.

So I was wondering what I am doing wrong here and if someone can help me with this.

If I missed something please let me know and I will post it in the comments below.

  • I hope you left out some code in your `initialize` method!?! – SedJ601 Dec 05 '17 at 21:58
  • Take a look at the answer to this question, which [provides info on a basic role based visibility implementation for JavaFX](https://stackoverflow.com/questions/19666982/is-there-a-way-to-implement-a-property-like-rendered-on-javafx). – jewelsea Dec 05 '17 at 22:11

1 Answers1

2

Hiding and displaying controls depending on the role of the user is a solution, but perhaps not the most appropriate. You have the option to take advantage of the principles of object-oriented programming.

Make two different views, one for workers and one for managers, using common identifiers for general controls. Do two different controllers, like the one for managers, to be the successor to the controller for the workers. This will give you a logical separation of both views and allowed features for each user group.

Here is a sample implementation.

These are the two controllers: worker_homescreen.fxml

<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.WorkerController">
    <children>
        <Label text="Worker"/>
        <Button text="Different Action Button" onAction="#handleDifferentActionButton"/>
        <Button text="General Action Button" onAction="#handleGeneralActionButton"/>
    </children>
</VBox>

and manager_homescreen.fxml

<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.ManagerController">
    <children>
        <Label text="Manager"/>
        <Button text="Manager Only Button" onAction="#handleManagerButton"/>
        <Button text="Different Action Button" onAction="#handleDifferentActionButton"/>
        <Button text="General Action Button" onAction="#handleGeneralActionButton"/>
    </children>
</VBox>

The General Action Button is a control available in both views, which performs the same function. Different Action Button is also available in both views but performs different things depending on which view is loaded. And, of course, Manager Only Button is only available in the Mannard view and performs a specific feature available only for Managers.

All this "magic" comes from the inheritance of the controllers used.

public class WorkerController {

    @FXML
    protected void initialize() {

    }

    @FXML
    protected void handleDifferentActionButton(ActionEvent event) {

    }

    @FXML
    protected void handleGeneralActionButton(ActionEvent event) {

    }
}

public class ManagerController extends WorkerController {

    @FXML
    @Override
    protected void initialize() {
        super.initialize();
    }

    @FXML
    protected void handleManagerButton(ActionEvent event) {

    }

    @FXML
    @Override
    protected void handleDifferentActionButton(ActionEvent event) {

    }
}

As you can see, logic is cleared and you do not have to constantly hide and show controls. You can concentrate on implementing business logic because you will not have to constantly check the role of the user. This is done once when logging in.

public class Controller {
    private static final String WORKER = "/sample/worker_homescreen.fxml";
    private static final String MANAGER = "/sample/manager_homescreen.fxml";

    @FXML
    private void handleLoginButton() {
        ...

        if (!resultSet.next()) {
            infoBox("Enter Correct Username and Password", "Failed", null);
        } else {
            if ("Employee".equals(function)) {
                infoBox("Login Successfull", "Success", null);
                showMainView(getClass().getResource(WORKER));

            } else if ("Manager".equals(function)) {
                infoBox("Login Successfull", "Success", null);
                showMainView(getClass().getResource(MANAGER));
            }
        }
    }

    private void showMainView(URL url) {
        try {
            Parent parent = FXMLLoader.load(url);

            Stage stage = new Stage();
            stage.setScene(new Scene(parent, 800, 600));
            stage.show();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The best thing about this approach is that you can easily add roles without affecting the existing logic.

mr mcwolf
  • 2,574
  • 2
  • 14
  • 27