#24 Deprecated and available medication properly implemented
Some checks failed
Quality Check / Linting Check (push) Failing after 12s
Quality Check / Javadoc Check (push) Successful in 21s

This commit is contained in:
Dorian Nemec 2024-05-20 16:49:57 +02:00
parent 67b1093661
commit 1a542a0d71
3 changed files with 51 additions and 2 deletions

Binary file not shown.

View file

@ -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();
}

View file

@ -134,7 +134,56 @@ public class MedicationDao implements Dao<Medication> {
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<Medication> medications = new ArrayList<>();
Map<Integer, List<Ingredient>> 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<Ingredient> 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<Ingredient> ingredients = ingredientMap.get(medication.getId());
if(ingredients.isEmpty()){
continue;
}
medication.setIngredients(ingredientMap.get(medication.getId()));
}
return medications;
}
public List<Medication> 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();