From 09637e602864ed53064b348d4e3c1190e2372860 Mon Sep 17 00:00:00 2001 From: Dorian Nemec Date: Mon, 20 May 2024 09:51:41 +0200 Subject: [PATCH] #24: Implemented isDeprecated in Dao --- db/nursingHome.db | Bin 28672 -> 28672 bytes .../DeprecatedMedicationController.java | 2 +- .../medication/database/MedicationDao.java | 60 ++++++++++++++++-- .../nhplus/medication/database/Medication.sql | 3 +- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/db/nursingHome.db b/db/nursingHome.db index df596e05e2ebebaf5fb16a358f7204e2cf400f90..577251236000a6007c68f368cf242e40da163013 100644 GIT binary patch delta 441 zcmZp8z}WDBae|Z(vo-?*13M7Iz~YHIMp8ggy`qJ@e193(I1?E7R`agl7vw&`WyYDX zvGD~5b7QdgO+QuA@$s+t8n7OzfZe|zw&Chq1frWpS z0RJog`~183S8Wzln9QHTz^lhB&sf4|&aB9oo0^gdv>`J;kB!$1BwCc3SdyEXSHhJC z6a<>d&xxc&fRoddS)4Jiw5T|hncIX}ny~<=7pR(zjf;&5D9FXe3)I8LueO=p;1@rL z&CI`wf&Ur*NucF?4s-ZfWSQ9*Hm~zv&c|cU#=#(~Y%R*j5uUg?F>Wa%Unm3rM*a+b zWxkVqeSD!n$2#(HAyiFH$)bDEn delta 402 zcmX|*O(;ZB6vyv0EcibtD6Mmk=3q8v0rks{-WdhCOn}k zyy)bMxvbASbo;L^dM;sYdKkMarDZ3e4siCr#WgdoH{hkLUsN8H7(`PdgIG#NAs}hz zJ6LdvbPx!h;SPl4$$j{CiORFdW4@Cnq=!({7?auABg``h&S4%}_&paqqdpzu?3ee| zVKL`+u-+g=&GjkO?Yapz9iYgrFC?&dbkl4|rzjSlQLN4DW22g4-NSX7D0zcy98(mH JIf`9+BVP-$Vn+Y~ diff --git a/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java b/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java index 6938407..fe38fe7 100644 --- a/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java @@ -70,7 +70,7 @@ public class DeprecatedMedicationController { public void readAllAndShowInTableView() { this.dao = DaoFactory.getInstance().createMedicationDAO(); try { - this.medications.setAll(dao.readAll()); + this.medications.setAll(dao.readAllDeprecated()); } 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 36a0e5e..41f5f7d 100644 --- a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java +++ b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java @@ -32,8 +32,8 @@ public class MedicationDao implements Dao { connection.setAutoCommit(false); //Switch to Manual Commit, to do an SQL Transaction final String medicationSQL = """ INSERT INTO medication - (name, manufacturer, possibleSideEffects, administrationMethod, currentStock) - VALUES (?, ?, ?, ?, ?); + (name, manufacturer, possibleSideEffects, administrationMethod, currentStock, isDeprecated) + VALUES (?, ?, ?, ?, ?, ?); """; PreparedStatement medicationStatement = this.connection.prepareStatement(medicationSQL); medicationStatement.setString(1, medication.getName()); @@ -41,6 +41,7 @@ public class MedicationDao implements Dao { medicationStatement.setString(3, medication.getPossibleSideEffects()); medicationStatement.setString(4, medication.getAdministrationMethod()); medicationStatement.setInt(5, medication.getCurrentStock()); + medicationStatement.setBoolean(6, medication.isDeprecated()); medicationStatement.execute(); ResultSet generatedKeys = connection.createStatement().executeQuery("SELECT last_insert_rowid()"); @@ -85,6 +86,7 @@ public class MedicationDao implements Dao { SELECT medication.*, medication_ingredient.name FROM medication LEFT JOIN medication_ingredient ON medication.id = medication_ingredient.id + WHERE isDeprecated = false """; ResultSet result = connection.prepareStatement(SQL).executeQuery(); @@ -109,7 +111,55 @@ public class MedicationDao implements Dao { medications.add(medication); } List ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>()); - String ingredientName = result.getString(7); + 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 readAllDeprecated() throws SQLException { + final String SQL = """ + SELECT medication.*, medication_ingredient.name + FROM medication LEFT JOIN + medication_ingredient ON medication.id = medication_ingredient.id + WHERE 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; } @@ -135,7 +185,8 @@ public class MedicationDao implements Dao { manufacturer = ?, possibleSideEffects = ?, administrationMethod = ?, - currentStock = ? + currentStock = ?, + isDeprecated = ? WHERE id = ? """; PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); @@ -145,6 +196,7 @@ public class MedicationDao implements Dao { preparedStatement.setString(4, medication.getAdministrationMethod()); preparedStatement.setInt(5, medication.getCurrentStock()); preparedStatement.setInt(6, medication.getId()); + preparedStatement.setBoolean(6, medication.isDeprecated()); preparedStatement.executeUpdate(); final String ingredientDeleteSQL = """ diff --git a/src/main/resources/de/hitec/nhplus/medication/database/Medication.sql b/src/main/resources/de/hitec/nhplus/medication/database/Medication.sql index fc7c049..6d68c0e 100644 --- a/src/main/resources/de/hitec/nhplus/medication/database/Medication.sql +++ b/src/main/resources/de/hitec/nhplus/medication/database/Medication.sql @@ -5,5 +5,6 @@ CREATE TABLE medication manufacturer TEXT NOT NULL, possibleSideEffects TEXT NOT NULL, administrationMethod TEXT NOT NULL, - currentStock INTEGER NOT NULL + currentStock INTEGER NOT NULL, + isDeprecated BOOLEAN NOT NULL DEFAULT FALSE ) \ No newline at end of file