diff --git a/db/nursingHome.db b/db/nursingHome.db index e05cb45..df596e0 100644 Binary files a/db/nursingHome.db and b/db/nursingHome.db differ diff --git a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java index 3ef6972..c5d0c0f 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java @@ -46,6 +46,14 @@ public class NurseFixture implements Fixture { "Armout", "9876543210" )); + + nurses.add(new Nurse( + "Björnd", + "Heideberger", + "69420", + true + )); + NurseDao dao = DaoFactory.getInstance().createNurseDAO(); for (Nurse nurse : nurses) { dao.create(nurse); diff --git a/src/main/java/de/hitec/nhplus/main/MainWindowController.java b/src/main/java/de/hitec/nhplus/main/MainWindowController.java index b0f4a47..293ba41 100644 --- a/src/main/java/de/hitec/nhplus/main/MainWindowController.java +++ b/src/main/java/de/hitec/nhplus/main/MainWindowController.java @@ -1,8 +1,10 @@ package de.hitec.nhplus.main; import de.hitec.nhplus.Main; +import de.hitec.nhplus.nurse.Nurse; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; +import javafx.scene.control.SelectionModel; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.layout.AnchorPane; @@ -16,6 +18,8 @@ import java.util.Objects; * * @author Bernd Heidemann * @author Dominik Säume + * @author Armin Ribic + * @author Dorian Nemec */ public class MainWindowController { @FXML @@ -29,14 +33,25 @@ public class MainWindowController { @FXML private Tab treatmentTab; @FXML - private AnchorPane nursePage; - @FXML private Tab nurseTab; @FXML + private TabPane nurseTabPane; + @FXML + private AnchorPane activeNursePage; + @FXML + private Tab activeNurseTab; + @FXML + private AnchorPane lockedNursePage; + @FXML + private Tab lockedNurseTab; + @FXML private AnchorPane medicationPage; @FXML private Tab medicationTab; + /** + * Initialization method that is called after the binding of all the fields. + */ @FXML public void initialize() { loadPatientPage(); @@ -46,6 +61,12 @@ public class MainWindowController { treatmentTab.setOnSelectionChanged(event -> loadTreatmentsPage()); nurseTab.setOnSelectionChanged(event -> loadNursePage()); medicationTab.setOnSelectionChanged(event -> loadMedicationPage()); + + + nurseTabPane.getSelectionModel().select(activeNurseTab); + + activeNurseTab.setOnSelectionChanged(event -> loadActiveNursePage()); + lockedNurseTab.setOnSelectionChanged(event -> loadLockedNursePage()); } /** @@ -67,7 +88,7 @@ public class MainWindowController { } /** - * Loads the treatment page into its tab. + * Loads the {@link } page into its tab. */ private void loadTreatmentsPage() { try { @@ -88,15 +109,47 @@ public class MainWindowController { * Loads the nurse page into its tab. */ private void loadNursePage() { + SelectionModel selectionModel = nurseTabPane.getSelectionModel(); + Tab selectedTab = selectionModel.getSelectedItem(); + if(selectedTab == activeNurseTab){ + loadActiveNursePage(); + } + if(selectedTab == lockedNurseTab){ + loadLockedNursePage(); + } + } + + /** + * Loads the active {@link Nurse} page into its tab. + */ + private void loadActiveNursePage() { try { - BorderPane nursePane = FXMLLoader.load( + BorderPane activeNursePane = FXMLLoader.load( Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/nurse/AllNurseView.fxml")) ); - nursePage.getChildren().setAll(nursePane); - AnchorPane.setTopAnchor(nursePane, 0d); - AnchorPane.setBottomAnchor(nursePane, 0d); - AnchorPane.setLeftAnchor(nursePane, 0d); - AnchorPane.setRightAnchor(nursePane, 0d); + activeNursePage.getChildren().setAll(activeNursePane); + AnchorPane.setTopAnchor(activeNursePane, 0d); + AnchorPane.setBottomAnchor(activeNursePane, 0d); + AnchorPane.setLeftAnchor(activeNursePane, 0d); + AnchorPane.setRightAnchor(activeNursePane, 0d); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + + /** + * Loads the locked {@link Nurse} page into its tab. + */ + private void loadLockedNursePage() { + try { + BorderPane lockedNursePane = FXMLLoader.load( + Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/nurse/LockedNurseView.fxml")) + ); + lockedNursePage.getChildren().setAll(lockedNursePane); + AnchorPane.setTopAnchor(lockedNursePane, 0d); + AnchorPane.setBottomAnchor(lockedNursePane, 0d); + AnchorPane.setLeftAnchor(lockedNursePane, 0d); + AnchorPane.setRightAnchor(lockedNursePane, 0d); } catch (IOException exception) { exception.printStackTrace(); } diff --git a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java index 86eccbd..77a7a5f 100644 --- a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java +++ b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java @@ -33,6 +33,8 @@ public class AllNurseController { @FXML public Button buttonAdd; @FXML + public Button buttonLock; + @FXML private TableView tableView; @FXML private TableColumn columnId; @@ -89,7 +91,7 @@ public class AllNurseController { this.nurses.clear(); this.dao = DaoFactory.getInstance().createNurseDAO(); try { - this.nurses.setAll(this.dao.readAll()); + this.nurses.setAll(this.dao.readAllActive()); }catch (SQLException exception){ exception.printStackTrace(); } @@ -119,5 +121,20 @@ public class AllNurseController { clearTextfields(); } + @FXML + public void handleLock(){ + Nurse selectedItem = this.tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null){ + return; + } + + try { + selectedItem.setLocked(true); + this.dao.update(selectedItem); + }catch (SQLException exception){ + exception.printStackTrace(); + } + readAllAndShowInTableView(); + } } diff --git a/src/main/java/de/hitec/nhplus/nurse/LockedNurseController.java b/src/main/java/de/hitec/nhplus/nurse/LockedNurseController.java new file mode 100644 index 0000000..f601b1b --- /dev/null +++ b/src/main/java/de/hitec/nhplus/nurse/LockedNurseController.java @@ -0,0 +1,136 @@ +package de.hitec.nhplus.nurse; + +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.nurse.database.NurseDao; +import de.hitec.nhplus.treatment.Treatment; +import de.hitec.nhplus.treatment.database.TreatmentDao; +import de.hitec.nhplus.utils.DateConverter; +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.time.LocalDate; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Controller for all locked nurses. + * @author Armin Ribic + * @author Dorian Nemec + */ +public class LockedNurseController { + @FXML + public Button buttonDelete; + @FXML + private Button buttonUnlock; + @FXML + public TableView tableView; + @FXML + public TableColumn columnId; + @FXML + public TableColumn columnFirstName; + @FXML + public TableColumn columnSurName; + @FXML + public TableColumn columnPhoneNumber; + @FXML + public TableColumn columnDeleteDate; + + private final ObservableList nurses = FXCollections.observableArrayList(); + private final Map> treatmentsPerNurse = new HashMap<>(); + private NurseDao dao; + private TreatmentDao treatmentDao; + + /** + * This method allows initializing a {@link LockedNurseController} object + * that is called after the binding of all the fields. + */ + public void initialize() { + this.readAllAndShowInTableView(); + + this.columnId.setCellValueFactory(new PropertyValueFactory<>("id")); + this.columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName")); + this.columnSurName.setCellValueFactory(new PropertyValueFactory<>("surName")); + this.columnPhoneNumber.setCellValueFactory(new PropertyValueFactory<>("phoneNumber")); + this.columnDeleteDate.setCellValueFactory(cellData -> new SimpleStringProperty( + DateConverter.convertLocalDateToString(cellData.getValue().calculateDeleteDate())) + ); + + buttonDelete.setDisable(true); + + this.tableView.setItems(this.nurses); + + } + + /** + * Reads all locked {@link Nurse} data and shows it in the table. + */ + private void readAllAndShowInTableView() { + this.nurses.clear(); + this.treatmentsPerNurse.clear(); + + this.dao = DaoFactory.getInstance().createNurseDAO(); + this.treatmentDao = DaoFactory.getInstance().createTreatmentDao(); + try { + this.nurses.addAll(this.dao.readAllLocked()); + this.nurses.forEach( + nurse -> { + try { + this.treatmentsPerNurse.put(nurse, this.treatmentDao.readTreatmentsByNurse(nurse.getId())); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + ); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + + @FXML + public void handleDelete() { + Nurse selectedItem = this.tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null) { + return; + } + try { + this.dao.delete(selectedItem.getId()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + + readAllAndShowInTableView(); + + } + + @FXML + public void handleMouseClick() { + Nurse nurse = tableView.getSelectionModel().getSelectedItem(); + boolean canBeDeleted = nurse.calculateDeleteDate().isBefore(LocalDate.now()); + buttonDelete.setDisable(!canBeDeleted); + } + + @FXML + public void unlockNurse() { + Nurse selectedItem = this.tableView.getSelectionModel().getSelectedItem(); + if (selectedItem == null) { + return; + } + + try { + selectedItem.setLocked(false); + this.dao.update(selectedItem); + } catch (SQLException exception) { + exception.printStackTrace(); + } + readAllAndShowInTableView(); + } + +} diff --git a/src/main/java/de/hitec/nhplus/nurse/Nurse.java b/src/main/java/de/hitec/nhplus/nurse/Nurse.java index 9afab12..22c4b49 100644 --- a/src/main/java/de/hitec/nhplus/nurse/Nurse.java +++ b/src/main/java/de/hitec/nhplus/nurse/Nurse.java @@ -1,26 +1,36 @@ package de.hitec.nhplus.nurse; +import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.main.Person; +import de.hitec.nhplus.treatment.Treatment; +import de.hitec.nhplus.utils.DateConverter; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; +import java.sql.SQLException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.List; import java.util.StringJoiner; /** * The model for a {@link Nurse}. * * @author Dominik Säume + * @author Armin Ribic + * @author Dorian Nemec */ public class Nurse extends Person { private SimpleIntegerProperty id; private final SimpleStringProperty phoneNumber; + private final SimpleBooleanProperty locked; /** * This constructor allows instantiating a {@link Nurse} object, * before it is stored in the database, by omitting the {@link Nurse#id ID} value. - * - * @implSpec Instances created with this constructor can be directly passed to - * {@link de.hitec.nhplus.nurse.database.NurseDao#create NurseDao.create}. + * It includes the locked Property. + * @implSpec This was added for usage in the {@link de.hitec.nhplus.fixtures.NurseFixture NurseFixture}. */ public Nurse( String firstName, @@ -29,6 +39,22 @@ public class Nurse extends Person { ) { super(firstName, surName); this.phoneNumber = new SimpleStringProperty(phoneNumber); + this.locked = new SimpleBooleanProperty(false); + } + + /** + * This constructor allows instantiating a {@link Nurse} object with + * specifying if the nurse is locked or not. + */ + public Nurse( + String firstName, + String surName, + String phoneNumber, + Boolean isLocked + ) { + super(firstName, surName); + this.phoneNumber = new SimpleStringProperty(phoneNumber); + this.locked = new SimpleBooleanProperty(isLocked); } /** @@ -38,11 +64,38 @@ public class Nurse extends Person { int id, String firstName, String surName, - String phoneNumber + String phoneNumber, + Boolean isLocked ) { super(firstName, surName); this.id = new SimpleIntegerProperty(id); this.phoneNumber = new SimpleStringProperty(phoneNumber); + this.locked = new SimpleBooleanProperty(isLocked); + } + + /** + * Calculates the date when the {@link Nurse} can be deleted. + */ + public LocalDate calculateDeleteDate() { + List treatments; + try { + treatments = DaoFactory.getInstance().createTreatmentDao().readTreatmentsByNurse(this.getId()); + }catch (SQLException exception){ + treatments = null; + } + if(treatments == null){ + return LocalDate.now().plusDays(1); + } + + LocalDate newestDate = LocalDate.of(1980, 1, 1); + for (Treatment treatment : treatments) { + LocalDate date = DateConverter.convertStringToLocalDate(treatment.getDate()); + if (date.isAfter(newestDate)) { + newestDate = date; + } + } + + return newestDate.plusYears(10); } public void setPhoneNumber(String phoneNumber) { @@ -65,6 +118,19 @@ public class Nurse extends Person { return phoneNumber; } + + public boolean isLocked() { + return locked.get(); + } + + public SimpleBooleanProperty lockedProperty() { + return locked; + } + + public void setLocked(boolean locked) { + this.locked.set(locked); + } + @Override public String toString() { return new StringJoiner(System.lineSeparator()) @@ -73,6 +139,7 @@ public class Nurse extends Person { .add("FirstName: " + this.getFirstName()) .add("SurName: " + this.getSurName()) .add("PhoneNumber: " + this.getPhoneNumber()) + .add("IsLocked: " + this.isLocked()) .toString(); } 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 1d1a0e5..f4b0b3c 100644 --- a/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java @@ -17,6 +17,7 @@ import java.util.List; * @author Dominik Säume */ public class NurseDao extends DaoImp { + public NurseDao(Connection connection) { super(connection); } @@ -25,13 +26,14 @@ public class NurseDao extends DaoImp { protected PreparedStatement getCreateStatement(Nurse nurse) throws SQLException { final String SQL = """ INSERT INTO nurse - (firstName, surName, phoneNumber) - VALUES (?, ?, ?) + (firstName, surName, phoneNumber, isLocked) + VALUES (?, ?, ?, ?) """; PreparedStatement statement = this.connection.prepareStatement(SQL); statement.setString(1, nurse.getFirstName()); statement.setString(2, nurse.getSurName()); statement.setString(3, nurse.getPhoneNumber()); + statement.setBoolean(4, nurse.isLocked()); return statement; } @@ -49,7 +51,8 @@ public class NurseDao extends DaoImp { result.getInt(1), result.getString(2), result.getString(3), - result.getString(4) + result.getString(4), + result.getBoolean(5) ); } @@ -58,7 +61,6 @@ public class NurseDao extends DaoImp { final String SQL = "SELECT * FROM nurse"; return this.connection.prepareStatement(SQL); } - @Override protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); @@ -68,20 +70,38 @@ public class NurseDao extends DaoImp { return list; } - @Override + /** + * Read all database entries of active {@link Nurse}s into a {@link List} of model instances. + */ + public List readAllActive() throws SQLException { + final String SQL = "SELECT * FROM nurse WHERE isLocked=false"; + return getListFromResultSet(this.connection.prepareStatement(SQL).executeQuery()); + } + + /** + * Read all database entries of locked {@link Nurse}s into a {@link List} of model instances. + */ + public List readAllLocked() throws SQLException { + final String SQL = "SELECT * FROM nurse WHERE isLocked=true"; + return getListFromResultSet(this.connection.prepareStatement(SQL).executeQuery()); + } + + @Override protected PreparedStatement getUpdateStatement(Nurse nurse) throws SQLException { final String SQL = """ UPDATE nurse SET firstName = ?, surName = ?, - phoneNumber = ? + phoneNumber = ?, + isLocked = ? WHERE id = ? """; PreparedStatement statement = this.connection.prepareStatement(SQL); statement.setString(1, nurse.getFirstName()); statement.setString(2, nurse.getSurName()); statement.setString(3, nurse.getPhoneNumber()); - statement.setInt(4, nurse.getId()); + statement.setBoolean(4, nurse.isLocked()); + statement.setInt(5, nurse.getId()); return statement; } @@ -92,4 +112,6 @@ public class NurseDao extends DaoImp { statement.setInt(1, id); return statement; } + + } diff --git a/src/main/resources/de/hitec/nhplus/main/MainWindowView.fxml b/src/main/resources/de/hitec/nhplus/main/MainWindowView.fxml index 2c63236..c5316ea 100644 --- a/src/main/resources/de/hitec/nhplus/main/MainWindowView.fxml +++ b/src/main/resources/de/hitec/nhplus/main/MainWindowView.fxml @@ -16,7 +16,14 @@ - + + + + + + + + diff --git a/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml b/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml index 2597997..3a7b76a 100644 --- a/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml +++ b/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml @@ -77,10 +77,11 @@ text="Hinzufügen" />