diff --git a/db/nursingHome.db b/db/nursingHome.db index df596e0..5772512 100644 Binary files a/db/nursingHome.db and b/db/nursingHome.db differ 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