From 9ebd585dd21cd67b4815444fc87d7b0cabc1891b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Thu, 11 Apr 2024 09:59:33 +0200 Subject: [PATCH 1/2] [#4]: Cleanup --- .../java/de/hitec/nhplus/controller/AllPatientController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/controller/AllPatientController.java b/src/main/java/de/hitec/nhplus/controller/AllPatientController.java index efdeb7c..9ff5fe8 100644 --- a/src/main/java/de/hitec/nhplus/controller/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/controller/AllPatientController.java @@ -119,7 +119,7 @@ public class AllPatientController { this.buttonAdd.setDisable(true); ChangeListener inputNewPatientListener = (observableValue, oldText, newText) -> - AllPatientController.this.buttonAdd.setDisable(!AllPatientController.this.areInputDataValid()); + AllPatientController.this.buttonAdd.setDisable(!AllPatientController.this.isInputDataValid()); this.textFieldSurname.textProperty().addListener(inputNewPatientListener); this.textFieldFirstName.textProperty().addListener(inputNewPatientListener); this.textFieldDateOfBirth.textProperty().addListener(inputNewPatientListener); @@ -274,7 +274,7 @@ public class AllPatientController { this.textFieldAssets.clear(); } - private boolean areInputDataValid() { + private boolean isInputDataValid() { if (!this.textFieldDateOfBirth.getText().isBlank()) { try { DateConverter.convertStringToLocalDate(this.textFieldDateOfBirth.getText()); From 9eeec42ddc7ea016e233b1d76d7dbee1111237b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Tue, 16 Apr 2024 15:13:47 +0200 Subject: [PATCH 2/2] [#4]: Validation of Patients & Treatments for Creating and Editing --- .../controller/AllPatientController.java | 257 ++++++++---------- .../controller/AllTreatmentController.java | 118 +++++--- .../controller/NewTreatmentController.java | 92 ++++--- .../controller/TreatmentController.java | 39 ++- .../java/de/hitec/nhplus/utils/Validator.java | 79 ++++++ .../de/hitec/nhplus/NewTreatmentView.fxml | 154 ++++++----- .../de/hitec/nhplus/TreatmentView.fxml | 167 ++++++------ 7 files changed, 531 insertions(+), 375 deletions(-) create mode 100644 src/main/java/de/hitec/nhplus/utils/Validator.java diff --git a/src/main/java/de/hitec/nhplus/controller/AllPatientController.java b/src/main/java/de/hitec/nhplus/controller/AllPatientController.java index 9ff5fe8..9aa813f 100644 --- a/src/main/java/de/hitec/nhplus/controller/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/controller/AllPatientController.java @@ -2,8 +2,9 @@ package de.hitec.nhplus.controller; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.PatientDao; +import de.hitec.nhplus.model.Patient; +import de.hitec.nhplus.utils.DateConverter; import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; @@ -13,82 +14,58 @@ import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.TextFieldTableCell; -import de.hitec.nhplus.model.Patient; -import de.hitec.nhplus.utils.DateConverter; import java.sql.SQLException; import java.time.LocalDate; +import static de.hitec.nhplus.utils.Validator.*; -/** - * The AllPatientController contains the entire logic of the patient view. It determines which data is displayed and how to react to events. - */ -public class AllPatientController { +public class AllPatientController +{ @FXML private TableView tableView; - @FXML private TableColumn columnId; - @FXML private TableColumn columnFirstName; - @FXML private TableColumn columnSurname; - @FXML private TableColumn columnDateOfBirth; - @FXML private TableColumn columnCareLevel; - @FXML private TableColumn columnRoomNumber; - @FXML private TableColumn columnAssets; - @FXML private Button buttonDelete; - @FXML private Button buttonAdd; - @FXML private TextField textFieldSurname; - @FXML private TextField textFieldFirstName; - @FXML private TextField textFieldDateOfBirth; - @FXML private TextField textFieldCareLevel; - @FXML private TextField textFieldRoomNumber; - @FXML private TextField textFieldAssets; private final ObservableList patients = FXCollections.observableArrayList(); private PatientDao dao; - /** - * When initialize() gets called, all fields are already initialized. For example from the FXMLLoader - * after loading an FXML-File. At this point of the lifecycle of the Controller, the fields can be accessed and - * configured. - */ - public void initialize() { + public void initialize() + { this.readAllAndShowInTableView(); this.columnId.setCellValueFactory(new PropertyValueFactory<>("pid")); - // CellValueFactory to show property values in TableView this.columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName")); - // CellFactory to write property values from with in the TableView this.columnFirstName.setCellFactory(TextFieldTableCell.forTableColumn()); this.columnSurname.setCellValueFactory(new PropertyValueFactory<>("surname")); @@ -106,146 +83,166 @@ public class AllPatientController { this.columnAssets.setCellValueFactory(new PropertyValueFactory<>("assets")); this.columnAssets.setCellFactory(TextFieldTableCell.forTableColumn()); - //Anzeigen der Daten this.tableView.setItems(this.patients); this.buttonDelete.setDisable(true); - this.tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observableValue, Patient oldPatient, Patient newPatient) {; - AllPatientController.this.buttonDelete.setDisable(newPatient == null); - } - }); + this.tableView + .getSelectionModel() + .selectedItemProperty() + .addListener((observableValue, oldPatient, newPatient) -> + { + AllPatientController.this.buttonDelete.setDisable(newPatient == null); + } + ); this.buttonAdd.setDisable(true); - ChangeListener inputNewPatientListener = (observableValue, oldText, newText) -> - AllPatientController.this.buttonAdd.setDisable(!AllPatientController.this.isInputDataValid()); - this.textFieldSurname.textProperty().addListener(inputNewPatientListener); - this.textFieldFirstName.textProperty().addListener(inputNewPatientListener); - this.textFieldDateOfBirth.textProperty().addListener(inputNewPatientListener); - this.textFieldCareLevel.textProperty().addListener(inputNewPatientListener); - this.textFieldRoomNumber.textProperty().addListener(inputNewPatientListener); - this.textFieldAssets.textProperty().addListener(inputNewPatientListener); + ChangeListener inputNewPatientValidationListener = (observableValue, oldText, newText) -> + { + boolean isValid = isValidDate(this.textFieldDateOfBirth.getText()) + && isValidFirstName(this.textFieldFirstName.getText()) + && isValidSurName(this.textFieldSurname.getText()) + && isValidDate(this.textFieldDateOfBirth.getText()) + && isValidCareLevel(this.textFieldCareLevel.getText()) + && isValidRoomNumber(textFieldRoomNumber.getText()) + && !this.textFieldAssets.getText().isBlank(); + + AllPatientController.this.buttonAdd.setDisable(!isValid); + }; + + this.textFieldSurname.textProperty().addListener(inputNewPatientValidationListener); + this.textFieldFirstName.textProperty().addListener(inputNewPatientValidationListener); + this.textFieldDateOfBirth.textProperty().addListener(inputNewPatientValidationListener); + this.textFieldCareLevel.textProperty().addListener(inputNewPatientValidationListener); + this.textFieldRoomNumber.textProperty().addListener(inputNewPatientValidationListener); + this.textFieldAssets.textProperty().addListener(inputNewPatientValidationListener); } - /** - * When a cell of the column with first names was changed, this method will be called, to persist the change. - * - * @param event Event including the changed object and the change. - */ @FXML - public void handleOnEditFirstname(TableColumn.CellEditEvent event) { - event.getRowValue().setFirstName(event.getNewValue()); + public void handleOnEditFirstname(TableColumn.CellEditEvent event) + { + String newFirstName = event.getNewValue(); + if (!isValidFirstName(newFirstName)) + { + showValidationError("First Name"); + event.getTableView().refresh(); + return; + } + event.getRowValue().setFirstName(newFirstName); this.doUpdate(event); } - /** - * When a cell of the column with surnames was changed, this method will be called, to persist the change. - * - * @param event Event including the changed object and the change. - */ @FXML - public void handleOnEditSurname(TableColumn.CellEditEvent event) { - event.getRowValue().setSurname(event.getNewValue()); + public void handleOnEditSurname(TableColumn.CellEditEvent event) + { + String newSurName = event.getNewValue(); + if (!isValidSurName(newSurName)) + { + showValidationError("Sur Name"); + event.getTableView().refresh(); + return; + } + event.getRowValue().setSurname(newSurName); this.doUpdate(event); } - /** - * When a cell of the column with dates of birth was changed, this method will be called, to persist the change. - * - * @param event Event including the changed object and the change. - */ @FXML - public void handleOnEditDateOfBirth(TableColumn.CellEditEvent event) { - event.getRowValue().setDateOfBirth(event.getNewValue()); + public void handleOnEditDateOfBirth(TableColumn.CellEditEvent event) + { + String newDateString = event.getNewValue(); + if (!isValidDate(newDateString)) + { + showValidationError("Date"); + event.getTableView().refresh(); + return; + } + event.getRowValue().setDateOfBirth(newDateString); this.doUpdate(event); } - /** - * When a cell of the column with care levels was changed, this method will be called, to persist the change. - * - * @param event Event including the changed object and the change. - */ @FXML - public void handleOnEditCareLevel(TableColumn.CellEditEvent event) { - event.getRowValue().setCareLevel(event.getNewValue()); + public void handleOnEditCareLevel(TableColumn.CellEditEvent event) + { + String newCareLevel = event.getNewValue(); + if (!isValidCareLevel(newCareLevel)) + { + showValidationError("Care Level"); + event.getTableView().refresh(); + return; + } + event.getRowValue().setCareLevel(newCareLevel); this.doUpdate(event); } - /** - * When a cell of the column with room numbers was changed, this method will be called, to persist the change. - * - * @param event Event including the changed object and the change. - */ @FXML - public void handleOnEditRoomNumber(TableColumn.CellEditEvent event){ - event.getRowValue().setRoomNumber(event.getNewValue()); + public void handleOnEditRoomNumber(TableColumn.CellEditEvent event) + { + String newRoomNumber = event.getNewValue(); + if (!isValidRoomNumber(newRoomNumber)) + { + showValidationError("Room Number"); + event.getTableView().refresh(); + return; + } + event.getRowValue().setRoomNumber(newRoomNumber); this.doUpdate(event); } - /** - * When a cell of the column with assets was changed, this method will be called, to persist the change. - * - * @param event Event including the changed object and the change. - */ @FXML - public void handleOnEditAssets(TableColumn.CellEditEvent event){ - event.getRowValue().setAssets(event.getNewValue()); + public void handleOnEditAssets(TableColumn.CellEditEvent event) + { + String newAssets = event.getNewValue(); + if(newAssets.isBlank()){ + event.getTableView().refresh(); + return; + } + event.getRowValue().setAssets(newAssets); this.doUpdate(event); } - /** - * Updates a patient by calling the method update() of {@link PatientDao}. - * - * @param event Event including the changed object and the change. - */ - private void doUpdate(TableColumn.CellEditEvent event) { - try { + private void doUpdate(TableColumn.CellEditEvent event) + { + try + { this.dao.update(event.getRowValue()); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } - /** - * Reloads all patients to the table by clearing the list of all patients and filling it again by all persisted - * patients, delivered by {@link PatientDao}. - */ - private void readAllAndShowInTableView() { + private void readAllAndShowInTableView() + { this.patients.clear(); this.dao = DaoFactory.getDaoFactory().createPatientDAO(); - try { + try + { this.patients.addAll(this.dao.readAll()); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } - /** - * This method handles events fired by the button to delete patients. It calls {@link PatientDao} to delete the - * patient from the database and removes the object from the list, which is the data source of the - * TableView. - */ @FXML - public void handleDelete() { + public void handleDelete() + { Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem(); - if (selectedItem != null) { - try { + if (selectedItem != null) + { + try + { DaoFactory.getDaoFactory().createPatientDAO().deleteById(selectedItem.getPid()); this.tableView.getItems().remove(selectedItem); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } } - /** - * This method handles the events fired by the button to add a patient. It collects the data from the - * TextFields, creates an object of class Patient of it and passes the object to - * {@link PatientDao} to persist the data. - */ @FXML - public void handleAdd() { + public void handleAdd() + { String surname = this.textFieldSurname.getText(); String firstName = this.textFieldFirstName.getText(); String birthday = this.textFieldDateOfBirth.getText(); @@ -253,19 +250,19 @@ public class AllPatientController { String careLevel = this.textFieldCareLevel.getText(); String roomNumber = this.textFieldRoomNumber.getText(); String assets = this.textFieldAssets.getText(); - try { + try + { this.dao.create(new Patient(firstName, surname, date, careLevel, roomNumber, assets)); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } readAllAndShowInTableView(); clearTextfields(); } - /** - * Clears all contents from all TextFields. - */ - private void clearTextfields() { + private void clearTextfields() + { this.textFieldFirstName.clear(); this.textFieldSurname.clear(); this.textFieldDateOfBirth.clear(); @@ -273,18 +270,4 @@ public class AllPatientController { this.textFieldRoomNumber.clear(); this.textFieldAssets.clear(); } - - private boolean isInputDataValid() { - if (!this.textFieldDateOfBirth.getText().isBlank()) { - try { - DateConverter.convertStringToLocalDate(this.textFieldDateOfBirth.getText()); - } catch (Exception exception) { - return false; - } - } - - return !this.textFieldFirstName.getText().isBlank() && !this.textFieldSurname.getText().isBlank() && - !this.textFieldDateOfBirth.getText().isBlank() && !this.textFieldCareLevel.getText().isBlank() && - !this.textFieldRoomNumber.getText().isBlank() && !this.textFieldAssets.getText().isBlank(); - } } diff --git a/src/main/java/de/hitec/nhplus/controller/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/controller/AllTreatmentController.java index 33f4370..b0ec78f 100644 --- a/src/main/java/de/hitec/nhplus/controller/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/controller/AllTreatmentController.java @@ -4,6 +4,8 @@ import de.hitec.nhplus.Main; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.PatientDao; import de.hitec.nhplus.datastorage.TreatmentDao; +import de.hitec.nhplus.model.Patient; +import de.hitec.nhplus.model.Treatment; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; @@ -13,14 +15,13 @@ import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; -import de.hitec.nhplus.model.Patient; -import de.hitec.nhplus.model.Treatment; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; -public class AllTreatmentController { +public class AllTreatmentController +{ @FXML private TableView tableView; @@ -54,7 +55,8 @@ public class AllTreatmentController { private final ObservableList patientSelection = FXCollections.observableArrayList(); private ArrayList patientList; - public void initialize() { + public void initialize() + { readAllAndShowInTableView(); comboBoxPatientSelection.setItems(patientSelection); comboBoxPatientSelection.getSelectionModel().select(0); @@ -67,67 +69,88 @@ public class AllTreatmentController { this.columnDescription.setCellValueFactory(new PropertyValueFactory<>("description")); this.tableView.setItems(this.treatments); - // Disabling the button to delete treatments as long, as no treatment was selected. this.buttonDelete.setDisable(true); - this.tableView.getSelectionModel().selectedItemProperty().addListener( - (observableValue, oldTreatment, newTreatment) -> - AllTreatmentController.this.buttonDelete.setDisable(newTreatment == null)); + this.tableView + .getSelectionModel() + .selectedItemProperty() + .addListener((observableValue, oldTreatment, newTreatment) -> + { + AllTreatmentController.this.buttonDelete.setDisable(newTreatment == null); + } + ); this.createComboBoxData(); } - public void readAllAndShowInTableView() { + public void readAllAndShowInTableView() + { this.treatments.clear(); comboBoxPatientSelection.getSelectionModel().select(0); this.dao = DaoFactory.getDaoFactory().createTreatmentDao(); - try { + try + { this.treatments.addAll(dao.readAll()); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } - private void createComboBoxData() { + private void createComboBoxData() + { PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO(); - try { + try + { patientList = (ArrayList) dao.readAll(); this.patientSelection.add("alle"); - for (Patient patient: patientList) { + for (Patient patient : patientList) + { this.patientSelection.add(patient.getSurname()); } - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } @FXML - public void handleComboBox() { + public void handleComboBox() + { String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem(); this.treatments.clear(); this.dao = DaoFactory.getDaoFactory().createTreatmentDao(); - if (selectedPatient.equals("alle")) { - try { + if (selectedPatient.equals("alle")) + { + try + { this.treatments.addAll(this.dao.readAll()); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } Patient patient = searchInList(selectedPatient); - if (patient !=null) { - try { + if (patient != null) + { + try + { this.treatments.addAll(this.dao.readTreatmentsByPid(patient.getPid())); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } } - private Patient searchInList(String surname) { - for (Patient patient : this.patientList) { - if (patient.getSurname().equals(surname)) { + private Patient searchInList(String surname) + { + for (Patient patient : this.patientList) + { + if (patient.getSurname().equals(surname)) + { return patient; } } @@ -135,24 +158,30 @@ public class AllTreatmentController { } @FXML - public void handleDelete() { + public void handleDelete() + { int index = this.tableView.getSelectionModel().getSelectedIndex(); Treatment t = this.treatments.remove(index); TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); - try { + try + { dao.deleteById(t.getTid()); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } @FXML - public void handleNewTreatment() { - try{ + public void handleNewTreatment() + { + try + { String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem(); Patient patient = searchInList(selectedPatient); newTreatmentWindow(patient); - } catch (NullPointerException exception){ + } catch (NullPointerException exception) + { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Information"); alert.setHeaderText("Patient für die Behandlung fehlt!"); @@ -162,9 +191,12 @@ public class AllTreatmentController { } @FXML - public void handleMouseClick() { - tableView.setOnMouseClicked(event -> { - if (event.getClickCount() == 2 && (tableView.getSelectionModel().getSelectedItem() != null)) { + public void handleMouseClick() + { + tableView.setOnMouseClicked(event -> + { + if (event.getClickCount() == 2 && (tableView.getSelectionModel().getSelectedItem() != null)) + { int index = this.tableView.getSelectionModel().getSelectedIndex(); Treatment treatment = this.treatments.get(index); treatmentWindow(treatment); @@ -172,8 +204,10 @@ public class AllTreatmentController { }); } - public void newTreatmentWindow(Patient patient) { - try { + public void newTreatmentWindow(Patient patient) + { + try + { FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/NewTreatmentView.fxml")); AnchorPane pane = loader.load(); Scene scene = new Scene(pane); @@ -187,13 +221,16 @@ public class AllTreatmentController { stage.setScene(scene); stage.setResizable(false); stage.showAndWait(); - } catch (IOException exception) { + } catch (IOException exception) + { exception.printStackTrace(); } } - public void treatmentWindow(Treatment treatment){ - try { + public void treatmentWindow(Treatment treatment) + { + try + { FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/TreatmentView.fxml")); AnchorPane pane = loader.load(); Scene scene = new Scene(pane); @@ -206,7 +243,8 @@ public class AllTreatmentController { stage.setScene(scene); stage.setResizable(false); stage.showAndWait(); - } catch (IOException exception) { + } catch (IOException exception) + { exception.printStackTrace(); } } diff --git a/src/main/java/de/hitec/nhplus/controller/NewTreatmentController.java b/src/main/java/de/hitec/nhplus/controller/NewTreatmentController.java index 0370b40..39e8c35 100644 --- a/src/main/java/de/hitec/nhplus/controller/NewTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/controller/NewTreatmentController.java @@ -2,20 +2,23 @@ package de.hitec.nhplus.controller; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.TreatmentDao; +import de.hitec.nhplus.model.Patient; +import de.hitec.nhplus.model.Treatment; +import de.hitec.nhplus.utils.DateConverter; import javafx.beans.value.ChangeListener; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.stage.Stage; -import de.hitec.nhplus.model.Patient; -import de.hitec.nhplus.model.Treatment; -import de.hitec.nhplus.utils.DateConverter; import javafx.util.StringConverter; import java.sql.SQLException; import java.time.LocalDate; import java.time.LocalTime; -public class NewTreatmentController { +import static de.hitec.nhplus.utils.Validator.*; + +public class NewTreatmentController +{ @FXML private Label labelFirstName; @@ -45,40 +48,66 @@ public class NewTreatmentController { private Patient patient; private Stage stage; - public void initialize(AllTreatmentController controller, Stage stage, Patient patient) { - this.controller= controller; + public void initialize(AllTreatmentController controller, Stage stage, Patient patient) + { + this.controller = controller; this.patient = patient; this.stage = stage; this.buttonAdd.setDisable(true); - ChangeListener inputNewPatientListener = (observableValue, oldText, newText) -> - NewTreatmentController.this.buttonAdd.setDisable(NewTreatmentController.this.areInputDataInvalid()); - this.textFieldBegin.textProperty().addListener(inputNewPatientListener); - this.textFieldEnd.textProperty().addListener(inputNewPatientListener); - this.textFieldDescription.textProperty().addListener(inputNewPatientListener); - this.textAreaRemarks.textProperty().addListener(inputNewPatientListener); - this.datePicker.valueProperty().addListener((observableValue, localDate, t1) -> NewTreatmentController.this.buttonAdd.setDisable(NewTreatmentController.this.areInputDataInvalid())); - this.datePicker.setConverter(new StringConverter<>() { + ChangeListener inputNewTreatmentTextValidationListener = (observableValue, oldText, newText) -> + { + boolean isValid = isValidDate(this.datePicker.getValue()) + && isValidTimeRange(this.textFieldBegin.getText(), this.textFieldEnd.getText()) + && isValidDescription(this.textFieldDescription.getText()); + + NewTreatmentController.this.buttonAdd.setDisable(!isValid); + }; + ChangeListener inputNewTreatmentDateValidationListener = (observableValue, localDate, t1) -> + { + boolean isValid = isValidDate(this.datePicker.getValue()) + && isValidTimeRange(this.textFieldBegin.getText(), this.textFieldEnd.getText()) + && isValidDescription(this.textFieldDescription.getText()); + + NewTreatmentController.this.buttonAdd.setDisable(!isValid); + }; + + this.textFieldBegin.textProperty().addListener(inputNewTreatmentTextValidationListener); + this.textFieldEnd.textProperty().addListener(inputNewTreatmentTextValidationListener); + this.textFieldDescription.textProperty().addListener(inputNewTreatmentTextValidationListener); + this.textAreaRemarks.textProperty().addListener(inputNewTreatmentTextValidationListener); + this.datePicker.valueProperty().addListener(inputNewTreatmentDateValidationListener); + + this.datePicker.setConverter(new StringConverter<>() + { @Override - public String toString(LocalDate localDate) { + public String toString(LocalDate localDate) + { return (localDate == null) ? "" : DateConverter.convertLocalDateToString(localDate); } @Override - public LocalDate fromString(String localDate) { + public LocalDate fromString(String localDate) + { + if (!isValidDate(localDate)) + { + return null; + } return DateConverter.convertStringToLocalDate(localDate); } }); this.showPatientData(); } - private void showPatientData(){ + private void showPatientData() + { this.labelFirstName.setText(patient.getFirstName()); this.labelSurname.setText(patient.getSurname()); } @FXML - public void handleAdd(){ + public void handleAdd() + { LocalDate date = this.datePicker.getValue(); LocalTime begin = DateConverter.convertStringToLocalTime(textFieldBegin.getText()); LocalTime end = DateConverter.convertStringToLocalTime(textFieldEnd.getText()); @@ -90,33 +119,22 @@ public class NewTreatmentController { stage.close(); } - private void createTreatment(Treatment treatment) { + private void createTreatment(Treatment treatment) + { TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); - try { + try + { dao.create(treatment); - } catch (SQLException exception) { + } catch (SQLException exception) + { exception.printStackTrace(); } } @FXML - public void handleCancel(){ + public void handleCancel() + { stage.close(); } - private boolean areInputDataInvalid() { - if (this.textFieldBegin.getText() == null || this.textFieldEnd.getText() == null) { - return true; - } - try { - LocalTime begin = DateConverter.convertStringToLocalTime(this.textFieldBegin.getText()); - LocalTime end = DateConverter.convertStringToLocalTime(this.textFieldEnd.getText()); - if (!end.isAfter(begin)) { - return true; - } - } catch (Exception exception) { - return true; - } - return this.textFieldDescription.getText().isBlank() || this.datePicker.getValue() == null; - } } \ No newline at end of file diff --git a/src/main/java/de/hitec/nhplus/controller/TreatmentController.java b/src/main/java/de/hitec/nhplus/controller/TreatmentController.java index a24f54a..12cbf84 100644 --- a/src/main/java/de/hitec/nhplus/controller/TreatmentController.java +++ b/src/main/java/de/hitec/nhplus/controller/TreatmentController.java @@ -13,6 +13,8 @@ import de.hitec.nhplus.utils.DateConverter; import java.sql.SQLException; import java.time.LocalDate; +import static de.hitec.nhplus.utils.Validator.*; + public class TreatmentController { @FXML @@ -44,6 +46,7 @@ public class TreatmentController { public void initializeController(AllTreatmentController controller, Stage stage, Treatment treatment) { this.stage = stage; this.controller= controller; + PatientDao pDao = DaoFactory.getDaoFactory().createPatientDAO(); try { this.patient = pDao.read((int) treatment.getPid()); @@ -67,11 +70,37 @@ public class TreatmentController { @FXML public void handleChange(){ - this.treatment.setDate(this.datePicker.getValue().toString()); - this.treatment.setBegin(textFieldBegin.getText()); - this.treatment.setEnd(textFieldEnd.getText()); - this.treatment.setDescription(textFieldDescription.getText()); - this.treatment.setRemarks(textAreaRemarks.getText()); + LocalDate newDate = this.datePicker.getValue(); + if(!isValidDate(newDate)){ + showValidationError("Date"); + return; + } + + String newDescription = textFieldDescription.getText(); + if(!isValidDescription(newDescription)){ + showValidationError("Description"); + return; + } + + String newBegin = textFieldBegin.getText(); + String newEnd = textFieldEnd.getText(); + if(!isValidTimeRange(newBegin, newEnd)){ + showValidationError("Time Range"); + return; + } + + String newRemarks = textAreaRemarks.getText(); + if(!isValidDescription(newRemarks)){ + showValidationError("Remarks"); + return; + } + + this.treatment.setDate(newDate.toString()); + this.treatment.setDescription(newDescription); + this.treatment.setBegin(newBegin); + this.treatment.setEnd(newEnd); + this.treatment.setRemarks(newRemarks); + doUpdate(); controller.readAllAndShowInTableView(); stage.close(); diff --git a/src/main/java/de/hitec/nhplus/utils/Validator.java b/src/main/java/de/hitec/nhplus/utils/Validator.java new file mode 100644 index 0000000..433341b --- /dev/null +++ b/src/main/java/de/hitec/nhplus/utils/Validator.java @@ -0,0 +1,79 @@ +package de.hitec.nhplus.utils; + +import javafx.scene.control.Alert; + +import java.time.LocalDate; +import java.time.LocalTime; + +public class Validator +{ + public static void showValidationError(String type){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Error"); + alert.setHeaderText(null); + alert.setContentText("Invalid " + type + " !"); + alert.showAndWait(); + } + public static boolean isValidDate(String text) { + if(text.isBlank()){ + return false; + } + try { + DateConverter.convertStringToLocalDate(text); + } catch (Exception exception) { + return false; + } + return true; + } + public static boolean isValidDate(LocalDate date) { + if(date == null){ + return false; + } + try { + DateConverter.convertStringToLocalDate(date.toString()); + } catch (Exception exception) { + return false; + } + return true; + } + public static boolean isValidTime(String text){ + if(text.isBlank()){ + return false; + } + try { + DateConverter.convertStringToLocalTime(text); + }catch (Exception exception){ + return false; + } + return true; + } + public static boolean isValidTimeRange(String start, String end){ + if( + !isValidTime(start) || !isValidTime(end) + ){ + return false; + } + LocalTime startTime = DateConverter.convertStringToLocalTime(start); + LocalTime endTime = DateConverter.convertStringToLocalTime(end); + return startTime.isBefore(endTime); + + } + + public static boolean isValidDescription(String text) { + return !text.isBlank(); + } + public static boolean isValidFirstName(String text){ + return !text.isBlank(); + } + public static boolean isValidSurName(String text){ + return !text.isBlank(); + } + public static boolean isValidCareLevel(String text){ + return !text.isBlank(); + } + public static boolean isValidRoomNumber(String text){ + return !text.isBlank(); + } + +} + diff --git a/src/main/resources/de/hitec/nhplus/NewTreatmentView.fxml b/src/main/resources/de/hitec/nhplus/NewTreatmentView.fxml index f5f8b37..f541e86 100644 --- a/src/main/resources/de/hitec/nhplus/NewTreatmentView.fxml +++ b/src/main/resources/de/hitec/nhplus/NewTreatmentView.fxml @@ -3,77 +3,85 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -