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] #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());