diff --git a/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java b/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java index fed7e20..8dc9f96 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java @@ -67,7 +67,6 @@ public class MedicationFixture implements Fixture { Ingredient warfarinnatrium = new Ingredient("Warfarinnatrium"); medications.add(new Medication( - 1, "Metformin", "AstraZeneca", List.of( @@ -81,7 +80,6 @@ public class MedicationFixture implements Fixture { 100 )); medications.add(new Medication( - 2, "Lisinopril", "Teva Pharmaceuticals", List.of( @@ -95,7 +93,6 @@ public class MedicationFixture implements Fixture { 150 )); medications.add(new Medication( - 3, "Simvastatin", "Mylan", List.of( @@ -109,7 +106,6 @@ public class MedicationFixture implements Fixture { 80 )); medications.add(new Medication( - 4, "Enoxaparin", "Sanofi", List.of( @@ -122,7 +118,6 @@ public class MedicationFixture implements Fixture { 120 )); medications.add(new Medication( - 5, "Levothyroxin", "Sandoz", List.of( @@ -136,7 +131,6 @@ public class MedicationFixture implements Fixture { 90 )); medications.add(new Medication( - 6, "Warfarin", "Apotex Inc.", List.of( diff --git a/src/main/java/de/hitec/nhplus/medication/Ingredient.java b/src/main/java/de/hitec/nhplus/medication/Ingredient.java index bbf2f36..49f93f6 100644 --- a/src/main/java/de/hitec/nhplus/medication/Ingredient.java +++ b/src/main/java/de/hitec/nhplus/medication/Ingredient.java @@ -17,7 +17,7 @@ public class Ingredient { } public String getName() { - return name.get(); + return name.getValue(); } public SimpleStringProperty nameProperty() { diff --git a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java index 556a623..e226770 100644 --- a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java +++ b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java @@ -29,6 +29,7 @@ public class MedicationDao implements Dao { @Override public void create(Medication medication) throws SQLException { + connection.setAutoCommit(false); //Switch to Manual Commit, to do an SQL Transaction final String medicationSQL = """ INSERT INTO medication (name, manufacturer, possibleSideEffects, administrationMethod, currentStock) @@ -42,6 +43,15 @@ public class MedicationDao implements Dao { medicationStatement.setInt(5, medication.getCurrentStock()); 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 = """ INSERT INTO medication_ingredient (id, name) @@ -49,7 +59,7 @@ public class MedicationDao implements Dao { """; for (Ingredient ingredient : medication.getIngredients()) { PreparedStatement ingredientStatement = this.connection.prepareStatement(ingredientSQL); - ingredientStatement.setInt(1, medication.getId()); + ingredientStatement.setInt(1, newId); ingredientStatement.setString(2, ingredient.getName()); ingredientStatement.execute(); } @@ -90,20 +100,26 @@ public class MedicationDao implements Dao { result.getInt(1), result.getString(2), result.getString(3), - List.of(), + new ArrayList<>(), result.getString(4), result.getString(5), result.getInt(6) ); medications.add(medication); - lastMedicationId = currentMedicationId; - continue; } List 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; } for (Medication medication : medications) { + List ingredients = ingredientMap.get(medication.getId()); + if(ingredients.isEmpty()){ + continue; + } medication.setIngredients(ingredientMap.get(medication.getId())); }