diff --git a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java index 477dc14..2b4aab7 100644 --- a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java @@ -2,6 +2,7 @@ package de.hitec.nhplus.medication; import java.io.IOException; import java.sql.SQLException; +import java.util.List; import java.util.stream.Collectors; import de.hitec.nhplus.Main; @@ -56,14 +57,20 @@ public class AllMedicationController { this.columnName.setCellValueFactory(new PropertyValueFactory<>("name")); this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer")); this.columnIngredient.setCellValueFactory( - cellData -> new SimpleStringProperty( - cellData - .getValue() - .getIngredients() - .stream() - .map(ingredient -> ingredient.getName()) - .collect(Collectors.joining("\n")) - )); + 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")) + ); + }); this.columnPossibleSideEffects.setCellValueFactory(new PropertyValueFactory<>("possibleSideEffects")); this.columnAdministrationMethod.setCellValueFactory(new PropertyValueFactory<>("administrationMethod")); this.columnCurrentStock.setCellValueFactory(new PropertyValueFactory<>("currentStock")); diff --git a/src/main/java/de/hitec/nhplus/medication/IngredientListCell.java b/src/main/java/de/hitec/nhplus/medication/IngredientListCell.java index a69eb64..0e15a32 100644 --- a/src/main/java/de/hitec/nhplus/medication/IngredientListCell.java +++ b/src/main/java/de/hitec/nhplus/medication/IngredientListCell.java @@ -66,7 +66,7 @@ public class IngredientListCell extends ListCell { } private void onTextFieldUpdate(ObservableValue observable, String oldValue, String newValue) { - commitEdit(new Ingredient(textField.getText())); + getItem().setName(textField.getText()); textField.setMinWidth(getTextFieldRequiredWidth(textField)); diff --git a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java index 280fd4a..c8f572f 100644 --- a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java +++ b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java @@ -1,19 +1,23 @@ package de.hitec.nhplus.medication; -import de.hitec.nhplus.treatment.AllTreatmentController; +import javafx.beans.value.ChangeListener; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; +import javafx.scene.control.Button; import javafx.scene.control.ListView; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.stage.Stage; import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +import static de.hitec.nhplus.utils.Validator.*; public class MedicationModalController { - @FXML public ListView listViewIngredients; @FXML @@ -26,9 +30,10 @@ public class MedicationModalController { public TextField textFieldCurrentStock; @FXML public TextArea textAreaPossibleSideEffects; + @FXML + public Button buttonSave; private Stage stage; - private Medication medication; private final ObservableList ingredients = FXCollections.observableArrayList(); private AllMedicationController controller; @@ -37,19 +42,38 @@ public class MedicationModalController { public void initialize(Stage stage, AllMedicationController controller, Medication medication) { this.stage = stage; this.controller=controller; - this.medication = medication != null ? medication : new Medication( - "", - "", - new ArrayList<>(), - "", - "", - 0 - ); + + if( medication != null){ + this.medication = medication; + }else { + this.medication = new Medication( + "", + "", + new ArrayList<>(), + "", + "", + 0 + ); + this.buttonSave.setDisable(true); + } listViewIngredients.setCellFactory(cellData -> new IngredientListCell()); listViewIngredients.setItems(ingredients); - showData(); + + ChangeListener inputMedicationValidationListener = (observableValue, oldText, newText) -> { + boolean isValid = isValidMedicationName(textFieldName.getText()) + && isValidMedicationManufacturer(textFieldManufacturer.getText()) + && isValidMedicationAdministrationMethod(textFieldAdministrationMethod.getText()) + && isValidStock(textFieldCurrentStock.getText()); + + this.buttonSave.setDisable(!isValid); + }; + + this.textFieldName.textProperty().addListener(inputMedicationValidationListener); + this.textFieldManufacturer.textProperty().addListener(inputMedicationValidationListener); + this.textFieldAdministrationMethod.textProperty().addListener(inputMedicationValidationListener); + this.textFieldCurrentStock.textProperty().addListener(inputMedicationValidationListener); } private void showData(){ @@ -68,7 +92,14 @@ public class MedicationModalController { this.medication.setAdministrationMethod(textFieldAdministrationMethod.getText()); this.medication.setCurrentStock(Integer.parseInt(textFieldCurrentStock.getText())); this.medication.setPossibleSideEffects(textAreaPossibleSideEffects.getText()); - this.medication.setIngredients(ingredients); + this.medication.setIngredients(ingredients + .stream() + .map(Ingredient::getName) + .distinct() + .filter(Predicate.not(String::isEmpty)) + .map(Ingredient::new) + .toList() + ); controller.createMedication(medication); controller.readAllAndShowInTableView(); @@ -77,10 +108,11 @@ public class MedicationModalController { @FXML public void handleCancel() { + stage.close(); } @FXML public void handleAddIngredient() { - ingredients.add(new Ingredient(null)); + ingredients.add(new Ingredient("")); } } diff --git a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java index e53349c..e226770 100644 --- a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java +++ b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java @@ -100,7 +100,7 @@ public class MedicationDao implements Dao { result.getInt(1), result.getString(2), result.getString(3), - List.of(), + new ArrayList<>(), result.getString(4), result.getString(5), result.getInt(6) @@ -108,11 +108,16 @@ public class MedicationDao implements Dao { medications.add(medication); } List ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>()); - ingredients.add(new Ingredient(result.getString(7))); + String ingredientName = result.getString(7); + if(ingredientName == null){ + continue; + } + ingredients.add(new Ingredient(ingredientName)); lastMedicationId = currentMedicationId; } for (Medication medication : medications) { - if(!ingredientMap.containsKey(medication.getId())){ + List ingredients = ingredientMap.get(medication.getId()); + if(ingredients.isEmpty()){ continue; } medication.setIngredients(ingredientMap.get(medication.getId())); diff --git a/src/main/java/de/hitec/nhplus/utils/Validator.java b/src/main/java/de/hitec/nhplus/utils/Validator.java index 7931700..f331fa9 100644 --- a/src/main/java/de/hitec/nhplus/utils/Validator.java +++ b/src/main/java/de/hitec/nhplus/utils/Validator.java @@ -148,4 +148,28 @@ public class Validator { public static boolean isValidRoomNumber(String text) { return !text.isBlank(); } + + public static boolean isValidMedicationName(String text) { + return !text.isBlank(); + } + + public static boolean isValidMedicationManufacturer(String text) { + return !text.isBlank(); + } + + public static boolean isValidMedicationAdministrationMethod(String text) { + return !text.isBlank(); + } + + public static boolean isValidStock(String text) { + if (text.isBlank()) { + return false; + } + try { + int stock = Integer.parseInt(text); + return stock >= 0; + } catch (Exception exception) { + return false; + } + } }