#32: Refactor Database Usage, to be more consistent and understandable #33

Merged
SZUT-Dominik merged 6 commits from task/refactor-dao into main 2024-05-06 12:23:41 +00:00
12 changed files with 42 additions and 47 deletions
Showing only changes of commit c71d3e128b - Show all commits

View file

@ -11,7 +11,7 @@ public class DaoFactory {
private DaoFactory() { private DaoFactory() {
} }
public static DaoFactory getDaoFactory() { public static DaoFactory getInstance() {
if (DaoFactory.instance == null) { if (DaoFactory.instance == null) {
DaoFactory.instance = new DaoFactory(); DaoFactory.instance = new DaoFactory();
} }

View file

@ -17,9 +17,9 @@ public abstract class DaoImp<T> implements Dao<T> {
} }
@Override @Override
public T read(int key) throws SQLException { public T read(int id) throws SQLException {
T object = null; T object = null;
ResultSet result = getReadByIDStatement(key).executeQuery(); ResultSet result = getReadByIDStatement(id).executeQuery();
if (result.next()) { if (result.next()) {
object = getInstanceFromResultSet(result); object = getInstanceFromResultSet(result);
} }
@ -37,21 +37,21 @@ public abstract class DaoImp<T> implements Dao<T> {
} }
@Override @Override
public void deleteById(int key) throws SQLException { public void deleteById(int id) throws SQLException {
getDeleteStatement(key).executeUpdate(); getDeleteStatement(id).executeUpdate();
} }
protected abstract T getInstanceFromResultSet(ResultSet set) throws SQLException; protected abstract T getInstanceFromResultSet(ResultSet result) throws SQLException;
protected abstract ArrayList<T> getListFromResultSet(ResultSet set) throws SQLException; protected abstract List<T> getListFromResultSet(ResultSet result) throws SQLException;
protected abstract PreparedStatement getCreateStatement(T t); 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 getReadAllStatement();
protected abstract PreparedStatement getUpdateStatement(T t); protected abstract PreparedStatement getUpdateStatement(T t);
protected abstract PreparedStatement getDeleteStatement(int key); protected abstract PreparedStatement getDeleteStatement(int id);
} }

View file

@ -52,7 +52,7 @@ public class NurseFixture implements Fixture<Nurse> {
"Armout", "Armout",
"9876543210" "9876543210"
)); ));
NurseDao dao = DaoFactory.getDaoFactory().createNurseDAO(); NurseDao dao = DaoFactory.getInstance().createNurseDAO();
for(Nurse nurse : nurses){ for(Nurse nurse : nurses){
dao.create(nurse); dao.create(nurse);
} }

View file

@ -96,7 +96,7 @@ public class PatientFixture implements Fixture<Patient>
"110" "110"
)); ));
PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO(); PatientDao dao = DaoFactory.getInstance().createPatientDAO();
for (Patient patient : patients){ for (Patient patient : patients){
dao.create(patient); dao.create(patient);
} }

View file

