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] #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