#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"?>
<project version="4">
<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/treatment/TreatmentDao.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/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" />
</component>
</project>

View file

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

View file

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

View file

@ -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<Nurse> {
@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<Nurse> {
"9876543210"
));
NurseDao dao = DaoFactory.getInstance().createNurseDAO();
for(Nurse nurse : nurses){
for (Nurse nurse : nurses) {
dao.create(nurse);
}
Map<String, Nurse> nursesByName = new HashMap<>();
for (Nurse nurse : dao.readAll()){
for (Nurse nurse : dao.readAll()) {
nursesByName.put(nurse.getFirstName(), nurse);
}
return nursesByName;

View file

@ -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<Patient>
{
public class PatientFixture implements Fixture<Patient> {
@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<String, Patient> load() throws SQLException
{
public Map<String, Patient> load() throws SQLException {
List<Patient> 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<String, Patient> patientsByName = new HashMap<>();
for (Patient patient : dao.readAll()){
for (Patient patient : dao.readAll()) {
patientsByName.put(patient.getFirstName(), patient);
}

View file

@ -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<Treatment> {
}
@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

View file

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

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.nurse.Nurse;
import java.sql.Connection;
import java.sql.PreparedStatement;

View file

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

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

View file

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

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

View file

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

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
)