Setup Utilities #12

Merged
SZUT-Dominik merged 7 commits from setup into main 2024-04-23 11:16:45 +00:00
6 changed files with 338 additions and 248 deletions
Showing only changes of commit 72781273e1 - Show all commits

View file

@ -35,4 +35,4 @@ template-lint: ## Corrects Code Linting in Templates
##|——[ Test Data ]——————————————————————————————————————————————————————————————————|
demo-data: ## Writes Demo Data to the local instance
@echo TODO
@mvn exec:java -Dexec.mainClass="de.hitec.nhplus.fixtures.Fixtures"

View file

@ -0,0 +1,12 @@
package de.hitec.nhplus.fixtures;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
public interface Fixture<T>
{
void dropTable(Connection connection);
void setupTable(Connection connection);
Map<String, T> load() throws SQLException;
}

View file

@ -0,0 +1,31 @@
package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.datastorage.ConnectionBuilder;
import de.hitec.nhplus.model.Patient;
import java.sql.Connection;
import java.util.Map;
public class Fixtures
{
public static void main(String[] args)
{
Connection connection = ConnectionBuilder.getConnection();
try
{
PatientFixture patientFixture = new PatientFixture();
patientFixture.dropTable(connection);
patientFixture.setupTable(connection);
Map<String, Patient> patientsByName = patientFixture.load();
TreatmentFixture treatmentFixture = new TreatmentFixture(patientsByName);
treatmentFixture.dropTable(connection);
treatmentFixture.setupTable(connection);
treatmentFixture.load();
} catch (Exception exception){
System.out.println(exception.getMessage());
}
}
}

View file

@ -0,0 +1,118 @@
package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.datastorage.PatientDao;
import de.hitec.nhplus.model.Patient;
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 static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate;
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());
}
}
@Override
public void setupTable(Connection connection)
{
final String SQL = "CREATE TABLE IF NOT EXISTS patient (" +
" pid INTEGER PRIMARY KEY AUTOINCREMENT, " +
" firstname TEXT NOT NULL, " +
" surname TEXT NOT NULL, " +
" dateOfBirth TEXT NOT NULL, " +
" carelevel TEXT NOT NULL, " +
" roomnumber TEXT NOT NULL, " +
" assets TEXt NOT NULL" +
");";
try (Statement statement = connection.createStatement())
{
statement.execute(SQL);
} catch (SQLException exception)
{
System.out.println(exception.getMessage());
}
}
@Override
public Map<String, Patient> load() throws SQLException
{
List<Patient> patients = new ArrayList<>();
patients.add(new Patient(
"Seppl",
"Herberger",
convertStringToLocalDate("1945-12-01"),
"4",
"202",
"vermögend"
));
patients.add(new Patient(
"Martina",
"Gerdsen",
convertStringToLocalDate("1954-08-12"),
"5",
"010",
"arm"
));
patients.add(new Patient(
"Gertrud",
"Franzen",
convertStringToLocalDate("1949-04-16"),
"3",
"002",
"normal"
));
patients.add(new Patient(
"Ahmet",
"Yilmaz",
convertStringToLocalDate("1941-02-22"),
"3",
"013",
"normal"
));
patients.add(new Patient(
"Hans",
"Neumann",
convertStringToLocalDate("1955-12-12"),
"2",
"001",
"sehr vermögend"
));
patients.add(new Patient(
"Elisabeth",
"Müller",
convertStringToLocalDate("1958-03-07"),
"5",
"110",
"arm"
));
PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO();
for (Patient patient : patients){
dao.create(patient);
}
Map<String, Patient> patientsByName = new HashMap<>();
for (Patient patient : dao.readAll()){
patientsByName.put(patient.getFirstName(), patient);
}
return patientsByName;
}
}

View file

