#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
16 changed files with 121 additions and 134 deletions
Showing only changes of commit 77b38ef159 - Show all commits

View file

@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="SqlDialectMappings"> <component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/patient/PatientDao.java" dialect="GenericSQL" /> <file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java" dialect="GenericSQL" /> <file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/treatment/database/NurseDao.java" dialect="GenericSQL" />
<file url="PROJECT" dialect="SQLite" /> <file url="PROJECT" dialect="SQLite" />
</component> </component>
</project> </project>

View file

@ -1,8 +1,8 @@
package de.hitec.nhplus.datastorage; package de.hitec.nhplus.datastorage;
import de.hitec.nhplus.nurse.NurseDao; import de.hitec.nhplus.nurse.database.NurseDao;
import de.hitec.nhplus.patient.PatientDao; import de.hitec.nhplus.patient.database.PatientDao;
import de.hitec.nhplus.treatment.TreatmentDao; import de.hitec.nhplus.treatment.database.TreatmentDao;
public class DaoFactory { public class DaoFactory {

View file

@ -6,7 +6,7 @@ import java.util.Map;
public interface Fixture<T> public interface Fixture<T>
{ {
void dropTable(Connection connection); void dropTable(Connection connection) throws SQLException;
void setupTable(Connection connection); void setupTable(Connection connection) throws SQLException;
Map<String, T> load() throws SQLException; Map<String, T> load() throws SQLException;
} }

View file

@ -1,40 +1,27 @@
package de.hitec.nhplus.fixtures; package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.Main;
import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.nurse.NurseDao;
import de.hitec.nhplus.nurse.Nurse; import de.hitec.nhplus.nurse.Nurse;
import de.hitec.nhplus.nurse.database.NurseDao;
import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class NurseFixture implements Fixture<Nurse> { public class NurseFixture implements Fixture<Nurse> {
@Override @Override
public void dropTable(Connection connection) { public void dropTable(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) { connection.createStatement().execute("DROP TABLE nurse");
statement.execute("DROP TABLE nurse");
} catch (SQLException exception) {
System.out.println(exception.getMessage());
}
} }
@Override @Override
public void setupTable(Connection connection) { public void setupTable(Connection connection) throws SQLException {
final String SQL = "CREATE TABLE IF NOT EXISTS nurse (" + final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/nurse/database/Nurse.sql");
"id INTEGER PRIMARY KEY AUTOINCREMENT, " + assert schema != null;
"firstname TEXT NOT NULL, " + final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next();
"surname TEXT NOT NULL, " + connection.createStatement().execute(SQL);
"phoneNumber TEXT NOT NULL" +
");";
try (Statement statement = connection.createStatement()) {
statement.execute(SQL);
} catch (SQLException exception) {
System.out.println(exception.getMessage());
}
} }
@Override @Override
@ -53,11 +40,11 @@ public class NurseFixture implements Fixture<Nurse> {
"9876543210" "9876543210"
)); ));
NurseDao dao = DaoFactory.getInstance().createNurseDAO(); NurseDao dao = DaoFactory.getInstance().createNurseDAO();
for(Nurse nurse : nurses){ for (Nurse nurse : nurses) {
dao.create(nurse); dao.create(nurse);
} }
Map<String, Nurse> nursesByName = new HashMap<>(); Map<String, Nurse> nursesByName = new HashMap<>();
for (Nurse nurse : dao.readAll()){ for (Nurse nurse : dao.readAll()) {
nursesByName.put(nurse.getFirstName(), nurse); nursesByName.put(nurse.getFirstName(), nurse);
} }
return nursesByName; return nursesByName;

View file

