From bcc6d1385f4305233136a1c6e6583f0dab84b17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Mon, 20 May 2024 09:00:23 +0200 Subject: [PATCH] #7 Cleanup --- db/nursingHome.db | Bin 28672 -> 28672 bytes .../nhplus/fixtures/TreatmentFixture.java | 59 ++++---- .../hitec/nhplus/nurse/database/NurseDao.java | 4 +- .../treatment/AllTreatmentController.java | 2 +- .../treatment/LockedTreatmentController.java | 41 +++--- .../de/hitec/nhplus/treatment/Treatment.java | 18 +-- .../treatment/database/TreatmentDao.java | 130 ++++++++++-------- .../nhplus/treatment/LockedTreatmentView.fxml | 3 +- 8 files changed, 125 insertions(+), 132 deletions(-) diff --git a/db/nursingHome.db b/db/nursingHome.db index 43fecf6d4eedeaf73da1af9b0a95d1e9cd0e4248..0cdc030c68d5c1da965119ae1d0f20ad97ae6082 100644 GIT binary patch delta 310 zcmZp8z}WDBae|Z(%UuQr26iBZfwvQNjHG~~dPNI)`TjDnaa1wzt>($*p2{_ecMnI^ zWkOTr)iMF9c;T7p_X delta 260 zcmZp8z}WDBae|Z(^DPDj26iBZf$bA@jHG~~dPNI)`TjDnaa1wzt>&7_dw;3bb*OXJic8 zyr0*K38?xP|4S}Lj?GV` { Nurse armin = nursesByName.get("Armin"); treatments.add(new Treatment( - 1, seppl, ole, convertStringToLocalDate("2023-06-03"), @@ -67,11 +66,9 @@ public class TreatmentFixture implements Fixture { "Gespräch", "Der Patient hat enorme Angstgefühle und glaubt, er sei überfallen worden." + "Ihm seien alle Wertsachen gestohlen worden." - + "Patient beruhigt sich erst, als alle Wertsachen im Zimmer gefunden worden sind.", - false + + "Patient beruhigt sich erst, als alle Wertsachen im Zimmer gefunden worden sind." )); treatments.add(new Treatment( - 2, seppl, armin, convertStringToLocalDate("2023-06-05"), @@ -80,102 +77,92 @@ public class TreatmentFixture implements Fixture { "Gespräch", "Patient irrt auf der Suche nach gestohlenen Wertsachen durch die Etage " + "und bezichtigt andere Bewohner des Diebstahls." - + "Patient wird in seinen Raum zurückbegleitet und erhält Beruhigungsmittel.", - false + + "Patient wird in seinen Raum zurückbegleitet und erhält Beruhigungsmittel." )); treatments.add(new Treatment( - 3, martina, ole, convertStringToLocalDate("2023-06-04"), convertStringToLocalTime("07:30"), convertStringToLocalTime("08:00"), "Waschen", - "Patient mit Waschlappen gewaschen und frisch angezogen. Patient gewendet.", - false + "Patient mit Waschlappen gewaschen und frisch angezogen. Patient gewendet." )); treatments.add(new Treatment( - 4, seppl, armin, convertStringToLocalDate("2023-06-06"), convertStringToLocalTime("15:10"), convertStringToLocalTime("16:00"), "Spaziergang", - "Spaziergang im Park, Patient döst im Rollstuhl ein", - false) - ); + "Spaziergang im Park, Patient döst im Rollstuhl ein" + + )); treatments.add(new Treatment( - 8, seppl, ole, convertStringToLocalDate("2023-06-08"), convertStringToLocalTime("15:00"), convertStringToLocalTime("16:00"), "Spaziergang", - "Parkspaziergang; Patient ist heute lebhafter und hat klare Momente; erzählt von seiner Tochter", - false) - ); + "Parkspaziergang; Patient ist heute lebhafter und hat klare Momente; erzählt von seiner Tochter" + )); treatments.add(new Treatment( - 9, martina, armin, convertStringToLocalDate("2023-06-07"), convertStringToLocalTime("11:00"), convertStringToLocalTime("11:30"), "Waschen", - "Waschen per Dusche auf einem Stuhl; Patientin gewendet;", - false) - ); + "Waschen per Dusche auf einem Stuhl; Patientin gewendet;" + )); treatments.add(new Treatment( - 12, hans, armin, convertStringToLocalDate("2023-06-08"), convertStringToLocalTime("15:00"), convertStringToLocalTime("15:30"), "Physiotherapie", - "Übungen zur Stabilisation und Mobilisierung der Rückenmuskulatur", - false) - ); + "Übungen zur Stabilisation und Mobilisierung der Rückenmuskulatur" + )); treatments.add(new Treatment( - 14, ahmet, ole, convertStringToLocalDate("2023-08-24"), convertStringToLocalTime("09:30"), convertStringToLocalTime("10:15"), "KG", - "Lympfdrainage", - false)); + "Lympfdrainage" + )); treatments.add(new Treatment( - 16, elisabeth, armin, convertStringToLocalDate("2023-08-31"), convertStringToLocalTime("13:30"), convertStringToLocalTime("13:45"), "Toilettengang", - "Hilfe beim Toilettengang; Patientin klagt über Schmerzen beim Stuhlgang. Gabe von Iberogast", - false) - ); - treatments.add(new Treatment( - 17, + "Hilfe beim Toilettengang; Patientin klagt über Schmerzen beim Stuhlgang. Gabe von Iberogast" + )); + Treatment lockedTreatment = new Treatment( elisabeth, ole, convertStringToLocalDate("2023-09-01"), convertStringToLocalTime("16:00"), convertStringToLocalTime("17:00"), "KG", - "Massage der Extremitäten zur Verbesserung der Durchblutung", - true) + "Massage der Extremitäten zur Verbesserung der Durchblutung" ); + lockedTreatment.setLocked(true); + treatments.add(lockedTreatment); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); Map treatmentsById = new HashMap<>(); for (Treatment treatment : treatments) { dao.create(treatment); + } + for (Treatment treatment : dao.readAll()) { treatmentsById.put(String.valueOf(treatment.getId()), treatment); + } return treatmentsById; } 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 f4b0b3c..f4972de 100644 --- a/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java @@ -74,7 +74,7 @@ public class NurseDao extends DaoImp { * 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"; + final String SQL = "SELECT * FROM nurse WHERE isLocked = false"; return getListFromResultSet(this.connection.prepareStatement(SQL).executeQuery()); } @@ -82,7 +82,7 @@ public class NurseDao extends DaoImp { * 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"; + final String SQL = "SELECT * FROM nurse WHERE isLocked = true"; return getListFromResultSet(this.connection.prepareStatement(SQL).executeQuery()); } diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index de8c9d0..055c4be 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -121,7 +121,7 @@ public class AllTreatmentController { public void readAllAndShowInTableView() { this.dao = DaoFactory.getInstance().createTreatmentDao(); try { - this.treatments.setAll(dao.readAll()); + this.treatments.setAll(dao.readAllActive()); } catch (SQLException exception) { exception.printStackTrace(); } diff --git a/src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java index 2fdb0ed..8b78e3e 100644 --- a/src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/LockedTreatmentController.java @@ -70,7 +70,6 @@ public class LockedTreatmentController { this.tableView.setItems(this.treatments); - handleDelete(); } /** @@ -82,11 +81,30 @@ public class LockedTreatmentController { this.treatmentDao = DaoFactory.getInstance().createTreatmentDao(); try { this.treatments.addAll(this.treatmentDao.readAllLocked()); + deleteOldTreatments(); } catch (SQLException exception) { exception.printStackTrace(); } } + /** + * Deletes all {@link Treatment}s which are older than allowed. + */ + private void deleteOldTreatments() { + LocalDate today = LocalDate.now(); + + for (Treatment treatment : treatments) { + LocalDate deleteDate = treatment.calculateDeleteDate(); + if (today.isEqual(deleteDate) || today.isAfter(deleteDate)) { + try { + treatmentDao.delete(treatment.getId()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + } + } + @FXML private void unlockTreatment() { Treatment selectedItem = this.tableView.getSelectionModel().getSelectedItem(); @@ -103,25 +121,4 @@ public class LockedTreatmentController { readAllAndShowInTableView(); } - @FXML - public void handleDelete() { - Treatment selecteditem; - LocalDate today = LocalDate.now(); - LocalDate deleteDate; - - for (Treatment treatment : treatments) { - selecteditem = treatment; - deleteDate = selecteditem.calculateDeleteDate(); - if (today.isEqual(deleteDate) || today.isAfter(deleteDate)) { - try { - treatmentDao.delete(selecteditem.getId()); - } catch (SQLException exception) { - exception.printStackTrace(); - } - } - } - readAllAndShowInTableView(); - } - - } diff --git a/src/main/java/de/hitec/nhplus/treatment/Treatment.java b/src/main/java/de/hitec/nhplus/treatment/Treatment.java index c2ce7ff..42150e3 100644 --- a/src/main/java/de/hitec/nhplus/treatment/Treatment.java +++ b/src/main/java/de/hitec/nhplus/treatment/Treatment.java @@ -1,6 +1,5 @@ package de.hitec.nhplus.treatment; -import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.nurse.Nurse; import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.utils.DateConverter; @@ -11,7 +10,6 @@ import javafx.beans.property.SimpleStringProperty; import java.time.LocalDate; import java.time.LocalTime; -import java.util.List; import java.util.StringJoiner; /** @@ -32,7 +30,7 @@ public class Treatment { private final SimpleStringProperty description; private final SimpleStringProperty remarks; private final SimpleBooleanProperty locked; - private final int sperrfrist = 10; + private final static int BLOCKED_YEARS = 10; /** * This constructor allows instantiating a {@link Treatment} object, @@ -85,6 +83,13 @@ public class Treatment { this.locked = new SimpleBooleanProperty(isLocked); } + /** + * Calculates delete date of treatment. + */ + public LocalDate calculateDeleteDate() { + return DateConverter.convertStringToLocalDate(getDate()).plusYears(BLOCKED_YEARS); + } + public boolean isLocked() { return locked.get(); } @@ -188,13 +193,8 @@ public class Treatment { .add("End: " + this.getEnd()) .add("Description: " + this.getDescription()) .add("Remarks: " + this.getRemarks()) + .add("isLocked: " + this.isLocked()) .toString(); } - /** - * Calculates delete date of treatment. - */ - public LocalDate calculateDeleteDate() { - return DateConverter.convertStringToLocalDate(getDate()).plusYears(sperrfrist); - } } diff --git a/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java index a02a485..7b05981 100644 --- a/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java @@ -30,62 +30,22 @@ public class TreatmentDao extends DaoImp { super(connection); } - @Override - protected PreparedStatement getCreateStatement(Treatment treatment) throws SQLException { - final String SQL = """ - INSERT INTO treatment - (patientId, nurseId, date, begin, end, description, remark, isLocked) - VALUES (?, ?, ?, ?, ?, ?, ?, ?) - """; - PreparedStatement statement = this.connection.prepareStatement(SQL); - statement.setInt(1, treatment.getPatient().getId()); - statement.setInt(2, treatment.getNurse().getId()); - statement.setString(3, treatment.getDate()); - statement.setString(4, treatment.getBegin()); - statement.setString(5, treatment.getEnd()); - statement.setString(6, treatment.getDescription()); - statement.setString(7, treatment.getRemarks()); - statement.setBoolean(8,treatment.isLocked()); - return statement; + /** + * Read all database entries of active {@link Treatment}s into a {@link List} of model instances. + */ + public List readAllActive() throws SQLException { + final String SQL = "SELECT * FROM treatment WHERE isLocked = false"; + return getListFromResultSet(this.connection.prepareStatement(SQL).executeQuery()); } - @Override - protected PreparedStatement getReadByIDStatement(int id) throws SQLException { - final String SQL = "SELECT * FROM treatment WHERE id = ?"; - PreparedStatement statement = this.connection.prepareStatement(SQL); - statement.setInt(1, id); - return statement; + /** + * Read all database entries of locked {@link Treatment}s into a {@link Treatment} of model instances. + */ + public List readAllLocked() throws SQLException { + final String SQL = "SELECT * FROM treatment WHERE isLocked = true"; + return getListFromResultSet(this.connection.prepareStatement(SQL).executeQuery()); } - @Override - protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException { - return new Treatment( - result.getInt(1), - DaoFactory.getInstance().createPatientDAO().read(result.getInt(2)), - DaoFactory.getInstance().createNurseDAO().read(result.getInt(3)), - DateConverter.convertStringToLocalDate(result.getString(4)), - DateConverter.convertStringToLocalTime(result.getString(5)), - DateConverter.convertStringToLocalTime(result.getString(6)), - result.getString(7), - result.getString(8), - false - ); - } - - @Override - protected PreparedStatement getReadAllStatement() throws SQLException { - final String SQL = "SELECT * FROM treatment where isLocked = false"; - return this.connection.prepareStatement(SQL); - } - - @Override - protected List getListFromResultSet(ResultSet result) throws SQLException { - ArrayList list = new ArrayList<>(); - while (result.next()) { - list.add(getInstanceFromResultSet(result)); - } - return list; - } /** * Retrieves a list of {@link Treatment}s associated with a specific @@ -117,6 +77,63 @@ public class TreatmentDao extends DaoImp { return getListFromResultSet(result); } + @Override + protected PreparedStatement getCreateStatement(Treatment treatment) throws SQLException { + final String SQL = """ + INSERT INTO treatment + (patientId, nurseId, date, begin, end, description, remark, isLocked) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, treatment.getPatient().getId()); + statement.setInt(2, treatment.getNurse().getId()); + statement.setString(3, treatment.getDate()); + statement.setString(4, treatment.getBegin()); + statement.setString(5, treatment.getEnd()); + statement.setString(6, treatment.getDescription()); + statement.setString(7, treatment.getRemarks()); + statement.setBoolean(8, treatment.isLocked()); + return statement; + } + + @Override + protected PreparedStatement getReadByIDStatement(int id) throws SQLException { + final String SQL = "SELECT * FROM treatment WHERE id = ?"; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement; + } + + @Override + protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException { + return new Treatment( + result.getInt(1), + DaoFactory.getInstance().createPatientDAO().read(result.getInt(2)), + DaoFactory.getInstance().createNurseDAO().read(result.getInt(3)), + DateConverter.convertStringToLocalDate(result.getString(4)), + DateConverter.convertStringToLocalTime(result.getString(5)), + DateConverter.convertStringToLocalTime(result.getString(6)), + result.getString(7), + result.getString(8), + result.getBoolean(9) + ); + } + + @Override + protected PreparedStatement getReadAllStatement() throws SQLException { + final String SQL = "SELECT * FROM treatment"; + return this.connection.prepareStatement(SQL); + } + + @Override + protected List getListFromResultSet(ResultSet result) throws SQLException { + ArrayList list = new ArrayList<>(); + while (result.next()) { + list.add(getInstanceFromResultSet(result)); + } + return list; + } + @Override protected PreparedStatement getUpdateStatement(Treatment treatment) throws SQLException { final String SQL = """ @@ -135,7 +152,7 @@ public class TreatmentDao extends DaoImp { statement.setString(3, treatment.getEnd()); statement.setString(4, treatment.getDescription()); statement.setString(5, treatment.getRemarks()); - statement.setBoolean(6,treatment.isLocked()); + statement.setBoolean(6, treatment.isLocked()); statement.setInt(7, treatment.getId()); return statement; } @@ -149,11 +166,4 @@ public class TreatmentDao extends DaoImp { return statement; } - /** - * Returns all locked treatments. - */ - public List readAllLocked() throws SQLException { - final String SQL = "SELECT * FROM treatment WHERE isLocked = true"; - return getListFromResultSet(this.connection.prepareStatement(SQL).executeQuery()); - } } diff --git a/src/main/resources/de/hitec/nhplus/treatment/LockedTreatmentView.fxml b/src/main/resources/de/hitec/nhplus/treatment/LockedTreatmentView.fxml index 6f501c8..2680436 100644 --- a/src/main/resources/de/hitec/nhplus/treatment/LockedTreatmentView.fxml +++ b/src/main/resources/de/hitec/nhplus/treatment/LockedTreatmentView.fxml @@ -40,9 +40,8 @@ fx:id="columnDeleteDate" minWidth="40.0" prefWidth="102.0" - text="Geloescht am" + text="Löschung am" /> -