#23: Implementing Medication Modal with Validation
This commit is contained in:
parent
71fd693f37
commit
c5237fed83
4 changed files with 199 additions and 11 deletions
|
@ -1,17 +1,24 @@
|
||||||
package de.hitec.nhplus.medication;
|
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;
|
||||||
import de.hitec.nhplus.datastorage.DaoFactory;
|
import de.hitec.nhplus.datastorage.DaoFactory;
|
||||||
import de.hitec.nhplus.medication.database.MedicationDao;
|
import de.hitec.nhplus.medication.database.MedicationDao;
|
||||||
import javafx.beans.property.SimpleStringProperty;
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
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.fxml.FXMLLoader;
|
||||||
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.TableColumn;
|
import javafx.scene.control.TableColumn;
|
||||||
import javafx.scene.control.TableView;
|
import javafx.scene.control.TableView;
|
||||||
import javafx.scene.control.cell.PropertyValueFactory;
|
import javafx.scene.control.cell.PropertyValueFactory;
|
||||||
|
import javafx.scene.layout.BorderPane;
|
||||||
import java.sql.SQLException;
|
import javafx.stage.Stage;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The controller for viewing all {@link Medication}s.
|
* The controller for viewing all {@link Medication}s.
|
||||||
|
@ -50,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"));
|
||||||
|
@ -76,4 +89,36 @@ public class AllMedicationController {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createMedication(Medication medication) {
|
||||||
|
dao = DaoFactory.getInstance().createMedicationDAO();
|
||||||
|
try {
|
||||||
|
dao.create(medication);
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void handleNewMedication() {
|
||||||
|
try {
|
||||||
|
FXMLLoader loader = new FXMLLoader(
|
||||||
|
Main.class.getResource("/de/hitec/nhplus/medication/MedicationModal.fxml")
|
||||||
|
);
|
||||||
|
BorderPane pane = loader.load();
|
||||||
|
Scene scene = new Scene(pane);
|
||||||
|
Stage stage = new Stage();
|
||||||
|
|
||||||
|
MedicationModalController controller = loader.getController();
|
||||||
|
controller.initialize(stage, this, null);
|
||||||
|
|
||||||
|
stage.setScene(scene);
|
||||||
|
stage.setTitle("NHPlus - Neues Medikament");
|
||||||
|
stage.setResizable(true);
|
||||||
|
stage.setAlwaysOnTop(true);
|
||||||
|
stage.showAndWait();
|
||||||
|
} catch (IOException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,118 @@
|
||||||
|
package de.hitec.nhplus.medication;
|
||||||
|
|
||||||
|
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
|
||||||
|
public TextField textFieldName;
|
||||||
|
@FXML
|
||||||
|
public TextField textFieldManufacturer;
|
||||||
|
@FXML
|
||||||
|
public TextField textFieldAdministrationMethod;
|
||||||
|
@FXML
|
||||||
|
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;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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(){
|
||||||
|
ingredients.setAll(medication.getIngredients());
|
||||||
|
textFieldName.setText(medication.getName());
|
||||||
|
textFieldManufacturer.setText(medication.getManufacturer());
|
||||||
|
textFieldAdministrationMethod.setText(medication.getAdministrationMethod());
|
||||||
|
textFieldCurrentStock.setText(String.valueOf(medication.getCurrentStock()));
|
||||||
|
textAreaPossibleSideEffects.setText(medication.getPossibleSideEffects());
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void handleSave() {
|
||||||
|
this.medication.setName(textFieldName.getText());
|
||||||
|
this.medication.setManufacturer(textFieldManufacturer.getText());
|
||||||
|
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()
|
||||||
|
);
|
||||||
|
|
||||||
|
controller.createMedication(medication);
|
||||||
|
controller.readAllAndShowInTableView();
|
||||||
|
stage.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void handleCancel() {
|
||||||
|
stage.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void handleAddIngredient() {
|
||||||
|
ingredients.add(new Ingredient(""));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,7 @@
|
||||||
fx:id="buttonAdd"
|
fx:id="buttonAdd"
|
||||||
mnemonicParsing="false"
|
mnemonicParsing="false"
|
||||||
prefWidth="90.0"
|
prefWidth="90.0"
|
||||||
|
onAction="#handleNewMedication"
|
||||||
text="Hinzufügen"
|
text="Hinzufügen"
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
|
|
Loading…
Reference in a new issue