From 63005098737779961e4acb3970b5415e3706d1b2 Mon Sep 17 00:00:00 2001 From: Dorian Nemec Date: Mon, 20 May 2024 07:50:54 +0200 Subject: [PATCH 1/5] #24: UI Veraltete Medikamenten addiert --- .../nhplus/main/MainWindowController.java | 14 +++- .../DeprecatedMedicationController.java | 79 +++++++++++++++++++ .../hitec/nhplus/medication/Medication.java | 19 ++++- .../medication/database/MedicationDao.java | 6 +- .../medication/DeprecatedMedicationView.fxml | 77 ++++++++++++++++++ 5 files changed, 188 insertions(+), 7 deletions(-) create mode 100644 src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java create mode 100644 src/main/resources/de/hitec/nhplus/medication/DeprecatedMedicationView.fxml diff --git a/src/main/java/de/hitec/nhplus/main/MainWindowController.java b/src/main/java/de/hitec/nhplus/main/MainWindowController.java index c402020..cc6ba17 100644 --- a/src/main/java/de/hitec/nhplus/main/MainWindowController.java +++ b/src/main/java/de/hitec/nhplus/main/MainWindowController.java @@ -80,10 +80,16 @@ public class MainWindowController { Permissions.MANAGEMENT | Permissions.OWNER ) )); - tabManager.setupTab(mainTabPane, new TabStruct( - "Medikamente", - "/de/hitec/nhplus/medication/AllMedicationView.fxml", - Permissions.MANAGEMENT + tabManager.setupSubTabPane(mainTabPane, "Medikamente", Permissions.MANAGEMENT, List.of( + new TabStruct( + "Medikamente", + "/de/hitec/nhplus/medication/AllMedicationView.fxml", + Permissions.MANAGEMENT + ), new TabStruct( + "Veraltete Medikamente", + "/de/hitec/nhplus/medication/DeprecatedMedicationView.fxml", + Permissions.MANAGEMENT + ) )); diff --git a/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java b/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java new file mode 100644 index 0000000..6938407 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java @@ -0,0 +1,79 @@ +package de.hitec.nhplus.medication; + +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.medication.database.MedicationDao; +import javafx.beans.property.SimpleStringProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.control.cell.PropertyValueFactory; + +import java.sql.SQLException; +import java.util.List; +import java.util.stream.Collectors; + +public class DeprecatedMedicationController { + @FXML + private TableView tableView; + @FXML + private TableColumn columnId; + @FXML + private TableColumn columnName; + @FXML + private TableColumn columnManufacturer; + @FXML + private TableColumn columnIngredient; + @FXML + private TableColumn columnPossibleSideEffects; + @FXML + private TableColumn columnAdministrationMethod; + @FXML + private TableColumn columnCurrentStock; + + private final ObservableList medications = FXCollections.observableArrayList(); + private MedicationDao dao; + + @FXML + public void initialize() { + readAllAndShowInTableView(); + + this.columnId.setCellValueFactory(new PropertyValueFactory<>("id")); + this.columnName.setCellValueFactory(new PropertyValueFactory<>("name")); + this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer")); + this.columnIngredient.setCellValueFactory( + cellData -> { + Medication medication = cellData.getValue(); + List ingredients = medication.getIngredients(); + if (ingredients.isEmpty()) { + return new SimpleStringProperty(""); + } + + return new SimpleStringProperty( + ingredients + .stream() + .map(ingredient -> ingredient.getName()) + .collect(Collectors.joining("\n")) + ); + }); + this.columnPossibleSideEffects.setCellValueFactory(new PropertyValueFactory<>("possibleSideEffects")); + this.columnAdministrationMethod.setCellValueFactory(new PropertyValueFactory<>("administrationMethod")); + this.columnCurrentStock.setCellValueFactory(new PropertyValueFactory<>("currentStock")); + + this.tableView.setItems(this.medications); + } + + /** + * Internal method to read all data and set it to the table view. + */ + public void readAllAndShowInTableView() { + this.dao = DaoFactory.getInstance().createMedicationDAO(); + try { + this.medications.setAll(dao.readAll()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + +} diff --git a/src/main/java/de/hitec/nhplus/medication/Medication.java b/src/main/java/de/hitec/nhplus/medication/Medication.java index 6320e58..a73c8b3 100644 --- a/src/main/java/de/hitec/nhplus/medication/Medication.java +++ b/src/main/java/de/hitec/nhplus/medication/Medication.java @@ -1,5 +1,6 @@ package de.hitec.nhplus.medication; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleListProperty; import javafx.beans.property.SimpleStringProperty; @@ -23,6 +24,7 @@ public class Medication { private final SimpleStringProperty possibleSideEffects; private final SimpleStringProperty administrationMethod; private final SimpleIntegerProperty currentStock; + private final SimpleBooleanProperty isDeprecated; /** * This constructor allows instantiating a {@link Medication} object, @@ -45,6 +47,7 @@ public class Medication { this.possibleSideEffects = new SimpleStringProperty(possibleSideEffects); this.administrationMethod = new SimpleStringProperty(administrationMethod); this.currentStock = new SimpleIntegerProperty(currentStock); + this.isDeprecated = new SimpleBooleanProperty(false); } /** @@ -57,7 +60,8 @@ public class Medication { List ingredients, String possibleSideEffects, String administrationMethod, - int currentStock + int currentStock, + boolean isDepreacted ) { this.id = new SimpleIntegerProperty(id); this.name = new SimpleStringProperty(name); @@ -66,6 +70,7 @@ public class Medication { this.possibleSideEffects = new SimpleStringProperty(possibleSideEffects); this.administrationMethod = new SimpleStringProperty(administrationMethod); this.currentStock = new SimpleIntegerProperty(currentStock); + this.isDeprecated = new SimpleBooleanProperty(isDepreacted); } public int getId() { @@ -84,6 +89,18 @@ public class Medication { return name; } + public boolean isDeprecated() { + return isDeprecated.get(); + } + + public SimpleBooleanProperty isDeprecatedProperty() { + return isDeprecated; + } + + public void setIsDeprecated(boolean isDeprecated) { + this.isDeprecated.set(isDeprecated); + } + public void setName(String name) { this.name.set(name); } 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 e226770..36a0e5e 100644 --- a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java +++ b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java @@ -103,7 +103,8 @@ public class MedicationDao implements Dao { new ArrayList<>(), result.getString(4), result.getString(5), - result.getInt(6) + result.getInt(6), + result.getBoolean(7) ); medications.add(medication); } @@ -191,7 +192,8 @@ public class MedicationDao implements Dao { List.of(), result.getString(4), result.getString(5), - result.getInt(6) + result.getInt(6), + result.getBoolean(7) ); List ingredients = new ArrayList<>(); diff --git a/src/main/resources/de/hitec/nhplus/medication/DeprecatedMedicationView.fxml b/src/main/resources/de/hitec/nhplus/medication/DeprecatedMedicationView.fxml new file mode 100644 index 0000000..f140ac2 --- /dev/null +++ b/src/main/resources/de/hitec/nhplus/medication/DeprecatedMedicationView.fxml @@ -0,0 +1,77 @@ + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + +
\ No newline at end of file -- 2.45.2 From 9fac5b34a3a71efab2dd616bd0f370be666282c4 Mon Sep 17 00:00:00 2001 From: Dorian Nemec Date: Mon, 20 May 2024 09:51:41 +0200 Subject: [PATCH 2/5] #24: Implemented isDeprecated in Dao --- .../DeprecatedMedicationController.java | 2 +- .../medication/database/MedicationDao.java | 60 +++++++++++++++++-- .../nhplus/medication/database/Medication.sql | 3 +- 3 files changed, 59 insertions(+), 6 deletions(-) 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 -- 2.45.2 From e584e220a57b7885568dc8f6f3b74137d391d7a2 Mon Sep 17 00:00:00 2001 From: arminribic Date: Mon, 20 May 2024 10:28:17 +0200 Subject: [PATCH 3/5] #24: Logik implementiert und UI fertig gestellt --- .../medication/AllMedicationController.java | 19 +++++++++++++++++++ .../DeprecatedMedicationController.java | 18 ++++++++++++++++++ .../medication/MedicationModalController.java | 1 + .../medication/database/MedicationDao.java | 6 +++--- .../nhplus/medication/AllMedicationView.fxml | 7 +++++++ .../medication/DeprecatedMedicationView.fxml | 9 ++++----- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java index 547d30f..8c2ca83 100644 --- a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java @@ -18,6 +18,7 @@ import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; +import javafx.scene.control.Button; import javafx.scene.control.SelectionModel; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; @@ -48,6 +49,8 @@ public class AllMedicationController { private TableColumn columnAdministrationMethod; @FXML private TableColumn columnCurrentStock; + @FXML + public Button buttonChangeAvailable; private final ObservableList medications = FXCollections.observableArrayList(); private MedicationDao dao; @@ -120,6 +123,22 @@ public class AllMedicationController { } } + @FXML + public void handleChangeAvailable() { + Medication selectedItem = tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null) { + return; + } + + try { + selectedItem.setIsDeprecated(true); + this.dao.update(selectedItem); + } catch (SQLException exception) { + exception.printStackTrace(); + } + readAllAndShowInTableView(); + } + /** * Internal method to create a {@link MedicationModalController MedicationModal}. * diff --git a/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java b/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java index fe38fe7..d992fe9 100644 --- a/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java @@ -6,6 +6,7 @@ import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; +import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; @@ -31,6 +32,8 @@ public class DeprecatedMedicationController { private TableColumn columnAdministrationMethod; @FXML private TableColumn columnCurrentStock; + @FXML + public Button buttonChangeAvailable; private final ObservableList medications = FXCollections.observableArrayList(); private MedicationDao dao; @@ -75,5 +78,20 @@ public class DeprecatedMedicationController { exception.printStackTrace(); } } + @FXML + public void handleChangeAvailable(){ + Medication selectedItem = tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null) { + return; + } + + try { + selectedItem.setIsDeprecated(false); + this.dao.update(selectedItem); + } catch (SQLException exception) { + exception.printStackTrace(); + } + readAllAndShowInTableView(); + } } diff --git a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java index 9489737..ab80e1d 100644 --- a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java +++ b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java @@ -39,6 +39,7 @@ public class MedicationModalController { @FXML public Button buttonSave; + private Stage stage; private Medication medication; private final ObservableList ingredients = FXCollections.observableArrayList(); 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 41f5f7d..f4f4655 100644 --- a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java +++ b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java @@ -86,7 +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 + WHERE medication.isDeprecated = false """; ResultSet result = connection.prepareStatement(SQL).executeQuery(); @@ -134,7 +134,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 = true + WHERE medication.isDeprecated = true """; ResultSet result = connection.prepareStatement(SQL).executeQuery(); @@ -196,7 +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.setBoolean(7, medication.isDeprecated()); preparedStatement.executeUpdate(); final String ingredientDeleteSQL = """ diff --git a/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml b/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml index 157caac..d9456b9 100644 --- a/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml +++ b/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml @@ -75,6 +75,13 @@ prefWidth="90.0" text="Löschen" /> +