From 1a542a0d71c99230a0c8a4e18b19a53092653559 Mon Sep 17 00:00:00 2001 From: Dorian Nemec Date: Mon, 20 May 2024 16:49:57 +0200 Subject: [PATCH] #24 Deprecated and available medication properly implemented --- db/nursingHome.db | Bin 28672 -> 28672 bytes .../medication/AllMedicationController.java | 2 +- .../medication/database/MedicationDao.java | 51 +++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/db/nursingHome.db b/db/nursingHome.db index 1ca878e5873aad433a25f97bd19f8286eb3f5ad8..3bff57f726f8dd562b34d439ff1a3f3262111f5c 100644 GIT binary patch delta 287 zcmZp8z}WDBae_3X%tRSyMwyKXJN)JNF7X}Z+rhVtKaAg#-8SW=W(n#--o%qYzkTyl6xQFf|`0yCp7 zn{Q%zUTQH=uDB#Mu_&>GTOOpuCo#Juzc_XBvN*STIc7#lmhi;l;?yEBS!PB<7T3J| zio^ntvATW`a|LCX8P!>wQuC@Ra}sm1^E2{u_@$W{m6?MxbITHoOA<>m^F*Y8)-hvP zCkeEU8EPG`1T&*NvvX=rPH7I%b`cJs4yMi7@#*}GoReAN0=S%+IT)11BSnE+-Tcb{ DgaA}6 delta 287 zcmZp8z}WDBae_1>>qHr6M%Il9JN)JNL-^hKE%=rAx%j^Gz2>{bca(1j-!i_5d@X$C ze2JR{6^`&u{uP_^Ag#Un+5T;2T3 E0Ma&7UH||9 diff --git a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java index d016a86..f85fa77 100644 --- a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java @@ -93,7 +93,7 @@ public class AllMedicationController { public void readAllAndShowInTableView() { this.dao = DaoFactory.getInstance().createMedicationDAO(); try { - this.medications.setAll(dao.readAll()); + this.medications.setAll(dao.readAllAvailable()); } catch (SQLException exception) { exception.printStackTrace(); } 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 e96e55a..5e31e43 100644 --- a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java +++ b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java @@ -134,7 +134,56 @@ public class MedicationDao implements Dao { FROM medication LEFT JOIN medication_ingredient ON medication.id = medication_ingredient.id - AND medication.isDeprecated = true + WHERE medication.isDeprecated = true + """; + ResultSet result = connection.prepareStatement(SQL).executeQuery(); + + List medications = new ArrayList<>(); + Map> 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), + new ArrayList<>(), + result.getString(4), + result.getString(5), + result.getInt(6), + result.getBoolean(7) + ); + medications.add(medication); + } + List ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>()); + String ingredientName = result.getString(8); + 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())); + } + + return medications; + } + + public List readAllAvailable() throws SQLException { + final String SQL = """ + SELECT medication.*, medication_ingredient.name + FROM medication LEFT JOIN + medication_ingredient + ON medication.id = medication_ingredient.id + WHERE medication.isDeprecated = false """; ResultSet result = connection.prepareStatement(SQL).executeQuery();