#24 Deprecated and available medication properly implemented
This commit is contained in:
parent
67b1093661
commit
1a542a0d71
3 changed files with 51 additions and 2 deletions
Binary file not shown.
|
@ -93,7 +93,7 @@ public class AllMedicationController {
|
||||||
public void readAllAndShowInTableView() {
|
public void readAllAndShowInTableView() {
|
||||||
this.dao = DaoFactory.getInstance().createMedicationDAO();
|
this.dao = DaoFactory.getInstance().createMedicationDAO();
|
||||||
try {
|
try {
|
||||||
this.medications.setAll(dao.readAll());
|
this.medications.setAll(dao.readAllAvailable());
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,7 +134,56 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
FROM medication LEFT JOIN
|
FROM medication LEFT JOIN
|
||||||
medication_ingredient
|
medication_ingredient
|
||||||
ON medication.id = medication_ingredient.id
|
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();
|
ResultSet result = connection.prepareStatement(SQL).executeQuery();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue