Compare commits

..

No commits in common. "547d0298ee09cefefbbb5517a511e7cb290cd6bc" and "e881adcc041aa1742c58ff1309d547be414a1b73" have entirely different histories.

5 changed files with 28 additions and 94 deletions

View file

@ -2,7 +2,6 @@ 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;
@ -57,20 +56,14 @@ public class AllMedicationController {
this.columnName.setCellValueFactory(new PropertyValueFactory<>("name"));
this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer"));
this.columnIngredient.setCellValueFactory(
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"))
);
});
cellData -> new SimpleStringProperty(
cellData
.getValue()
.getIngredients()
.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) {
getItem().setName(textField.getText());
commitEdit(new Ingredient(textField.getText()));
textField.setMinWidth(getTextFieldRequiredWidth(textField));

View file

@ -1,23 +1,19 @@
package de.hitec.nhplus.medication;
import javafx.beans.value.ChangeListener;
import de.hitec.nhplus.treatment.AllTreatmentController;
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
@ -30,10 +26,9 @@ 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;
@ -42,38 +37,19 @@ public class MedicationModalController {
public void initialize(Stage stage, AllMedicationController controller, Medication medication) {
this.stage = stage;
this.controller=controller;
if( medication != null){
this.medication = medication;
}else {
this.medication = new Medication(
"",
"",
new ArrayList<>(),
"",
"",
0
);
this.buttonSave.setDisable(true);
}
this.medication = medication != null ? medication : new Medication(
"",
"",
new ArrayList<>(),
"",
"",
0
);
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(){
@ -92,14 +68,7 @@ public class MedicationModalController {
this.medication.setAdministrationMethod(textFieldAdministrationMethod.getText());
this.medication.setCurrentStock(Integer.parseInt(textFieldCurrentStock.getText()));
this.medication.setPossibleSideEffects(textAreaPossibleSideEffects.getText());
this.medication.setIngredients(ingredients
.stream()
.map(Ingredient::getName)
.distinct()
.filter(Predicate.not(String::isEmpty))
.map(Ingredient::new)
.toList()
);
this.medication.setIngredients(ingredients);
controller.createMedication(medication);
controller.readAllAndShowInTableView();
@ -108,11 +77,10 @@ public class MedicationModalController {
@FXML
public void handleCancel() {
stage.close();
}
@FXML
public void handleAddIngredient() {
ingredients.add(new Ingredient(""));
ingredients.add(new Ingredient(null));
}
}

View file

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

View file

@ -148,28 +148,4 @@ 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;
}
}
}