From 3c1445f430b7f772af3fcf16d5f3918a89f0cc22 Mon Sep 17 00:00:00 2001 From: Dorian Nemec Date: Mon, 20 May 2024 08:21:23 +0200 Subject: [PATCH] #24 Implement logic --- .../nhplus/main/MainWindowController.java | 73 ++++++++-- .../nhplus/nurse/AllNurseController.java | 2 + .../treatment/AllTreatmentController.java | 42 +++--- .../treatment/LockedTreatmentController.java | 127 ++++++++++++++++++ 4 files changed, 216 insertions(+), 28 deletions(-) create mode 100644 src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java diff --git a/src/main/java/de/hitec/nhplus/main/MainWindowController.java b/src/main/java/de/hitec/nhplus/main/MainWindowController.java index 293ba41..5b7b4e4 100644 --- a/src/main/java/de/hitec/nhplus/main/MainWindowController.java +++ b/src/main/java/de/hitec/nhplus/main/MainWindowController.java @@ -9,6 +9,9 @@ import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; +import de.hitec.nhplus.treatment.Treatment; +import de.hitec.nhplus.medication.Medication; +import de.hitec.nhplus.patient.Patient; import java.io.IOException; import java.util.Objects; @@ -29,10 +32,18 @@ public class MainWindowController { @FXML private Tab patientTab; @FXML - private AnchorPane treatmentPage; + private AnchorPane activeTreatmentPage; @FXML private Tab treatmentTab; @FXML + private TabPane treatmentTabPane; + @FXML + private Tab activeTreatmentTab; + @FXML + private AnchorPane lockedTreatmentPage; + @FXML + private Tab lockedTreatmentTab; + @FXML private Tab nurseTab; @FXML private TabPane nurseTabPane; @@ -58,19 +69,25 @@ public class MainWindowController { mainTabPane.getSelectionModel().select(patientTab); patientTab.setOnSelectionChanged(event -> loadPatientPage()); - treatmentTab.setOnSelectionChanged(event -> loadTreatmentsPage()); - nurseTab.setOnSelectionChanged(event -> loadNursePage()); medicationTab.setOnSelectionChanged(event -> loadMedicationPage()); + nurseTab.setOnSelectionChanged(event -> loadNursePage()); nurseTabPane.getSelectionModel().select(activeNurseTab); activeNurseTab.setOnSelectionChanged(event -> loadActiveNursePage()); lockedNurseTab.setOnSelectionChanged(event -> loadLockedNursePage()); + + treatmentTab.setOnSelectionChanged(event -> loadTreatmentPage()); + treatmentTabPane.getSelectionModel().select(activeTreatmentTab); + + activeTreatmentTab.setOnSelectionChanged(event -> loadActiveTreatmentPage()); + lockedTreatmentTab.setOnSelectionChanged(event -> loadLockedTreatmentPage()); + } /** - * Loads the patient page into its tab. + * Loads the {@link Patient} page into its tab. */ private void loadPatientPage() { try { @@ -88,14 +105,46 @@ public class MainWindowController { } /** - * Loads the {@link } page into its tab. + * Loads the {@link Treatment } tab. */ - private void loadTreatmentsPage() { + private void loadTreatmentPage() { + SelectionModel selectionModel = treatmentTabPane.getSelectionModel(); + Tab selectedTab = selectionModel.getSelectedItem(); + if (selectedTab == activeTreatmentTab) { + loadActiveTreatmentPage(); + } + if (selectedTab == lockedTreatmentTab) { + loadLockedTreatmentPage(); + } + } + + /** + * Loads the active {@link Treatment} page into its tab. + */ + private void loadActiveTreatmentPage() { try { - BorderPane treatmentsPane = FXMLLoader.load( + BorderPane activeTreatmentPane = FXMLLoader.load( Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/treatment/AllTreatmentView.fxml")) ); - treatmentPage.getChildren().setAll(treatmentsPane); + activeTreatmentPage.getChildren().setAll(activeTreatmentPane); + AnchorPane.setTopAnchor(activeTreatmentPane, 0d); + AnchorPane.setBottomAnchor(activeTreatmentPane, 0d); + AnchorPane.setLeftAnchor(activeTreatmentPane, 0d); + AnchorPane.setRightAnchor(activeTreatmentPane, 0d); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + + /** + * Loads the locked {@link Treatment} page into its tab. + */ + private void loadLockedTreatmentPage() { + try { + BorderPane treatmentsPane = FXMLLoader.load( + Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/treatment/LockedTreatmentView.fxml")) + ); + lockedTreatmentPage.getChildren().setAll(treatmentsPane); AnchorPane.setTopAnchor(treatmentsPane, 0d); AnchorPane.setBottomAnchor(treatmentsPane, 0d); AnchorPane.setLeftAnchor(treatmentsPane, 0d); @@ -106,15 +155,15 @@ public class MainWindowController { } /** - * Loads the nurse page into its tab. + * Loads the {@link Nurse} page into its tab. */ private void loadNursePage() { SelectionModel selectionModel = nurseTabPane.getSelectionModel(); Tab selectedTab = selectionModel.getSelectedItem(); - if(selectedTab == activeNurseTab){ + if (selectedTab == activeNurseTab) { loadActiveNursePage(); } - if(selectedTab == lockedNurseTab){ + if (selectedTab == lockedNurseTab) { loadLockedNursePage(); } } @@ -156,7 +205,7 @@ public class MainWindowController { } /** - * Loads the medication page into its tab. + * Loads the {@link Medication} page into its tab. */ private void loadMedicationPage() { try { diff --git a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java index d6b89f8..6f72582 100644 --- a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java +++ b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java @@ -22,6 +22,8 @@ import java.sql.SQLException; * * @author Dominik Säume * @author Ole Kück + * @author Armin Ribic + * @author Dorian Nemec */ public class AllNurseController { @FXML diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index 3384c3f..de8c9d0 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -20,6 +20,7 @@ import javafx.stage.Stage; import java.io.IOException; import java.sql.SQLException; +import java.time.LocalDate; import java.util.ArrayList; /** @@ -27,6 +28,8 @@ import java.util.ArrayList; * * @author Bernd Heidemann * @author Dominik Säume + * @author Armin Ribic + * @author Dorian Nemec */ public class AllTreatmentController { @@ -61,7 +64,7 @@ public class AllTreatmentController { public ComboBox comboBoxNurseSelection; @FXML - private Button buttonDelete; + private Button buttonLock; private final ObservableList treatments = FXCollections.observableArrayList(); private TreatmentDao dao; @@ -100,15 +103,16 @@ public class AllTreatmentController { this.columnDescription.setCellValueFactory(new PropertyValueFactory<>("description")); this.tableView.setItems(this.treatments); - this.buttonDelete.setDisable(true); + this.buttonLock.setDisable(true); this.tableView .getSelectionModel() .selectedItemProperty() .addListener((observableValue, oldTreatment, newTreatment) -> - AllTreatmentController.this.buttonDelete.setDisable(newTreatment == null) + AllTreatmentController.this.buttonLock.setDisable(newTreatment == null) ); this.createComboBoxData(); + } /** @@ -136,7 +140,7 @@ public class AllTreatmentController { this.patientSelection.add(patient.getSurName()); } - nurseList = (ArrayList) nurseDao.readAll(); + nurseList = (ArrayList) nurseDao.readAllActive(); for (Nurse nurse : nurseList) { this.nurseSelection.add(nurse.getSurName()); } @@ -258,18 +262,6 @@ public class AllTreatmentController { } } - @FXML - public void handleDelete() { - int index = this.tableView.getSelectionModel().getSelectedIndex(); - Treatment t = this.treatments.remove(index); - TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); - try { - dao.delete(t.getId()); - } catch (SQLException exception) { - exception.printStackTrace(); - } - } - @FXML public void handleNewTreatment() { String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem(); @@ -316,4 +308,22 @@ public class AllTreatmentController { }); } + + @FXML + public void handleLock(){ + Treatment selectedItem = this.tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null){ + return; + } + + try { + selectedItem.setLocked(true); + this.dao.update(selectedItem); + }catch (SQLException exception){ + exception.printStackTrace(); + } + readAllAndShowInTableView(); + } + + } diff --git a/src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java new file mode 100644 index 0000000..2fdb0ed --- /dev/null +++ b/src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java @@ -0,0 +1,127 @@ +package de.hitec.nhplus.treatment; + +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.nurse.Nurse; +import de.hitec.nhplus.patient.Patient; +import de.hitec.nhplus.treatment.database.TreatmentDao; +import de.hitec.nhplus.utils.DateConverter; +import javafx.beans.property.SimpleStringProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.control.cell.PropertyValueFactory; + +import java.sql.SQLException; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +/** + * Controller for all locked treatments. + * + * @author Armin Ribic + * @author Dorian Nemec + */ +public class LockedTreatmentController { + + @FXML + public Button buttonUnlock; + @FXML + public TableView tableView; + @FXML + public TableColumn columnId; + @FXML + private TableColumn columnPatient; + @FXML + private TableColumn columnNurse; + @FXML + private TableColumn columnDeleteDate; + + private final ObservableList treatments = FXCollections.observableArrayList(); + private TreatmentDao treatmentDao; + + /** + * This method allows initializing a {@link LockedTreatmentController} object + * that is called after the binding of all the fields. + */ + public void initialize() { + this.readAllAndShowInTableView(); + + this.columnId.setCellValueFactory(new PropertyValueFactory<>("id")); + + this.columnPatient.setCellValueFactory( + cellData -> { + Patient patient = cellData.getValue().getPatient(); + return new SimpleStringProperty(patient.getSurName() + ", " + patient.getFirstName()); + } + ); + + this.columnNurse.setCellValueFactory( + cellData -> { + Nurse nurse = cellData.getValue().getNurse(); + return new SimpleStringProperty(nurse.getSurName() + ", " + nurse.getFirstName()); + } + ); + this.columnDeleteDate.setCellValueFactory(cellData -> new SimpleStringProperty( + DateConverter.convertLocalDateToString(cellData.getValue().calculateDeleteDate()))); + + + this.tableView.setItems(this.treatments); + handleDelete(); + } + + /** + * Reads all locked {@link Treatment} data and shows it in the table. + */ + private void readAllAndShowInTableView() { + this.treatments.clear(); + + this.treatmentDao = DaoFactory.getInstance().createTreatmentDao(); + try { + this.treatments.addAll(this.treatmentDao.readAllLocked()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + + @FXML + private void unlockTreatment() { + Treatment selectedItem = this.tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null) { + return; + } + + try { + selectedItem.setLocked(false); + this.treatmentDao.update(selectedItem); + } catch (SQLException exception) { + exception.printStackTrace(); + } + readAllAndShowInTableView(); + } + + @FXML + public void handleDelete() { + Treatment selecteditem; + LocalDate today = LocalDate.now(); + LocalDate deleteDate; + + for (Treatment treatment : treatments) { + selecteditem = treatment; + deleteDate = selecteditem.calculateDeleteDate(); + if (today.isEqual(deleteDate) || today.isAfter(deleteDate)) { + try { + treatmentDao.delete(selecteditem.getId()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + } + readAllAndShowInTableView(); + } + + +}