From ad3bd8ba0ae834666471a2f12727a81ebd3c8151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Fri, 17 May 2024 15:32:29 +0200 Subject: [PATCH 1/2] #26: Implementing Editing of Medications --- .../medication/AllMedicationController.java | 40 ++++++++++++++++--- .../medication/MedicationModalController.java | 9 ++++- .../nhplus/medication/AllMedicationView.fxml | 2 +- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java index 1370ff0..deba95a 100644 --- a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java @@ -8,12 +8,17 @@ import java.util.stream.Collectors; import de.hitec.nhplus.Main; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.medication.database.MedicationDao; +import de.hitec.nhplus.nurse.Nurse; +import de.hitec.nhplus.patient.Patient; +import de.hitec.nhplus.treatment.Treatment; +import de.hitec.nhplus.treatment.TreatmentModalController; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; +import javafx.scene.control.SelectionModel; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; @@ -91,6 +96,15 @@ public class AllMedicationController { } } + public void updateMedication(Medication medication) { + dao = DaoFactory.getInstance().createMedicationDAO(); + try { + dao.update(medication); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + /** * Method to create a new {@link Medication}. */ @@ -103,21 +117,20 @@ public class AllMedicationController { } } - @FXML - public void handleNewMedication() { + public void medicationWindow(Medication medication, String title) { try { FXMLLoader loader = new FXMLLoader( - Main.class.getResource("/de/hitec/nhplus/medication/MedicationModal.fxml") + Main.class.getResource("/de/hitec/nhplus/medication/MedicationModal.fxml") ); BorderPane pane = loader.load(); Scene scene = new Scene(pane); Stage stage = new Stage(); MedicationModalController controller = loader.getController(); - controller.initialize(stage, this, null); + controller.initialize(stage, this, medication); stage.setScene(scene); - stage.setTitle("NHPlus - Neues Medikament"); + stage.setTitle(title); stage.setResizable(true); stage.setAlwaysOnTop(true); stage.showAndWait(); @@ -125,4 +138,21 @@ public class AllMedicationController { exception.printStackTrace(); } } + + @FXML + public void handleNewMedication() { + medicationWindow(null, "NHPlus - Neues Medikament"); + } + + @FXML + public void handleMouseClick() { + tableView.setOnMouseClicked(event -> { + SelectionModel selectionModel = tableView.getSelectionModel(); + if(event.getClickCount() == 2 && (selectionModel.getSelectedItem() != null)){ + int index = selectionModel.getSelectedIndex(); + Medication medication = this.medications.get(index); + medicationWindow(medication, "NHPlus - Medikament"); + } + }); + } } diff --git a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java index 76a97c3..43ad998 100644 --- a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java +++ b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java @@ -43,6 +43,7 @@ public class MedicationModalController { private Medication medication; private final ObservableList ingredients = FXCollections.observableArrayList(); private AllMedicationController controller; + private boolean isNewMedication = false; /** * Initialization method that is called after the binding of all the fields. @@ -59,6 +60,7 @@ public class MedicationModalController { if( medication != null){ this.medication = medication; }else { + isNewMedication = true; this.medication = new Medication( "", "", @@ -117,7 +119,12 @@ public class MedicationModalController { .toList() ); - controller.createMedication(medication); + if(isNewMedication){ + controller.createMedication(medication); + }else { + controller.updateMedication(medication); + } + controller.readAllAndShowInTableView(); stage.close(); } diff --git a/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml b/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml index 53e03b9..157caac 100644 --- a/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml +++ b/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml @@ -12,7 +12,7 @@
- + Date: Fri, 17 May 2024 15:35:03 +0200 Subject: [PATCH 2/2] #26: Cleanup & Javadoc --- .../medication/AllMedicationController.java | 37 ++++++++++++------- .../medication/MedicationModalController.java | 20 +++++----- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java index deba95a..547d30f 100644 --- a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java @@ -63,20 +63,20 @@ public class AllMedicationController { this.columnName.setCellValueFactory(new PropertyValueFactory<>("name")); this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer")); this.columnIngredient.setCellValueFactory( - cellData -> { - Medication medication = cellData.getValue(); - List ingredients = medication.getIngredients(); - if(ingredients.isEmpty()){ - return new SimpleStringProperty(""); - } + cellData -> { + Medication medication = cellData.getValue(); + List ingredients = medication.getIngredients(); + if (ingredients.isEmpty()) { + return new SimpleStringProperty(""); + } - return new SimpleStringProperty( - ingredients - .stream() - .map(ingredient -> ingredient.getName()) - .collect(Collectors.joining("\n")) - ); - }); + return new SimpleStringProperty( + ingredients + .stream() + .map(ingredient -> ingredient.getName()) + .collect(Collectors.joining("\n")) + ); + }); this.columnPossibleSideEffects.setCellValueFactory(new PropertyValueFactory<>("possibleSideEffects")); this.columnAdministrationMethod.setCellValueFactory(new PropertyValueFactory<>("administrationMethod")); this.columnCurrentStock.setCellValueFactory(new PropertyValueFactory<>("currentStock")); @@ -96,6 +96,9 @@ public class AllMedicationController { } } + /** + * Method to save the changes to a {@link Medication}. + */ public void updateMedication(Medication medication) { dao = DaoFactory.getInstance().createMedicationDAO(); try { @@ -117,6 +120,12 @@ public class AllMedicationController { } } + /** + * Internal method to create a {@link MedicationModalController MedicationModal}. + * + * @param medication The {@link Medication} which should be edited. Set null to create a new one. + * @param title The Title of the created modal. + */ public void medicationWindow(Medication medication, String title) { try { FXMLLoader loader = new FXMLLoader( @@ -148,7 +157,7 @@ public class AllMedicationController { public void handleMouseClick() { tableView.setOnMouseClicked(event -> { SelectionModel selectionModel = tableView.getSelectionModel(); - if(event.getClickCount() == 2 && (selectionModel.getSelectedItem() != null)){ + if (event.getClickCount() == 2 && (selectionModel.getSelectedItem() != null)) { int index = selectionModel.getSelectedIndex(); Medication medication = this.medications.get(index); medicationWindow(medication, "NHPlus - Medikament"); diff --git a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java index 43ad998..9489737 100644 --- a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java +++ b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java @@ -55,11 +55,11 @@ public class MedicationModalController { Medication medication ) { this.stage = stage; - this.controller=controller; + this.controller = controller; - if( medication != null){ - this.medication = medication; - }else { + if (medication != null) { + this.medication = medication; + } else { isNewMedication = true; this.medication = new Medication( "", @@ -78,9 +78,9 @@ public class MedicationModalController { ChangeListener inputMedicationValidationListener = (observableValue, oldText, newText) -> { boolean isValid = isValidMedicationName(textFieldName.getText()) - && isValidMedicationManufacturer(textFieldManufacturer.getText()) - && isValidMedicationAdministrationMethod(textFieldAdministrationMethod.getText()) - && isValidStock(textFieldCurrentStock.getText()); + && isValidMedicationManufacturer(textFieldManufacturer.getText()) + && isValidMedicationAdministrationMethod(textFieldAdministrationMethod.getText()) + && isValidStock(textFieldCurrentStock.getText()); this.buttonSave.setDisable(!isValid); }; @@ -94,7 +94,7 @@ public class MedicationModalController { /** * Internal method to show the data in the view. */ - private void showData(){ + private void showData() { ingredients.setAll(medication.getIngredients()); textFieldName.setText(medication.getName()); textFieldManufacturer.setText(medication.getManufacturer()); @@ -119,9 +119,9 @@ public class MedicationModalController { .toList() ); - if(isNewMedication){ + if (isNewMedication) { controller.createMedication(medication); - }else { + } else { controller.updateMedication(medication); }