Projects / Hotel Management System / src / hoteldraft / SettingsController.java
SettingsController.java
Raw
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hoteldraft;

import static hoteldraft.HomeController.hotelNameOriginal;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.animation.RotateTransition;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import static javafx.scene.paint.Color.rgb;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Hala
 */
public class SettingsController implements Initializable {

    private String MasterKey = "BestGroupEver";

    private double xOffSet = 0;
    private double yOffSet = 0;

    private Stage stage;
    private Scene scene;
    private Parent root;
    @FXML
    private Button settingsImgBtn;
    @FXML
    private ImageView settingsImg;
    @FXML
    private Label exit;
    @FXML
    private Label minimize;

    @FXML
    private Button submitMasterKey;
    @FXML
    private TextField empIDTextfield;
    @FXML
    private TextField empNameTextfield;
    @FXML
    private PasswordField empPassTextfield;
    @FXML
    private Button addEmpBtn;
    @FXML
    private PasswordField masterKeyTextfield;
    @FXML
    private Text masterKeyAlert;
    @FXML
    private Label empIDLabel;
    @FXML
    private Label empNameLabel;
    @FXML
    private Label empPassLabel;
    @FXML
    private Text addEmpAlert;
    @FXML
    private Label hotelNameLabel;
    @FXML
    private TextField hotelNameTextfield;
    @FXML
    private Button hotelNameBtn;
    @FXML
    private Text hotelNameAlert;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        hotelNameTextfield.setText(HomeController.hotelNameOriginal);
        //Listener to limit textfield to 20 letters length
        masterKeyTextfield.textProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue,
                    String newValue) {
                if (masterKeyTextfield.getText().length() > 20) {
                    String s = masterKeyTextfield.getText().substring(0, 20);
                    masterKeyTextfield.setText(s);
                }
            }
        });

        //Listener to limit textfield to 20 letters length and prevent letters from input
        empIDTextfield.textProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue,
                    String newValue) {
                if (!newValue.matches("\\d*")) {
                    empIDTextfield.setText(newValue.replaceAll("[^\\d]", ""));
                }
                if (empIDTextfield.getText().length() > 20) {
                    String s = empIDTextfield.getText().substring(0, 20);
                    empIDTextfield.setText(s);
                }
            }
        });
        //Listener to limit textfield to 20 letters length
        empNameTextfield.textProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue,
                    String newValue) {
                if (empNameTextfield.getText().length() > 20) {
                    String s = empNameTextfield.getText().substring(0, 20);
                    empNameTextfield.setText(s);
                }
            }
        });
        //Listener to limit textfield to 20 letters length
        empPassTextfield.textProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue,
                    String newValue) {
                if (empPassTextfield.getText().length() > 20) {
                    String s = empPassTextfield.getText().substring(0, 20);
                    empPassTextfield.setText(s);
                }
            }
        });

        //to change settings imageview color
        Lighting lighting = new Lighting(new Light.Distant(45, 90, rgb(105, 152, 171)));
        ColorAdjust bright = new ColorAdjust(0, 1, 1, 1);
        lighting.setContentInput(bright);
        lighting.setSurfaceScale(0.0);
        settingsImg.setEffect(lighting);

        // to rotate settings button on hover
        RotateTransition rotation = new RotateTransition(Duration.seconds(0.5), settingsImgBtn);
        rotation.setCycleCount(1);
        rotation.setByAngle(180);
        settingsImgBtn.setOnMouseEntered(e -> rotation.play());
        settingsImgBtn.setOnMouseExited(e -> rotation.pause());
    }

    @FXML
    private void checkMasterKey(ActionEvent event) {
        if (masterKeyTextfield.getText().isEmpty()) {
            masterKeyAlert.setText("No input found.");
        } else if (masterKeyTextfield.getText().equals(MasterKey)) {
            masterKeyAlert.setText("Access granted.");

            empIDTextfield.setDisable(false);
            empIDLabel.setDisable(false);

            empNameTextfield.setDisable(false);
            empNameLabel.setDisable(false);

            empPassTextfield.setDisable(false);
            empPassLabel.setDisable(false);

            addEmpBtn.setDisable(false);

            hotelNameLabel.setDisable(false);
            hotelNameTextfield.setDisable(false);
            hotelNameBtn.setDisable(false);
        } else {
            masterKeyAlert.setText("Incorrect master key.");
        }
    }

    @FXML
    private void enterCheckMasterKey(KeyEvent event) {
        if (event.getCode() == KeyCode.ENTER) {
            if (masterKeyTextfield.getText().isEmpty()) {
                masterKeyAlert.setText("No input found.");
            } else if (masterKeyTextfield.getText().equals(MasterKey)) {
                masterKeyAlert.setText("Access granted.");

                empIDTextfield.setDisable(false);
                empIDLabel.setDisable(false);

                empNameTextfield.setDisable(false);
                empNameLabel.setDisable(false);

                empPassTextfield.setDisable(false);
                empPassLabel.setDisable(false);

                addEmpBtn.setDisable(false);

                hotelNameLabel.setDisable(false);
                hotelNameTextfield.setDisable(false);
                hotelNameBtn.setDisable(false);
            } else {
                masterKeyAlert.setText("Incorrect master key.");
            }
        }
    }

    @FXML
    private void addEmployee(ActionEvent event) {
        if (empIDTextfield.getText().isEmpty() && empNameTextfield.getText().isEmpty() && empPassTextfield.getText().isEmpty()) {
            addEmpAlert.setText("No input found.");
        } else if (empIDTextfield.getText().isEmpty() || empNameTextfield.getText().isEmpty() || empPassTextfield.getText().isEmpty()) {
            addEmpAlert.setText("Fill all fields.");
        } else {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();

            String HQL = "from Employees";
            Query q = session.createQuery(HQL);

            List<Employees> empList = q.list();

            boolean doesEmpExist = false;
            for (Employees emp : empList) {
                if (emp.getEmployeeID() == Integer.parseInt(empIDTextfield.getText())) {
                    doesEmpExist = true;
                }
            }

            if (doesEmpExist == true) {
                addEmpAlert.setText("Employee ID already exists.");
            } else if (doesEmpExist == false) {
                Employees newEmp = new Employees(Integer.parseInt(empIDTextfield.getText()), empNameTextfield.getText(), empPassTextfield.getText());
                session.save(newEmp);
                addEmpAlert.setText("Employee successfully added!");
            }
            tx.commit();
            session.close();
        }
    }

    @FXML
    private void enterAddEmployee(KeyEvent event) {
        if (event.getCode() == KeyCode.ENTER) {
            if (empIDTextfield.getText().isEmpty() && empNameTextfield.getText().isEmpty() && empPassTextfield.getText().isEmpty()) {
                addEmpAlert.setText("No input found.");
            } else if (empIDTextfield.getText().isEmpty() || empNameTextfield.getText().isEmpty() || empPassTextfield.getText().isEmpty()) {
                addEmpAlert.setText("Fill all fields.");
            } else {
                Session session = HibernateUtil.getSessionFactory().openSession();
                Transaction tx = session.beginTransaction();

                String HQL = "from Employees";
                Query q = session.createQuery(HQL);

                List<Employees> empList = q.list();

                boolean doesEmpExist = false;
                for (Employees emp : empList) {
                    if (emp.getEmployeeID() == Integer.parseInt(empIDTextfield.getText())) {
                        doesEmpExist = true;
                    }
                }

                if (doesEmpExist == true) {
                    addEmpAlert.setText("Employee ID already exists.");
                } else if (doesEmpExist == false) {
                    Employees newEmp = new Employees(Integer.parseInt(empIDTextfield.getText()), empNameTextfield.getText(), empPassTextfield.getText());
                    session.save(newEmp);
                    addEmpAlert.setText("Employee successfully added!");
                }
                tx.commit();
                session.close();
            }
        }
    }

    public void changeHotelName(ActionEvent event) {
        if (hotelNameTextfield.getText().isEmpty()) {
            hotelNameAlert.setText("No input found.");
        } else if (hotelNameTextfield.getText().contains(HomeController.hotelNameOriginal)) {
            hotelNameAlert.setText("Name already exists.");
        } else {
            HomeController.setHotelNameOriginal(hotelNameTextfield.getText());
            hotelNameAlert.setText("Name successfully changed.");
        }
    }

    //Methods to close and minimize window.
    @FXML
    private void close_app(javafx.scene.input.MouseEvent event) {
        System.exit(0);
    }

    @FXML
    private void minimize_app(javafx.scene.input.MouseEvent event) {
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        stage.setIconified(true);
    }

    /*---------------------class switching events---------------------*/
    @FXML
    private void switchToReservations(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Reservations.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    private void switchToNewReservation(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("NewReservation.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();

    }

    @FXML
    private void switchToLoggedIn(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("LoggedIn.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    private void switchToRooms(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Rooms.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    private void switchToServices(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Services.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    private void switchToSearch(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Search.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    private void switchToSettings(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Settings.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }
}