Compare commits

..

2 commits

Author SHA1 Message Date
547d0298ee
NOTICKET: Fix this
All checks were successful
Quality Check / Linting Check (push) Successful in 12s
Quality Check / Javadoc Check (push) Successful in 21s
2024-05-17 11:51:14 +02:00
b76f172079
NOTICKET: Fix MedicationDao ReadAll 2024-05-17 09:18:40 +02:00
5 changed files with 94 additions and 28 deletions

View file

@ -2,6 +2,7 @@ package de.hitec.nhplus.medication;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import de.hitec.nhplus.Main; import de.hitec.nhplus.Main;
@ -56,14 +57,20 @@ public class AllMedicationController {
this.columnName.setCellValueFactory(new PropertyValueFactory<>("name")); this.columnName.setCellValueFactory(new PropertyValueFactory<>("name"));
this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer")); this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer"));
this.columnIngredient.setCellValueFactory( this.columnIngredient.setCellValueFactory(
cellData -> new SimpleStringProperty( cellData -> {
cellData Medication medication = cellData.getValue();
.getValue() List<Ingredient> ingredients = medication.getIngredients();
.getIngredients() if(ingredients.isEmpty()){
.stream() return new SimpleStringProperty("");
.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.columnPossibleSideEffects.setCellValueFactory(new PropertyValueFactory<>("possibleSideEffects"));
this.columnAdministrationMethod.setCellValueFactory(new PropertyValueFactory<>("administrationMethod")); this.columnAdministrationMethod.setCellValueFactory(new PropertyValueFactory<>("administrationMethod"));
this.columnCurrentStock.setCellValueFactory(new PropertyValueFactory<>("currentStock")); this.columnCurrentStock.setCellValueFactory(new PropertyValueFactory<>("currentStock"));

View file

@ -66,7 +66,7 @@ public class IngredientListCell extends ListCell<Ingredient> {
} }
private void onTextFieldUpdate(ObservableValue<? extends String> observable, String oldValue, String newValue) { private void onTextFieldUpdate(ObservableValue<? extends String> observable, String oldValue, String newValue) {
commitEdit(new Ingredient(textField.getText())); getItem().setName(textField.getText());
textField.setMinWidth(getTextFieldRequiredWidth(textField)); textField.setMinWidth(getTextFieldRequiredWidth(textField));

View file

@ -1,19 +1,23 @@
package de.hitec.nhplus.medication; package de.hitec.nhplus.medication;
import de.hitec.nhplus.treatment.AllTreatmentController; import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.control.TextArea; import javafx.scene.control.TextArea;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import static de.hitec.nhplus.utils.Validator.*;
public class MedicationModalController { public class MedicationModalController {
@FXML @FXML
public ListView<Ingredient> listViewIngredients; public ListView<Ingredient> listViewIngredients;
@FXML @FXML
@ -26,9 +30,10 @@ public class MedicationModalController {
public TextField textFieldCurrentStock; public TextField textFieldCurrentStock;
@FXML @FXML
public TextArea textAreaPossibleSideEffects; public TextArea textAreaPossibleSideEffects;
@FXML
public Button buttonSave;
private Stage stage; private Stage stage;
private Medication medication; private Medication medication;
private final ObservableList<Ingredient> ingredients = FXCollections.observableArrayList(); private final ObservableList<Ingredient> ingredients = FXCollections.observableArrayList();
private AllMedicationController controller; private AllMedicationController controller;
@ -37,19 +42,38 @@ public class MedicationModalController {
public void initialize(Stage stage, AllMedicationController controller, Medication medication) { public void initialize(Stage stage, AllMedicationController controller, Medication medication) {
this.stage = stage; this.stage = stage;
this.controller=controller; this.controller=controller;
this.medication = medication != null ? medication : new Medication(
"", if( medication != null){
"", this.medication = medication;
new ArrayList<>(), }else {
"", this.medication = new Medication(
"", "",
0 "",
); new ArrayList<>(),
"",
"",
0
);
this.buttonSave.setDisable(true);
}
listViewIngredients.setCellFactory(cellData -> new IngredientListCell()); listViewIngredients.setCellFactory(cellData -> new IngredientListCell());
listViewIngredients.setItems(ingredients); listViewIngredients.setItems(ingredients);
showData(); showData();
ChangeListener<String> 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(){ private void showData(){
@ -68,7 +92,14 @@ public class MedicationModalController {
this.medication.setAdministrationMethod(textFieldAdministrationMethod.getText()); this.medication.setAdministrationMethod(textFieldAdministrationMethod.getText());
this.medication.setCurrentStock(Integer.parseInt(textFieldCurrentStock.getText())); this.medication.setCurrentStock(Integer.parseInt(textFieldCurrentStock.getText()));
this.medication.setPossibleSideEffects(textAreaPossibleSideEffects.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.createMedication(medication);
controller.readAllAndShowInTableView(); controller.readAllAndShowInTableView();
@ -77,10 +108,11 @@ public class MedicationModalController {
@FXML @FXML
public void handleCancel() { public void handleCancel() {
stage.close();
} }
@FXML @FXML
public void handleAddIngredient() { public void handleAddIngredient() {
ingredients.add(new Ingredient(null)); ingredients.add(new Ingredient(""));
} }
} }

View file

@ -100,21 +100,24 @@ public class MedicationDao implements Dao<Medication> {
result.getInt(1), result.getInt(1),
result.getString(2), result.getString(2),
result.getString(3), result.getString(3),
List.of(), new ArrayList<>(),
result.getString(4), result.getString(4),
result.getString(5), result.getString(5),
result.getInt(6) result.getInt(6)
); );
medications.add(medication); medications.add(medication);
lastMedicationId = currentMedicationId;
continue;
} }
List<Ingredient> ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>()); List<Ingredient> 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; lastMedicationId = currentMedicationId;
} }
for (Medication medication : medications) { for (Medication medication : medications) {
if(!ingredientMap.containsKey(medication.getId())){ List<Ingredient> ingredients = ingredientMap.get(medication.getId());
if(ingredients.isEmpty()){
continue; continue;
} }
medication.setIngredients(ingredientMap.get(medication.getId())); medication.setIngredients(ingredientMap.get(medication.getId()));

View file

@ -148,4 +148,28 @@ public class Validator {
public static boolean isValidRoomNumber(String text) { public static boolean isValidRoomNumber(String text) {
return !text.isBlank(); 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;
}
}
} }