diff --git a/db/nursingHome.db b/db/nursingHome.db index b89b146..9ce1178 100644 Binary files a/db/nursingHome.db and b/db/nursingHome.db differ diff --git a/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java b/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java index 8dc9f96..025c222 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/MedicationFixture.java @@ -117,7 +117,7 @@ public class MedicationFixture implements Fixture { "Unterhautinjektion", 120 )); - medications.add(new Medication( + Medication deprecatedMedication = new Medication( "Levothyroxin", "Sandoz", List.of( @@ -129,7 +129,9 @@ public class MedicationFixture implements Fixture { "Herzrasen, Gewichtsverlust", "Oral", 90 - )); + ); + deprecatedMedication.setIsDeprecated(true); + medications.add(deprecatedMedication); medications.add(new Medication( "Warfarin", "Apotex Inc.", 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/AllMedicationController.java b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java index 547d30f..769a787 100644 --- a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java @@ -7,6 +7,8 @@ import java.util.stream.Collectors; import de.hitec.nhplus.Main; import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.login.Permissions; +import de.hitec.nhplus.main.MainWindowController; import de.hitec.nhplus.medication.database.MedicationDao; import de.hitec.nhplus.nurse.Nurse; import de.hitec.nhplus.patient.Patient; @@ -15,9 +17,11 @@ import de.hitec.nhplus.treatment.TreatmentModalController; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import javafx.event.ActionEvent; 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,16 +52,26 @@ public class AllMedicationController { private TableColumn columnAdministrationMethod; @FXML private TableColumn columnCurrentStock; + @FXML + public Button buttonChangeAvailable; + @FXML + public Button buttonAdd; + @FXML + public Button buttonDelete; private final ObservableList medications = FXCollections.observableArrayList(); private MedicationDao dao; + private boolean hasEditPermissions; /** * Initialization method that is called after the binding of all the fields. */ @FXML public void initialize() { - readAllAndShowInTableView(); + int editPermissions = Permissions.MANAGEMENT | Permissions.OWNER; + int userPermissions = MainWindowController.getInstance().getUser().getPermissions(); + hasEditPermissions = (userPermissions & editPermissions) != 0; + this.readAllAndShowInTableView(); this.columnId.setCellValueFactory(new PropertyValueFactory<>("id")); this.columnName.setCellValueFactory(new PropertyValueFactory<>("name")); @@ -82,6 +96,13 @@ public class AllMedicationController { this.columnCurrentStock.setCellValueFactory(new PropertyValueFactory<>("currentStock")); this.tableView.setItems(this.medications); + + if (!hasEditPermissions) { + this.buttonAdd.setDisable(true); + this.buttonDelete.setDisable(true); + this.buttonChangeAvailable.setDisable(true); + } + } /** @@ -90,7 +111,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(); } @@ -120,6 +141,38 @@ public class AllMedicationController { } } + @FXML + public void handleChangeAvailable() { + Medication selectedItem = this.tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null) { + return; + } + + try { + selectedItem.setIsDeprecated(true); + this.dao.update(selectedItem); + } catch (SQLException exception) { + exception.printStackTrace(); + } + this.readAllAndShowInTableView(); + } + + + @FXML + public void handleDelete() { + Medication selectedItem = this.tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null) { + return; + } + + try { + this.dao.delete(selectedItem.getId()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + this.readAllAndShowInTableView(); + } + /** * Internal method to create a {@link MedicationModalController MedicationModal}. * @@ -164,4 +217,5 @@ public class AllMedicationController { } }); } + } 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..03d5947 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/medication/DeprecatedMedicationController.java @@ -0,0 +1,108 @@ +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.Button; +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; + +/** + * The controller for viewing all deprecated {@link Medication}s. + * + * @author Armin Ribic + * @author Dorian Nemec + */ +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; + @FXML + public Button buttonChangeAvailable; + + private final ObservableList medications = FXCollections.observableArrayList(); + private MedicationDao dao; + + /** + * Initialization method that is called after the binding of all the fields. + */ + public void initialize() { + this.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.medications.clear(); + this.dao = DaoFactory.getInstance().createMedicationDAO(); + try { + this.medications.setAll(this.dao.readAllDeprecated()); + } catch (SQLException exception) { + 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(); + } + this.readAllAndShowInTableView(); + } + +} diff --git a/src/main/java/de/hitec/nhplus/medication/Medication.java b/src/main/java/de/hitec/nhplus/medication/Medication.java index 6320e58..f33a98e 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 isDeprecated ) { 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(isDeprecated); } 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/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 e226770..5e31e43 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()"); @@ -103,12 +104,111 @@ 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); } 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 medication.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; + } + 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 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(); + + 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; } @@ -134,7 +234,8 @@ public class MedicationDao implements Dao { manufacturer = ?, possibleSideEffects = ?, administrationMethod = ?, - currentStock = ? + currentStock = ?, + isDeprecated = ? WHERE id = ? """; PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); @@ -143,7 +244,8 @@ public class MedicationDao implements Dao { preparedStatement.setString(3, medication.getPossibleSideEffects()); preparedStatement.setString(4, medication.getAdministrationMethod()); preparedStatement.setInt(5, medication.getCurrentStock()); - preparedStatement.setInt(6, medication.getId()); + preparedStatement.setBoolean(6, medication.isDeprecated()); + preparedStatement.setInt(7, medication.getId()); preparedStatement.executeUpdate(); final String ingredientDeleteSQL = """ @@ -191,7 +293,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/java/de/hitec/nhplus/nurse/database/NurseDao.java b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java index f4972de..902f69e 100644 --- a/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java @@ -112,6 +112,4 @@ public class NurseDao extends DaoImp { statement.setInt(1, id); return statement; } - - } diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index 055c4be..ec8a4a9 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -78,7 +78,7 @@ public class AllTreatmentController { */ @FXML public void initialize() { - readAllAndShowInTableView(); + comboBoxPatientSelection.setItems(patientSelection); comboBoxPatientSelection.getSelectionModel().select("alle"); @@ -112,7 +112,7 @@ public class AllTreatmentController { ); this.createComboBoxData(); - + readAllAndShowInTableView(); } /** diff --git a/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml b/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml index 157caac..63407cc 100644 --- a/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml +++ b/src/main/resources/de/hitec/nhplus/medication/AllMedicationView.fxml @@ -73,8 +73,16 @@ fx:id="buttonDelete" mnemonicParsing="false" prefWidth="90.0" + onAction="#handleDelete" text="Löschen" /> +