From c71d3e128b18252fc98bf332b384083e84c2770d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Sun, 5 May 2024 11:33:32 +0200 Subject: [PATCH 1/6] #32: Refactor DAO Code, without changing the Structure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .../hitec/nhplus/datastorage/DaoFactory.java | 2 +- .../de/hitec/nhplus/datastorage/DaoImp.java | 16 +++++++-------- .../hitec/nhplus/fixtures/NurseFixture.java | 2 +- .../hitec/nhplus/fixtures/PatientFixture.java | 2 +- .../nhplus/fixtures/TreatmentFixture.java | 2 +- .../nhplus/nurse/AllNurseController.java | 2 +- .../java/de/hitec/nhplus/nurse/NurseDao.java | 3 ++- .../nhplus/patient/AllPatientController.java | 4 ++-- .../de/hitec/nhplus/patient/PatientDao.java | 3 ++- .../treatment/AllTreatmentController.java | 16 +++++++-------- .../de/hitec/nhplus/treatment/Treatment.java | 17 ++++++++-------- .../hitec/nhplus/treatment/TreatmentDao.java | 20 ++++++------------- 12 files changed, 42 insertions(+), 47 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java index a109f59..a46df91 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java @@ -11,7 +11,7 @@ public class DaoFactory { private DaoFactory() { } - public static DaoFactory getDaoFactory() { + public static DaoFactory getInstance() { if (DaoFactory.instance == null) { DaoFactory.instance = new DaoFactory(); } diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java index 59e001c..91f902c 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java @@ -17,9 +17,9 @@ public abstract class DaoImp implements Dao { } @Override - public T read(int key) throws SQLException { + public T read(int id) throws SQLException { T object = null; - ResultSet result = getReadByIDStatement(key).executeQuery(); + ResultSet result = getReadByIDStatement(id).executeQuery(); if (result.next()) { object = getInstanceFromResultSet(result); } @@ -37,21 +37,21 @@ public abstract class DaoImp implements Dao { } @Override - public void deleteById(int key) throws SQLException { - getDeleteStatement(key).executeUpdate(); + public void deleteById(int id) throws SQLException { + getDeleteStatement(id).executeUpdate(); } - protected abstract T getInstanceFromResultSet(ResultSet set) throws SQLException; + protected abstract T getInstanceFromResultSet(ResultSet result) throws SQLException; - protected abstract ArrayList getListFromResultSet(ResultSet set) throws SQLException; + protected abstract List getListFromResultSet(ResultSet result) throws SQLException; protected abstract PreparedStatement getCreateStatement(T t); - protected abstract PreparedStatement getReadByIDStatement(int key); + protected abstract PreparedStatement getReadByIDStatement(int id); protected abstract PreparedStatement getReadAllStatement(); protected abstract PreparedStatement getUpdateStatement(T t); - protected abstract PreparedStatement getDeleteStatement(int key); + protected abstract PreparedStatement getDeleteStatement(int id); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java index 0ec5bfe..c7a51ee 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java @@ -52,7 +52,7 @@ public class NurseFixture implements Fixture { "Armout", "9876543210" )); - NurseDao dao = DaoFactory.getDaoFactory().createNurseDAO(); + NurseDao dao = DaoFactory.getInstance().createNurseDAO(); for(Nurse nurse : nurses){ dao.create(nurse); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java index 44c48ad..cfd5c4e 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java @@ -96,7 +96,7 @@ public class PatientFixture implements Fixture "110" )); - PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO(); + PatientDao dao = DaoFactory.getInstance().createPatientDAO(); for (Patient patient : patients){ dao.create(patient); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java index 78eeab7..2e418e9 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java @@ -165,7 +165,7 @@ public class TreatmentFixture implements Fixture "Massage der Extremitäten zur Verbesserung der Durchblutung") ); - TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); + TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); Map treatmentsById = new HashMap<>(); for (Treatment treatment : treatments){ dao.create(treatment); diff --git a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java index 7f7cfbb..2b3b412 100644 --- a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java +++ b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java @@ -73,7 +73,7 @@ public class AllNurseController { private void readAllAndShowInTableView(){ this.nurses.clear(); - this.dao = DaoFactory.getDaoFactory().createNurseDAO(); + this.dao = DaoFactory.getInstance().createNurseDAO(); try { this.nurses.addAll(this.dao.readAll()); }catch (SQLException exception){ diff --git a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java b/src/main/java/de/hitec/nhplus/nurse/NurseDao.java index e8d7419..f7bbbc6 100644 --- a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/NurseDao.java @@ -7,6 +7,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; public class NurseDao extends DaoImp { public NurseDao(Connection connection) { @@ -65,7 +66,7 @@ public class NurseDao extends DaoImp { } @Override - protected ArrayList getListFromResultSet(ResultSet result) throws SQLException { + protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); while (result.next()) { list.add(new Nurse( diff --git a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java index 7055619..b11a04d 100644 --- a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java @@ -174,7 +174,7 @@ public class AllPatientController { private void readAllAndShowInTableView() { this.patients.clear(); - this.dao = DaoFactory.getDaoFactory().createPatientDAO(); + this.dao = DaoFactory.getInstance().createPatientDAO(); try { this.patients.addAll(this.dao.readAll()); } catch (SQLException exception) { @@ -187,7 +187,7 @@ public class AllPatientController { Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem(); if (selectedItem != null) { try { - DaoFactory.getDaoFactory().createPatientDAO().deleteById(selectedItem.getId()); + DaoFactory.getInstance().createPatientDAO().deleteById(selectedItem.getId()); this.tableView.getItems().remove(selectedItem); } catch (SQLException exception) { exception.printStackTrace(); diff --git a/src/main/java/de/hitec/nhplus/patient/PatientDao.java b/src/main/java/de/hitec/nhplus/patient/PatientDao.java index 71b3502..0648164 100644 --- a/src/main/java/de/hitec/nhplus/patient/PatientDao.java +++ b/src/main/java/de/hitec/nhplus/patient/PatientDao.java @@ -8,6 +8,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; public class PatientDao extends DaoImp { @@ -70,7 +71,7 @@ public class PatientDao extends DaoImp { } @Override - protected ArrayList getListFromResultSet(ResultSet result) throws SQLException { + protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); while (result.next()) { Patient patient = new Patient( diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index c878133..52313c5 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 { public void readAllAndShowInTableView() { comboBoxPatientSelection.getSelectionModel().select(0); - this.dao = DaoFactory.getDaoFactory().createTreatmentDao(); + this.dao = DaoFactory.getInstance().createTreatmentDao(); try { this.treatments.setAll(dao.readAll()); } catch (SQLException exception) { @@ -87,7 +87,7 @@ public class AllTreatmentController { } private void createComboBoxData() { - PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO(); + PatientDao dao = DaoFactory.getInstance().createPatientDAO(); try { patientList = (ArrayList) dao.readAll(); this.patientSelection.add("alle"); @@ -104,7 +104,7 @@ public class AllTreatmentController { public void handleComboBox() { String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem(); this.treatments.clear(); - this.dao = DaoFactory.getDaoFactory().createTreatmentDao(); + this.dao = DaoFactory.getInstance().createTreatmentDao(); if (selectedPatient.equals("alle")) { try { @@ -137,7 +137,7 @@ public class AllTreatmentController { public void handleDelete() { int index = this.tableView.getSelectionModel().getSelectedIndex(); Treatment t = this.treatments.remove(index); - TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); + TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); try { dao.deleteById(t.getId()); } catch (SQLException exception) { @@ -209,12 +209,12 @@ public class AllTreatmentController { Stage stage = new Stage(); TreatmentModalController controller = loader.getController(); - PatientDao pDao = DaoFactory.getDaoFactory().createPatientDAO(); + PatientDao pDao = DaoFactory.getInstance().createPatientDAO(); controller.initialize( this, stage, treatment, - pDao.read(treatment.getPatientId()) + pDao.read(treatment.getPatient().getId()) ); stage.setScene(scene); @@ -228,7 +228,7 @@ public class AllTreatmentController { } public void createTreatment(Treatment treatment) { - TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); + TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); try { dao.create(treatment); } catch (SQLException exception) { @@ -237,7 +237,7 @@ public class AllTreatmentController { } public void updateTreatment(Treatment treatment) { - TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); + TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); try { dao.update(treatment); } catch (SQLException exception) { diff --git a/src/main/java/de/hitec/nhplus/treatment/Treatment.java b/src/main/java/de/hitec/nhplus/treatment/Treatment.java index 87f0171..fb15f06 100644 --- a/src/main/java/de/hitec/nhplus/treatment/Treatment.java +++ b/src/main/java/de/hitec/nhplus/treatment/Treatment.java @@ -1,5 +1,6 @@ package de.hitec.nhplus.treatment; +import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.utils.DateConverter; import java.time.LocalDate; @@ -8,16 +9,16 @@ import java.util.StringJoiner; public class Treatment { private int id; - private final int patientId; + private final Patient patient; private LocalDate date; private LocalTime begin; private LocalTime end; private String description; private String remarks; - public Treatment(int patientId, LocalDate date, LocalTime begin, + public Treatment(Patient patient, LocalDate date, LocalTime begin, LocalTime end, String description, String remarks) { - this.patientId = patientId; + this.patient = patient; this.date = date; this.begin = begin; this.end = end; @@ -25,10 +26,10 @@ public class Treatment { this.remarks = remarks; } - public Treatment(int id, int patientId, LocalDate date, LocalTime begin, + public Treatment(int id, Patient patient, LocalDate date, LocalTime begin, LocalTime end, String description, String remarks) { this.id = id; - this.patientId = patientId; + this.patient = patient; this.date = date; this.begin = begin; this.end = end; @@ -40,8 +41,8 @@ public class Treatment { return id; } - public int getPatientId() { - return this.patientId; + public Patient getPatient() { + return this.patient; } public String getDate() { @@ -88,7 +89,7 @@ public class Treatment { return new StringJoiner(System.lineSeparator()) .add("TREATMENT") .add("ID: " + this.getId()) - .add("PatientID: " + this.getPatientId()) + .add("Patient: " + this.getPatient().getSurname() + " " + this.getPatient().getFirstName()) .add("Date: " + this.getDate()) .add("Begin: " + this.getBegin()) .add("End: " + this.getEnd()) diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java index fee4640..3521e7e 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java @@ -1,5 +1,6 @@ package de.hitec.nhplus.treatment; +import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoImp; import de.hitec.nhplus.utils.DateConverter; @@ -23,7 +24,7 @@ public class TreatmentDao extends DaoImp { final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " + "VALUES (?, ?, ?, ?, ?, ?)"; preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, treatment.getPatientId()); + preparedStatement.setInt(1, treatment.getPatient().getId()); preparedStatement.setString(2, treatment.getDate()); preparedStatement.setString(3, treatment.getBegin()); preparedStatement.setString(4, treatment.getEnd()); @@ -52,7 +53,7 @@ public class TreatmentDao extends DaoImp { protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException { return new Treatment( result.getInt(1), - result.getInt(2), + DaoFactory.getInstance().createPatientDAO().read(result.getInt(2)), DateConverter.convertStringToLocalDate(result.getString(3)), DateConverter.convertStringToLocalTime(result.getString(4)), DateConverter.convertStringToLocalTime(result.getString(5)), @@ -74,19 +75,10 @@ public class TreatmentDao extends DaoImp { } @Override - protected ArrayList getListFromResultSet(ResultSet result) throws SQLException { + protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); while (result.next()) { - Treatment treatment = new Treatment( - result.getInt(1), - result.getInt(2), - DateConverter.convertStringToLocalDate(result.getString(3)), - DateConverter.convertStringToLocalTime(result.getString(4)), - DateConverter.convertStringToLocalTime(result.getString(5)), - result.getString(6), - result.getString(7) - ); - list.add(treatment); + list.add(getInstanceFromResultSet(result)); } return list; } @@ -122,7 +114,7 @@ public class TreatmentDao extends DaoImp { "remark = ? " + "WHERE id = ?"; preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, treatment.getPatientId()); + preparedStatement.setInt(1, treatment.getPatient().getId()); preparedStatement.setString(2, treatment.getDate()); preparedStatement.setString(3, treatment.getBegin()); preparedStatement.setString(4, treatment.getEnd()); From c01960cfd9f154ee768bc939568aa7a043e59658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Sun, 5 May 2024 11:59:15 +0200 Subject: [PATCH 2/6] #32: Cleanup Useless Line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .../de/hitec/nhplus/datastorage/DaoImp.java | 16 +- .../nhplus/fixtures/TreatmentFixture.java | 197 +++++++++--------- .../java/de/hitec/nhplus/nurse/NurseDao.java | 93 +++------ .../de/hitec/nhplus/patient/PatientDao.java | 107 ++++------ .../hitec/nhplus/treatment/TreatmentDao.java | 119 ++++------- .../treatment/TreatmentModalController.java | 2 +- 6 files changed, 216 insertions(+), 318 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java index 91f902c..632e21e 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java @@ -1,7 +1,9 @@ package de.hitec.nhplus.datastorage; -import java.sql.*; -import java.util.ArrayList; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.List; public abstract class DaoImp implements Dao { @@ -45,13 +47,13 @@ public abstract class DaoImp implements Dao { protected abstract List getListFromResultSet(ResultSet result) throws SQLException; - protected abstract PreparedStatement getCreateStatement(T t); + protected abstract PreparedStatement getCreateStatement(T t) throws SQLException; - protected abstract PreparedStatement getReadByIDStatement(int id); + protected abstract PreparedStatement getReadByIDStatement(int id) throws SQLException; - protected abstract PreparedStatement getReadAllStatement(); + protected abstract PreparedStatement getReadAllStatement() throws SQLException; - protected abstract PreparedStatement getUpdateStatement(T t); + protected abstract PreparedStatement getUpdateStatement(T t) throws SQLException; - protected abstract PreparedStatement getDeleteStatement(int id); + protected abstract PreparedStatement getDeleteStatement(int id) throws SQLException; } diff --git a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java index 2e418e9..e63aef4 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java @@ -1,9 +1,9 @@ package de.hitec.nhplus.fixtures; import de.hitec.nhplus.datastorage.DaoFactory; -import de.hitec.nhplus.treatment.TreatmentDao; import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.treatment.Treatment; +import de.hitec.nhplus.treatment.TreatmentDao; import java.sql.Connection; import java.sql.SQLException; @@ -16,53 +16,44 @@ import java.util.Map; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalTime; -public class TreatmentFixture implements Fixture -{ +public class TreatmentFixture implements Fixture { private final Map patientsByName; - public TreatmentFixture(Map patientsByName) - { + public TreatmentFixture(Map patientsByName) { this.patientsByName = patientsByName; } @Override - public void dropTable(Connection connection) - { - try (Statement statement = connection.createStatement()) - { + public void dropTable(Connection connection) { + try (Statement statement = connection.createStatement()) { statement.execute("DROP TABLE treatment"); - } catch (SQLException exception) - { + } catch (SQLException exception) { System.out.println(exception.getMessage()); } } @Override - public void setupTable(Connection connection) - { + public void setupTable(Connection connection) { final String SQL = "CREATE TABLE IF NOT EXISTS treatment (" + - " id INTEGER PRIMARY KEY AUTOINCREMENT, " + - " patientId INTEGER NOT NULL, " + - " treatment_date TEXT NOT NULL, " + - " begin TEXT NOT NULL, " + - " end TEXT NOT NULL, " + - " description TEXT NOT NULL, " + - " remark TEXT NOT NULL," + - " FOREIGN KEY (patientId) REFERENCES patient (id) ON DELETE CASCADE " + - ");"; + " id INTEGER PRIMARY KEY AUTOINCREMENT, " + + " patientId INTEGER NOT NULL, " + + " treatment_date TEXT NOT NULL, " + + " begin TEXT NOT NULL, " + + " end TEXT NOT NULL, " + + " description TEXT NOT NULL, " + + " remark TEXT NOT NULL," + + " FOREIGN KEY (patientId) REFERENCES patient (id) ON DELETE CASCADE " + + ");"; - try (Statement statement = connection.createStatement()) - { + try (Statement statement = connection.createStatement()) { statement.execute(SQL); - } catch (SQLException exception) - { + } catch (SQLException exception) { System.out.println(exception.getMessage()); } } @Override - public Map load() throws SQLException - { + public Map load() throws SQLException { List treatments = new ArrayList<>(); Patient seppl = patientsByName.get("Seppl"); @@ -72,102 +63,102 @@ public class TreatmentFixture implements Fixture Patient elisabeth = patientsByName.get("Elisabeth"); treatments.add(new Treatment( - 1, - seppl.getId(), - convertStringToLocalDate("2023-06-03"), - convertStringToLocalTime("11:00"), - convertStringToLocalTime("15:00"), - "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." + 1, + seppl, + convertStringToLocalDate("2023-06-03"), + convertStringToLocalTime("11:00"), + convertStringToLocalTime("15:00"), + "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." )); treatments.add(new Treatment( - 2, - seppl.getId(), - convertStringToLocalDate("2023-06-05"), - convertStringToLocalTime("11:00"), - convertStringToLocalTime("12:30"), - "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." + 2, + seppl, + convertStringToLocalDate("2023-06-05"), + convertStringToLocalTime("11:00"), + convertStringToLocalTime("12:30"), + "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." )); treatments.add(new Treatment( - 3, - martina.getId(), - convertStringToLocalDate("2023-06-04"), - convertStringToLocalTime("07:30"), - convertStringToLocalTime("08:00"), - "Waschen", - "Patient mit Waschlappen gewaschen und frisch angezogen. Patient gewendet." + 3, + martina, + convertStringToLocalDate("2023-06-04"), + convertStringToLocalTime("07:30"), + convertStringToLocalTime("08:00"), + "Waschen", + "Patient mit Waschlappen gewaschen und frisch angezogen. Patient gewendet." )); treatments.add(new Treatment( - 4, - seppl.getId(), - convertStringToLocalDate("2023-06-06"), - convertStringToLocalTime("15:10"), - convertStringToLocalTime("16:00"), - "Spaziergang", - "Spaziergang im Park, Patient döst im Rollstuhl ein") + 4, + seppl, + convertStringToLocalDate("2023-06-06"), + convertStringToLocalTime("15:10"), + convertStringToLocalTime("16:00"), + "Spaziergang", + "Spaziergang im Park, Patient döst im Rollstuhl ein") ); treatments.add(new Treatment( - 8, - seppl.getId(), - 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") + 8, + seppl, + 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") ); treatments.add(new Treatment( - 9, - martina.getId(), - convertStringToLocalDate("2023-06-07"), - convertStringToLocalTime("11:00"), - convertStringToLocalTime("11:30"), - "Waschen", - "Waschen per Dusche auf einem Stuhl; Patientin gewendet;") + 9, + martina, + convertStringToLocalDate("2023-06-07"), + convertStringToLocalTime("11:00"), + convertStringToLocalTime("11:30"), + "Waschen", + "Waschen per Dusche auf einem Stuhl; Patientin gewendet;") ); treatments.add(new Treatment( - 12, - hans.getId(), - convertStringToLocalDate("2023-06-08"), - convertStringToLocalTime("15:00"), - convertStringToLocalTime("15:30"), - "Physiotherapie", - "Übungen zur Stabilisation und Mobilisierung der Rückenmuskulatur") + 12, + hans, + convertStringToLocalDate("2023-06-08"), + convertStringToLocalTime("15:00"), + convertStringToLocalTime("15:30"), + "Physiotherapie", + "Übungen zur Stabilisation und Mobilisierung der Rückenmuskulatur") ); treatments.add(new Treatment( - 14, - ahmet.getId(), - convertStringToLocalDate("2023-08-24"), - convertStringToLocalTime("09:30"), - convertStringToLocalTime("10:15"), - "KG", - "Lympfdrainage")); + 14, + ahmet, + convertStringToLocalDate("2023-08-24"), + convertStringToLocalTime("09:30"), + convertStringToLocalTime("10:15"), + "KG", + "Lympfdrainage")); treatments.add(new Treatment( - 16, - elisabeth.getId(), - convertStringToLocalDate("2023-08-31"), - convertStringToLocalTime("13:30"), - convertStringToLocalTime("13:45"), - "Toilettengang", - "Hilfe beim Toilettengang; Patientin klagt über Schmerzen beim Stuhlgang. Gabe von Iberogast") + 16, + elisabeth, + convertStringToLocalDate("2023-08-31"), + convertStringToLocalTime("13:30"), + convertStringToLocalTime("13:45"), + "Toilettengang", + "Hilfe beim Toilettengang; Patientin klagt über Schmerzen beim Stuhlgang. Gabe von Iberogast") ); treatments.add(new Treatment( - 17, - elisabeth.getId(), - convertStringToLocalDate("2023-09-01"), - convertStringToLocalTime("16:00"), - convertStringToLocalTime("17:00"), - "KG", - "Massage der Extremitäten zur Verbesserung der Durchblutung") + 17, + elisabeth, + convertStringToLocalDate("2023-09-01"), + convertStringToLocalTime("16:00"), + convertStringToLocalTime("17:00"), + "KG", + "Massage der Extremitäten zur Verbesserung der Durchblutung") ); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); Map treatmentsById = new HashMap<>(); - for (Treatment treatment : treatments){ + for (Treatment treatment : treatments) { dao.create(treatment); treatmentsById.put(String.valueOf(treatment.getId()), treatment); } diff --git a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java b/src/main/java/de/hitec/nhplus/nurse/NurseDao.java index f7bbbc6..1af8329 100644 --- a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/NurseDao.java @@ -15,31 +15,21 @@ public class NurseDao extends DaoImp { } @Override - protected PreparedStatement getCreateStatement(Nurse nurse) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "INSERT INTO nurse (firstname, surname, phoneNumber)" + - "VALUES (?, ?, ?)"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, nurse.getFirstName()); - preparedStatement.setString(2, nurse.getSurname()); - preparedStatement.setString(3, nurse.getPhoneNumber()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getCreateStatement(Nurse nurse) throws SQLException { + final String SQL = "INSERT INTO nurse (firstname, surname, phoneNumber)" + + "VALUES (?, ?, ?)"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setString(1, nurse.getFirstName()); + preparedStatement.setString(2, nurse.getSurname()); + preparedStatement.setString(3, nurse.getPhoneNumber()); return preparedStatement; } @Override - protected PreparedStatement getReadByIDStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM nurse WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getReadByIDStatement(int id) throws SQLException { + final String SQL = "SELECT * FROM nurse WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); return preparedStatement; } @@ -54,63 +44,42 @@ public class NurseDao extends DaoImp { } @Override - protected PreparedStatement getReadAllStatement() { - PreparedStatement statement = null; - try { - final String SQL = "SELECT * FROM nurse"; - statement = this.connection.prepareStatement(SQL); - } catch (SQLException exception) { - exception.printStackTrace(); - } - return statement; + protected PreparedStatement getReadAllStatement() throws SQLException { + final String SQL = "SELECT * FROM nurse"; + return this.connection.prepareStatement(SQL); } @Override protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); while (result.next()) { - list.add(new Nurse( - result.getInt(1), - result.getString(2), - result.getString(3), - result.getString(4) - )); + list.add(getInstanceFromResultSet(result)); } return list; } @Override - protected PreparedStatement getUpdateStatement(Nurse nurse) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "UPDATE nurse SET " + - "firstname = ?, " + - "surname = ?, " + - "phoneNumber = ?, " + - "WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, nurse.getFirstName()); - preparedStatement.setString(2, nurse.getSurname()); - preparedStatement.setString(3, nurse.getPhoneNumber()); - preparedStatement.setInt(4, nurse.getId()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getUpdateStatement(Nurse nurse) throws SQLException { + final String SQL = + "UPDATE nurse SET " + + "firstname = ?, " + + "surname = ?, " + + "phoneNumber = ? " + + "WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setString(1, nurse.getFirstName()); + preparedStatement.setString(2, nurse.getSurname()); + preparedStatement.setString(3, nurse.getPhoneNumber()); + preparedStatement.setInt(4, nurse.getId()); return preparedStatement; } @Override - protected PreparedStatement getDeleteStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "DELETE FROM nurse WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getDeleteStatement(int id) throws SQLException { + final String SQL = "DELETE FROM nurse WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); return preparedStatement; } } diff --git a/src/main/java/de/hitec/nhplus/patient/PatientDao.java b/src/main/java/de/hitec/nhplus/patient/PatientDao.java index 0648164..a4a779f 100644 --- a/src/main/java/de/hitec/nhplus/patient/PatientDao.java +++ b/src/main/java/de/hitec/nhplus/patient/PatientDao.java @@ -17,33 +17,23 @@ public class PatientDao extends DaoImp { } @Override - protected PreparedStatement getCreateStatement(Patient patient) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "INSERT INTO patient (firstname, surname, dateOfBirth, carelevel, roomnumber) " + - "VALUES (?, ?, ?, ?, ?)"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, patient.getFirstName()); - preparedStatement.setString(2, patient.getSurname()); - preparedStatement.setString(3, patient.getDateOfBirth()); - preparedStatement.setString(4, patient.getCareLevel()); - preparedStatement.setString(5, patient.getRoomNumber()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getCreateStatement(Patient patient) throws SQLException { + final String SQL = "INSERT INTO patient (firstname, surname, dateOfBirth, carelevel, roomnumber) " + + "VALUES (?, ?, ?, ?, ?)"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setString(1, patient.getFirstName()); + preparedStatement.setString(2, patient.getSurname()); + preparedStatement.setString(3, patient.getDateOfBirth()); + preparedStatement.setString(4, patient.getCareLevel()); + preparedStatement.setString(5, patient.getRoomNumber()); return preparedStatement; } @Override - protected PreparedStatement getReadByIDStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM patient WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getReadByIDStatement(int id) throws SQLException { + final String SQL = "SELECT * FROM patient WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); return preparedStatement; } @@ -59,68 +49,45 @@ public class PatientDao extends DaoImp { } @Override - protected PreparedStatement getReadAllStatement() { - PreparedStatement statement = null; - try { - final String SQL = "SELECT * FROM patient"; - statement = this.connection.prepareStatement(SQL); - } catch (SQLException exception) { - exception.printStackTrace(); - } - return statement; + protected PreparedStatement getReadAllStatement() throws SQLException { + final String SQL = "SELECT * FROM patient"; + return this.connection.prepareStatement(SQL); } @Override protected List getListFromResultSet(ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); while (result.next()) { - Patient patient = new Patient( - result.getInt(1), - result.getString(2), - result.getString(3), - DateConverter.convertStringToLocalDate(result.getString(4)), - result.getString(5), - result.getString(6)); - list.add(patient); + list.add(getInstanceFromResultSet(result)); } return list; } @Override - protected PreparedStatement getUpdateStatement(Patient patient) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "UPDATE patient SET " + - "firstname = ?, " + - "surname = ?, " + - "dateOfBirth = ?, " + - "carelevel = ?, " + - "roomnumber = ?, " + - "WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, patient.getFirstName()); - preparedStatement.setString(2, patient.getSurname()); - preparedStatement.setString(3, patient.getDateOfBirth()); - preparedStatement.setString(4, patient.getCareLevel()); - preparedStatement.setString(5, patient.getRoomNumber()); - preparedStatement.setInt(6, patient.getId()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getUpdateStatement(Patient patient) throws SQLException { + final String SQL = + "UPDATE patient SET " + + "firstname = ?, " + + "surname = ?, " + + "dateOfBirth = ?, " + + "carelevel = ?, " + + "roomnumber = ?, " + + "WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setString(1, patient.getFirstName()); + preparedStatement.setString(2, patient.getSurname()); + preparedStatement.setString(3, patient.getDateOfBirth()); + preparedStatement.setString(4, patient.getCareLevel()); + preparedStatement.setString(5, patient.getRoomNumber()); + preparedStatement.setInt(6, patient.getId()); return preparedStatement; } @Override - protected PreparedStatement getDeleteStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "DELETE FROM patient WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getDeleteStatement(int id) throws SQLException { + final String SQL = "DELETE FROM patient WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); return preparedStatement; } } diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java index 3521e7e..710b6ad 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java @@ -18,34 +18,24 @@ public class TreatmentDao extends DaoImp { } @Override - protected PreparedStatement getCreateStatement(Treatment treatment) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " + - "VALUES (?, ?, ?, ?, ?, ?)"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, treatment.getPatient().getId()); - preparedStatement.setString(2, treatment.getDate()); - preparedStatement.setString(3, treatment.getBegin()); - preparedStatement.setString(4, treatment.getEnd()); - preparedStatement.setString(5, treatment.getDescription()); - preparedStatement.setString(6, treatment.getRemarks()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getCreateStatement(Treatment treatment) throws SQLException { + final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " + + "VALUES (?, ?, ?, ?, ?, ?)"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, treatment.getPatient().getId()); + preparedStatement.setString(2, treatment.getDate()); + preparedStatement.setString(3, treatment.getBegin()); + preparedStatement.setString(4, treatment.getEnd()); + preparedStatement.setString(5, treatment.getDescription()); + preparedStatement.setString(6, treatment.getRemarks()); return preparedStatement; } @Override - protected PreparedStatement getReadByIDStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM treatment WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getReadByIDStatement(int id) throws SQLException { + final String SQL = "SELECT * FROM treatment WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); return preparedStatement; } @@ -63,15 +53,9 @@ public class TreatmentDao extends DaoImp { } @Override - protected PreparedStatement getReadAllStatement() { - PreparedStatement statement = null; - try { - final String SQL = "SELECT * FROM treatment"; - statement = this.connection.prepareStatement(SQL); - } catch (SQLException exception) { - exception.printStackTrace(); - } - return statement; + protected PreparedStatement getReadAllStatement() throws SQLException { + final String SQL = "SELECT * FROM treatment"; + return this.connection.prepareStatement(SQL); } @Override @@ -83,15 +67,10 @@ public class TreatmentDao extends DaoImp { return list; } - private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) { - PreparedStatement preparedStatement = null; - try { - final String SQL = "SELECT * FROM treatment WHERE patientId = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, patientId); - } catch (SQLException exception) { - exception.printStackTrace(); - } + private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) throws SQLException { + final String SQL = "SELECT * FROM treatment WHERE patientId = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, patientId); return preparedStatement; } @@ -101,43 +80,33 @@ public class TreatmentDao extends DaoImp { } @Override - protected PreparedStatement getUpdateStatement(Treatment treatment) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "UPDATE treatment SET " + - "patientId = ?, " + - "treatment_date = ?, " + - "begin = ?, " + - "end = ?, " + - "description = ?, " + - "remark = ? " + - "WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, treatment.getPatient().getId()); - preparedStatement.setString(2, treatment.getDate()); - preparedStatement.setString(3, treatment.getBegin()); - preparedStatement.setString(4, treatment.getEnd()); - preparedStatement.setString(5, treatment.getDescription()); - preparedStatement.setString(6, treatment.getRemarks()); - preparedStatement.setInt(7, treatment.getId()); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getUpdateStatement(Treatment treatment) throws SQLException { + final String SQL = + "UPDATE treatment SET " + + "patientId = ?, " + + "treatment_date = ?, " + + "begin = ?, " + + "end = ?, " + + "description = ?, " + + "remark = ? " + + "WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, treatment.getPatient().getId()); + preparedStatement.setString(2, treatment.getDate()); + preparedStatement.setString(3, treatment.getBegin()); + preparedStatement.setString(4, treatment.getEnd()); + preparedStatement.setString(5, treatment.getDescription()); + preparedStatement.setString(6, treatment.getRemarks()); + preparedStatement.setInt(7, treatment.getId()); return preparedStatement; } @Override - protected PreparedStatement getDeleteStatement(int id) { - PreparedStatement preparedStatement = null; - try { - final String SQL = - "DELETE FROM treatment WHERE id = ?"; - preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - } catch (SQLException exception) { - exception.printStackTrace(); - } + protected PreparedStatement getDeleteStatement(int id) throws SQLException { + final String SQL = + "DELETE FROM treatment WHERE id = ?"; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); return preparedStatement; } } diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java index 2d61fae..f6e4927 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java @@ -44,7 +44,7 @@ public class TreatmentModalController { isNewTreatment = true; LocalTime currentTime = LocalTime.now(); this.treatment = new Treatment( - patient.getId(), + patient, LocalDate.now(), LocalTime.of(currentTime.getHour(), currentTime.getMinute()), LocalTime.of(currentTime.getHour(), currentTime.getMinute()), From 77b38ef1591ca6b171896f71a081df941a0ab411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Mon, 6 May 2024 08:37:29 +0200 Subject: [PATCH 3/6] #32: Move Schemas to Resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .idea/sqldialects.xml | 5 +- .../hitec/nhplus/datastorage/DaoFactory.java | 6 +- .../de/hitec/nhplus/fixtures/Fixture.java | 4 +- .../hitec/nhplus/fixtures/NurseFixture.java | 39 ++---- .../hitec/nhplus/fixtures/PatientFixture.java | 114 +++++++----------- .../nhplus/fixtures/TreatmentFixture.java | 40 ++---- .../nhplus/nurse/AllNurseController.java | 1 + .../nhplus/nurse/{ => database}/NurseDao.java | 3 +- .../nhplus/patient/AllPatientController.java | 1 + .../patient/{ => database}/PatientDao.java | 3 +- .../treatment/AllTreatmentController.java | 3 +- .../{ => database}/TreatmentDao.java | 3 +- src/main/java/module-info.java | 6 + .../de/hitec/nhplus/nurse/database/Nurse.sql | 7 ++ .../hitec/nhplus/patient/database/Patient.sql | 9 ++ .../nhplus/treatment/database/Treatment.sql | 11 ++ 16 files changed, 121 insertions(+), 134 deletions(-) rename src/main/java/de/hitec/nhplus/nurse/{ => database}/NurseDao.java (97%) rename src/main/java/de/hitec/nhplus/patient/{ => database}/PatientDao.java (97%) rename src/main/java/de/hitec/nhplus/treatment/{ => database}/TreatmentDao.java (98%) create mode 100644 src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql create mode 100644 src/main/resources/de/hitec/nhplus/patient/database/Patient.sql create mode 100644 src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index 25de883..aecddda 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -1,8 +1,9 @@ - - + + + \ No newline at end of file diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java index a46df91..08c2ba7 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java @@ -1,8 +1,8 @@ package de.hitec.nhplus.datastorage; -import de.hitec.nhplus.nurse.NurseDao; -import de.hitec.nhplus.patient.PatientDao; -import de.hitec.nhplus.treatment.TreatmentDao; +import de.hitec.nhplus.nurse.database.NurseDao; +import de.hitec.nhplus.patient.database.PatientDao; +import de.hitec.nhplus.treatment.database.TreatmentDao; public class DaoFactory { diff --git a/src/main/java/de/hitec/nhplus/fixtures/Fixture.java b/src/main/java/de/hitec/nhplus/fixtures/Fixture.java index aaa2124..69d300e 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/Fixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/Fixture.java @@ -6,7 +6,7 @@ import java.util.Map; public interface Fixture { - void dropTable(Connection connection); - void setupTable(Connection connection); + void dropTable(Connection connection) throws SQLException; + void setupTable(Connection connection) throws SQLException; Map load() throws SQLException; } diff --git a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java index c7a51ee..ec36258 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java @@ -1,40 +1,27 @@ package de.hitec.nhplus.fixtures; +import de.hitec.nhplus.Main; import de.hitec.nhplus.datastorage.DaoFactory; -import de.hitec.nhplus.nurse.NurseDao; import de.hitec.nhplus.nurse.Nurse; +import de.hitec.nhplus.nurse.database.NurseDao; +import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class NurseFixture implements Fixture { @Override - public void dropTable(Connection connection) { - try (Statement statement = connection.createStatement()) { - statement.execute("DROP TABLE nurse"); - } catch (SQLException exception) { - System.out.println(exception.getMessage()); - } + public void dropTable(Connection connection) throws SQLException { + connection.createStatement().execute("DROP TABLE nurse"); } @Override - public void setupTable(Connection connection) { - final String SQL = "CREATE TABLE IF NOT EXISTS nurse (" + - "id INTEGER PRIMARY KEY AUTOINCREMENT, " + - "firstname TEXT NOT NULL, " + - "surname TEXT NOT NULL, " + - "phoneNumber TEXT NOT NULL" + - ");"; - try (Statement statement = connection.createStatement()) { - statement.execute(SQL); - } catch (SQLException exception) { - System.out.println(exception.getMessage()); - } + public void setupTable(Connection connection) throws SQLException { + final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/nurse/database/Nurse.sql"); + assert schema != null; + final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); + connection.createStatement().execute(SQL); } @Override @@ -53,11 +40,11 @@ public class NurseFixture implements Fixture { "9876543210" )); NurseDao dao = DaoFactory.getInstance().createNurseDAO(); - for(Nurse nurse : nurses){ + for (Nurse nurse : nurses) { dao.create(nurse); } Map nursesByName = new HashMap<>(); - for (Nurse nurse : dao.readAll()){ + for (Nurse nurse : dao.readAll()) { nursesByName.put(nurse.getFirstName(), nurse); } return nursesByName; diff --git a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java index cfd5c4e..f263258 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java @@ -1,107 +1,85 @@ package de.hitec.nhplus.fixtures; +import de.hitec.nhplus.Main; import de.hitec.nhplus.datastorage.DaoFactory; -import de.hitec.nhplus.patient.PatientDao; import de.hitec.nhplus.patient.Patient; +import de.hitec.nhplus.patient.database.PatientDao; +import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate; -public class PatientFixture implements Fixture -{ +public class PatientFixture implements Fixture { @Override - public void dropTable(Connection connection) - { - try (Statement statement = connection.createStatement()) - { - statement.execute("DROP TABLE patient"); - } catch (SQLException exception) - { - System.out.println(exception.getMessage()); - } + public void dropTable(Connection connection) throws SQLException { + connection.createStatement().execute("DROP TABLE patient"); } @Override - public void setupTable(Connection connection) - { - final String SQL = "CREATE TABLE IF NOT EXISTS patient (" + - " id INTEGER PRIMARY KEY AUTOINCREMENT, " + - " firstname TEXT NOT NULL, " + - " surname TEXT NOT NULL, " + - " dateOfBirth TEXT NOT NULL, " + - " carelevel TEXT NOT NULL, " + - " roomnumber TEXT NOT NULL" + - ");"; - try (Statement statement = connection.createStatement()) - { - statement.execute(SQL); - } catch (SQLException exception) - { - System.out.println(exception.getMessage()); - } + public void setupTable(Connection connection) throws SQLException { + // @SuppressWarnings("checkstyle:LineLength") + final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/patient/database/Patient.sql"); + assert schema != null; + final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); + connection.createStatement().execute(SQL); } @Override - public Map load() throws SQLException - { + public Map load() throws SQLException { List patients = new ArrayList<>(); patients.add(new Patient( - "Seppl", - "Herberger", - convertStringToLocalDate("1945-12-01"), - "4", - "202" + "Seppl", + "Herberger", + convertStringToLocalDate("1945-12-01"), + "4", + "202" )); patients.add(new Patient( - "Martina", - "Gerdsen", - convertStringToLocalDate("1954-08-12"), - "5", - "010" + "Martina", + "Gerdsen", + convertStringToLocalDate("1954-08-12"), + "5", + "010" )); patients.add(new Patient( - "Gertrud", - "Franzen", - convertStringToLocalDate("1949-04-16"), - "3", - "002" + "Gertrud", + "Franzen", + convertStringToLocalDate("1949-04-16"), + "3", + "002" )); patients.add(new Patient( - "Ahmet", - "Yilmaz", - convertStringToLocalDate("1941-02-22"), - "3", - "013" + "Ahmet", + "Yilmaz", + convertStringToLocalDate("1941-02-22"), + "3", + "013" )); patients.add(new Patient( - "Hans", - "Neumann", - convertStringToLocalDate("1955-12-12"), - "2", - "001" + "Hans", + "Neumann", + convertStringToLocalDate("1955-12-12"), + "2", + "001" )); patients.add(new Patient( - "Elisabeth", - "Müller", - convertStringToLocalDate("1958-03-07"), - "5", - "110" + "Elisabeth", + "Müller", + convertStringToLocalDate("1958-03-07"), + "5", + "110" )); PatientDao dao = DaoFactory.getInstance().createPatientDAO(); - for (Patient patient : patients){ + for (Patient patient : patients) { dao.create(patient); } Map patientsByName = new HashMap<>(); - for (Patient patient : dao.readAll()){ + for (Patient patient : dao.readAll()) { patientsByName.put(patient.getFirstName(), patient); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java index e63aef4..bbb83f4 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java @@ -1,17 +1,15 @@ package de.hitec.nhplus.fixtures; +import de.hitec.nhplus.Main; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.treatment.Treatment; -import de.hitec.nhplus.treatment.TreatmentDao; +import de.hitec.nhplus.treatment.database.TreatmentDao; +import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalTime; @@ -24,32 +22,16 @@ public class TreatmentFixture implements Fixture { } @Override - public void dropTable(Connection connection) { - try (Statement statement = connection.createStatement()) { - statement.execute("DROP TABLE treatment"); - } catch (SQLException exception) { - System.out.println(exception.getMessage()); - } + public void dropTable(Connection connection) throws SQLException { + connection.createStatement().execute("DROP TABLE treatment"); } @Override - public void setupTable(Connection connection) { - final String SQL = "CREATE TABLE IF NOT EXISTS treatment (" + - " id INTEGER PRIMARY KEY AUTOINCREMENT, " + - " patientId INTEGER NOT NULL, " + - " treatment_date TEXT NOT NULL, " + - " begin TEXT NOT NULL, " + - " end TEXT NOT NULL, " + - " description TEXT NOT NULL, " + - " remark TEXT NOT NULL," + - " FOREIGN KEY (patientId) REFERENCES patient (id) ON DELETE CASCADE " + - ");"; - - try (Statement statement = connection.createStatement()) { - statement.execute(SQL); - } catch (SQLException exception) { - System.out.println(exception.getMessage()); - } + public void setupTable(Connection connection) throws SQLException { + final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/treatment/database/Treatment.sql"); + assert schema != null; + final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); + connection.createStatement().execute(SQL); } @Override diff --git a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java index 2b3b412..517fb52 100644 --- a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java +++ b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java @@ -3,6 +3,7 @@ package de.hitec.nhplus.nurse; import static de.hitec.nhplus.utils.Validator.*; import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.nurse.database.NurseDao; import javafx.beans.value.ChangeListener; import javafx.collections.FXCollections; import javafx.collections.ObservableList; diff --git a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java similarity index 97% rename from src/main/java/de/hitec/nhplus/nurse/NurseDao.java rename to src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java index 1af8329..7f555da 100644 --- a/src/main/java/de/hitec/nhplus/nurse/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java @@ -1,6 +1,7 @@ -package de.hitec.nhplus.nurse; +package de.hitec.nhplus.nurse.database; import de.hitec.nhplus.datastorage.DaoImp; +import de.hitec.nhplus.nurse.Nurse; import java.sql.Connection; import java.sql.PreparedStatement; diff --git a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java index b11a04d..592223a 100644 --- a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java @@ -1,6 +1,7 @@ package de.hitec.nhplus.patient; import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.patient.database.PatientDao; import de.hitec.nhplus.utils.DateConverter; import javafx.beans.value.ChangeListener; import javafx.collections.FXCollections; diff --git a/src/main/java/de/hitec/nhplus/patient/PatientDao.java b/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java similarity index 97% rename from src/main/java/de/hitec/nhplus/patient/PatientDao.java rename to src/main/java/de/hitec/nhplus/patient/database/PatientDao.java index a4a779f..a15b2f6 100644 --- a/src/main/java/de/hitec/nhplus/patient/PatientDao.java +++ b/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java @@ -1,6 +1,7 @@ -package de.hitec.nhplus.patient; +package de.hitec.nhplus.patient.database; import de.hitec.nhplus.datastorage.DaoImp; +import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.utils.DateConverter; import java.sql.Connection; diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index 52313c5..b0b5f26 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -3,7 +3,8 @@ package de.hitec.nhplus.treatment; import de.hitec.nhplus.Main; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.patient.Patient; -import de.hitec.nhplus.patient.PatientDao; +import de.hitec.nhplus.patient.database.PatientDao; +import de.hitec.nhplus.treatment.database.TreatmentDao; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java similarity index 98% rename from src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java rename to src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java index 710b6ad..fd1756d 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java @@ -1,7 +1,8 @@ -package de.hitec.nhplus.treatment; +package de.hitec.nhplus.treatment.database; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoImp; +import de.hitec.nhplus.treatment.Treatment; import de.hitec.nhplus.utils.DateConverter; import java.sql.Connection; diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index b707e16..84a02fd 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -11,11 +11,17 @@ module de.hitec.nhplus { opens de.hitec.nhplus.main to javafx.base, javafx.fxml; exports de.hitec.nhplus.patient; + exports de.hitec.nhplus.patient.database; + opens de.hitec.nhplus.patient.database to javafx.base, javafx.fxml; opens de.hitec.nhplus.patient to javafx.base, javafx.fxml; exports de.hitec.nhplus.treatment; + exports de.hitec.nhplus.treatment.database; opens de.hitec.nhplus.treatment to javafx.base, javafx.fxml; + opens de.hitec.nhplus.treatment.database to javafx.base, javafx.fxml; exports de.hitec.nhplus.nurse; + exports de.hitec.nhplus.nurse.database; opens de.hitec.nhplus.nurse to javafx.base, javafx.fxml; + opens de.hitec.nhplus.nurse.database to javafx.base, javafx.fxml; } diff --git a/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql b/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql new file mode 100644 index 0000000..d51bbf3 --- /dev/null +++ b/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS nurse +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + firstname TEXT NOT NULL, + surname TEXT NOT NULL, + phoneNumber TEXT NOT NULL +) \ No newline at end of file diff --git a/src/main/resources/de/hitec/nhplus/patient/database/Patient.sql b/src/main/resources/de/hitec/nhplus/patient/database/Patient.sql new file mode 100644 index 0000000..23e7d8a --- /dev/null +++ b/src/main/resources/de/hitec/nhplus/patient/database/Patient.sql @@ -0,0 +1,9 @@ +CREATE TABLE patient +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + firstname TEXT NOT NULL, + surname TEXT NOT NULL, + dateOfBirth TEXT NOT NULL, + carelevel TEXT NOT NULL, + roomnumber TEXT NOT NULL +); \ No newline at end of file diff --git a/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql b/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql new file mode 100644 index 0000000..46e71ef --- /dev/null +++ b/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS treatment +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + patientId INTEGER NOT NULL, + treatment_date TEXT NOT NULL, + begin TEXT NOT NULL, + end TEXT NOT NULL, + description TEXT NOT NULL, + remark TEXT NOT NULL, + FOREIGN KEY (patientId) REFERENCES patient (id) ON DELETE CASCADE +) \ No newline at end of file From afe306a3647f54f94a8d6a0bd67f3fc3540f5a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Mon, 6 May 2024 09:06:13 +0200 Subject: [PATCH 4/6] #32: use Constants & Multiline Strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .../hitec/nhplus/fixtures/NurseFixture.java | 3 +- .../hitec/nhplus/fixtures/PatientFixture.java | 3 +- .../nhplus/fixtures/TreatmentFixture.java | 3 +- .../nhplus/main/MainWindowController.java | 7 +- .../hitec/nhplus/nurse/database/NurseDao.java | 55 ++++++------- .../nhplus/patient/database/PatientDao.java | 66 ++++++++-------- .../treatment/database/TreatmentDao.java | 78 ++++++++++--------- 7 files changed, 115 insertions(+), 100 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java index ec36258..c31bb4d 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java @@ -6,6 +6,7 @@ import de.hitec.nhplus.nurse.Nurse; import de.hitec.nhplus.nurse.database.NurseDao; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; import java.util.*; @@ -20,7 +21,7 @@ public class NurseFixture implements Fixture { public void setupTable(Connection connection) throws SQLException { final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/nurse/database/Nurse.sql"); assert schema != null; - final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); + final String SQL = new Scanner(schema, StandardCharsets.UTF_8).useDelimiter("\\A").next(); connection.createStatement().execute(SQL); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java index f263258..dc040bf 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java @@ -6,6 +6,7 @@ import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.patient.database.PatientDao; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; import java.util.*; @@ -23,7 +24,7 @@ public class PatientFixture implements Fixture { // @SuppressWarnings("checkstyle:LineLength") final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/patient/database/Patient.sql"); assert schema != null; - final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); + final String SQL = new Scanner(schema, StandardCharsets.UTF_8).useDelimiter("\\A").next(); connection.createStatement().execute(SQL); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java index bbb83f4..3925d3a 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java @@ -7,6 +7,7 @@ import de.hitec.nhplus.treatment.Treatment; import de.hitec.nhplus.treatment.database.TreatmentDao; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; import java.util.*; @@ -30,7 +31,7 @@ public class TreatmentFixture implements Fixture { public void setupTable(Connection connection) throws SQLException { final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/treatment/database/Treatment.sql"); assert schema != null; - final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next(); + final String SQL = new Scanner(schema, StandardCharsets.UTF_8).useDelimiter("\\A").next(); connection.createStatement().execute(SQL); } diff --git a/src/main/java/de/hitec/nhplus/main/MainWindowController.java b/src/main/java/de/hitec/nhplus/main/MainWindowController.java index 36e4faf..6f0ee03 100644 --- a/src/main/java/de/hitec/nhplus/main/MainWindowController.java +++ b/src/main/java/de/hitec/nhplus/main/MainWindowController.java @@ -9,6 +9,7 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import java.io.IOException; +import java.util.Objects; public class MainWindowController { @FXML @@ -40,7 +41,7 @@ public class MainWindowController { private void loadPatientPage() { try { BorderPane patientsPane = FXMLLoader.load( - Main.class.getResource("/de/hitec/nhplus/patient/AllPatientView.fxml") + Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/patient/AllPatientView.fxml")) ); patientPage.getChildren().setAll(patientsPane); AnchorPane.setTopAnchor(patientsPane, 0d); @@ -55,7 +56,7 @@ public class MainWindowController { private void loadTreatmentsPage() { try { BorderPane treatmentsPane = FXMLLoader.load( - Main.class.getResource("/de/hitec/nhplus/treatment/AllTreatmentView.fxml") + Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/treatment/AllTreatmentView.fxml")) ); treatmentPage.getChildren().setAll(treatmentsPane); AnchorPane.setTopAnchor(treatmentsPane, 0d); @@ -70,7 +71,7 @@ public class MainWindowController { private void loadNursePage() { try { BorderPane nursePane = FXMLLoader.load( - Main.class.getResource("/de/hitec/nhplus/nurse/AllNurseView.fxml") + Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/nurse/AllNurseView.fxml")) ); nursePage.getChildren().setAll(nursePane); AnchorPane.setTopAnchor(nursePane, 0d); 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 7f555da..0c967b6 100644 --- a/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java @@ -17,21 +17,24 @@ public class NurseDao extends DaoImp { @Override protected PreparedStatement getCreateStatement(Nurse nurse) throws SQLException { - final String SQL = "INSERT INTO nurse (firstname, surname, phoneNumber)" + - "VALUES (?, ?, ?)"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, nurse.getFirstName()); - preparedStatement.setString(2, nurse.getSurname()); - preparedStatement.setString(3, nurse.getPhoneNumber()); - return preparedStatement; + final String SQL = """ + INSERT INTO nurse + (firstname, surname, phoneNumber) + VALUES (?, ?, ?) + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setString(1, nurse.getFirstName()); + statement.setString(2, nurse.getSurname()); + statement.setString(3, nurse.getPhoneNumber()); + return statement; } @Override protected PreparedStatement getReadByIDStatement(int id) throws SQLException { final String SQL = "SELECT * FROM nurse WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - return preparedStatement; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement; } @Override @@ -59,28 +62,28 @@ public class NurseDao extends DaoImp { return list; } - @Override protected PreparedStatement getUpdateStatement(Nurse nurse) throws SQLException { - final String SQL = - "UPDATE nurse SET " + - "firstname = ?, " + - "surname = ?, " + - "phoneNumber = ? " + - "WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, nurse.getFirstName()); - preparedStatement.setString(2, nurse.getSurname()); - preparedStatement.setString(3, nurse.getPhoneNumber()); - preparedStatement.setInt(4, nurse.getId()); - return preparedStatement; + final String SQL = """ + UPDATE nurse SET + firstname = ?, + surname = ?, + phoneNumber = ? + 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()); + return statement; } @Override protected PreparedStatement getDeleteStatement(int id) throws SQLException { final String SQL = "DELETE FROM nurse WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - return preparedStatement; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement; } } diff --git a/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java b/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java index a15b2f6..3d16e8a 100644 --- a/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java +++ b/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java @@ -19,23 +19,26 @@ public class PatientDao extends DaoImp { @Override protected PreparedStatement getCreateStatement(Patient patient) throws SQLException { - final String SQL = "INSERT INTO patient (firstname, surname, dateOfBirth, carelevel, roomnumber) " + - "VALUES (?, ?, ?, ?, ?)"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, patient.getFirstName()); - preparedStatement.setString(2, patient.getSurname()); - preparedStatement.setString(3, patient.getDateOfBirth()); - preparedStatement.setString(4, patient.getCareLevel()); - preparedStatement.setString(5, patient.getRoomNumber()); - return preparedStatement; + final String SQL = """ + INSERT INTO patient + (firstname, surname, dateOfBirth, carelevel, roomnumber) + VALUES (?, ?, ?, ?, ?) + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setString(1, patient.getFirstName()); + statement.setString(2, patient.getSurname()); + statement.setString(3, patient.getDateOfBirth()); + statement.setString(4, patient.getCareLevel()); + statement.setString(5, patient.getRoomNumber()); + return statement; } @Override protected PreparedStatement getReadByIDStatement(int id) throws SQLException { final String SQL = "SELECT * FROM patient WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - return preparedStatement; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement; } @Override @@ -66,29 +69,30 @@ public class PatientDao extends DaoImp { @Override protected PreparedStatement getUpdateStatement(Patient patient) throws SQLException { - final String SQL = - "UPDATE patient SET " + - "firstname = ?, " + - "surname = ?, " + - "dateOfBirth = ?, " + - "carelevel = ?, " + - "roomnumber = ?, " + - "WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setString(1, patient.getFirstName()); - preparedStatement.setString(2, patient.getSurname()); - preparedStatement.setString(3, patient.getDateOfBirth()); - preparedStatement.setString(4, patient.getCareLevel()); - preparedStatement.setString(5, patient.getRoomNumber()); - preparedStatement.setInt(6, patient.getId()); - return preparedStatement; + final String SQL = """ + UPDATE patient SET + firstname = ?, + surname = ?, + dateOfBirth = ?, + carelevel = ?, + roomnumber = ?, + WHERE id = ? + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setString(1, patient.getFirstName()); + statement.setString(2, patient.getSurname()); + statement.setString(3, patient.getDateOfBirth()); + statement.setString(4, patient.getCareLevel()); + statement.setString(5, patient.getRoomNumber()); + statement.setInt(6, patient.getId()); + return statement; } @Override protected PreparedStatement getDeleteStatement(int id) throws SQLException { final String SQL = "DELETE FROM patient WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - return preparedStatement; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement; } } 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 fd1756d..6a32e86 100644 --- a/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java @@ -20,24 +20,27 @@ public class TreatmentDao extends DaoImp { @Override protected PreparedStatement getCreateStatement(Treatment treatment) throws SQLException { - final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " + - "VALUES (?, ?, ?, ?, ?, ?)"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, treatment.getPatient().getId()); - preparedStatement.setString(2, treatment.getDate()); - preparedStatement.setString(3, treatment.getBegin()); - preparedStatement.setString(4, treatment.getEnd()); - preparedStatement.setString(5, treatment.getDescription()); - preparedStatement.setString(6, treatment.getRemarks()); - return preparedStatement; + final String SQL = """ + INSERT INTO treatment + (patientId, treatment_date, begin, end, description, remark) + VALUES (?, ?, ?, ?, ?, ?) + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, treatment.getPatient().getId()); + statement.setString(2, treatment.getDate()); + statement.setString(3, treatment.getBegin()); + statement.setString(4, treatment.getEnd()); + statement.setString(5, treatment.getDescription()); + statement.setString(6, treatment.getRemarks()); + return statement; } @Override protected PreparedStatement getReadByIDStatement(int id) throws SQLException { final String SQL = "SELECT * FROM treatment WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - return preparedStatement; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement; } @Override @@ -70,9 +73,9 @@ public class TreatmentDao extends DaoImp { private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) throws SQLException { final String SQL = "SELECT * FROM treatment WHERE patientId = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, patientId); - return preparedStatement; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, patientId); + return statement; } public List readTreatmentsByPid(int patientId) throws SQLException { @@ -82,32 +85,33 @@ public class TreatmentDao extends DaoImp { @Override protected PreparedStatement getUpdateStatement(Treatment treatment) throws SQLException { - final String SQL = - "UPDATE treatment SET " + - "patientId = ?, " + - "treatment_date = ?, " + - "begin = ?, " + - "end = ?, " + - "description = ?, " + - "remark = ? " + - "WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, treatment.getPatient().getId()); - preparedStatement.setString(2, treatment.getDate()); - preparedStatement.setString(3, treatment.getBegin()); - preparedStatement.setString(4, treatment.getEnd()); - preparedStatement.setString(5, treatment.getDescription()); - preparedStatement.setString(6, treatment.getRemarks()); - preparedStatement.setInt(7, treatment.getId()); - return preparedStatement; + final String SQL = """ + UPDATE treatment SET + patientId = ?, + treatment_date = ?, + begin = ?, + end = ?, + description = ?, + remark = ? + WHERE id = ? + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, treatment.getPatient().getId()); + statement.setString(2, treatment.getDate()); + statement.setString(3, treatment.getBegin()); + statement.setString(4, treatment.getEnd()); + statement.setString(5, treatment.getDescription()); + statement.setString(6, treatment.getRemarks()); + statement.setInt(7, treatment.getId()); + return statement; } @Override protected PreparedStatement getDeleteStatement(int id) throws SQLException { final String SQL = "DELETE FROM treatment WHERE id = ?"; - PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); - preparedStatement.setInt(1, id); - return preparedStatement; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement; } } From f5fba1ac23762a80516734d6ebea98d849f427d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Mon, 6 May 2024 09:30:03 +0200 Subject: [PATCH 5/6] #32: Make Consistent Naming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .idea/dataSources.xml | 2 +- .idea/sqlDataSources.xml | 35 +++++++ db/nursingHome.db | Bin 20480 -> 20480 bytes .../java/de/hitec/nhplus/main/Person.java | 18 ++-- .../nhplus/nurse/AllNurseController.java | 26 ++--- .../java/de/hitec/nhplus/nurse/Nurse.java | 12 +-- .../hitec/nhplus/nurse/database/NurseDao.java | 10 +- .../nhplus/patient/AllPatientController.java | 18 ++-- .../java/de/hitec/nhplus/patient/Patient.java | 12 +-- .../nhplus/patient/database/PatientDao.java | 14 +-- .../treatment/AllTreatmentController.java | 15 ++- .../de/hitec/nhplus/treatment/Treatment.java | 91 ++++++++++++------ .../treatment/TreatmentModalController.java | 4 +- .../treatment/database/TreatmentDao.java | 4 +- .../de/hitec/nhplus/nurse/AllNurseView.fxml | 6 +- .../de/hitec/nhplus/nurse/database/Nurse.sql | 6 +- .../hitec/nhplus/patient/AllPatientView.fxml | 4 +- .../hitec/nhplus/patient/database/Patient.sql | 8 +- .../nhplus/treatment/AllTreatmentView.fxml | 6 +- .../nhplus/treatment/TreatmentModal.fxml | 2 +- .../nhplus/treatment/database/Treatment.sql | 16 +-- 21 files changed, 189 insertions(+), 120 deletions(-) create mode 100644 .idea/sqlDataSources.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 98f48a2..fed79a7 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,7 +1,7 @@ - + sqlite.xerial true org.sqlite.JDBC diff --git a/.idea/sqlDataSources.xml b/.idea/sqlDataSources.xml new file mode 100644 index 0000000..e75a467 --- /dev/null +++ b/.idea/sqlDataSources.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/db/nursingHome.db b/db/nursingHome.db index f25badf0e50452f1f1ae63b41747e16a5b5374df..2f42a9d3d3514a912207d2b54e56471170d2b12e 100644 GIT binary patch delta 884 zcmZozz}T>Wae|Z(6B`2q11k{2K>0)+BQc<;UeQ8co&yXl-1ZE7t9e&&+w;8K*m#ELf0Ol9`%U;t7)T^b2uycMVeT z^AAz*3-$5Q0jo$!EJ=lGQwVX52m!HCRV1aRXXZhbU{{fvhp-!_!XMR;l+@znqRfJl z%=|nY))b}YCKhEw?134AWX9xFJ{LAFO}@Vjnv;+4sB$&hn6QhBi!(NbPd>$?gd-3# zz+OztEGjPXOUwoN6Kpb)u|T2X(jurNR_TI_{Jd1Z(%hufB2?X6noSHw?BdeWjLqSb z75O!AxWS%6H%wm1FJ}ifBRR1s)hD$qH3z#tit_Vwq3%R;=;T#=p&X3NL&2$=|MN2o z@LXhI=0C*A|BC+*|NYH^4lDT`dBvIK8B2;%6H9n`m0`?WVAAFS<{Cc6ywalLR3;#k YQ-xU^%HvjImS%)z05-PGKlC{i01}qkssI20 delta 599 zcmZozz}T>Wae|Z(;~WMC238=3f#``kMq)rwy`qJ@e192Ocn&b|t>zc!jpI46Sx~{7 zhqbX$T8(}35gt|cqT*Bqjm(tEj{M373Tc@|#U*)(xv2^vt`Q*$e*Qol>f@uMP+VGs zMR;-#uN1dtlb0&HxV$`LOY&q-ZasDd4Fv@Sh=KJ$mO?>dNoHzZiD!y}r(cMxyK4}V zu|VmPqSVBaT%dyZl*AGYdw~j)Qqwc@(A@(RPt8lgBASv~oLrPyP?DJs)T4ŽE; z)ZE0PY&3n7t9V=(6(+CZ3squ}5|@@{Yz{9;Ov*_uNMvJEV*`h2W10#(SVnm=JHH09 zAI*TK;tn^U9FCy# columnFirstName; @FXML - private TableColumn columnSurname; + private TableColumn columnSurName; @FXML private TableColumn columnPhoneNumber; @@ -48,8 +48,8 @@ public class AllNurseController { this.columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName")); this.columnFirstName.setCellFactory(TextFieldTableCell.forTableColumn()); - this.columnSurname.setCellValueFactory(new PropertyValueFactory<>("surname")); - this.columnSurname.setCellFactory(TextFieldTableCell.forTableColumn()); + this.columnSurName.setCellValueFactory(new PropertyValueFactory<>("surName")); + this.columnSurName.setCellFactory(TextFieldTableCell.forTableColumn()); this.columnPhoneNumber.setCellValueFactory(new PropertyValueFactory<>("phoneNumber")); @@ -60,15 +60,15 @@ public class AllNurseController { this.buttonAdd.setDisable(true); ChangeListener inputNewNurseValidationListener = (observableValue, oldText, newText)-> { - boolean isValid = isValidFirstName(this.textFieldFirstname.getText()) - && isValidSurName(this.textFieldSurname.getText()) + boolean isValid = isValidFirstName(this.textFieldFirstName.getText()) + && isValidSurName(this.textFieldSurName.getText()) && isValidPhoneNumber(this.textFieldPhoneNumber.getText()); AllNurseController.this.buttonAdd.setDisable(!isValid); }; - this.textFieldFirstname.textProperty().addListener(inputNewNurseValidationListener); - this.textFieldSurname.textProperty().addListener(inputNewNurseValidationListener); + this.textFieldFirstName.textProperty().addListener(inputNewNurseValidationListener); + this.textFieldSurName.textProperty().addListener(inputNewNurseValidationListener); this.textFieldPhoneNumber.textProperty().addListener(inputNewNurseValidationListener); } @@ -83,8 +83,8 @@ public class AllNurseController { } @FXML public void handleAdd(){ - String surname=this.textFieldSurname.getText(); - String firstName=this.textFieldFirstname.getText(); + String surname=this.textFieldSurName.getText(); + String firstName=this.textFieldFirstName.getText(); String phoneNumber=this.textFieldPhoneNumber.getText(); try { this.dao.create(new Nurse(firstName, surname, phoneNumber)); @@ -97,8 +97,8 @@ public class AllNurseController { } private void clearTextfields() { - this.textFieldFirstname.clear(); - this.textFieldSurname.clear(); + this.textFieldFirstName.clear(); + this.textFieldSurName.clear(); this.textFieldPhoneNumber.clear(); } diff --git a/src/main/java/de/hitec/nhplus/nurse/Nurse.java b/src/main/java/de/hitec/nhplus/nurse/Nurse.java index 7243cc0..18e87f1 100644 --- a/src/main/java/de/hitec/nhplus/nurse/Nurse.java +++ b/src/main/java/de/hitec/nhplus/nurse/Nurse.java @@ -12,20 +12,20 @@ public class Nurse extends Person { public Nurse( String firstName, - String surname, + String surName, String phoneNumber ) { - super(firstName, surname); + super(firstName, surName); this.phoneNumber = new SimpleStringProperty(phoneNumber); } public Nurse( int id, String firstName, - String surname, + String surName, String phoneNumber ) { - super(firstName, surname); + super(firstName, surName); this.id = new SimpleIntegerProperty(id); this.phoneNumber = new SimpleStringProperty(phoneNumber); } @@ -55,8 +55,8 @@ public class Nurse extends Person { return new StringJoiner(System.lineSeparator()) .add("NURSE") .add("ID: " + this.getId()) - .add("Firstname: " + this.getFirstName()) - .add("Surname: " + this.getSurname()) + .add("FirstName: " + this.getFirstName()) + .add("SurName: " + this.getSurName()) .add("PhoneNumber: " + this.getPhoneNumber()) .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 0c967b6..a86fcbd 100644 --- a/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java +++ b/src/main/java/de/hitec/nhplus/nurse/database/NurseDao.java @@ -19,12 +19,12 @@ public class NurseDao extends DaoImp { protected PreparedStatement getCreateStatement(Nurse nurse) throws SQLException { final String SQL = """ INSERT INTO nurse - (firstname, surname, phoneNumber) + (firstName, surName, phoneNumber) VALUES (?, ?, ?) """; PreparedStatement statement = this.connection.prepareStatement(SQL); statement.setString(1, nurse.getFirstName()); - statement.setString(2, nurse.getSurname()); + statement.setString(2, nurse.getSurName()); statement.setString(3, nurse.getPhoneNumber()); return statement; } @@ -66,14 +66,14 @@ public class NurseDao extends DaoImp { protected PreparedStatement getUpdateStatement(Nurse nurse) throws SQLException { final String SQL = """ UPDATE nurse SET - firstname = ?, - surname = ?, + firstName = ?, + surName = ?, phoneNumber = ? WHERE id = ? """; PreparedStatement statement = this.connection.prepareStatement(SQL); statement.setString(1, nurse.getFirstName()); - statement.setString(2, nurse.getSurname()); + statement.setString(2, nurse.getSurName()); statement.setString(3, nurse.getPhoneNumber()); statement.setInt(4, nurse.getId()); return statement; diff --git a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java index 592223a..e9df51d 100644 --- a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java @@ -28,7 +28,7 @@ public class AllPatientController { @FXML private TableColumn columnFirstName; @FXML - private TableColumn columnSurname; + private TableColumn columnSurName; @FXML private TableColumn columnDateOfBirth; @FXML @@ -40,7 +40,7 @@ public class AllPatientController { @FXML private Button buttonAdd; @FXML - private TextField textFieldSurname; + private TextField textFieldSurName; @FXML private TextField textFieldFirstName; @FXML @@ -61,8 +61,8 @@ public class AllPatientController { this.columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName")); this.columnFirstName.setCellFactory(TextFieldTableCell.forTableColumn()); - this.columnSurname.setCellValueFactory(new PropertyValueFactory<>("surname")); - this.columnSurname.setCellFactory(TextFieldTableCell.forTableColumn()); + this.columnSurName.setCellValueFactory(new PropertyValueFactory<>("surName")); + this.columnSurName.setCellFactory(TextFieldTableCell.forTableColumn()); this.columnDateOfBirth.setCellValueFactory(new PropertyValueFactory<>("dateOfBirth")); this.columnDateOfBirth.setCellFactory(TextFieldTableCell.forTableColumn()); @@ -89,7 +89,7 @@ public class AllPatientController { { boolean isValid = isValidDate(this.textFieldDateOfBirth.getText()) && isValidFirstName(this.textFieldFirstName.getText()) - && isValidSurName(this.textFieldSurname.getText()) + && isValidSurName(this.textFieldSurName.getText()) && isValidDate(this.textFieldDateOfBirth.getText()) && isValidCareLevel(this.textFieldCareLevel.getText()) && isValidRoomNumber(textFieldRoomNumber.getText()); @@ -97,7 +97,7 @@ public class AllPatientController { AllPatientController.this.buttonAdd.setDisable(!isValid); }; - this.textFieldSurname.textProperty().addListener(inputNewPatientValidationListener); + this.textFieldSurName.textProperty().addListener(inputNewPatientValidationListener); this.textFieldFirstName.textProperty().addListener(inputNewPatientValidationListener); this.textFieldDateOfBirth.textProperty().addListener(inputNewPatientValidationListener); this.textFieldCareLevel.textProperty().addListener(inputNewPatientValidationListener); @@ -124,7 +124,7 @@ public class AllPatientController { event.getTableView().refresh(); return; } - event.getRowValue().setSurname(newSurName); + event.getRowValue().setSurName(newSurName); this.doUpdate(event); } @@ -198,7 +198,7 @@ public class AllPatientController { @FXML public void handleAdd() { - String surname = this.textFieldSurname.getText(); + String surname = this.textFieldSurName.getText(); String firstName = this.textFieldFirstName.getText(); String birthday = this.textFieldDateOfBirth.getText(); LocalDate date = DateConverter.convertStringToLocalDate(birthday); @@ -215,7 +215,7 @@ public class AllPatientController { private void clearTextfields() { this.textFieldFirstName.clear(); - this.textFieldSurname.clear(); + this.textFieldSurName.clear(); this.textFieldDateOfBirth.clear(); this.textFieldCareLevel.clear(); this.textFieldRoomNumber.clear(); diff --git a/src/main/java/de/hitec/nhplus/patient/Patient.java b/src/main/java/de/hitec/nhplus/patient/Patient.java index 1b9928d..c7c72ee 100644 --- a/src/main/java/de/hitec/nhplus/patient/Patient.java +++ b/src/main/java/de/hitec/nhplus/patient/Patient.java @@ -20,12 +20,12 @@ public class Patient extends Person { public Patient( String firstName, - String surname, + String surName, LocalDate dateOfBirth, String careLevel, String roomNumber ) { - super(firstName, surname); + super(firstName, surName); this.dateOfBirth = new SimpleStringProperty(DateConverter.convertLocalDateToString(dateOfBirth)); this.careLevel = new SimpleStringProperty(careLevel); this.roomNumber = new SimpleStringProperty(roomNumber); @@ -34,12 +34,12 @@ public class Patient extends Person { public Patient( int id, String firstName, - String surname, + String surName, LocalDate dateOfBirth, String careLevel, String roomNumber ) { - super(firstName, surname); + super(firstName, surName); this.id = new SimpleIntegerProperty(id); this.dateOfBirth = new SimpleStringProperty(DateConverter.convertLocalDateToString(dateOfBirth)); this.careLevel = new SimpleStringProperty(careLevel); @@ -103,8 +103,8 @@ public class Patient extends Person { return new StringJoiner(System.lineSeparator()) .add("PATIENT") .add("ID: " + this.getId()) - .add("Firstname: " + this.getFirstName()) - .add("Surname: " + this.getSurname()) + .add("FirstName: " + this.getFirstName()) + .add("SurName: " + this.getSurName()) .add("Birthday: " + this.getDateOfBirth()) .add("Carelevel: " + this.getCareLevel()) .add("RoomNumber: " + this.getRoomNumber()) diff --git a/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java b/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java index 3d16e8a..90ba560 100644 --- a/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java +++ b/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java @@ -21,12 +21,12 @@ public class PatientDao extends DaoImp { protected PreparedStatement getCreateStatement(Patient patient) throws SQLException { final String SQL = """ INSERT INTO patient - (firstname, surname, dateOfBirth, carelevel, roomnumber) + (firstName, surName, dateOfBirth, careLevel, roomNumber) VALUES (?, ?, ?, ?, ?) """; PreparedStatement statement = this.connection.prepareStatement(SQL); statement.setString(1, patient.getFirstName()); - statement.setString(2, patient.getSurname()); + statement.setString(2, patient.getSurName()); statement.setString(3, patient.getDateOfBirth()); statement.setString(4, patient.getCareLevel()); statement.setString(5, patient.getRoomNumber()); @@ -71,16 +71,16 @@ public class PatientDao extends DaoImp { protected PreparedStatement getUpdateStatement(Patient patient) throws SQLException { final String SQL = """ UPDATE patient SET - firstname = ?, - surname = ?, + firstName = ?, + surName = ?, dateOfBirth = ?, - carelevel = ?, - roomnumber = ?, + careLevel = ?, + roomNumber = ?, WHERE id = ? """; PreparedStatement statement = this.connection.prepareStatement(SQL); statement.setString(1, patient.getFirstName()); - statement.setString(2, patient.getSurname()); + statement.setString(2, patient.getSurName()); statement.setString(3, patient.getDateOfBirth()); statement.setString(4, patient.getCareLevel()); statement.setString(5, patient.getRoomNumber()); diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index b0b5f26..fd982ae 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -5,6 +5,8 @@ import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.patient.database.PatientDao; import de.hitec.nhplus.treatment.database.TreatmentDao; +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; @@ -28,7 +30,7 @@ public class AllTreatmentController { private TableColumn columnId; @FXML - private TableColumn columnPatientId; + private TableColumn columnPatientName; @FXML private TableColumn columnDate; @@ -59,7 +61,12 @@ public class AllTreatmentController { comboBoxPatientSelection.getSelectionModel().select(0); this.columnId.setCellValueFactory(new PropertyValueFactory<>("id")); - this.columnPatientId.setCellValueFactory(new PropertyValueFactory<>("patientId")); + this.columnPatientName.setCellValueFactory( + cellData -> { + Patient patient = cellData.getValue().getPatient(); + return new SimpleStringProperty(patient.getSurName() + ", " + patient.getFirstName()); + } + ); this.columnDate.setCellValueFactory(new PropertyValueFactory<>("date")); this.columnBegin.setCellValueFactory(new PropertyValueFactory<>("begin")); this.columnEnd.setCellValueFactory(new PropertyValueFactory<>("end")); @@ -93,7 +100,7 @@ public class AllTreatmentController { patientList = (ArrayList) dao.readAll(); this.patientSelection.add("alle"); for (Patient patient : patientList) { - this.patientSelection.add(patient.getSurname()); + this.patientSelection.add(patient.getSurName()); } } catch (SQLException exception) { exception.printStackTrace(); @@ -127,7 +134,7 @@ public class AllTreatmentController { private Patient searchInList(String surname) { for (Patient patient : this.patientList) { - if (patient.getSurname().equals(surname)) { + if (patient.getSurName().equals(surname)) { return patient; } } diff --git a/src/main/java/de/hitec/nhplus/treatment/Treatment.java b/src/main/java/de/hitec/nhplus/treatment/Treatment.java index fb15f06..4551560 100644 --- a/src/main/java/de/hitec/nhplus/treatment/Treatment.java +++ b/src/main/java/de/hitec/nhplus/treatment/Treatment.java @@ -2,94 +2,121 @@ package de.hitec.nhplus.treatment; import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.utils.DateConverter; +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.property.SimpleStringProperty; import java.time.LocalDate; import java.time.LocalTime; import java.util.StringJoiner; public class Treatment { - private int id; - private final Patient patient; - private LocalDate date; - private LocalTime begin; - private LocalTime end; - private String description; - private String remarks; + private SimpleIntegerProperty id; + private final SimpleObjectProperty patient; + private final SimpleObjectProperty date; + private final SimpleObjectProperty begin; + private final SimpleObjectProperty end; + private final SimpleStringProperty description; + private final SimpleStringProperty remarks; public Treatment(Patient patient, LocalDate date, LocalTime begin, LocalTime end, String description, String remarks) { - this.patient = patient; - this.date = date; - this.begin = begin; - this.end = end; - this.description = description; - this.remarks = remarks; + this.patient = new SimpleObjectProperty<>(patient); + this.date = new SimpleObjectProperty<>(date); + this.begin = new SimpleObjectProperty<>(begin); + this.end = new SimpleObjectProperty<>(end); + this.description = new SimpleStringProperty(description); + this.remarks = new SimpleStringProperty(remarks); } public Treatment(int id, Patient patient, LocalDate date, LocalTime begin, LocalTime end, String description, String remarks) { - this.id = id; - this.patient = patient; - this.date = date; - this.begin = begin; - this.end = end; - this.description = description; - this.remarks = remarks; + this.id = new SimpleIntegerProperty(id); + this.patient = new SimpleObjectProperty<>(patient); + this.date = new SimpleObjectProperty<>(date); + this.begin = new SimpleObjectProperty<>(begin); + this.end = new SimpleObjectProperty<>(end); + this.description = new SimpleStringProperty(description); + this.remarks = new SimpleStringProperty(remarks); } public int getId() { - return id; + return id.getValue(); } public Patient getPatient() { - return this.patient; + return patient.getValue(); } public String getDate() { - return date.toString(); + return date.getValue().toString(); } public String getBegin() { - return begin.toString(); + return begin.getValue().toString(); } public String getEnd() { - return end.toString(); + return end.getValue().toString(); } public void setDate(String date) { - this.date = DateConverter.convertStringToLocalDate(date); + this.date.set(DateConverter.convertStringToLocalDate(date)); } public void setBegin(LocalTime begin) { - this.begin = begin; + this.begin.set(begin); } public void setEnd(LocalTime end) { - this.end = end; + this.end.set(end); } public String getDescription() { - return description; + return description.getValue(); } public void setDescription(String description) { - this.description = description; + this.description.set(description); } public String getRemarks() { + return remarks.getValue(); + } + + public SimpleObjectProperty patientProperty() { + return patient; + } + + public SimpleObjectProperty dateProperty() { + return date; + } + + public SimpleObjectProperty beginProperty() { + return begin; + } + + public SimpleObjectProperty endProperty() { + return end; + } + + public SimpleStringProperty descriptionProperty() { + return description; + } + + public SimpleStringProperty remarksProperty() { return remarks; } public void setRemarks(String remarks) { - this.remarks = remarks; + this.remarks.set(remarks); } public String toString() { return new StringJoiner(System.lineSeparator()) .add("TREATMENT") .add("ID: " + this.getId()) - .add("Patient: " + this.getPatient().getSurname() + " " + this.getPatient().getFirstName()) + .add("Patient: " + this.getPatient().getSurName() + ", " + this.getPatient().getFirstName()) .add("Date: " + this.getDate()) .add("Begin: " + this.getBegin()) .add("End: " + this.getEnd()) diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java index f6e4927..af788f0 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java @@ -17,7 +17,7 @@ public class TreatmentModalController { @FXML private Label labelFirstName; @FXML - private Label labelSurname; + private Label labelSurName; @FXML private TextField textFieldBegin; @FXML @@ -99,7 +99,7 @@ public class TreatmentModalController { private void showData() { this.labelFirstName.setText(patient.getFirstName()); - this.labelSurname.setText(patient.getSurname()); + this.labelSurName.setText(patient.getSurName()); LocalDate date = DateConverter.convertStringToLocalDate(treatment.getDate()); this.datePicker.setValue(date); this.textFieldBegin.setText(this.treatment.getBegin()); 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 6a32e86..51ccde3 100644 --- a/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java @@ -22,7 +22,7 @@ public class TreatmentDao extends DaoImp { protected PreparedStatement getCreateStatement(Treatment treatment) throws SQLException { final String SQL = """ INSERT INTO treatment - (patientId, treatment_date, begin, end, description, remark) + (patientId, date, begin, end, description, remark) VALUES (?, ?, ?, ?, ?, ?) """; PreparedStatement statement = this.connection.prepareStatement(SQL); @@ -88,7 +88,7 @@ public class TreatmentDao extends DaoImp { final String SQL = """ UPDATE treatment SET patientId = ?, - treatment_date = ?, + date = ?, begin = ?, end = ?, description = ?, diff --git a/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml b/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml index 4a7fb33..eec0384 100644 --- a/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml +++ b/src/main/resources/de/hitec/nhplus/nurse/AllNurseView.fxml @@ -20,7 +20,7 @@ text="ID" /> @@ -51,12 +51,12 @@ diff --git a/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql b/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql index d51bbf3..000823e 100644 --- a/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql +++ b/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql @@ -1,7 +1,7 @@ -CREATE TABLE IF NOT EXISTS nurse +CREATE TABLE nurse ( id INTEGER PRIMARY KEY AUTOINCREMENT, - firstname TEXT NOT NULL, - surname TEXT NOT NULL, + firstName TEXT NOT NULL, + surName TEXT NOT NULL, phoneNumber TEXT NOT NULL ) \ No newline at end of file diff --git a/src/main/resources/de/hitec/nhplus/patient/AllPatientView.fxml b/src/main/resources/de/hitec/nhplus/patient/AllPatientView.fxml index 9d63e7c..50941c6 100644 --- a/src/main/resources/de/hitec/nhplus/patient/AllPatientView.fxml +++ b/src/main/resources/de/hitec/nhplus/patient/AllPatientView.fxml @@ -23,7 +23,7 @@ text="ID" /> diff --git a/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql b/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql index 46e71ef..0fdee0d 100644 --- a/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql +++ b/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql @@ -1,11 +1,11 @@ -CREATE TABLE IF NOT EXISTS treatment +CREATE TABLE treatment ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - patientId INTEGER NOT NULL, - treatment_date TEXT NOT NULL, - begin TEXT NOT NULL, - end TEXT NOT NULL, - description TEXT NOT NULL, - remark TEXT NOT NULL, + id INTEGER PRIMARY KEY AUTOINCREMENT, + patientId INTEGER NOT NULL, + date TEXT NOT NULL, + begin TEXT NOT NULL, + end TEXT NOT NULL, + description TEXT NOT NULL, + remark TEXT NOT NULL, FOREIGN KEY (patientId) REFERENCES patient (id) ON DELETE CASCADE ) \ No newline at end of file From db367aaa5e428bcf5f9a08d032d15f3fcdc0cfa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Mon, 6 May 2024 11:24:19 +0200 Subject: [PATCH 6/6] #32: Dao Javadoc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .../nhplus/datastorage/ConnectionBuilder.java | 19 +++++++- .../java/de/hitec/nhplus/datastorage/Dao.java | 40 ++++++++++++++++- .../hitec/nhplus/datastorage/DaoFactory.java | 27 ++++++++++++ .../de/hitec/nhplus/datastorage/DaoImp.java | 44 ++++++++++++++++++- .../nhplus/patient/AllPatientController.java | 2 +- .../treatment/AllTreatmentController.java | 3 +- 6 files changed, 128 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java b/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java index ea22873..82cea08 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java +++ b/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java @@ -1,11 +1,17 @@ package de.hitec.nhplus.datastorage; +import org.sqlite.SQLiteConfig; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; -import org.sqlite.SQLiteConfig; - +/** + * The {@link DaoFactory} allows a safe connection to the Database. + * + * @author Bernd Heidemann + * @version 1.0 + */ public class ConnectionBuilder { private static final String DB_NAME = "nursingHome.db"; @@ -13,6 +19,10 @@ public class ConnectionBuilder { private static Connection connection; + /** + * @return a Thread-safe {@link Connection} to the Database. + * @author Bernd Heidemann + */ synchronized public static Connection getConnection() { try { if (ConnectionBuilder.connection == null) { @@ -27,6 +37,11 @@ public class ConnectionBuilder { return ConnectionBuilder.connection; } + /** + * Closes the Connection to the Database. + * + * @author Bernd Heidemann + */ synchronized public static void closeConnection() { try { if (ConnectionBuilder.connection != null) { diff --git a/src/main/java/de/hitec/nhplus/datastorage/Dao.java b/src/main/java/de/hitec/nhplus/datastorage/Dao.java index 172bea1..0a38d48 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/Dao.java +++ b/src/main/java/de/hitec/nhplus/datastorage/Dao.java @@ -3,14 +3,52 @@ package de.hitec.nhplus.datastorage; import java.sql.SQLException; import java.util.List; +/** + * {@link Dao} is the Abbreviation of Data-Access-Object. + * This Interface has the Basic Methods which are needed on any DAO to work as expected. + * + * @param The Model for which that DAO is implemented + * @author Bernd Heidemann + * @version 1.0 + * @implSpec The Implementations should be added to the {@link DaoFactory} + */ public interface Dao { + /** + * Create a Database Entry from a Model object. + * + * @param t the Model instance + * @author Bernd Heidemann + */ void create(T t) throws SQLException; + /** + * Read a Database Entry to a Model object. + * + * @param id of the Element in the Database + * @author Bernd Heidemann + */ T read(int id) throws SQLException; + /** + * Read all Database Entries to a {@link List} of Model objects. + * + * @author Bernd Heidemann + */ List readAll() throws SQLException; + /** + * Update the Database according to the Values of the Model object. + * + * @param t the Model instance. + * @author Bernd Heidemann + */ void update(T t) throws SQLException; - void deleteById(int id) throws SQLException; + /** + * Delete a Database Entry. + * + * @param id of the Element in the Database. + * @author Bernd Heidemann + */ + void delete(int id) throws SQLException; } diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java index 08c2ba7..f4fc8dc 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java @@ -4,13 +4,28 @@ import de.hitec.nhplus.nurse.database.NurseDao; import de.hitec.nhplus.patient.database.PatientDao; import de.hitec.nhplus.treatment.database.TreatmentDao; +/** + * The {@link DaoFactory} is a Singleton({@link DaoFactory#getInstance}) which should be used to get {@link Dao}s. + * + * @author Bernd Heidemann + * @version 1.0 + */ public class DaoFactory { private static DaoFactory instance; + /** + * A Private Constructor according to the Singleton Pattern. + * + * @author Bernd Heidemann + */ private DaoFactory() { } + /** + * @return {@link DaoFactory}, the Singleton Instance + * @author Bernd Heidemann + */ public static DaoFactory getInstance() { if (DaoFactory.instance == null) { DaoFactory.instance = new DaoFactory(); @@ -18,14 +33,26 @@ public class DaoFactory { return DaoFactory.instance; } + /** + * @return a {@link TreatmentDao} + * @author Bernd Heidemann + */ public TreatmentDao createTreatmentDao() { return new TreatmentDao(ConnectionBuilder.getConnection()); } + /** + * @return a {@link PatientDao} + * @author Bernd Heidemann + */ public PatientDao createPatientDAO() { return new PatientDao(ConnectionBuilder.getConnection()); } + /** + * @return a {@link NurseDao} + * @author Dominik Säume + */ public NurseDao createNurseDAO() { return new NurseDao(ConnectionBuilder.getConnection()); } diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java index 632e21e..417c285 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java @@ -6,18 +6,43 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; +/** + * The {@link DaoImp} is a Generic Base Implementation of the {@link Dao}, + * which should fit most use cases. + * + * @param The Model for which that DAO is implemented + * @author Bernd Heidemann + * @version 1.0 + * @implSpec The Implementations should be added to the {@link DaoFactory} + */ public abstract class DaoImp implements Dao { protected final Connection connection; + /** + * @param connection a Database Connection, which should be gotten from {@link ConnectionBuilder} + * @author Bernd Heidemann + */ public DaoImp(Connection connection) { this.connection = connection; } + /** + * Creates a new Database Entry from a Model object. + * + * @param t the Model instance + * @author Bernd Heidemann + */ @Override public void create(T t) throws SQLException { getCreateStatement(t).executeUpdate(); } + /** + * Read a Database Entry to a Model object. + * + * @param id of the Element in the Database + * @author Bernd Heidemann + */ @Override public T read(int id) throws SQLException { T object = null; @@ -28,18 +53,35 @@ public abstract class DaoImp implements Dao { return object; } + /** + * Read all Database Entries to a {@link List} of Model objects. + * + * @author Bernd Heidemann + */ @Override public List readAll() throws SQLException { return getListFromResultSet(getReadAllStatement().executeQuery()); } + /** + * Update the Database according to the Values of the Model object. + * + * @param t the Model instance. + * @author Bernd Heidemann + */ @Override public void update(T t) throws SQLException { getUpdateStatement(t).executeUpdate(); } + /** + * Delete a Database Entry. + * + * @param id of the Element in the Database. + * @author Bernd Heidemann + */ @Override - public void deleteById(int id) throws SQLException { + public void delete(int id) throws SQLException { getDeleteStatement(id).executeUpdate(); } diff --git a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java index e9df51d..1e99995 100644 --- a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java @@ -188,7 +188,7 @@ public class AllPatientController { Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem(); if (selectedItem != null) { try { - DaoFactory.getInstance().createPatientDAO().deleteById(selectedItem.getId()); + DaoFactory.getInstance().createPatientDAO().delete(selectedItem.getId()); this.tableView.getItems().remove(selectedItem); } catch (SQLException exception) { exception.printStackTrace(); diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index fd982ae..c0f01c3 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -5,7 +5,6 @@ import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.patient.database.PatientDao; import de.hitec.nhplus.treatment.database.TreatmentDao; -import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; @@ -147,7 +146,7 @@ public class AllTreatmentController { Treatment t = this.treatments.remove(index); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); try { - dao.deleteById(t.getId()); + dao.delete(t.getId()); } catch (SQLException exception) { exception.printStackTrace(); }