@ -1,107 +1,85 @@
package de.hitec.nhplus.fixtures; package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.Main;
import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.patient.PatientDao;
import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.patient.Patient;
import de.hitec.nhplus.patient.database.PatientDao;
import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate;
public class PatientFixture implements Fixture<Patient> public class PatientFixture implements Fixture<Patient> {
{
@Override @Override
public void dropTable(Connection connection) public void dropTable(Connection connection) throws SQLException {
{ connection.createStatement().execute("DROP TABLE patient");
try (Statement statement = connection.createStatement())
{
statement.execute("DROP TABLE patient");
} catch (SQLException exception)
{
System.out.println(exception.getMessage());
}
} }
@Override @Override
public void setupTable(Connection connection) public void setupTable(Connection connection) throws SQLException {
{ // @SuppressWarnings("checkstyle:LineLength")
final String SQL = "CREATE TABLE IF NOT EXISTS patient (" + final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/patient/database/Patient.sql");
" id INTEGER PRIMARY KEY AUTOINCREMENT, " + assert schema != null;
" firstname TEXT NOT NULL, " + final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next();
" surname TEXT NOT NULL, " + connection.createStatement().execute(SQL);
" 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());
}
} }
@Override @Override
public Map<String, Patient> load() throws SQLException public Map<String, Patient> load() throws SQLException {
{
List<Patient> patients = new ArrayList<>(); List<Patient> patients = new ArrayList<>();
patients.add(new Patient( patients.add(new Patient(
"Seppl", "Seppl",
"Herberger", "Herberger",
convertStringToLocalDate("1945-12-01"), convertStringToLocalDate("1945-12-01"),
"4", "4",
"202" "202"
)); ));
patients.add(new Patient( patients.add(new Patient(
"Martina", "Martina",
"Gerdsen", "Gerdsen",
convertStringToLocalDate("1954-08-12"), convertStringToLocalDate("1954-08-12"),
"5", "5",
"010" "010"
)); ));
patients.add(new Patient( patients.add(new Patient(
"Gertrud", "Gertrud",
"Franzen", "Franzen",
convertStringToLocalDate("1949-04-16"), convertStringToLocalDate("1949-04-16"),
"3", "3",
"002" "002"
)); ));
patients.add(new Patient( patients.add(new Patient(
"Ahmet", "Ahmet",
"Yilmaz", "Yilmaz",
convertStringToLocalDate("1941-02-22"), convertStringToLocalDate("1941-02-22"),
"3", "3",
"013" "013"
)); ));
patients.add(new Patient( patients.add(new Patient(
"Hans", "Hans",
"Neumann", "Neumann",
convertStringToLocalDate("1955-12-12"), convertStringToLocalDate("1955-12-12"),
"2", "2",
"001" "001"
)); ));
patients.add(new Patient( patients.add(new Patient(
"Elisabeth", "Elisabeth",
"Müller", "Müller",
convertStringToLocalDate("1958-03-07"), convertStringToLocalDate("1958-03-07"),
"5", "5",
"110" "110"
)); ));
PatientDao dao = DaoFactory.getInstance().createPatientDAO(); PatientDao dao = DaoFactory.getInstance().createPatientDAO();
for (Patient patient : patients){ for (Patient patient : patients) {
dao.create(patient); dao.create(patient);
} }
Map<String, Patient> patientsByName = new HashMap<>(); Map<String, Patient> patientsByName = new HashMap<>();
for (Patient patient : dao.readAll()){ for (Patient patient : dao.readAll()) {
patientsByName.put(patient.getFirstName(), patient); patientsByName.put(patient.getFirstName(), patient);
} }

View file

@ -1,17 +1,15 @@
package de.hitec.nhplus.fixtures; package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.Main;
import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.patient.Patient;
import de.hitec.nhplus.treatment.Treatment; 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.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate;
import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalTime; import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalTime;
@ -24,32 +22,16 @@ public class TreatmentFixture implements Fixture<Treatment> {
} }
@Override @Override
public void dropTable(Connection connection) { public void dropTable(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) { connection.createStatement().execute("DROP TABLE treatment");
statement.execute("DROP TABLE treatment");
} catch (SQLException exception) {
System.out.println(exception.getMessage());
}
} }
@Override @Override
public void setupTable(Connection connection) { public void setupTable(Connection connection) throws SQLException {
final String SQL = "CREATE TABLE IF NOT EXISTS treatment (" + final InputStream schema = Main.class.getResourceAsStream("/de/hitec/nhplus/treatment/database/Treatment.sql");
" id INTEGER PRIMARY KEY AUTOINCREMENT, " + assert schema != null;
" patientId INTEGER NOT NULL, " + final String SQL = new Scanner(schema, "UTF-8").useDelimiter("\\A").next();
" treatment_date TEXT NOT NULL, " + connection.createStatement().execute(SQL);
" 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());
}
} }
@Override @Override

View file

@ -3,6 +3,7 @@ package de.hitec.nhplus.nurse;
import static de.hitec.nhplus.utils.Validator.*; import static de.hitec.nhplus.utils.Validator.*;
import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.nurse.database.NurseDao;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;

View file

@ -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.datastorage.DaoImp;
import de.hitec.nhplus.nurse.Nurse;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;

View file

@ -1,6 +1,7 @@
package de.hitec.nhplus.patient; package de.hitec.nhplus.patient;
import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.patient.database.PatientDao;
import de.hitec.nhplus.utils.DateConverter; import de.hitec.nhplus.utils.DateConverter;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;

View file

@ -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.datastorage.DaoImp;
import de.hitec.nhplus.patient.Patient;
import de.hitec.nhplus.utils.DateConverter; import de.hitec.nhplus.utils.DateConverter;
import java.sql.Connection; import java.sql.Connection;

View file

@ -3,7 +3,8 @@ package de.hitec.nhplus.treatment;
import de.hitec.nhplus.Main; import de.hitec.nhplus.Main;
import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.patient.Patient; 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.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;

View file

@ -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.DaoFactory;
import de.hitec.nhplus.datastorage.DaoImp; import de.hitec.nhplus.datastorage.DaoImp;
import de.hitec.nhplus.treatment.Treatment;
import de.hitec.nhplus.utils.DateConverter; import de.hitec.nhplus.utils.DateConverter;
import java.sql.Connection; import java.sql.Connection;

View file

@ -11,11 +11,17 @@ module de.hitec.nhplus {
opens de.hitec.nhplus.main to javafx.base, javafx.fxml; opens de.hitec.nhplus.main to javafx.base, javafx.fxml;
exports de.hitec.nhplus.patient; 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; opens de.hitec.nhplus.patient to javafx.base, javafx.fxml;
exports de.hitec.nhplus.treatment; 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 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;
exports de.hitec.nhplus.nurse.database;
opens de.hitec.nhplus.nurse to javafx.base, javafx.fxml; opens de.hitec.nhplus.nurse to javafx.base, javafx.fxml;
opens de.hitec.nhplus.nurse.database to javafx.base, javafx.fxml;
} }

View file

@ -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
)

View file

@ -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
);

View file

@ -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
)