#22: Add Medication View and Update DAO
All checks were successful
Quality Check / Qualty Check (push) Successful in 8s
Quality Check / Qualty Check (pull_request) Successful in 9s

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-07 13:05:14 +02:00
parent bbbb569b55
commit 7f13ad3980
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
7 changed files with 213 additions and 43 deletions

View file

@ -26,6 +26,10 @@ public class MainWindowController {
private AnchorPane nursePage;
@FXML
private Tab nurseTab;
@FXML
private AnchorPane medicationPage;
@FXML
private Tab medicationTab;
@FXML
public void initialize() {
@ -35,7 +39,7 @@ public class MainWindowController {
patientTab.setOnSelectionChanged(event -> loadPatientPage());
treatmentTab.setOnSelectionChanged(event -> loadTreatmentsPage());
nurseTab.setOnSelectionChanged(event -> loadNursePage());
medicationTab.setOnSelectionChanged(event -> loadMedicationPage());
}
private void loadPatientPage() {
@ -82,4 +86,19 @@ public class MainWindowController {
exception.printStackTrace();
}
}
private void loadMedicationPage() {
try {
BorderPane medicationPane = FXMLLoader.load(
Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/medication/AllMedicationView.fxml"))
);
medicationPage.getChildren().setAll(medicationPane);
AnchorPane.setTopAnchor(medicationPane, 0d);
AnchorPane.setBottomAnchor(medicationPane, 0d);
AnchorPane.setLeftAnchor(medicationPane, 0d);
AnchorPane.setRightAnchor(medicationPane, 0d);
} catch (IOException exception) {
exception.printStackTrace();
}
}
}

View file

@ -0,0 +1,69 @@
package de.hitec.nhplus.medication;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.medication.database.MedicationDao;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.sql.SQLException;
import java.util.stream.Collectors;
public class AllMedicationController {
@FXML
private TableView<Medication> tableView;
@FXML
private TableColumn<Medication, Integer> columnId;
@FXML
private TableColumn<Medication, String> columnName;
@FXML
private TableColumn<Medication, String> columnManufacturer;
@FXML
private TableColumn<Medication, String> columnIngredient;
@FXML
private TableColumn<Medication, String> columnPossibleSideEffects;
@FXML
private TableColumn<Medication, String> columnAdministrationMethod;
@FXML
private TableColumn<Medication, Integer> columnCurrentStock;
private final ObservableList<Medication> medications = FXCollections.observableArrayList();
private MedicationDao dao;
public void initialize() {
readAllAndShowInTableView();
this.columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
this.columnName.setCellValueFactory(new PropertyValueFactory<>("name"));
this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer"));
this.columnIngredient.setCellValueFactory(
cellData -> {
return 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"));
this.tableView.setItems(this.medications);
}
public void readAllAndShowInTableView() {
this.dao = DaoFactory.getInstance().createMedicationDAO();
try {
this.medications.setAll(dao.readAll());
} catch (SQLException exception) {
exception.printStackTrace();
}
}
}

View file

@ -64,24 +64,42 @@ public class MedicationDao implements Dao<Medication> {
@Override
public List<Medication> readAll() throws SQLException {
final String SQL = """
SELECT medication.*, medication_ingredient.id
SELECT medication.*, medication_ingredient.name
FROM medication LEFT JOIN
medication_ingredient ON medication.id = medication_ingredient.id
""";
ResultSet result = connection.prepareStatement(SQL).executeQuery();
Map<Integer, List<ResultSet>> resultSetMap = new HashMap<>();
while (result.next()) {
List<ResultSet> resultSetList = resultSetMap.computeIfAbsent(result.getInt(0), k -> new ArrayList<>());
resultSetList.add(result);
}
List<Medication> medications = new ArrayList<>();
for (Map.Entry<Integer, List<ResultSet>> entry : resultSetMap.entrySet()) {
medications.add(getInstanceFromResultSet(this.mergeResultSets(entry.getValue())));
}
return medications;
Map<Integer, List<Ingredient>> ingredientMap = new HashMap<>();
int currentMedicationId;
int lastMedicationId = -1;
while (result.next()) {
currentMedicationId = result.getInt(1);
if(currentMedicationId != lastMedicationId) {
Medication medication = new Medication(
result.getInt(1),
result.getString(2),
result.getString(3),
List.of(),
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)));
lastMedicationId = currentMedicationId;
}
for (Medication medication : medications) {
medication.setIngredients(ingredientMap.get(medication.getId()));
}
return medications;
}
@Override
@ -120,7 +138,7 @@ public class MedicationDao implements Dao<Medication> {
result.getInt(1),
result.getString(2),
result.getString(3),
null,
List.of(),
result.getString(4),
result.getString(5),
result.getInt(6)
@ -134,28 +152,4 @@ public class MedicationDao implements Dao<Medication> {
medication.setIngredients(ingredients);
return medication;
}
/**
* Merges a List of ResultSets to one for further use.
*
* @param resultSetList the {@link List} of {@link ResultSet} to merge
* @return merged {@link ResultSet}
*/
private ResultSet mergeResultSets(List<ResultSet> resultSetList) throws SQLException {
ResultSetMetaData metaData = resultSetList.get(0).getMetaData();
CachedRowSet cachedRowSet = RowSetProvider.newFactory().createCachedRowSet();
cachedRowSet.populate(resultSetList.get(0));
for (int i = 1; i < resultSetList.size(); i++) {
ResultSet resultSet = resultSetList.get(i);
while (resultSet.next()) {
cachedRowSet.moveToInsertRow();
for (int j = 1; j <= metaData.getColumnCount(); j++) {
cachedRowSet.updateObject(j, resultSet.getObject(j));
}
cachedRowSet.insertRow();
}
}
cachedRowSet.moveToCurrentRow();
return cachedRowSet;
}
}

View file

@ -24,4 +24,9 @@ module de.hitec.nhplus {
exports de.hitec.nhplus.nurse.database;
opens de.hitec.nhplus.nurse to javafx.base, javafx.fxml;
opens de.hitec.nhplus.nurse.database to javafx.base, javafx.fxml;
exports de.hitec.nhplus.medication;
exports de.hitec.nhplus.medication.database;
opens de.hitec.nhplus.medication to javafx.base, javafx.fxml;
opens de.hitec.nhplus.medication.database to javafx.base, javafx.fxml;
}

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<TabPane
fx:id="mainTabPane"
@ -16,7 +15,10 @@
<Tab fx:id="treatmentTab" text="Behandlungen">
<AnchorPane fx:id="treatmentPage"/>
</Tab>
<Tab fx:id="nurseTab" text="Pfleger">
<AnchorPane fx:id="nursePage"/>
</Tab>
<Tab fx:id="nurseTab" text="Pfleger">
<AnchorPane fx:id="nursePage"/>
</Tab>
<Tab fx:id="medicationTab" text="Medikamente">
<AnchorPane fx:id="medicationPage"/>
</Tab>
</TabPane>

View file

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="de.hitec.nhplus.medication.AllMedicationController"
>
<padding>
<Insets top="8" left="8" right="8" bottom="8"/>
</padding>
<center>
<TableView fx:id="tableView">
<columns>
<TableColumn
fx:id="columnId"
minWidth="40.0"
text="ID"
/>
<TableColumn
fx:id="columnName"
minWidth="140.0"
text="Name"
/>
<TableColumn
fx:id="columnManufacturer"
minWidth="140.0"
text="Hersteller"
/>
<TableColumn
fx:id="columnIngredient"
minWidth="140.0"
text="Inhaltsstoffe"
/>
<TableColumn
fx:id="columnPossibleSideEffects"
minWidth="200.0"
text="Mögliche Nebenwirkungen"
/>
<TableColumn
fx:id="columnAdministrationMethod"
minWidth="180.0"
text="Verabreichungsmethode"
/>
<TableColumn
fx:id="columnCurrentStock"
minWidth="100.0"
text="Lagerbestand"
/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
</center>
<bottom>
<BorderPane>
<BorderPane.margin>
<Insets top="8.0"/>
</BorderPane.margin>
<right>
<HBox spacing="8.0">
<Button
fx:id="buttonAdd"
mnemonicParsing="false"
prefWidth="90.0"
text="Hinzufügen"
/>
<Button
fx:id="buttonDelete"
mnemonicParsing="false"
prefWidth="90.0"
text="Löschen"
/>
</HBox>
</right>
</BorderPane>
</bottom>
</BorderPane>

View file

@ -12,7 +12,7 @@
<Insets top="8" left="8" right="8" bottom="8"/>
</padding>
<center>
<TableView fx:id="tableView" editable="true" layoutX="31.0" layoutY="40.0">
<TableView fx:id="tableView" editable="true">
<columns>
<TableColumn
fx:id="columnId"