@ -0,0 +1,176 @@
package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.datastorage.TreatmentDao;
import de.hitec.nhplus.model.Patient;
import de.hitec.nhplus.model.Treatment;
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 static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate;
import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalTime;
public class TreatmentFixture implements Fixture<Treatment>
{
private Map<String, Patient> patientsByName;
public TreatmentFixture(Map<String, Patient> patientsByName)
{
this.patientsByName = patientsByName;
}
@Override
public void dropTable(Connection connection)
{
try (Statement statement = connection.createStatement())
{
statement.execute("DROP TABLE treatment");
} catch (SQLException exception)
{
System.out.println(exception.getMessage());
}
}
@Override
public void setupTable(Connection connection)
{
final String SQL = "CREATE TABLE IF NOT EXISTS treatment (" +
" tid INTEGER PRIMARY KEY AUTOINCREMENT, " +
" pid 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 (pid) REFERENCES patient (pid) ON DELETE CASCADE " +
");";
try (Statement statement = connection.createStatement())
{
statement.execute(SQL);
} catch (SQLException exception)
{
System.out.println(exception.getMessage());
}
}
@Override
public Map<String, Treatment> load() throws SQLException
{
List<Treatment> treatments = new ArrayList<>();
Patient seppl = patientsByName.get("Seppl");
Patient martina = patientsByName.get("Martina");
Patient hans = patientsByName.get("Hans");
Patient ahmet = patientsByName.get("Ahmet");
Patient elisabeth = patientsByName.get("Elisabeth");
treatments.add(new Treatment(
1,
seppl.getPid(),
convertStringToLocalDate("2023-06-03"),
convertStringToLocalTime("11:00"),
convertStringToLocalTime("15:00"),
"Gespräch",
"Der Patient hat enorme Angstgefühle und glaubt, er sei überfallen worden."
+ "Ihm seien alle Wertsachen gestohlen worden."
+ "Patient beruhigt sich erst, als alle Wertsachen im Zimmer gefunden worden sind."
));
treatments.add(new Treatment(
2,
seppl.getPid(),
convertStringToLocalDate("2023-06-05"),
convertStringToLocalTime("11:00"),
convertStringToLocalTime("12:30"),
"Gespräch",
"Patient irrt auf der Suche nach gestohlenen Wertsachen durch die Etage "
+ "und bezichtigt andere Bewohner des Diebstahls."
+ "Patient wird in seinen Raum zurückbegleitet und erhält Beruhigungsmittel."
));
treatments.add(new Treatment(
3,
martina.getPid(),
convertStringToLocalDate("2023-06-04"),
convertStringToLocalTime("07:30"),
convertStringToLocalTime("08:00"),
"Waschen",
"Patient mit Waschlappen gewaschen und frisch angezogen. Patient gewendet."
));
treatments.add(new Treatment(
4,
seppl.getPid(),
convertStringToLocalDate("2023-06-06"),
convertStringToLocalTime("15:10"),
convertStringToLocalTime("16:00"),
"Spaziergang",
"Spaziergang im Park, Patient döst im Rollstuhl ein")
);
treatments.add(new Treatment(
8,
seppl.getPid(),
convertStringToLocalDate("2023-06-08"),
convertStringToLocalTime("15:00"),
convertStringToLocalTime("16:00"),
"Spaziergang",
"Parkspaziergang; Patient ist heute lebhafter und hat klare Momente; erzählt von seiner Tochter")
);
treatments.add(new Treatment(
9,
martina.getPid(),
convertStringToLocalDate("2023-06-07"),
convertStringToLocalTime("11:00"),
convertStringToLocalTime("11:30"),
"Waschen",
"Waschen per Dusche auf einem Stuhl; Patientin gewendet;")
);
treatments.add(new Treatment(
12,
hans.getPid(),
convertStringToLocalDate("2023-06-08"),
convertStringToLocalTime("15:00"),
convertStringToLocalTime("15:30"),
"Physiotherapie",
"Übungen zur Stabilisation und Mobilisierung der Rückenmuskulatur")
);
treatments.add(new Treatment(
14,
ahmet.getPid(),
convertStringToLocalDate("2023-08-24"),
convertStringToLocalTime("09:30"),
convertStringToLocalTime("10:15"),
"KG",
"Lympfdrainage"));
treatments.add(new Treatment(
16,
elisabeth.getPid(),
convertStringToLocalDate("2023-08-31"),
convertStringToLocalTime("13:30"),
convertStringToLocalTime("13:45"),
"Toilettengang",
"Hilfe beim Toilettengang; Patientin klagt über Schmerzen beim Stuhlgang. Gabe von Iberogast")
);
treatments.add(new Treatment(
17,
elisabeth.getPid(),
convertStringToLocalDate("2023-09-01"),
convertStringToLocalTime("16:00"),
convertStringToLocalTime("17:00"),
"KG",
"Massage der Extremitäten zur Verbesserung der Durchblutung")
);
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao();
Map<String, Treatment> treatmentsById = new HashMap<>();
for (Treatment treatment : treatments){
dao.create(treatment);
treatmentsById.put(String.valueOf(treatment.getTid()), treatment);
}
return treatmentsById;
}
}

