#24 story/medikamente-als-veraltet-markieren #46
3 changed files with 59 additions and 6 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -32,8 +32,8 @@ public class MedicationDao implements Dao<Medication> {
|
|||
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<Medication> {
|
|||
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<Medication> {
|
|||
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<Medication> {
|
|||
medications.add(medication);
|
||||
}
|
||||
List<Ingredient> 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<Ingredient> ingredients = ingredientMap.get(medication.getId());
|
||||
if(ingredients.isEmpty()){
|
||||
continue;
|
||||
}
|
||||
medication.setIngredients(ingredientMap.get(medication.getId()));
|
||||
}
|
||||
|
||||
return medications;
|
||||
}
|
||||
|
||||
public List<Medication> 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<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;
|
||||
}
|
||||
|
@ -135,7 +185,8 @@ public class MedicationDao implements Dao<Medication> {
|
|||
manufacturer = ?,
|
||||
possibleSideEffects = ?,
|
||||
administrationMethod = ?,
|
||||
currentStock = ?
|
||||
currentStock = ?,
|
||||
isDeprecated = ?
|
||||
WHERE id = ?
|
||||
""";
|
||||
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL);
|
||||
|
@ -145,6 +196,7 @@ public class MedicationDao implements Dao<Medication> {
|
|||
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 = """
|
||||
|
|
|
@ -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
|
||||
)
|
Loading…
Reference in a new issue