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.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<Ingredient> 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"));

View file

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

View file

@ -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<Ingredient> 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<Ingredient> 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<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(){
@ -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(""));
}
}

View file

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

View file

@ -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;
}
}
}