View file

@ -1,247 +0,0 @@
package de.hitec.nhplus.utils;
import de.hitec.nhplus.datastorage.ConnectionBuilder;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.datastorage.PatientDao;
import de.hitec.nhplus.datastorage.TreatmentDao;
import de.hitec.nhplus.model.Patient;
import de.hitec.nhplus.model.Treatment;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate;
import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalTime;
/**
* Call static class provides to static methods to set up and wipe the database. It uses the class ConnectionBuilder
* and its path to build up the connection to the database. The class is executable. Executing the class will build
* up a connection to the database and calls setUpDb() to wipe the database, build up a clean database and fill the
* database with some test data.
*/
public class SetUpDB {
/**
* This method wipes the database by dropping the tables. Then the method calls DDL statements to build it up from
* scratch and DML statements to fill the database with hard coded test data.
*/
public static void setUpDb() {
Connection connection = ConnectionBuilder.getConnection();
SetUpDB.wipeDb(connection);
SetUpDB.setUpTablePatient(connection);
SetUpDB.setUpTableTreatment(connection);
SetUpDB.setUpPatients();
SetUpDB.setUpTreatments();
}
/**
* This method wipes the database by dropping the tables.
*/
public static void wipeDb(Connection connection) {
try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE patient");
statement.execute("DROP TABLE treatment");
} catch (SQLException exception) {
System.out.println(exception.getMessage());
}
}
private static void setUpTablePatient(Connection connection) {
final String SQL = "CREATE TABLE IF NOT EXISTS patient (" +
" pid INTEGER PRIMARY KEY AUTOINCREMENT, " +
" firstname TEXT NOT NULL, " +
" surname TEXT NOT NULL, " +
" dateOfBirth TEXT NOT NULL, " +
" carelevel TEXT NOT NULL, " +
" roomnumber TEXT NOT NULL, " +
" assets TEXt NOT NULL" +
");";
try (Statement statement = connection.createStatement()) {
statement.execute(SQL);
} catch (SQLException exception) {
System.out.println(exception.getMessage());
}
}
private static void setUpTableTreatment(Connection connection) {
final String SQL = "CREATE TABLE IF NOT EXISTS treatment (" +
" tid INTEGER PRIMARY KEY AUTOINCREMENT, " +
" pid 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 (pid) REFERENCES patient (pid) ON DELETE CASCADE " +
");";
try (Statement statement = connection.createStatement()) {
statement.execute(SQL);
} catch (SQLException exception) {
System.out.println(exception.getMessage());
}
}
private static void setUpPatients() {
try {
PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO();
dao.create(new Patient(
"Seppl",
"Herberger",
convertStringToLocalDate("1945-12-01"),
"4",
"202",
"vermögend"
));
dao.create(new Patient(
"Martina",
"Gerdsen",
convertStringToLocalDate("1954-08-12"),
"5",
"010",
"arm"
));
dao.create(new Patient(
"Gertrud",
"Franzen",
convertStringToLocalDate("1949-04-16"),
"3",
"002",
"normal"
));
dao.create(new Patient(
"Ahmet",
"Yilmaz",
convertStringToLocalDate("1941-02-22"),
"3",
"013",
"normal"
));
dao.create(new Patient(
"Hans",
"Neumann",
convertStringToLocalDate("1955-12-12"),
"2",
"001",
"sehr vermögend"
));
dao.create(new Patient(
"Elisabeth",
"Müller",
convertStringToLocalDate("1958-03-07"),
"5",
"110",
"arm"
));
} catch (SQLException exception) {
exception.printStackTrace();
}
}
private static void setUpTreatments() {
try {
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao();
dao.create(new Treatment(
1,
1,
convertStringToLocalDate("2023-06-03"),
convertStringToLocalTime("11:00"),
convertStringToLocalTime("15:00"),
"Gespräch",
"Der Patient hat enorme Angstgefühle und glaubt, er sei überfallen worden."
+ "Ihm seien alle Wertsachen gestohlen worden."
+ "Patient beruhigt sich erst, als alle Wertsachen im Zimmer gefunden worden sind."
));
dao.create(new Treatment(
2,
1,
convertStringToLocalDate("2023-06-05"),
convertStringToLocalTime("11:00"),
convertStringToLocalTime("12:30"),
"Gespräch",
"Patient irrt auf der Suche nach gestohlenen Wertsachen durch die Etage "
+ "und bezichtigt andere Bewohner des Diebstahls."
+ "Patient wird in seinen Raum zurückbegleitet und erhält Beruhigungsmittel."
));
dao.create(new Treatment(
3,
2,
convertStringToLocalDate("2023-06-04"),
convertStringToLocalTime("07:30"),
convertStringToLocalTime("08:00"),
"Waschen",
"Patient mit Waschlappen gewaschen und frisch angezogen. Patient gewendet."
));
dao.create(new Treatment(
4,
1,
convertStringToLocalDate("2023-06-06"),
convertStringToLocalTime("15:10"),
convertStringToLocalTime("16:00"),
"Spaziergang",
"Spaziergang im Park, Patient döst im Rollstuhl ein")
);
dao.create(new Treatment(
8,
1,
convertStringToLocalDate("2023-06-08"),
convertStringToLocalTime("15:00"),
convertStringToLocalTime("16:00"),
"Spaziergang",
"Parkspaziergang; Patient ist heute lebhafter und hat klare Momente; erzählt von seiner Tochter")
);
dao.create(new Treatment(
9,
2,
convertStringToLocalDate("2023-06-07"),
convertStringToLocalTime("11:00"),
convertStringToLocalTime("11:30"),
"Waschen",
"Waschen per Dusche auf einem Stuhl; Patientin gewendet;")
);
dao.create(new Treatment(
12,
5,
convertStringToLocalDate("2023-06-08"),
convertStringToLocalTime("15:00"),
convertStringToLocalTime("15:30"),
"Physiotherapie",
"Übungen zur Stabilisation und Mobilisierung der Rückenmuskulatur")
);
dao.create(new Treatment(
14,
4,
convertStringToLocalDate("2023-08-24"),
convertStringToLocalTime("09:30"),
convertStringToLocalTime("10:15"),
"KG",
"Lympfdrainage"));
dao.create(new Treatment(
16,
6,
convertStringToLocalDate("2023-08-31"),
convertStringToLocalTime("13:30"),
convertStringToLocalTime("13:45"),
"Toilettengang",
"Hilfe beim Toilettengang; Patientin klagt über Schmerzen beim Stuhlgang. Gabe von Iberogast")
);
dao.create(new Treatment(
17,
6,
convertStringToLocalDate("2023-09-01"),
convertStringToLocalTime("16:00"),
convertStringToLocalTime("17:00"),
"KG",
"Massage der Extremitäten zur Verbesserung der Durchblutung")
);
} catch (SQLException exception) {
exception.printStackTrace();
}
}
public static void main(String[] args) {
SetUpDB.setUpDb();
}
}