#32: Refactor Database Usage, to be more consistent and understandable #33
16 changed files with 121 additions and 134 deletions
|
@ -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>
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -1,56 +1,34 @@
|
||||||
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(
|
||||||
|
@ -97,11 +75,11 @@ public class PatientFixture implements Fixture<Patient>
|
||||||
));
|
));
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
|
@ -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
|
||||||
|
);
|
|
@ -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
|
||||||
|
)
|
Loading…
Reference in a new issue