Refaktoring #21

Merged
SZUT-Dominik merged 12 commits from refactor into main 2024-04-30 12:21:17 +00:00
18 changed files with 114 additions and 278 deletions
Showing only changes of commit 2e5ca8f4dc - Show all commits

Binary file not shown.

View file

@ -6,11 +6,11 @@ import java.util.List;
public interface Dao<T> { public interface Dao<T> {
void create(T t) throws SQLException; void create(T t) throws SQLException;
T read(long key) throws SQLException; T read(int id) throws SQLException;
List<T> readAll() throws SQLException; List<T> readAll() throws SQLException;
void update(T t) throws SQLException; void update(T t) throws SQLException;
void deleteById(long key) throws SQLException; void deleteById(int id) throws SQLException;
} }

View file

@ -17,7 +17,7 @@ public abstract class DaoImp<T> implements Dao<T> {
} }
@Override @Override
public T read(long key) throws SQLException { public T read(int key) throws SQLException {
T object = null; T object = null;
ResultSet result = getReadByIDStatement(key).executeQuery(); ResultSet result = getReadByIDStatement(key).executeQuery();
if (result.next()) { if (result.next()) {
@ -37,7 +37,7 @@ public abstract class DaoImp<T> implements Dao<T> {
} }
@Override @Override
public void deleteById(long key) throws SQLException { public void deleteById(int key) throws SQLException {
getDeleteStatement(key).executeUpdate(); getDeleteStatement(key).executeUpdate();
} }
@ -47,11 +47,11 @@ public abstract class DaoImp<T> implements Dao<T> {
protected abstract PreparedStatement getCreateStatement(T t); protected abstract PreparedStatement getCreateStatement(T t);
protected abstract PreparedStatement getReadByIDStatement(long key); protected abstract PreparedStatement getReadByIDStatement(int key);
protected abstract PreparedStatement getReadAllStatement(); protected abstract PreparedStatement getReadAllStatement();
protected abstract PreparedStatement getUpdateStatement(T t); protected abstract PreparedStatement getUpdateStatement(T t);
protected abstract PreparedStatement getDeleteStatement(long key); protected abstract PreparedStatement getDeleteStatement(int key);
} }

View file

@ -32,7 +32,7 @@ public class PatientFixture implements Fixture<Patient>
public void setupTable(Connection connection) public void setupTable(Connection connection)
{ {
final String SQL = "CREATE TABLE IF NOT EXISTS patient (" + final String SQL = "CREATE TABLE IF NOT EXISTS patient (" +
" pid INTEGER PRIMARY KEY AUTOINCREMENT, " + " id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" firstname TEXT NOT NULL, " + " firstname TEXT NOT NULL, " +
" surname TEXT NOT NULL, " + " surname TEXT NOT NULL, " +
" dateOfBirth TEXT NOT NULL, " + " dateOfBirth TEXT NOT NULL, " +

View file

@ -41,14 +41,14 @@ public class TreatmentFixture implements Fixture<Treatment>
public void setupTable(Connection connection) public void setupTable(Connection connection)
{ {
final String SQL = "CREATE TABLE IF NOT EXISTS treatment (" + final String SQL = "CREATE TABLE IF NOT EXISTS treatment (" +
" tid INTEGER PRIMARY KEY AUTOINCREMENT, " + " id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" pid INTEGER NOT NULL, " + " patientId INTEGER NOT NULL, " +
" treatment_date TEXT NOT NULL, " + " treatment_date TEXT NOT NULL, " +
" begin TEXT NOT NULL, " + " begin TEXT NOT NULL, " +
" end TEXT NOT NULL, " + " end TEXT NOT NULL, " +
" description TEXT NOT NULL, " + " description TEXT NOT NULL, " +
" remark TEXT NOT NULL," + " remark TEXT NOT NULL," +
" FOREIGN KEY (pid) REFERENCES patient (pid) ON DELETE CASCADE " + " FOREIGN KEY (patientId) REFERENCES patient (id) ON DELETE CASCADE " +
");"; ");";
try (Statement statement = connection.createStatement()) try (Statement statement = connection.createStatement())
@ -73,7 +73,7 @@ public class TreatmentFixture implements Fixture<Treatment>
treatments.add(new Treatment( treatments.add(new Treatment(
1, 1,
seppl.getPid(), seppl.getId(),
convertStringToLocalDate("2023-06-03"), convertStringToLocalDate("2023-06-03"),
convertStringToLocalTime("11:00"), convertStringToLocalTime("11:00"),
convertStringToLocalTime("15:00"), convertStringToLocalTime("15:00"),
@ -84,7 +84,7 @@ public class TreatmentFixture implements Fixture<Treatment>
)); ));
treatments.add(new Treatment( treatments.add(new Treatment(
2, 2,
seppl.getPid(), seppl.getId(),
convertStringToLocalDate("2023-06-05"), convertStringToLocalDate("2023-06-05"),
convertStringToLocalTime("11:00"), convertStringToLocalTime("11:00"),
convertStringToLocalTime("12:30"), convertStringToLocalTime("12:30"),
@ -95,7 +95,7 @@ public class TreatmentFixture implements Fixture<Treatment>
)); ));
treatments.add(new Treatment( treatments.add(new Treatment(
3, 3,
martina.getPid(), martina.getId(),
convertStringToLocalDate("2023-06-04"), convertStringToLocalDate("2023-06-04"),
convertStringToLocalTime("07:30"), convertStringToLocalTime("07:30"),
convertStringToLocalTime("08:00"), convertStringToLocalTime("08:00"),
@ -104,7 +104,7 @@ public class TreatmentFixture implements Fixture<Treatment>
)); ));
treatments.add(new Treatment( treatments.add(new Treatment(
4, 4,
seppl.getPid(), seppl.getId(),
convertStringToLocalDate("2023-06-06"), convertStringToLocalDate("2023-06-06"),
convertStringToLocalTime("15:10"), convertStringToLocalTime("15:10"),
convertStringToLocalTime("16:00"), convertStringToLocalTime("16:00"),
@ -113,7 +113,7 @@ public class TreatmentFixture implements Fixture<Treatment>
); );
treatments.add(new Treatment( treatments.add(new Treatment(
8, 8,
seppl.getPid(), seppl.getId(),
convertStringToLocalDate("2023-06-08"), convertStringToLocalDate("2023-06-08"),
convertStringToLocalTime("15:00"), convertStringToLocalTime("15:00"),
convertStringToLocalTime("16:00"), convertStringToLocalTime("16:00"),
@ -122,7 +122,7 @@ public class TreatmentFixture implements Fixture<Treatment>
); );
treatments.add(new Treatment( treatments.add(new Treatment(
9, 9,
martina.getPid(), martina.getId(),
convertStringToLocalDate("2023-06-07"), convertStringToLocalDate("2023-06-07"),
convertStringToLocalTime("11:00"), convertStringToLocalTime("11:00"),
convertStringToLocalTime("11:30"), convertStringToLocalTime("11:30"),
@ -131,7 +131,7 @@ public class TreatmentFixture implements Fixture<Treatment>
); );
treatments.add(new Treatment( treatments.add(new Treatment(
12, 12,
hans.getPid(), hans.getId(),
convertStringToLocalDate("2023-06-08"), convertStringToLocalDate("2023-06-08"),
convertStringToLocalTime("15:00"), convertStringToLocalTime("15:00"),
convertStringToLocalTime("15:30"), convertStringToLocalTime("15:30"),
@ -140,7 +140,7 @@ public class TreatmentFixture implements Fixture<Treatment>
); );
treatments.add(new Treatment( treatments.add(new Treatment(
14, 14,
ahmet.getPid(), ahmet.getId(),
convertStringToLocalDate("2023-08-24"), convertStringToLocalDate("2023-08-24"),
convertStringToLocalTime("09:30"), convertStringToLocalTime("09:30"),
convertStringToLocalTime("10:15"), convertStringToLocalTime("10:15"),
@ -148,7 +148,7 @@ public class TreatmentFixture implements Fixture<Treatment>
"Lympfdrainage")); "Lympfdrainage"));
treatments.add(new Treatment( treatments.add(new Treatment(
16, 16,
elisabeth.getPid(), elisabeth.getId(),
convertStringToLocalDate("2023-08-31"), convertStringToLocalDate("2023-08-31"),
convertStringToLocalTime("13:30"), convertStringToLocalTime("13:30"),
convertStringToLocalTime("13:45"), convertStringToLocalTime("13:45"),
@ -157,7 +157,7 @@ public class TreatmentFixture implements Fixture<Treatment>
); );
treatments.add(new Treatment( treatments.add(new Treatment(
17, 17,
elisabeth.getPid(), elisabeth.getId(),
convertStringToLocalDate("2023-09-01"), convertStringToLocalDate("2023-09-01"),
convertStringToLocalTime("16:00"), convertStringToLocalTime("16:00"),
convertStringToLocalTime("17:00"), convertStringToLocalTime("17:00"),
@ -169,7 +169,7 @@ public class TreatmentFixture implements Fixture<Treatment>
Map<String, Treatment> treatmentsById = new HashMap<>(); Map<String, Treatment> treatmentsById = new HashMap<>();
for (Treatment treatment : treatments){ for (Treatment treatment : treatments){
dao.create(treatment); dao.create(treatment);
treatmentsById.put(String.valueOf(treatment.getTid()), treatment); treatmentsById.put(String.valueOf(treatment.getId()), treatment);
} }
return treatmentsById; return treatmentsById;
} }

View file

@ -19,7 +19,7 @@ public class MainWindowController {
@FXML @FXML
private void handleShowAllPatient(ActionEvent event) { private void handleShowAllPatient(ActionEvent event) {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/AllPatientView.fxml")); FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/patient/AllPatientView.fxml"));
try { try {
mainBorderPane.setCenter(loader.load()); mainBorderPane.setCenter(loader.load());
} catch (IOException exception) { } catch (IOException exception) {
@ -29,7 +29,7 @@ public class MainWindowController {
@FXML @FXML
private void handleShowAllTreatments(ActionEvent event) { private void handleShowAllTreatments(ActionEvent event) {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/AllTreatmentView.fxml")); FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/treatment/AllTreatmentView.fxml"));
try { try {
mainBorderPane.setCenter(loader.load()); mainBorderPane.setCenter(loader.load());
} catch (IOException exception) { } catch (IOException exception) {

View file

@ -55,7 +55,7 @@ public class AllPatientController {
public void initialize() { public void initialize() {
this.readAllAndShowInTableView(); this.readAllAndShowInTableView();
this.columnId.setCellValueFactory(new PropertyValueFactory<>("pid")); this.columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
this.columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName")); this.columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName"));
this.columnFirstName.setCellFactory(TextFieldTableCell.forTableColumn()); this.columnFirstName.setCellFactory(TextFieldTableCell.forTableColumn());
@ -189,7 +189,7 @@ public class AllPatientController {
Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem(); Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem();
if (selectedItem != null) { if (selectedItem != null) {
try { try {
DaoFactory.getDaoFactory().createPatientDAO().deleteById(selectedItem.getPid()); DaoFactory.getDaoFactory().createPatientDAO().deleteById(selectedItem.getId());
this.tableView.getItems().remove(selectedItem); this.tableView.getItems().remove(selectedItem);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();

View file

@ -3,6 +3,7 @@ package de.hitec.nhplus.patient;
import de.hitec.nhplus.main.Person; import de.hitec.nhplus.main.Person;
import de.hitec.nhplus.treatment.Treatment; import de.hitec.nhplus.treatment.Treatment;
import de.hitec.nhplus.utils.DateConverter; import de.hitec.nhplus.utils.DateConverter;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleLongProperty; import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
@ -10,26 +11,13 @@ import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* Patients live in a NURSING home and are treated by nurses.
*/
public class Patient extends Person { public class Patient extends Person {
private SimpleLongProperty pid; private SimpleIntegerProperty id;
private final SimpleStringProperty dateOfBirth; private final SimpleStringProperty dateOfBirth;
private final SimpleStringProperty careLevel; private final SimpleStringProperty careLevel;
private final SimpleStringProperty roomNumber; private final SimpleStringProperty roomNumber;
private final List<Treatment> allTreatments = new ArrayList<>(); private final List<Treatment> allTreatments = new ArrayList<>();
/**
* Constructor to initiate an object of class <code>Patient</code> with the given parameter. Use this constructor
* to initiate objects, which are not persisted yet, because it will not have a patient id (pid).
*
* @param firstName First name of the patient.
* @param surname Last name of the patient.
* @param dateOfBirth Date of birth of the patient.
* @param careLevel Care level of the patient.
* @param roomNumber Room number of the patient.
*/
public Patient( public Patient(
String firstName, String firstName,
String surname, String surname,
@ -43,19 +31,8 @@ public class Patient extends Person {
this.roomNumber = new SimpleStringProperty(roomNumber); this.roomNumber = new SimpleStringProperty(roomNumber);
} }
/**
* Constructor to initiate an object of class <code>Patient</code> with the given parameter. Use this constructor
* to initiate objects, which are already persisted and have a patient id (pid).
*
* @param pid Patient id.
* @param firstName First name of the patient.
* @param surname Last name of the patient.
* @param dateOfBirth Date of birth of the patient.
* @param careLevel Care level of the patient.
* @param roomNumber Room number of the patient.
*/
public Patient( public Patient(
long pid, int id,
String firstName, String firstName,
String surname, String surname,
LocalDate dateOfBirth, LocalDate dateOfBirth,
@ -63,18 +40,18 @@ public class Patient extends Person {
String roomNumber String roomNumber
) { ) {
super(firstName, surname); super(firstName, surname);
this.pid = new SimpleLongProperty(pid); this.id = new SimpleIntegerProperty(id);
this.dateOfBirth = new SimpleStringProperty(DateConverter.convertLocalDateToString(dateOfBirth)); this.dateOfBirth = new SimpleStringProperty(DateConverter.convertLocalDateToString(dateOfBirth));
this.careLevel = new SimpleStringProperty(careLevel); this.careLevel = new SimpleStringProperty(careLevel);
this.roomNumber = new SimpleStringProperty(roomNumber); this.roomNumber = new SimpleStringProperty(roomNumber);
} }
public long getPid() { public int getId() {
return pid.get(); return id.get();
} }
public SimpleLongProperty pidProperty() { public SimpleIntegerProperty pidProperty() {
return pid; return id;
} }
public String getDateOfBirth() { public String getDateOfBirth() {
@ -85,11 +62,6 @@ public class Patient extends Person {
return dateOfBirth; return dateOfBirth;
} }
/**
* Stores the given string as new <code>birthOfDate</code>.
*
* @param dateOfBirth as string in the following format: YYYY-MM-DD.
*/
public void setDateOfBirth(String dateOfBirth) { public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth.set(dateOfBirth); this.dateOfBirth.set(dateOfBirth);
} }
@ -119,13 +91,6 @@ public class Patient extends Person {
this.roomNumber.set(roomNumber); this.roomNumber.set(roomNumber);
} }
/**
* Adds a treatment to the list of treatments, if the list does not already contain the treatment.
*
* @param treatment Treatment to add.
* @return False, if the treatment was already part of the list, else true.
*/
public boolean add(Treatment treatment) { public boolean add(Treatment treatment) {
if (this.allTreatments.contains(treatment)) { if (this.allTreatments.contains(treatment)) {
return false; return false;
@ -135,7 +100,7 @@ public class Patient extends Person {
} }
public String toString() { public String toString() {
return "Patient" + "\nMNID: " + this.pid + return "Patient" + "\nMNID: " + this.id +
"\nFirstname: " + this.getFirstName() + "\nFirstname: " + this.getFirstName() +
"\nSurname: " + this.getSurname() + "\nSurname: " + this.getSurname() +
"\nBirthday: " + this.dateOfBirth + "\nBirthday: " + this.dateOfBirth +

View file

@ -1,34 +1,20 @@
package de.hitec.nhplus.patient; package de.hitec.nhplus.patient;
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.*; import java.sql.Connection;
import java.time.LocalDate; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
/**
* Implements the Interface <code>DaoImp</code>. Overrides methods to generate specific <code>PreparedStatements</code>,
* to execute the specific SQL Statements.
*/
public class PatientDao extends DaoImp<Patient> { public class PatientDao extends DaoImp<Patient> {
/**
* The constructor initiates an object of <code>PatientDao</code> and passes the connection to its super class.
*
* @param connection Object of <code>Connection</code> to execute the SQL-statements.
*/
public PatientDao(Connection connection) { public PatientDao(Connection connection) {
super(connection); super(connection);
} }
/**
* Generates a <code>PreparedStatement</code> to persist the given object of <code>Patient</code>.
*
* @param patient Object of <code>Patient</code> to persist.
* @return <code>PreparedStatement</code> to insert the given patient.
*/
@Override @Override
protected PreparedStatement getCreateStatement(Patient patient) { protected PreparedStatement getCreateStatement(Patient patient) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
@ -47,31 +33,19 @@ public class PatientDao extends DaoImp<Patient> {
return preparedStatement; return preparedStatement;
} }
/**
* Generates a <code>PreparedStatement</code> to query a patient by a given patient id (pid).
*
* @param pid Patient id to query.
* @return <code>PreparedStatement</code> to query the patient.
*/
@Override @Override
protected PreparedStatement getReadByIDStatement(long pid) { protected PreparedStatement getReadByIDStatement(int id) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
final String SQL = "SELECT * FROM patient WHERE pid = ?"; final String SQL = "SELECT * FROM patient WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, pid); preparedStatement.setInt(1, id);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }
return preparedStatement; return preparedStatement;
} }
/**
* Maps a <code>ResultSet</code> of one patient to an object of <code>Patient</code>.
*
* @param result ResultSet with a single row. Columns will be mapped to an object of class <code>Patient</code>.
* @return Object of class <code>Patient</code> with the data from the resultSet.
*/
@Override @Override
protected Patient getInstanceFromResultSet(ResultSet result) throws SQLException { protected Patient getInstanceFromResultSet(ResultSet result) throws SQLException {
return new Patient( return new Patient(
@ -83,11 +57,6 @@ public class PatientDao extends DaoImp<Patient> {
result.getString(6)); result.getString(6));
} }
/**
* Generates a <code>PreparedStatement</code> to query all patients.
*
* @return <code>PreparedStatement</code> to query all patients.
*/
@Override @Override
protected PreparedStatement getReadAllStatement() { protected PreparedStatement getReadAllStatement() {
PreparedStatement statement = null; PreparedStatement statement = null;
@ -100,33 +69,22 @@ public class PatientDao extends DaoImp<Patient> {
return statement; return statement;
} }
/**
* Maps a <code>ResultSet</code> of all patients to an <code>ArrayList</code> of <code>Patient</code> objects.
*
* @param result ResultSet with all rows. The Columns will be mapped to objects of class <code>Patient</code>.
* @return <code>ArrayList</code> with objects of class <code>Patient</code> of all rows in the
* <code>ResultSet</code>.
*/
@Override @Override
protected ArrayList<Patient> getListFromResultSet(ResultSet result) throws SQLException { protected ArrayList<Patient> getListFromResultSet(ResultSet result) throws SQLException {
ArrayList<Patient> list = new ArrayList<>(); ArrayList<Patient> list = new ArrayList<>();
while (result.next()) { while (result.next()) {
LocalDate date = DateConverter.convertStringToLocalDate(result.getString(4)); Patient patient = new Patient(
Patient patient = new Patient(result.getInt(1), result.getString(2), result.getInt(1),
result.getString(3), date, result.getString(2),
result.getString(5), result.getString(6)); result.getString(3),
DateConverter.convertStringToLocalDate(result.getString(4)),
result.getString(5),
result.getString(6));
list.add(patient); list.add(patient);
} }
return list; return list;
} }
/**
* Generates a <code>PreparedStatement</code> to update the given patient, identified
* by the id of the patient (pid).
*
* @param patient Patient object to update.
* @return <code>PreparedStatement</code> to update the given patient.
*/
@Override @Override
protected PreparedStatement getUpdateStatement(Patient patient) { protected PreparedStatement getUpdateStatement(Patient patient) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
@ -138,33 +96,27 @@ public class PatientDao extends DaoImp<Patient> {
"dateOfBirth = ?, " + "dateOfBirth = ?, " +
"carelevel = ?, " + "carelevel = ?, " +
"roomnumber = ?, " + "roomnumber = ?, " +
"WHERE pid = ?"; "WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setString(1, patient.getFirstName()); preparedStatement.setString(1, patient.getFirstName());
preparedStatement.setString(2, patient.getSurname()); preparedStatement.setString(2, patient.getSurname());
preparedStatement.setString(3, patient.getDateOfBirth()); preparedStatement.setString(3, patient.getDateOfBirth());
preparedStatement.setString(4, patient.getCareLevel()); preparedStatement.setString(4, patient.getCareLevel());
preparedStatement.setString(5, patient.getRoomNumber()); preparedStatement.setString(5, patient.getRoomNumber());
preparedStatement.setLong(6, patient.getPid()); preparedStatement.setInt(6, patient.getId());
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }
return preparedStatement; return preparedStatement;
} }
/**
* Generates a <code>PreparedStatement</code> to delete a patient with the given id.
*
* @param pid Id of the patient to delete.
* @return <code>PreparedStatement</code> to delete patient with the given id.
*/
@Override @Override
protected PreparedStatement getDeleteStatement(long pid) { protected PreparedStatement getDeleteStatement(int id) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
final String SQL = "DELETE FROM patient WHERE pid = ?"; final String SQL = "DELETE FROM patient WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, pid); preparedStatement.setInt(1, id);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }

View file

@ -28,7 +28,7 @@ public class AllTreatmentController
private TableColumn<Treatment, Integer> columnId; private TableColumn<Treatment, Integer> columnId;
@FXML @FXML
private TableColumn<Treatment, Integer> columnPid; private TableColumn<Treatment, Integer> columnPatientId;
@FXML @FXML
private TableColumn<Treatment, String> columnDate; private TableColumn<Treatment, String> columnDate;
@ -59,8 +59,8 @@ public class AllTreatmentController
comboBoxPatientSelection.setItems(patientSelection); comboBoxPatientSelection.setItems(patientSelection);
comboBoxPatientSelection.getSelectionModel().select(0); comboBoxPatientSelection.getSelectionModel().select(0);
this.columnId.setCellValueFactory(new PropertyValueFactory<>("tid")); this.columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
this.columnPid.setCellValueFactory(new PropertyValueFactory<>("pid")); this.columnPatientId.setCellValueFactory(new PropertyValueFactory<>("patientId"));
this.columnDate.setCellValueFactory(new PropertyValueFactory<>("date")); this.columnDate.setCellValueFactory(new PropertyValueFactory<>("date"));
this.columnBegin.setCellValueFactory(new PropertyValueFactory<>("begin")); this.columnBegin.setCellValueFactory(new PropertyValueFactory<>("begin"));
this.columnEnd.setCellValueFactory(new PropertyValueFactory<>("end")); this.columnEnd.setCellValueFactory(new PropertyValueFactory<>("end"));
@ -135,7 +135,7 @@ public class AllTreatmentController
{ {
try try
{ {
this.treatments.addAll(this.dao.readTreatmentsByPid(patient.getPid())); this.treatments.addAll(this.dao.readTreatmentsByPid(patient.getId()));
} catch (SQLException exception) } catch (SQLException exception)
{ {
exception.printStackTrace(); exception.printStackTrace();
@ -163,7 +163,7 @@ public class AllTreatmentController
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao(); TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao();
try try
{ {
dao.deleteById(t.getTid()); dao.deleteById(t.getId());
} catch (SQLException exception) } catch (SQLException exception)
{ {
exception.printStackTrace(); exception.printStackTrace();
@ -206,7 +206,7 @@ public class AllTreatmentController
{ {
try try
{ {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/NewTreatmentView.fxml")); FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/treatment/NewTreatmentView.fxml"));
AnchorPane pane = loader.load(); AnchorPane pane = loader.load();
Scene scene = new Scene(pane); Scene scene = new Scene(pane);
@ -229,7 +229,7 @@ public class AllTreatmentController
{ {
try try
{ {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/TreatmentView.fxml")); FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/treatment/TreatmentView.fxml"));
AnchorPane pane = loader.load(); AnchorPane pane = loader.load();
Scene scene = new Scene(pane); Scene scene = new Scene(pane);

View file

@ -111,7 +111,7 @@ public class NewTreatmentController
LocalTime end = DateConverter.convertStringToLocalTime(textFieldEnd.getText()); LocalTime end = DateConverter.convertStringToLocalTime(textFieldEnd.getText());
String description = textFieldDescription.getText(); String description = textFieldDescription.getText();
String remarks = textAreaRemarks.getText(); String remarks = textAreaRemarks.getText();
Treatment treatment = new Treatment(patient.getPid(), date, begin, end, description, remarks); Treatment treatment = new Treatment(patient.getId(), date, begin, end, description, remarks);
createTreatment(treatment); createTreatment(treatment);
controller.readAllAndShowInTableView(); controller.readAllAndShowInTableView();
stage.close(); stage.close();

View file

@ -6,28 +6,17 @@ import java.time.LocalDate;
import java.time.LocalTime; import java.time.LocalTime;
public class Treatment { public class Treatment {
private long tid; private int id;
private final long pid; private final int patientId;
private LocalDate date; private LocalDate date;
private LocalTime begin; private LocalTime begin;
private LocalTime end; private LocalTime end;
private String description; private String description;
private String remarks; private String remarks;
/** public Treatment(int patientId, LocalDate date, LocalTime begin,
* Constructor to initiate an object of class <code>Treatment</code> with the given parameter. Use this constructor
* to initiate objects, which are not persisted yet, because it will not have a treatment id (tid).
*
* @param pid Id of the treated patient.
* @param date Date of the Treatment.
* @param begin Time of the start of the treatment in format "hh:MM"
* @param end Time of the end of the treatment in format "hh:MM".
* @param description Description of the treatment.
* @param remarks Remarks to the treatment.
*/
public Treatment(long pid, LocalDate date, LocalTime begin,
LocalTime end, String description, String remarks) { LocalTime end, String description, String remarks) {
this.pid = pid; this.patientId = patientId;
this.date = date; this.date = date;
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
@ -35,22 +24,10 @@ public class Treatment {
this.remarks = remarks; this.remarks = remarks;
} }
/** public Treatment(int id, int patientId, LocalDate date, LocalTime begin,
* Constructor to initiate an object of class <code>Treatment</code> with the given parameter. Use this constructor
* to initiate objects, which are already persisted and have a treatment id (tid).
*
* @param tid Id of the treatment.
* @param pid Id of the treated patient.
* @param date Date of the Treatment.
* @param begin Time of the start of the treatment in format "hh:MM"
* @param end Time of the end of the treatment in format "hh:MM".
* @param description Description of the treatment.
* @param remarks Remarks to the treatment.
*/
public Treatment(long tid, long pid, LocalDate date, LocalTime begin,
LocalTime end, String description, String remarks) { LocalTime end, String description, String remarks) {
this.tid = tid; this.id = id;
this.pid = pid; this.patientId = patientId;
this.date = date; this.date = date;
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
@ -58,12 +35,12 @@ public class Treatment {
this.remarks = remarks; this.remarks = remarks;
} }
public long getTid() { public int getId() {
return tid; return id;
} }
public long getPid() { public int getPatientId() {
return this.pid; return this.patientId;
} }
public String getDate() { public String getDate() {
@ -107,8 +84,8 @@ public class Treatment {
} }
public String toString() { public String toString() {
return "\nBehandlung" + "\nTID: " + this.tid + return "\nBehandlung" + "\nID: " + this.id +
"\nPID: " + this.pid + "\nPatient ID: " + this.patientId +
"\nDate: " + this.date + "\nDate: " + this.date +
"\nBegin: " + this.begin + "\nBegin: " + this.begin +
"\nEnd: " + this.end + "\nEnd: " + this.end +

View file

@ -47,7 +47,7 @@ public class TreatmentController {
PatientDao pDao = DaoFactory.getDaoFactory().createPatientDAO(); PatientDao pDao = DaoFactory.getDaoFactory().createPatientDAO();
try { try {
this.patient = pDao.read((int) treatment.getPid()); this.patient = pDao.read((int) treatment.getPatientId());
this.treatment = treatment; this.treatment = treatment;
showData(); showData();
} catch (SQLException exception) { } catch (SQLException exception) {

View file

@ -3,41 +3,27 @@ package de.hitec.nhplus.treatment;
import de.hitec.nhplus.datastorage.DaoImp; import de.hitec.nhplus.datastorage.DaoImp;
import de.hitec.nhplus.utils.DateConverter; import de.hitec.nhplus.utils.DateConverter;
import java.sql.*; import java.sql.Connection;
import java.time.LocalDate; import java.sql.PreparedStatement;
import java.time.LocalTime; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* Implements the Interface <code>DaoImp</code>. Overrides methods to generate specific <code>PreparedStatements</code>,
* to execute the specific SQL Statements.
*/
public class TreatmentDao extends DaoImp<Treatment> { public class TreatmentDao extends DaoImp<Treatment> {
/**
* The constructor initiates an object of <code>TreatmentDao</code> and passes the connection to its super class.
*
* @param connection Object of <code>Connection</code> to execute the SQL-statements.
*/
public TreatmentDao(Connection connection) { public TreatmentDao(Connection connection) {
super(connection); super(connection);
} }
/**
* Generates a <code>PreparedStatement</code> to persist the given object of <code>Treatment</code>.
*
* @param treatment Object of <code>Treatment</code> to persist.
* @return <code>PreparedStatement</code> to insert the given patient.
*/
@Override @Override
protected PreparedStatement getCreateStatement(Treatment treatment) { protected PreparedStatement getCreateStatement(Treatment treatment) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
final String SQL = "INSERT INTO treatment (pid, treatment_date, begin, end, description, remark) " + final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " +
"VALUES (?, ?, ?, ?, ?, ?)"; "VALUES (?, ?, ?, ?, ?, ?)";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, treatment.getPid()); preparedStatement.setLong(1, treatment.getPatientId());
preparedStatement.setString(2, treatment.getDate()); preparedStatement.setString(2, treatment.getDate());
preparedStatement.setString(3, treatment.getBegin()); preparedStatement.setString(3, treatment.getBegin());
preparedStatement.setString(4, treatment.getEnd()); preparedStatement.setString(4, treatment.getEnd());
@ -49,45 +35,32 @@ public class TreatmentDao extends DaoImp<Treatment> {
return preparedStatement; return preparedStatement;
} }
/**
* Generates a <code>PreparedStatement</code> to query a treatment by a given treatment id (tid).
*
* @param tid Treatment id to query.
* @return <code>PreparedStatement</code> to query the treatment.
*/
@Override @Override
protected PreparedStatement getReadByIDStatement(long tid) { protected PreparedStatement getReadByIDStatement(int id) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
final String SQL = "SELECT * FROM treatment WHERE tid = ?"; final String SQL = "SELECT * FROM treatment WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, tid); preparedStatement.setInt(1, id);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }
return preparedStatement; return preparedStatement;
} }
/**
* Maps a <code>ResultSet</code> of one treatment to an object of <code>Treatment</code>.
*
* @param result ResultSet with a single row. Columns will be mapped to an object of class <code>Treatment</code>.
* @return Object of class <code>Treatment</code> with the data from the resultSet.
*/
@Override @Override
protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException { protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException {
LocalDate date = DateConverter.convertStringToLocalDate(result.getString(3)); return new Treatment(
LocalTime begin = DateConverter.convertStringToLocalTime(result.getString(4)); result.getInt(1),
LocalTime end = DateConverter.convertStringToLocalTime(result.getString(5)); result.getInt(2),
return new Treatment(result.getLong(1), result.getLong(2), DateConverter.convertStringToLocalDate(result.getString(3)),
date, begin, end, result.getString(6), result.getString(7)); DateConverter.convertStringToLocalTime(result.getString(4)),
DateConverter.convertStringToLocalTime(result.getString(5)),
result.getString(6),
result.getString(7)
);
} }
/**
* Generates a <code>PreparedStatement</code> to query all treatments.
*
* @return <code>PreparedStatement</code> to query all treatments.
*/
@Override @Override
protected PreparedStatement getReadAllStatement() { protected PreparedStatement getReadAllStatement() {
PreparedStatement statement = null; PreparedStatement statement = null;
@ -100,73 +73,48 @@ public class TreatmentDao extends DaoImp<Treatment> {
return statement; return statement;
} }
/**
* Maps a <code>ResultSet</code> of all treatments to an <code>ArrayList</code> with objects of class
* <code>Treatment</code>.
*
* @param result ResultSet with all rows. The columns will be mapped to objects of class <code>Treatment</code>.
* @return <code>ArrayList</code> with objects of class <code>Treatment</code> of all rows in the
* <code>ResultSet</code>.
*/
@Override @Override
protected ArrayList<Treatment> getListFromResultSet(ResultSet result) throws SQLException { protected ArrayList<Treatment> getListFromResultSet(ResultSet result) throws SQLException {
ArrayList<Treatment> list = new ArrayList<Treatment>(); ArrayList<Treatment> list = new ArrayList<>();
while (result.next()) { while (result.next()) {
LocalDate date = DateConverter.convertStringToLocalDate(result.getString(3)); Treatment treatment = new Treatment(
LocalTime begin = DateConverter.convertStringToLocalTime(result.getString(4)); result.getInt(1),
LocalTime end = DateConverter.convertStringToLocalTime(result.getString(5)); result.getInt(2),
Treatment treatment = new Treatment(result.getLong(1), result.getLong(2), DateConverter.convertStringToLocalDate(result.getString(3)),
date, begin, end, result.getString(6), result.getString(7)); DateConverter.convertStringToLocalTime(result.getString(4)),
DateConverter.convertStringToLocalTime(result.getString(5)),
result.getString(6),
result.getString(7)
);
list.add(treatment); list.add(treatment);
} }
return list; return list;
} }
/** private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) {
* Generates a <code>PreparedStatement</code> to query all treatments of a patient with a given patient id (pid).
*
* @param pid Patient id to query all treatments referencing this id.
* @return <code>PreparedStatement</code> to query all treatments of the given patient id (pid).
*/
private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(long pid) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
final String SQL = "SELECT * FROM treatment WHERE pid = ?"; final String SQL = "SELECT * FROM treatment WHERE patientId = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, pid); preparedStatement.setInt(1, patientId);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }
return preparedStatement; return preparedStatement;
} }
/** public List<Treatment> readTreatmentsByPid(int patientId) throws SQLException {
* Queries all treatments of a given patient id (pid) and maps the results to an <code>ArrayList</code> with ResultSet result = getReadAllTreatmentsOfOnePatientByPid(patientId).executeQuery();
* objects of class <code>Treatment</code>.
*
* @param pid Patient id to query all treatments referencing this id.
* @return <code>ArrayList</code> with objects of class <code>Treatment</code> of all rows in the
* <code>ResultSet</code>.
*/
public List<Treatment> readTreatmentsByPid(long pid) throws SQLException {
ResultSet result = getReadAllTreatmentsOfOnePatientByPid(pid).executeQuery();
return getListFromResultSet(result); return getListFromResultSet(result);
} }
/**
* Generates a <code>PreparedStatement</code> to update the given treatment, identified
* by the id of the treatment (tid).
*
* @param treatment Treatment object to update.
* @return <code>PreparedStatement</code> to update the given treatment.
*/
@Override @Override
protected PreparedStatement getUpdateStatement(Treatment treatment) { protected PreparedStatement getUpdateStatement(Treatment treatment) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
final String SQL = final String SQL =
"UPDATE treatment SET " + "UPDATE treatment SET " +
"pid = ?, " + "patientId = ?, " +
"treatment_date = ?, " + "treatment_date = ?, " +
"begin = ?, " + "begin = ?, " +
"end = ?, " + "end = ?, " +
@ -174,33 +122,27 @@ public class TreatmentDao extends DaoImp<Treatment> {
"remark = ? " + "remark = ? " +
"WHERE tid = ?"; "WHERE tid = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, treatment.getPid()); preparedStatement.setInt(1, treatment.getPatientId());
preparedStatement.setString(2, treatment.getDate()); preparedStatement.setString(2, treatment.getDate());
preparedStatement.setString(3, treatment.getBegin()); preparedStatement.setString(3, treatment.getBegin());
preparedStatement.setString(4, treatment.getEnd()); preparedStatement.setString(4, treatment.getEnd());
preparedStatement.setString(5, treatment.getDescription()); preparedStatement.setString(5, treatment.getDescription());
preparedStatement.setString(6, treatment.getRemarks()); preparedStatement.setString(6, treatment.getRemarks());
preparedStatement.setLong(7, treatment.getTid()); preparedStatement.setLong(7, treatment.getId());
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }
return preparedStatement; return preparedStatement;
} }
/**
* Generates a <code>PreparedStatement</code> to delete a treatment with the given id.
*
* @param tid Id of the Treatment to delete.
* @return <code>PreparedStatement</code> to delete treatment with the given id.
*/
@Override @Override
protected PreparedStatement getDeleteStatement(long tid) { protected PreparedStatement getDeleteStatement(int id) {
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
final String SQL = final String SQL =
"DELETE FROM treatment WHERE tid = ?"; "DELETE FROM treatment WHERE tid = ?";
preparedStatement = this.connection.prepareStatement(SQL); preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, tid); preparedStatement.setInt(1, id);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }

View file

@ -18,7 +18,7 @@
<TableView fx:id="tableView" editable="true" layoutX="31.0" layoutY="35.0" onMouseClicked="#handleMouseClick" prefHeight="364.0" prefWidth="825.0" AnchorPane.bottomAnchor="75.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="80.0"> <TableView fx:id="tableView" editable="true" layoutX="31.0" layoutY="35.0" onMouseClicked="#handleMouseClick" prefHeight="364.0" prefWidth="825.0" AnchorPane.bottomAnchor="75.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="80.0">
<columns> <columns>
<TableColumn fx:id="columnId" maxWidth="-1.0" minWidth="40.0" prefWidth="50.0" text="ID" /> <TableColumn fx:id="columnId" maxWidth="-1.0" minWidth="40.0" prefWidth="50.0" text="ID" />
<TableColumn fx:id="columnPid" maxWidth="-1.0" minWidth="100.0" prefWidth="120.0" text="PatientID" /> <TableColumn fx:id="columnPatientId" maxWidth="-1.0" minWidth="100.0" prefWidth="120.0" text="PatientID" />
<TableColumn fx:id="columnDate" maxWidth="-1.0" minWidth="140.0" prefWidth="150.0" text="Datum" /> <TableColumn fx:id="columnDate" maxWidth="-1.0" minWidth="140.0" prefWidth="150.0" text="Datum" />
<TableColumn fx:id="columnBegin" maxWidth="-1.0" minWidth="140.0" prefWidth="150.0" text="Beginn" /> <TableColumn fx:id="columnBegin" maxWidth="-1.0" minWidth="140.0" prefWidth="150.0" text="Beginn" />
<TableColumn fx:id="columnEnd" maxWidth="-1.0" minWidth="140.0" prefWidth="150.0" text="Ende" /> <TableColumn fx:id="columnEnd" maxWidth="-1.0" minWidth="140.0" prefWidth="150.0" text="Ende" />