#23 Medikamente Anlegen #40

Merged
SZUT-Ole merged 5 commits from story/medikamente-modul-medikamente-anlegen into main 2024-05-17 12:34:28 +00:00
3 changed files with 22 additions and 12 deletions
Showing only changes of commit 2d64beaece - Show all commits

View file

@ -67,7 +67,6 @@ public class MedicationFixture implements Fixture<Medication> {
Ingredient warfarinnatrium = new Ingredient("Warfarinnatrium"); Ingredient warfarinnatrium = new Ingredient("Warfarinnatrium");
medications.add(new Medication( medications.add(new Medication(
1,
"Metformin", "Metformin",
"AstraZeneca", "AstraZeneca",
List.of( List.of(
@ -81,7 +80,6 @@ public class MedicationFixture implements Fixture<Medication> {
100 100
)); ));
medications.add(new Medication( medications.add(new Medication(
2,
"Lisinopril", "Lisinopril",
"Teva Pharmaceuticals", "Teva Pharmaceuticals",
List.of( List.of(
@ -95,7 +93,6 @@ public class MedicationFixture implements Fixture<Medication> {
150 150
)); ));
medications.add(new Medication( medications.add(new Medication(
3,
"Simvastatin", "Simvastatin",
"Mylan", "Mylan",
List.of( List.of(
@ -109,7 +106,6 @@ public class MedicationFixture implements Fixture<Medication> {
80 80
)); ));
medications.add(new Medication( medications.add(new Medication(
4,
"Enoxaparin", "Enoxaparin",
"Sanofi", "Sanofi",
List.of( List.of(
@ -122,7 +118,6 @@ public class MedicationFixture implements Fixture<Medication> {
120 120
)); ));
medications.add(new Medication( medications.add(new Medication(
5,
"Levothyroxin", "Levothyroxin",
"Sandoz", "Sandoz",
List.of( List.of(
@ -136,7 +131,6 @@ public class MedicationFixture implements Fixture<Medication> {
90 90
)); ));
medications.add(new Medication( medications.add(new Medication(
6,
"Warfarin", "Warfarin",
"Apotex Inc.", "Apotex Inc.",
List.of( List.of(

View file

@ -17,7 +17,7 @@ public class Ingredient {
} }
public String getName() { public String getName() {
return name.get(); return name.getValue();
} }
public SimpleStringProperty nameProperty() { public SimpleStringProperty nameProperty() {

View file

@ -29,6 +29,7 @@ public class MedicationDao implements Dao<Medication> {
@Override @Override
public void create(Medication medication) throws SQLException { public void create(Medication medication) throws SQLException {
connection.setAutoCommit(false); //Switch to Manual Commit, to do an SQL Transaction
final String medicationSQL = """ final String medicationSQL = """
INSERT INTO medication INSERT INTO medication
(name, manufacturer, possibleSideEffects, administrationMethod, currentStock) (name, manufacturer, possibleSideEffects, administrationMethod, currentStock)
@ -42,6 +43,15 @@ public class MedicationDao implements Dao<Medication> {
medicationStatement.setInt(5, medication.getCurrentStock()); medicationStatement.setInt(5, medication.getCurrentStock());
medicationStatement.execute(); medicationStatement.execute();
ResultSet generatedKeys = connection.createStatement().executeQuery("SELECT last_insert_rowid()");
connection.commit(); //Finish SQL Transaction
connection.setAutoCommit(true); //Switch back Mode
if (!generatedKeys.next()) {
return;
}
int newId = generatedKeys.getInt(1);
final String ingredientSQL = """ final String ingredientSQL = """
INSERT INTO medication_ingredient INSERT INTO medication_ingredient
(id, name) (id, name)
@ -49,7 +59,7 @@ public class MedicationDao implements Dao<Medication> {
"""; """;
for (Ingredient ingredient : medication.getIngredients()) { for (Ingredient ingredient : medication.getIngredients()) {
PreparedStatement ingredientStatement = this.connection.prepareStatement(ingredientSQL); PreparedStatement ingredientStatement = this.connection.prepareStatement(ingredientSQL);
ingredientStatement.setInt(1, medication.getId()); ingredientStatement.setInt(1, newId);
ingredientStatement.setString(2, ingredient.getName()); ingredientStatement.setString(2, ingredient.getName());
ingredientStatement.execute(); ingredientStatement.execute();
} }
@ -90,20 +100,26 @@ public class MedicationDao implements Dao<Medication> {
result.getInt(1), result.getInt(1),
result.getString(2), result.getString(2),
result.getString(3), result.getString(3),
List.of(), new ArrayList<>(),
result.getString(4), result.getString(4),
result.getString(5), result.getString(5),
result.getInt(6) result.getInt(6)
); );
medications.add(medication); medications.add(medication);
lastMedicationId = currentMedicationId;
continue;
} }
List<Ingredient> ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>()); 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; lastMedicationId = currentMedicationId;
} }
for (Medication medication : medications) { for (Medication medication : medications) {
List<Ingredient> ingredients = ingredientMap.get(medication.getId());
if(ingredients.isEmpty()){
continue;
}
medication.setIngredients(ingredientMap.get(medication.getId())); medication.setIngredients(ingredientMap.get(medication.getId()));
} }