NOTICKET: Cleanup ID Naming

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-04-28 00:48:56 +02:00 committed by Snoweuph
parent 076bd7f140
commit 775b28d86d
18 changed files with 114 additions and 278 deletions

Binary file not shown.

View file

@ -6,11 +6,11 @@ import java.util.List;
public interface Dao<T> {
void create(T t) throws SQLException;
T read(long key) throws SQLException;
T read(int id) throws SQLException;
List<T> readAll() 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
public T read(long key) throws SQLException {
public T read(int key) throws SQLException {
T object = null;
ResultSet result = getReadByIDStatement(key).executeQuery();
if (result.next()) {
@ -37,7 +37,7 @@ public abstract class DaoImp<T> implements Dao<T> {
}
@Override
public void deleteById(long key) throws SQLException {
public void deleteById(int key) throws SQLException {
getDeleteStatement(key).executeUpdate();
}
@ -47,11 +47,11 @@ public abstract class DaoImp<T> implements Dao<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 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)
{
final String SQL = "CREATE TABLE IF NOT EXISTS patient (" +
" pid INTEGER PRIMARY KEY AUTOINCREMENT, " +
" id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" firstname TEXT NOT NULL, " +
" surname TEXT NOT NULL, " +
" dateOfBirth TEXT NOT NULL, " +

View file

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

View file

@ -19,7 +19,7 @@ public class MainWindowController {
@FXML
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 {
mainBorderPane.setCenter(loader.load());
} catch (IOException exception) {
@ -29,7 +29,7 @@ public class MainWindowController {
@FXML
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 {
mainBorderPane.setCenter(loader.load());
} catch (IOException exception) {

View file

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

View file

@ -3,6 +3,7 @@ package de.hitec.nhplus.patient;
import de.hitec.nhplus.main.Person;
import de.hitec.nhplus.treatment.Treatment;
import de.hitec.nhplus.utils.DateConverter;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
@ -10,26 +11,13 @@ import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
/**
* Patients live in a NURSING home and are treated by nurses.
*/
public class Patient extends Person {
private SimpleLongProperty pid;
private SimpleIntegerProperty id;
private final SimpleStringProperty dateOfBirth;
private final SimpleStringProperty careLevel;
private final SimpleStringProperty roomNumber;
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(
String firstName,
String surname,
@ -43,19 +31,8 @@ public class Patient extends Person {
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(
long pid,
int id,
String firstName,
String surname,
LocalDate dateOfBirth,
@ -63,18 +40,18 @@ public class Patient extends Person {
String roomNumber
) {
super(firstName, surname);
this.pid = new SimpleLongProperty(pid);
this.id = new SimpleIntegerProperty(id);
this.dateOfBirth = new SimpleStringProperty(DateConverter.convertLocalDateToString(dateOfBirth));
this.careLevel = new SimpleStringProperty(careLevel);
this.roomNumber = new SimpleStringProperty(roomNumber);
}
public long getPid() {
return pid.get();
public int getId() {
return id.get();
}
public SimpleLongProperty pidProperty() {
return pid;
public SimpleIntegerProperty pidProperty() {
return id;
}
public String getDateOfBirth() {
@ -85,11 +62,6 @@ public class Patient extends Person {
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) {
this.dateOfBirth.set(dateOfBirth);
}
@ -119,13 +91,6 @@ public class Patient extends Person {
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) {
if (this.allTreatments.contains(treatment)) {
return false;
@ -135,7 +100,7 @@ public class Patient extends Person {
}
public String toString() {
return "Patient" + "\nMNID: " + this.pid +
return "Patient" + "\nMNID: " + this.id +
"\nFirstname: " + this.getFirstName() +
"\nSurname: " + this.getSurname() +
"\nBirthday: " + this.dateOfBirth +

View file

@ -1,34 +1,20 @@
package de.hitec.nhplus.patient;
import de.hitec.nhplus.datastorage.DaoImp;
import de.hitec.nhplus.patient.Patient;
import de.hitec.nhplus.utils.DateConverter;
import java.sql.*;
import java.time.LocalDate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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> {
/**
* 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) {
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
protected PreparedStatement getCreateStatement(Patient patient) {
PreparedStatement preparedStatement = null;
@ -47,31 +33,19 @@ public class PatientDao extends DaoImp<Patient> {
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
protected PreparedStatement getReadByIDStatement(long pid) {
protected PreparedStatement getReadByIDStatement(int id) {
PreparedStatement preparedStatement = null;
try {
final String SQL = "SELECT * FROM patient WHERE pid = ?";
final String SQL = "SELECT * FROM patient WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, pid);
preparedStatement.setInt(1, id);
} catch (SQLException exception) {
exception.printStackTrace();
}
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
protected Patient getInstanceFromResultSet(ResultSet result) throws SQLException {
return new Patient(
@ -83,11 +57,6 @@ public class PatientDao extends DaoImp<Patient> {
result.getString(6));
}
/**
* Generates a <code>PreparedStatement</code> to query all patients.
*
* @return <code>PreparedStatement</code> to query all patients.
*/
@Override
protected PreparedStatement getReadAllStatement() {
PreparedStatement statement = null;
@ -100,33 +69,22 @@ public class PatientDao extends DaoImp<Patient> {
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
protected ArrayList<Patient> getListFromResultSet(ResultSet result) throws SQLException {
ArrayList<Patient> list = new ArrayList<>();
while (result.next()) {
LocalDate date = DateConverter.convertStringToLocalDate(result.getString(4));
Patient patient = new Patient(result.getInt(1), result.getString(2),
result.getString(3), date,
result.getString(5), result.getString(6));
Patient patient = new Patient(
result.getInt(1),
result.getString(2),
result.getString(3),
DateConverter.convertStringToLocalDate(result.getString(4)),
result.getString(5),
result.getString(6));
list.add(patient);
}
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
protected PreparedStatement getUpdateStatement(Patient patient) {
PreparedStatement preparedStatement = null;
@ -138,33 +96,27 @@ public class PatientDao extends DaoImp<Patient> {
"dateOfBirth = ?, " +
"carelevel = ?, " +
"roomnumber = ?, " +
"WHERE pid = ?";
"WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setString(1, patient.getFirstName());
preparedStatement.setString(2, patient.getSurname());
preparedStatement.setString(3, patient.getDateOfBirth());
preparedStatement.setString(4, patient.getCareLevel());
preparedStatement.setString(5, patient.getRoomNumber());
preparedStatement.setLong(6, patient.getPid());
preparedStatement.setInt(6, patient.getId());
} catch (SQLException exception) {
exception.printStackTrace();
}
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
protected PreparedStatement getDeleteStatement(long pid) {
protected PreparedStatement getDeleteStatement(int id) {
PreparedStatement preparedStatement = null;
try {
final String SQL = "DELETE FROM patient WHERE pid = ?";
final String SQL = "DELETE FROM patient WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, pid);
preparedStatement.setInt(1, id);
} catch (SQLException exception) {
exception.printStackTrace();
}

View file

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

View file

@ -111,7 +111,7 @@ public class NewTreatmentController
LocalTime end = DateConverter.convertStringToLocalTime(textFieldEnd.getText());
String description = textFieldDescription.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);
controller.readAllAndShowInTableView();
stage.close();

View file

@ -6,28 +6,17 @@ import java.time.LocalDate;
import java.time.LocalTime;
public class Treatment {
private long tid;
private final long pid;
private int id;
private final int patientId;
private LocalDate date;
private LocalTime begin;
private LocalTime end;
private String description;
private String remarks;
/**
* 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,
public Treatment(int patientId, LocalDate date, LocalTime begin,
LocalTime end, String description, String remarks) {
this.pid = pid;
this.patientId = patientId;
this.date = date;
this.begin = begin;
this.end = end;
@ -35,22 +24,10 @@ public class Treatment {
this.remarks = remarks;
}
/**
* 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,
public Treatment(int id, int patientId, LocalDate date, LocalTime begin,
LocalTime end, String description, String remarks) {
this.tid = tid;
this.pid = pid;
this.id = id;
this.patientId = patientId;
this.date = date;
this.begin = begin;
this.end = end;
@ -58,12 +35,12 @@ public class Treatment {
this.remarks = remarks;
}
public long getTid() {
return tid;
public int getId() {
return id;
}
public long getPid() {
return this.pid;
public int getPatientId() {
return this.patientId;
}
public String getDate() {
@ -107,8 +84,8 @@ public class Treatment {
}
public String toString() {
return "\nBehandlung" + "\nTID: " + this.tid +
"\nPID: " + this.pid +
return "\nBehandlung" + "\nID: " + this.id +
"\nPatient ID: " + this.patientId +
"\nDate: " + this.date +
"\nBegin: " + this.begin +
"\nEnd: " + this.end +

View file

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

View file

@ -3,41 +3,27 @@ package de.hitec.nhplus.treatment;
import de.hitec.nhplus.datastorage.DaoImp;
import de.hitec.nhplus.utils.DateConverter;
import java.sql.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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> {
/**
* 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) {
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
protected PreparedStatement getCreateStatement(Treatment treatment) {
PreparedStatement preparedStatement = null;
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 (?, ?, ?, ?, ?, ?)";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, treatment.getPid());
preparedStatement.setLong(1, treatment.getPatientId());
preparedStatement.setString(2, treatment.getDate());
preparedStatement.setString(3, treatment.getBegin());
preparedStatement.setString(4, treatment.getEnd());
@ -49,45 +35,32 @@ public class TreatmentDao extends DaoImp<Treatment> {
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
protected PreparedStatement getReadByIDStatement(long tid) {
protected PreparedStatement getReadByIDStatement(int id) {
PreparedStatement preparedStatement = null;
try {
final String SQL = "SELECT * FROM treatment WHERE tid = ?";
final String SQL = "SELECT * FROM treatment WHERE id = ?";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, tid);
preparedStatement.setInt(1, id);
} catch (SQLException exception) {
exception.printStackTrace();
}
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
protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException {
LocalDate date = DateConverter.convertStringToLocalDate(result.getString(3));
LocalTime begin = DateConverter.convertStringToLocalTime(result.getString(4));
LocalTime end = DateConverter.convertStringToLocalTime(result.getString(5));
return new Treatment(result.getLong(1), result.getLong(2),
date, begin, end, result.getString(6), result.getString(7));
return new Treatment(
result.getInt(1),
result.getInt(2),
DateConverter.convertStringToLocalDate(result.getString(3)),
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
protected PreparedStatement getReadAllStatement() {
PreparedStatement statement = null;
@ -100,73 +73,48 @@ public class TreatmentDao extends DaoImp<Treatment> {
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
protected ArrayList<Treatment> getListFromResultSet(ResultSet result) throws SQLException {
ArrayList<Treatment> list = new ArrayList<Treatment>();
ArrayList<Treatment> list = new ArrayList<>();
while (result.next()) {
LocalDate date = DateConverter.convertStringToLocalDate(result.getString(3));
LocalTime begin = DateConverter.convertStringToLocalTime(result.getString(4));
LocalTime end = DateConverter.convertStringToLocalTime(result.getString(5));
Treatment treatment = new Treatment(result.getLong(1), result.getLong(2),
date, begin, end, result.getString(6), result.getString(7));
Treatment treatment = new Treatment(
result.getInt(1),
result.getInt(2),
DateConverter.convertStringToLocalDate(result.getString(3)),
DateConverter.convertStringToLocalTime(result.getString(4)),
DateConverter.convertStringToLocalTime(result.getString(5)),
result.getString(6),
result.getString(7)
);
list.add(treatment);
}
return list;
}
/**
* 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) {
private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) {
PreparedStatement preparedStatement = null;
try {
final String SQL = "SELECT * FROM treatment WHERE pid = ?";
final String SQL = "SELECT * FROM treatment WHERE patientId = ?";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, pid);
preparedStatement.setInt(1, patientId);
} catch (SQLException exception) {
exception.printStackTrace();
}
return preparedStatement;
}
/**
* Queries all treatments of a given patient id (pid) and maps the results to an <code>ArrayList</code> with
* 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();
public List<Treatment> readTreatmentsByPid(int patientId) throws SQLException {
ResultSet result = getReadAllTreatmentsOfOnePatientByPid(patientId).executeQuery();
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
protected PreparedStatement getUpdateStatement(Treatment treatment) {
PreparedStatement preparedStatement = null;
try {
final String SQL =
"UPDATE treatment SET " +
"pid = ?, " +
"patientId = ?, " +
"treatment_date = ?, " +
"begin = ?, " +
"end = ?, " +
@ -174,33 +122,27 @@ public class TreatmentDao extends DaoImp<Treatment> {
"remark = ? " +
"WHERE tid = ?";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, treatment.getPid());
preparedStatement.setInt(1, treatment.getPatientId());
preparedStatement.setString(2, treatment.getDate());
preparedStatement.setString(3, treatment.getBegin());
preparedStatement.setString(4, treatment.getEnd());
preparedStatement.setString(5, treatment.getDescription());
preparedStatement.setString(6, treatment.getRemarks());
preparedStatement.setLong(7, treatment.getTid());
preparedStatement.setLong(7, treatment.getId());
} catch (SQLException exception) {
exception.printStackTrace();
}
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
protected PreparedStatement getDeleteStatement(long tid) {
protected PreparedStatement getDeleteStatement(int id) {
PreparedStatement preparedStatement = null;
try {
final String SQL =
"DELETE FROM treatment WHERE tid = ?";
preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setLong(1, tid);
preparedStatement.setInt(1, id);
} catch (SQLException exception) {
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">
<columns>
<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="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" />