@ -165,7 +165,7 @@ public class TreatmentFixture implements Fixture<Treatment>
"Massage der Extremitäten zur Verbesserung der Durchblutung") "Massage der Extremitäten zur Verbesserung der Durchblutung")
); );
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
Map<String, Treatment> treatmentsById = new HashMap<>(); Map<String, Treatment> treatmentsById = new HashMap<>();
for (Treatment treatment : treatments){ for (Treatment treatment : treatments){
dao.create(treatment); dao.create(treatment);

View file

@ -73,7 +73,7 @@ public class AllNurseController {
private void readAllAndShowInTableView(){ private void readAllAndShowInTableView(){
this.nurses.clear(); this.nurses.clear();
this.dao = DaoFactory.getDaoFactory().createNurseDAO(); this.dao = DaoFactory.getInstance().createNurseDAO();
try { try {
this.nurses.addAll(this.dao.readAll()); this.nurses.addAll(this.dao.readAll());
}catch (SQLException exception){ }catch (SQLException exception){

View file

@ -7,6 +7,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class NurseDao extends DaoImp<Nurse> { public class NurseDao extends DaoImp<Nurse> {
public NurseDao(Connection connection) { public NurseDao(Connection connection) {
@ -65,7 +66,7 @@ public class NurseDao extends DaoImp<Nurse> {
} }
@Override @Override
protected ArrayList<Nurse> getListFromResultSet(ResultSet result) throws SQLException { protected List<Nurse> getListFromResultSet(ResultSet result) throws SQLException {
ArrayList<Nurse> list = new ArrayList<>(); ArrayList<Nurse> list = new ArrayList<>();
while (result.next()) { while (result.next()) {
list.add(new Nurse( list.add(new Nurse(

View file

@ -174,7 +174,7 @@ public class AllPatientController {
private void readAllAndShowInTableView() { private void readAllAndShowInTableView() {
this.patients.clear(); this.patients.clear();
this.dao = DaoFactory.getDaoFactory().createPatientDAO(); this.dao = DaoFactory.getInstance().createPatientDAO();
try { try {
this.patients.addAll(this.dao.readAll()); this.patients.addAll(this.dao.readAll());
} catch (SQLException exception) { } catch (SQLException exception) {
@ -187,7 +187,7 @@ public class AllPatientController {
Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem(); Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem();
if (selectedItem != null) { if (selectedItem != null) {
try { try {
DaoFactory.getDaoFactory().createPatientDAO().deleteById(selectedItem.getId()); DaoFactory.getInstance().createPatientDAO().deleteById(selectedItem.getId());
this.tableView.getItems().remove(selectedItem); this.tableView.getItems().remove(selectedItem);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();

View file

@ -8,6 +8,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class PatientDao extends DaoImp<Patient> { public class PatientDao extends DaoImp<Patient> {
@ -70,7 +71,7 @@ public class PatientDao extends DaoImp<Patient> {
} }
@Override @Override
protected ArrayList<Patient> getListFromResultSet(ResultSet result) throws SQLException { protected List<Patient> getListFromResultSet(ResultSet result) throws SQLException {
ArrayList<Patient> list = new ArrayList<>(); ArrayList<Patient> list = new ArrayList<>();
while (result.next()) { while (result.next()) {
Patient patient = new Patient( Patient patient = new Patient(

View file

@ -78,7 +78,7 @@ public class AllTreatmentController {
public void readAllAndShowInTableView() { public void readAllAndShowInTableView() {
comboBoxPatientSelection.getSelectionModel().select(0); comboBoxPatientSelection.getSelectionModel().select(0);
this.dao = DaoFactory.getDaoFactory().createTreatmentDao(); this.dao = DaoFactory.getInstance().createTreatmentDao();
try { try {
this.treatments.setAll(dao.readAll()); this.treatments.setAll(dao.readAll());
} catch (SQLException exception) { } catch (SQLException exception) {
@ -87,7 +87,7 @@ public class AllTreatmentController {
} }
private void createComboBoxData() { private void createComboBoxData() {
PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO(); PatientDao dao = DaoFactory.getInstance().createPatientDAO();
try { try {
patientList = (ArrayList<Patient>) dao.readAll(); patientList = (ArrayList<Patient>) dao.readAll();
this.patientSelection.add("alle"); this.patientSelection.add("alle");
@ -104,7 +104,7 @@ public class AllTreatmentController {
public void handleComboBox() { public void handleComboBox() {
String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem(); String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem();
this.treatments.clear(); this.treatments.clear();
this.dao = DaoFactory.getDaoFactory().createTreatmentDao(); this.dao = DaoFactory.getInstance().createTreatmentDao();
if (selectedPatient.equals("alle")) { if (selectedPatient.equals("alle")) {
try { try {
@ -137,7 +137,7 @@ public class AllTreatmentController {
public void handleDelete() { public void handleDelete() {
int index = this.tableView.getSelectionModel().getSelectedIndex(); int index = this.tableView.getSelectionModel().getSelectedIndex();
Treatment t = this.treatments.remove(index); Treatment t = this.treatments.remove(index);
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
try { try {
dao.deleteById(t.getId()); dao.deleteById(t.getId());
} catch (SQLException exception) { } catch (SQLException exception) {
@ -209,12 +209,12 @@ public class AllTreatmentController {
Stage stage = new Stage(); Stage stage = new Stage();
TreatmentModalController controller = loader.getController(); TreatmentModalController controller = loader.getController();
PatientDao pDao = DaoFactory.getDaoFactory().createPatientDAO(); PatientDao pDao = DaoFactory.getInstance().createPatientDAO();
controller.initialize( controller.initialize(
this, this,
stage, stage,
treatment, treatment,
pDao.read(treatment.getPatientId()) pDao.read(treatment.getPatient().getId())
); );
stage.setScene(scene); stage.setScene(scene);
@ -228,7 +228,7 @@ public class AllTreatmentController {
} }
public void createTreatment(Treatment treatment) { public void createTreatment(Treatment treatment) {
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
try { try {
dao.create(treatment); dao.create(treatment);
} catch (SQLException exception) { } catch (SQLException exception) {
@ -237,7 +237,7 @@ public class AllTreatmentController {
} }
public void updateTreatment(Treatment treatment) { public void updateTreatment(Treatment treatment) {
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
try { try {
dao.update(treatment); dao.update(treatment);
} catch (SQLException exception) { } catch (SQLException exception) {

View file

@ -1,5 +1,6 @@
package de.hitec.nhplus.treatment; package de.hitec.nhplus.treatment;
import de.hitec.nhplus.patient.Patient;
import de.hitec.nhplus.utils.DateConverter; import de.hitec.nhplus.utils.DateConverter;
import java.time.LocalDate; import java.time.LocalDate;
@ -8,16 +9,16 @@ import java.util.StringJoiner;
public class Treatment { public class Treatment {
private int id; private int id;
private final int patientId; private final Patient patient;
private LocalDate date; private LocalDate date;
private LocalTime begin; private LocalTime begin;
private LocalTime end; private LocalTime end;
private String description; private String description;
private String remarks; 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) { LocalTime end, String description, String remarks) {
this.patientId = patientId; this.patient = patient;
this.date = date; this.date = date;
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
@ -25,10 +26,10 @@ public class Treatment {
this.remarks = remarks; 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) { LocalTime end, String description, String remarks) {
this.id = id; this.id = id;
this.patientId = patientId; this.patient = patient;
this.date = date; this.date = date;
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
@ -40,8 +41,8 @@ public class Treatment {
return id; return id;
} }
public int getPatientId() { public Patient getPatient() {
return this.patientId; return this.patient;
} }
public String getDate() { public String getDate() {
@ -88,7 +89,7 @@ public class Treatment {
return new StringJoiner(System.lineSeparator()) return new StringJoiner(System.lineSeparator())
.add("TREATMENT") .add("TREATMENT")
.add("ID: " + this.getId()) .add("ID: " + this.getId())
.add("PatientID: " + this.getPatientId()) .add("Patient: " + this.getPatient().getSurname() + " " + this.getPatient().getFirstName())
.add("Date: " + this.getDate()) .add("Date: " + this.getDate())
.add("Begin: " + this.getBegin()) .add("Begin: " + this.getBegin())
.add("End: " + this.getEnd()) .add("End: " + this.getEnd())

View file

@ -1,5 +1,6 @@
package de.hitec.nhplus.treatment; package de.hitec.nhplus.treatment;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.datastorage.DaoImp; import de.hitec.nhplus.datastorage.DaoImp;
import de.hitec.nhplus.utils.DateConverter; import de.hitec.nhplus.utils.DateConverter;
@ -23,7 +24,7 @@ public class TreatmentDao extends DaoImp<Treatment> {
final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " + final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " +
"VALUES (?, ?, ?, ?, ?, ?)"; "VALUES (?, ?, ?, ?, ?, ?)";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, treatment.getPatientId()); preparedStatement.setInt(1, treatment.getPatient().getId());
preparedStatement.setString(2, treatment.getDate()); preparedStatement.setString(2, treatment.getDate());
preparedStatement.setString(3, treatment.getBegin()); preparedStatement.setString(3, treatment.getBegin());
preparedStatement.setString(4, treatment.getEnd()); preparedStatement.setString(4, treatment.getEnd());
@ -52,7 +53,7 @@ public class TreatmentDao extends DaoImp<Treatment> {
protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException { protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException {
return new Treatment( return new Treatment(
result.getInt(1), result.getInt(1),
result.getInt(2), DaoFactory.getInstance().createPatientDAO().read(result.getInt(2)),
DateConverter.convertStringToLocalDate(result.getString(3)), DateConverter.convertStringToLocalDate(result.getString(3)),
DateConverter.convertStringToLocalTime(result.getString(4)), DateConverter.convertStringToLocalTime(result.getString(4)),
DateConverter.convertStringToLocalTime(result.getString(5)), DateConverter.convertStringToLocalTime(result.getString(5)),
@ -74,19 +75,10 @@ public class TreatmentDao extends DaoImp<Treatment> {
} }
@Override @Override
protected ArrayList<Treatment> getListFromResultSet(ResultSet result) throws SQLException { protected List<Treatment> getListFromResultSet(ResultSet result) throws SQLException {
ArrayList<Treatment> list = new ArrayList<>(); ArrayList<Treatment> list = new ArrayList<>();
while (result.next()) { while (result.next()) {
Treatment treatment = new Treatment( list.add(getInstanceFromResultSet(result));
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);
} }
return list; return list;
} }
@ -122,7 +114,7 @@ public class TreatmentDao extends DaoImp<Treatment> {
"remark = ? " + "remark = ? " +
"WHERE id = ?"; "WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, treatment.getPatientId()); preparedStatement.setInt(1, treatment.getPatient().getId());
preparedStatement.setString(2, treatment.getDate()); preparedStatement.setString(2, treatment.getDate());
preparedStatement.setString(3, treatment.getBegin()); preparedStatement.setString(3, treatment.getBegin());
preparedStatement.setString(4, treatment.getEnd()); preparedStatement.setString(4, treatment.getEnd());