Projects / Hotel Management System / src / hoteldraft / ReservationsController.java
ReservationsController.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 java.io.IOException;
import java.net.URL;
import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.List;
import java.util.ResourceBundle;
import javafx.animation.RotateTransition;
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.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import static javafx.scene.paint.Color.rgb;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

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

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

    private Stage stage;
    private Scene scene;
    private Parent root;
    @FXML
    private ImageView settingsImg;
    @FXML
    private Label exit;
    @FXML
    private Label minimize;
    @FXML
    private Button settingsImgBtn;
    @FXML
    private ListView gnList;
    @FXML
    private ListView adList;
    @FXML
    private ListView ddList;
    @FXML
    private ListView rnList;
    @FXML
    private ListView gcList;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //retrieve from DB
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery("from Reservation");
        Transaction tx = session.beginTransaction();
        List<Reservation> RList = q.list();
        for (Reservation R : RList) {
            if (R.getDepartureDate().equals(Date.valueOf(LocalDate.now()))||R.getDepartureDate().before(Date.valueOf(LocalDate.now()))) {
                History history = new History(R.getID(), R.getFname(), R.getLname(), R.getEmail(), R.getRoomType(), R.getRoomNo(), R.getGuestCount(), R.getArrivalDate(), R.getDepartureDate(), R.getSpecialRequest());
                session.save(history);
                session.delete(R);
            } else {
                gnList.getItems().add(R.getFname() + " " + R.getLname());
                adList.getItems().add(R.getArrivalDate().toLocalDate().format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)));
                ddList.getItems().add(R.getDepartureDate().toLocalDate().format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)));
                rnList.getItems().add(R.getRoomNo());
                gcList.getItems().add(R.getGuestCount());
            }
        }
        tx.commit();
        session.close();
        
        //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());

        //Listeners to select all row if one listview item is selected
        gnList.getSelectionModel().selectedItemProperty().addListener(e -> {

            gnList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = gnList.getSelectionModel().getSelectedIndex();
            //System.out.println(index);
            adList.getSelectionModel().select(index);
            ddList.getSelectionModel().select(index);
            rnList.getSelectionModel().select(index);
            gcList.getSelectionModel().select(index);

        });
        adList.getSelectionModel().selectedItemProperty().addListener(e -> {
            adList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = adList.getSelectionModel().getSelectedIndex();
            gnList.getSelectionModel().select(index);
            ddList.getSelectionModel().select(index);
            rnList.getSelectionModel().select(index);
            gcList.getSelectionModel().select(index);
        });
        ddList.getSelectionModel().selectedItemProperty().addListener(e -> {
            ddList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = ddList.getSelectionModel().getSelectedIndex();
            gnList.getSelectionModel().select(index);
            adList.getSelectionModel().select(index);
            rnList.getSelectionModel().select(index);
            gcList.getSelectionModel().select(index);
        });
        rnList.getSelectionModel().selectedItemProperty().addListener(e -> {
            rnList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = rnList.getSelectionModel().getSelectedIndex();
            gnList.getSelectionModel().select(index);
            adList.getSelectionModel().select(index);
            ddList.getSelectionModel().select(index);
            gcList.getSelectionModel().select(index);
        });
        gcList.getSelectionModel().selectedItemProperty().addListener(e -> {
            gcList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = gcList.getSelectionModel().getSelectedIndex();
            gnList.getSelectionModel().select(index);
            adList.getSelectionModel().select(index);
            ddList.getSelectionModel().select(index);
            rnList.getSelectionModel().select(index);
        });
    }
    
    public void delete(ActionEvent event) {

        if(gnList.getItems().isEmpty()){
            return;
        }
        int index = rnList.getSelectionModel().getSelectedIndex();
        if (index == (-1)) {
            System.out.println("index -1");
        } else {
            Reservation reservation = null;
            Session session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Reservation");
            List<Reservation> rList = query.list();
            session.close();
            int i = 0;
            for (Reservation r : rList) {
                if (index == i) {
                    reservation = new Reservation(r.getID(),r.getArrivalDate());
                    break;
                }else{
                   i++; 
                }
                
            }
            Session session2 = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session2.beginTransaction();
            session2.delete(reservation);
            tx.commit();
            session2.close();
            
            gcList.getItems().remove(gcList.getSelectionModel().selectedItemProperty().getValue());
            gnList.getItems().remove(gnList.getSelectionModel().selectedItemProperty().getValue());
            adList.getItems().remove(adList.getSelectionModel().selectedItemProperty().getValue());
            ddList.getItems().remove(ddList.getSelectionModel().selectedItemProperty().getValue());
            rnList.getItems().remove(rnList.getSelectionModel().selectedItemProperty().getValue());
            
        }
    }

    //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
    public 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();
    }

    @FXML
    public 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
    public 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
    public 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
    public 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
    public 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
    public 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();
    }

    @FXML
    private void switchToUpdateReservation(MouseEvent e) throws IOException {
        if (e.getButton().equals(MouseButton.PRIMARY)) {
            if (e.getClickCount() == 2) {

                int index = gnList.getSelectionModel().getSelectedIndex();
                UpdateReservationController.indexOfUpdate = index;

                System.out.println("Double clicked");
                Parent root = FXMLLoader.load(getClass().getResource("UpdateReservation.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();
            }
        }
    }
}