#27: WIP
All checks were successful
Quality Check / Linting Check (push) Successful in 14s
Quality Check / Javadoc Check (push) Successful in 25s

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-22 11:56:53 +02:00 committed by Ole Kück
parent 275aa3252a
commit ef8d7397d0
8 changed files with 245 additions and 106 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="nursingHome.db" uuid="5a5b8be1-080b-4129-b89d-42f1ea832b90">
<data-source source="LOCAL" name="Database" uuid="5a5b8be1-080b-4129-b89d-42f1ea832b90">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>

Binary file not shown.

View file

@ -1,17 +1,17 @@
package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.Main;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.medication.Ingredient;
import de.hitec.nhplus.medication.Medication;
import de.hitec.nhplus.medication.database.MedicationDao;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import de.hitec.nhplus.Main;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.medication.Ingredient;
import de.hitec.nhplus.medication.Medication;
import de.hitec.nhplus.medication.database.MedicationDao;
/**
* {@link Fixture} for {@link Medication}.
*
@ -20,11 +20,13 @@ import java.util.*;
public class MedicationFixture implements Fixture<Medication> {
private static final String SCHEMA = "/de/hitec/nhplus/medication/database/Medication.sql";
private static final String INGREDIENT_SCHEMA = "/de/hitec/nhplus/medication/database/Medication_Ingredient.sql";
private static final String ALTERNATIVE_SCHEMA = "/de/hitec/nhplus/medication/database/Medication_Alternative.sql";
@Override
public void dropTable(Connection connection) throws SQLException {
connection.createStatement().execute("DROP TABLE IF EXISTS medication");
connection.createStatement().execute("DROP TABLE IF EXISTS medication_ingredient");
connection.createStatement().execute("DROP TABLE IF EXISTS medication_alternative");
}
@Override
@ -32,16 +34,25 @@ public class MedicationFixture implements Fixture<Medication> {
final InputStream schema = Main.class.getResourceAsStream(SCHEMA);
final InputStream ingredientSchema = Main.class.getResourceAsStream(INGREDIENT_SCHEMA);
final InputStream alternativeSchema = Main.class.getResourceAsStream(ALTERNATIVE_SCHEMA);
assert schema != null;
assert ingredientSchema != null;
assert alternativeSchema != null;
String SQL = new Scanner(schema, StandardCharsets.UTF_8)
.useDelimiter("\\A")
.next();
String ingredientSQL = ";" + new Scanner(ingredientSchema, StandardCharsets.UTF_8)
.useDelimiter("\\A")
.next();
String alternativeSQL = ";" + new Scanner(alternativeSchema, StandardCharsets.UTF_8)
.useDelimiter("\\A")
.next();
connection.createStatement().execute(SQL);
connection.createStatement().execute(ingredientSQL);
connection.createStatement().execute(alternativeSQL);
}
@ -77,7 +88,8 @@ public class MedicationFixture implements Fixture<Medication> {
),
"Übelkeit, Durchfall, Laktatazidose (selten)",
"Oral",
100
100,
new ArrayList<>()
));
medications.add(new Medication(
"Lisinopril",
@ -90,7 +102,8 @@ public class MedicationFixture implements Fixture<Medication> {
),
"Schwindel, trockener Husten",
"Oral",
150
150,
new ArrayList<>()
));
medications.add(new Medication(
"Simvastatin",
@ -103,7 +116,8 @@ public class MedicationFixture implements Fixture<Medication> {
),
"Muskelschmerzen, Leberprobleme(selten)",
"Oral",
80
80,
new ArrayList<>()
));
medications.add(new Medication(
"Enoxaparin",
@ -115,7 +129,8 @@ public class MedicationFixture implements Fixture<Medication> {
),
"Blutungen, Reaktionen an der Injektionsstelle",
"Unterhautinjektion",
120
120,
new ArrayList<>()
));
medications.add(new Medication(
"Levothyroxin",
@ -128,7 +143,8 @@ public class MedicationFixture implements Fixture<Medication> {
),
"Herzrasen, Gewichtsverlust",
"Oral",
90
90,
new ArrayList<>()
));
medications.add(new Medication(
"Warfarin",
@ -141,7 +157,8 @@ public class MedicationFixture implements Fixture<Medication> {
),
"Blutungen, Blutergüsse",
"Oral",
110
110,
new ArrayList<>()
));
MedicationDao dao = DaoFactory.getInstance().createMedicationDAO();
Map<String, Medication> medicationsByName = new HashMap<>();

View file

@ -23,6 +23,7 @@ public class Medication {
private final SimpleStringProperty possibleSideEffects;
private final SimpleStringProperty administrationMethod;
private final SimpleIntegerProperty currentStock;
private final SimpleListProperty<Medication> alternativeMedication;
/**
* This constructor allows instantiating a {@link Medication} object,
@ -37,7 +38,8 @@ public class Medication {
List<Ingredient> ingredients,
String possibleSideEffects,
String administrationMethod,
int currentStock
int currentStock,
List<Medication> alternativeMedication
) {
this.name = new SimpleStringProperty(name);
this.manufacturer = new SimpleStringProperty(manufacturer);
@ -45,6 +47,7 @@ public class Medication {
this.possibleSideEffects = new SimpleStringProperty(possibleSideEffects);
this.administrationMethod = new SimpleStringProperty(administrationMethod);
this.currentStock = new SimpleIntegerProperty(currentStock);
this.alternativeMedication = new SimpleListProperty<>(FXCollections.observableArrayList(alternativeMedication));
}
/**
@ -57,7 +60,8 @@ public class Medication {
List<Ingredient> ingredients,
String possibleSideEffects,
String administrationMethod,
int currentStock
int currentStock,
List<Medication> alternativeMedication
) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
@ -66,6 +70,7 @@ public class Medication {
this.possibleSideEffects = new SimpleStringProperty(possibleSideEffects);
this.administrationMethod = new SimpleStringProperty(administrationMethod);
this.currentStock = new SimpleIntegerProperty(currentStock);
this.alternativeMedication = new SimpleListProperty<>(FXCollections.observableArrayList(alternativeMedication));
}
public int getId() {
@ -124,6 +129,18 @@ public class Medication {
this.possibleSideEffects.set(possibleSideEffects);
}
public ObservableList<Medication> getAlternativeMedication() {
return alternativeMedication.get();
}
public SimpleListProperty<Medication> alternativeMedicationProperty() {
return alternativeMedication;
}
public void setAlternativeMedication(List<Medication> alternativeMedication) {
this.alternativeMedication.set(FXCollections.observableArrayList(alternativeMedication));
}
public String getAdministrationMethod() {
return administrationMethod.get();
}
@ -162,6 +179,7 @@ public class Medication {
.add("Possible Side Effects: " + this.getPossibleSideEffects())
.add("Administration Method: " + this.getAdministrationMethod())
.add("Current Stock: " + this.getCurrentStock())
.add("Alternative Medication" + this.getAlternativeMedication())
.toString();
}

View file

@ -68,7 +68,13 @@ public class MedicationListCell extends ListCell<Medication> {
Medication oldValue,
Medication newValue
) {
Medication selectedMedication = comboBox.getValue();
ListView<Medication> listView = getListView();
listView.getItems().remove(getItem());
listView.getItems().add(selectedMedication);
setItem(selectedMedication);
double max = listView.lookupAll("*")
.stream()
.filter(node -> node instanceof MedicationListCell)

View file

@ -70,7 +70,8 @@ public class MedicationModalController {
new ArrayList<>(),
"",
"",
0
0,
new ArrayList<>()
);
this.buttonSave.setDisable(true);
}
@ -103,6 +104,7 @@ public class MedicationModalController {
*/
private void showData() {
ingredients.setAll(medication.getIngredients());
alternativeMediaction.setAll(medication.getAlternativeMedication());
try {
allOtherMedications = controller.getDao().readAll();
if (!isNewMedication) {
@ -137,6 +139,14 @@ public class MedicationModalController {
.toList()
);
List<Medication> meds = alternativeMediaction
.stream()
.distinct()
.toList();
this.medication.setAlternativeMedication(
meds
);
if (isNewMedication) {
controller.createMedication(medication);
} else {
@ -161,10 +171,11 @@ public class MedicationModalController {
alternativeMediaction.add(new Medication(
null,
null,
List.of(),
new ArrayList<>(),
null,
null,
-1
-1,
new ArrayList<>()
));
}
}

View file

@ -63,33 +63,41 @@ public class MedicationDao implements Dao<Medication> {
ingredientStatement.setString(2, ingredient.getName());
ingredientStatement.execute();
}
final String alternativeMedicationSQL = """
INSERT INTO medication_alternative
(id, alternativeId)
VALUES (?, ?);
""";
for (Medication alternative : medication.getAlternativeMedication()) {
PreparedStatement alternativeStatement = this.connection.prepareStatement(alternativeMedicationSQL);
alternativeStatement.setInt(1, newId);
alternativeStatement.setInt(2, alternative.getId());
alternativeStatement.execute();
}
}
@Override
public Medication read(int id) throws SQLException {
final String SQL = """
SELECT medication.*, medication_ingredient.id
FROM medication
LEFT JOIN medication_ingredient ON medication.id = medication_ingredient.id
WHERE medication.id = ?
""";
PreparedStatement statement = this.connection.prepareStatement(SQL);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();
ResultSet result = getReadStatement(id).executeQuery();
return getInstanceFromResultSet(result);
}
@Override
public List<Medication> readAll() throws SQLException {
final String SQL = """
SELECT medication.*, medication_ingredient.name
FROM medication LEFT JOIN
SELECT medication.*, medication_ingredient.name, medication_alternative.alternativeId
FROM medication
LEFT JOIN
medication_ingredient ON medication.id = medication_ingredient.id
LEFT JOIN
medication_alternative ON medication.id = medication_alternative.id
""";
ResultSet result = connection.prepareStatement(SQL).executeQuery();
List<Medication> medications = new ArrayList<>();
Map<Integer, Medication> medications = new HashMap<>();
Map<Integer, List<Ingredient>> ingredientMap = new HashMap<>();
Map<Integer, List<Integer>> alternativesMap = new HashMap<>();
int currentMedicationId;
int lastMedicationId = -1;
@ -103,27 +111,41 @@ public class MedicationDao implements Dao<Medication> {
new ArrayList<>(),
result.getString(4),
result.getString(5),
result.getInt(6)
result.getInt(6),
new ArrayList<>()
);
medications.add(medication);
medications.put(currentMedicationId, medication);
}
List<Ingredient> ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>());
String ingredientName = result.getString(7);
if(ingredientName == null){
continue;
}
if (ingredientName != null) {
ingredients.add(new Ingredient(ingredientName));
}
List<Integer> alternatives = alternativesMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>());
int alternativeId = result.getInt(8);
if (alternativeId != 0) {
alternatives.add(alternativeId);
}
lastMedicationId = currentMedicationId;
}
for (Medication medication : medications) {
for (Medication medication : medications.values()) {
List<Ingredient> ingredients = ingredientMap.get(medication.getId());
if (ingredients.isEmpty()) {
continue;
}
medication.setIngredients(ingredientMap.get(medication.getId()));
List<Integer> alternativeIds = alternativesMap.get(medication.getId());
List<Medication> alternativs = new ArrayList<>();
for (Integer alternativeId : alternativeIds) {
alternativs.add(medications.get(alternativeId));
}
medication.setAlternativeMedication(alternativs);
}
return medications;
return medications.values().stream().toList();
}
@Override
@ -164,6 +186,25 @@ public class MedicationDao implements Dao<Medication> {
statement.setString(2, ingredient.getName());
statement.execute();
}
final String alternativeDeleteSQL = """
DELETE FROM medication_alternative WHERE id = ?
""";
PreparedStatement alternativeStatement = this.connection.prepareStatement(alternativeDeleteSQL);
alternativeStatement.setInt(1, medication.getId());
alternativeStatement.executeUpdate();
final String alternativeCreateSQL = """
INSERT INTO medication_alternative
(id, alternativeId)
VALUES (?, ?);
""";
for (Medication alternative : medication.getAlternativeMedication()) {
PreparedStatement statement = this.connection.prepareStatement(alternativeCreateSQL);
statement.setInt(1, medication.getId());
statement.setInt(2, alternative.getId());
statement.execute();
}
}
@Override
@ -176,6 +217,23 @@ public class MedicationDao implements Dao<Medication> {
preparedStatement.executeUpdate();
}
/**
* @param id The ID of the database entry to read.
* @return A {@link PreparedStatement} to read a specific entry by its ID.
*/
public PreparedStatement getReadStatement(int id) throws SQLException {
final String SQL = """
SELECT medication.*, medication_ingredient.name, medication_alternative.alternativeId
FROM medication
LEFT JOIN medication_ingredient ON medication.id = medication_ingredient.id
LEFT JOIN medication_alternative ON medication.id = medication_alternative.id
WHERE medication.id = ?
""";
PreparedStatement statement = this.connection.prepareStatement(SQL);
statement.setInt(1, id);
return statement;
}
/**
* Constructs a {@link Medication} object from the {@link ResultSet} obtained after executing a database query.
* This method is used internally to map the {@link ResultSet} data to a {@link Medication} object.
@ -188,18 +246,40 @@ 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)
result.getInt(6),
new ArrayList<>()
);
List<Ingredient> ingredients = new ArrayList<>();
List<Medication> alternatives = new ArrayList<>();
while (result.next()) {
ingredients.add(new Ingredient(result.getString(2)));
String ingredientName = result.getString(7);
if (ingredientName != null) {
ingredients.add(new Ingredient(ingredientName));
}
int alternativeId = result.getInt(8);
if (alternativeId != 0) {
ResultSet alternativeResult = getReadStatement(alternativeId).executeQuery();
Medication alternativeMedication = new Medication(
alternativeResult.getInt(1),
alternativeResult.getString(2),
alternativeResult.getString(3),
new ArrayList<>(),
alternativeResult.getString(4),
alternativeResult.getString(5),
alternativeResult.getInt(6),
new ArrayList<>()
);
alternatives.add(alternativeMedication);
}
}
medication.setIngredients(ingredients);
medication.setAlternativeMedication(alternatives);
return medication;
}
}

View file

@ -0,0 +1,7 @@
CREATE TABLE medication_alternative
(
id INTEGER NOT NULL ,
alternativeId INTEGER NOT NULL ,
FOREIGN KEY (id) REFERENCES medication (id) ON DELETE CASCADE,
FOREIGN KEY (alternativeId) REFERENCES medication (id) ON DELETE CASCADE
)