#24: Implemented isDeprecated in Dao
This commit is contained in:
parent
6300509873
commit
9fac5b34a3
3 changed files with 59 additions and 6 deletions
|
@ -70,7 +70,7 @@ public class DeprecatedMedicationController {
|
||||||
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.readAllDeprecated());
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,8 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
connection.setAutoCommit(false); //Switch to Manual Commit, to do an SQL Transaction
|
connection.setAutoCommit(false); //Switch to Manual Commit, to do an SQL Transaction
|
||||||
final String medicationSQL = """
|
final String medicationSQL = """
|
||||||
INSERT INTO medication
|
INSERT INTO medication
|
||||||
(name, manufacturer, possibleSideEffects, administrationMethod, currentStock)
|
(name, manufacturer, possibleSideEffects, administrationMethod, currentStock, isDeprecated)
|
||||||
VALUES (?, ?, ?, ?, ?);
|
VALUES (?, ?, ?, ?, ?, ?);
|
||||||
""";
|
""";
|
||||||
PreparedStatement medicationStatement = this.connection.prepareStatement(medicationSQL);
|
PreparedStatement medicationStatement = this.connection.prepareStatement(medicationSQL);
|
||||||
medicationStatement.setString(1, medication.getName());
|
medicationStatement.setString(1, medication.getName());
|
||||||
|
@ -41,6 +41,7 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
medicationStatement.setString(3, medication.getPossibleSideEffects());
|
medicationStatement.setString(3, medication.getPossibleSideEffects());
|
||||||
medicationStatement.setString(4, medication.getAdministrationMethod());
|
medicationStatement.setString(4, medication.getAdministrationMethod());
|
||||||
medicationStatement.setInt(5, medication.getCurrentStock());
|
medicationStatement.setInt(5, medication.getCurrentStock());
|
||||||
|
medicationStatement.setBoolean(6, medication.isDeprecated());
|
||||||
medicationStatement.execute();
|
medicationStatement.execute();
|
||||||
|
|
||||||
ResultSet generatedKeys = connection.createStatement().executeQuery("SELECT last_insert_rowid()");
|
ResultSet generatedKeys = connection.createStatement().executeQuery("SELECT last_insert_rowid()");
|
||||||
|
@ -85,6 +86,7 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
SELECT medication.*, medication_ingredient.name
|
SELECT medication.*, medication_ingredient.name
|
||||||
FROM medication LEFT JOIN
|
FROM medication LEFT JOIN
|
||||||
medication_ingredient ON medication.id = medication_ingredient.id
|
medication_ingredient ON medication.id = medication_ingredient.id
|
||||||
|
WHERE isDeprecated = false
|
||||||
""";
|
""";
|
||||||
ResultSet result = connection.prepareStatement(SQL).executeQuery();
|
ResultSet result = connection.prepareStatement(SQL).executeQuery();
|
||||||
|
|
||||||
|
@ -109,7 +111,55 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
medications.add(medication);
|
medications.add(medication);
|
||||||
}
|
}
|
||||||
List<Ingredient> ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>());
|
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){
|
if(ingredientName == null){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -135,7 +185,8 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
manufacturer = ?,
|
manufacturer = ?,
|
||||||
possibleSideEffects = ?,
|
possibleSideEffects = ?,
|
||||||
administrationMethod = ?,
|
administrationMethod = ?,
|
||||||
currentStock = ?
|
currentStock = ?,
|
||||||
|
isDeprecated = ?
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
""";
|
""";
|
||||||
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL);
|
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL);
|
||||||
|
@ -145,6 +196,7 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
preparedStatement.setString(4, medication.getAdministrationMethod());
|
preparedStatement.setString(4, medication.getAdministrationMethod());
|
||||||
preparedStatement.setInt(5, medication.getCurrentStock());
|
preparedStatement.setInt(5, medication.getCurrentStock());
|
||||||
preparedStatement.setInt(6, medication.getId());
|
preparedStatement.setInt(6, medication.getId());
|
||||||
|
preparedStatement.setBoolean(6, medication.isDeprecated());
|
||||||
preparedStatement.executeUpdate();
|
preparedStatement.executeUpdate();
|
||||||
|
|
||||||
final String ingredientDeleteSQL = """
|
final String ingredientDeleteSQL = """
|
||||||
|
|
|
@ -5,5 +5,6 @@ CREATE TABLE medication
|
||||||
manufacturer TEXT NOT NULL,
|
manufacturer TEXT NOT NULL,
|
||||||
possibleSideEffects TEXT NOT NULL,
|
possibleSideEffects TEXT NOT NULL,
|
||||||
administrationMethod 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