From 044b8088db3e55e12ae7d5858bca10e8ef430a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Sun, 28 Apr 2024 00:48:56 +0200 Subject: [PATCH] NOTICKET: Cleanup ID Naming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- db/nursingHome.db | Bin 16384 -> 16384 bytes .../java/de/hitec/nhplus/datastorage/Dao.java | 4 +- .../de/hitec/nhplus/datastorage/DaoImp.java | 8 +- .../hitec/nhplus/fixtures/PatientFixture.java | 2 +- .../nhplus/fixtures/TreatmentFixture.java | 28 ++-- .../nhplus/patient/AllPatientController.java | 4 +- .../java/de/hitec/nhplus/patient/Patient.java | 53 ++----- .../de/hitec/nhplus/patient/PatientDao.java | 86 +++-------- .../treatment/AllTreatmentController.java | 10 +- .../treatment/NewTreatmentController.java | 2 +- .../de/hitec/nhplus/treatment/Treatment.java | 49 ++----- .../nhplus/treatment/TreatmentController.java | 2 +- .../hitec/nhplus/treatment/TreatmentDao.java | 134 +++++------------- .../de/hitec/nhplus/AllTreatmentView.fxml | 2 +- 14 files changed, 110 insertions(+), 274 deletions(-) diff --git a/db/nursingHome.db b/db/nursingHome.db index f808c140f638bd6c7f689fd8140ec68b631fb69b..2ad60e6721cd64692601f8c9c4337e8cfd465c15 100644 GIT binary patch delta 228 zcmZo@U~Fh$oFFaukb!}L1&ColZ=#N|@IwZ@qJ_MCe;Js0;~4l>^NaJwZ5C8`!^73& zCBQB&FVEPLJh_P1c(NbACPzVHNoHzZiRa{tysPA(>=aFfAXhinAXh(U*I)&x3I&bH zQ}}WerNpJB8JojP5|eUL3liDb1lYhUBkgJqN_EAZ=X_T%3n!1jcJ|1sVwPttDN0Q&$pt!qi<=Q4yZOI9qW}Q?z&suR delta 202 zcmZo@U~Fh$oFFZ@lYxPO1&ColX`+s?@J3U* zz{M^uFVEQGKRJWfn6YHCAHOD3LFVK$ysH>9C)e_cGZsvq!k42UDK0I|*c@Jxn3R)R zkjTcx1~$Gios%6bp*mTSUzf39vmgHs0k%g>{2%!r0nI%Sddtf36x^v+WcRiQ2+q=U^6xV diff --git a/src/main/java/de/hitec/nhplus/datastorage/Dao.java b/src/main/java/de/hitec/nhplus/datastorage/Dao.java index a2e0223..1a0b68d 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/Dao.java +++ b/src/main/java/de/hitec/nhplus/datastorage/Dao.java @@ -6,11 +6,11 @@ import java.util.List; public interface Dao { void create(T t) throws SQLException; - T read(long key) throws SQLException; + T read(int key) throws SQLException; List readAll() throws SQLException; void update(T t) throws SQLException; - void deleteById(long key) throws SQLException; + void deleteById(int key) throws SQLException; } diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java index be502bd..4ba5fb4 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java @@ -17,7 +17,7 @@ public abstract class DaoImp implements Dao { } @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 implements Dao { } @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 implements Dao { 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); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java index 739058d..44c48ad 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/PatientFixture.java @@ -32,7 +32,7 @@ public class PatientFixture implements Fixture 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, " + diff --git a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java index e6bf5ad..8611aaa 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java +++ b/src/main/java/de/hitec/nhplus/fixtures/TreatmentFixture.java @@ -41,14 +41,14 @@ public class TreatmentFixture implements Fixture 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 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 )); 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 )); 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 )); 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 ); 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 ); 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 ); 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 ); 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 "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 ); 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 Map 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; } diff --git a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java index d853c72..5eb4f18 100644 --- a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java @@ -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(); diff --git a/src/main/java/de/hitec/nhplus/patient/Patient.java b/src/main/java/de/hitec/nhplus/patient/Patient.java index 26e95fd..6ac3326 100644 --- a/src/main/java/de/hitec/nhplus/patient/Patient.java +++ b/src/main/java/de/hitec/nhplus/patient/Patient.java @@ -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 allTreatments = new ArrayList<>(); - /** - * Constructor to initiate an object of class Patient 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 Patient 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 birthOfDate. - * - * @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 + diff --git a/src/main/java/de/hitec/nhplus/patient/PatientDao.java b/src/main/java/de/hitec/nhplus/patient/PatientDao.java index 0d5c315..71b3502 100644 --- a/src/main/java/de/hitec/nhplus/patient/PatientDao.java +++ b/src/main/java/de/hitec/nhplus/patient/PatientDao.java @@ -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 DaoImp. Overrides methods to generate specific PreparedStatements, - * to execute the specific SQL Statements. - */ public class PatientDao extends DaoImp { - /** - * The constructor initiates an object of PatientDao and passes the connection to its super class. - * - * @param connection Object of Connection to execute the SQL-statements. - */ public PatientDao(Connection connection) { super(connection); } - /** - * Generates a PreparedStatement to persist the given object of Patient. - * - * @param patient Object of Patient to persist. - * @return PreparedStatement to insert the given patient. - */ @Override protected PreparedStatement getCreateStatement(Patient patient) { PreparedStatement preparedStatement = null; @@ -47,31 +33,19 @@ public class PatientDao extends DaoImp { return preparedStatement; } - /** - * Generates a PreparedStatement to query a patient by a given patient id (pid). - * - * @param pid Patient id to query. - * @return PreparedStatement 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 ResultSet of one patient to an object of Patient. - * - * @param result ResultSet with a single row. Columns will be mapped to an object of class Patient. - * @return Object of class Patient 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 { result.getString(6)); } - /** - * Generates a PreparedStatement to query all patients. - * - * @return PreparedStatement to query all patients. - */ @Override protected PreparedStatement getReadAllStatement() { PreparedStatement statement = null; @@ -100,33 +69,22 @@ public class PatientDao extends DaoImp { return statement; } - /** - * Maps a ResultSet of all patients to an ArrayList of Patient objects. - * - * @param result ResultSet with all rows. The Columns will be mapped to objects of class Patient. - * @return ArrayList with objects of class Patient of all rows in the - * ResultSet. - */ @Override protected ArrayList getListFromResultSet(ResultSet result) throws SQLException { ArrayList 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 PreparedStatement to update the given patient, identified - * by the id of the patient (pid). - * - * @param patient Patient object to update. - * @return PreparedStatement to update the given patient. - */ @Override protected PreparedStatement getUpdateStatement(Patient patient) { PreparedStatement preparedStatement = null; @@ -138,33 +96,27 @@ public class PatientDao extends DaoImp { "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 PreparedStatement to delete a patient with the given id. - * - * @param pid Id of the patient to delete. - * @return PreparedStatement 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(); } diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index a1ef777..eea41f2 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -28,7 +28,7 @@ public class AllTreatmentController private TableColumn columnId; @FXML - private TableColumn columnPid; + private TableColumn columnPatientId; @FXML private TableColumn 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(); diff --git a/src/main/java/de/hitec/nhplus/treatment/NewTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/NewTreatmentController.java index 466dc76..a1053ab 100644 --- a/src/main/java/de/hitec/nhplus/treatment/NewTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/NewTreatmentController.java @@ -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(); diff --git a/src/main/java/de/hitec/nhplus/treatment/Treatment.java b/src/main/java/de/hitec/nhplus/treatment/Treatment.java index 7270ada..7fb5991 100644 --- a/src/main/java/de/hitec/nhplus/treatment/Treatment.java +++ b/src/main/java/de/hitec/nhplus/treatment/Treatment.java @@ -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 Treatment 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 Treatment 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 + diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentController.java index a3fe6ce..f166412 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentController.java @@ -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) { diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java index fd2b375..6b2b995 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentDao.java @@ -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 DaoImp. Overrides methods to generate specific PreparedStatements, - * to execute the specific SQL Statements. - */ public class TreatmentDao extends DaoImp { - /** - * The constructor initiates an object of TreatmentDao and passes the connection to its super class. - * - * @param connection Object of Connection to execute the SQL-statements. - */ public TreatmentDao(Connection connection) { super(connection); } - /** - * Generates a PreparedStatement to persist the given object of Treatment. - * - * @param treatment Object of Treatment to persist. - * @return PreparedStatement 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 { return preparedStatement; } - /** - * Generates a PreparedStatement to query a treatment by a given treatment id (tid). - * - * @param tid Treatment id to query. - * @return PreparedStatement 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 ResultSet of one treatment to an object of Treatment. - * - * @param result ResultSet with a single row. Columns will be mapped to an object of class Treatment. - * @return Object of class Treatment 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 PreparedStatement to query all treatments. - * - * @return PreparedStatement to query all treatments. - */ @Override protected PreparedStatement getReadAllStatement() { PreparedStatement statement = null; @@ -100,73 +73,48 @@ public class TreatmentDao extends DaoImp { return statement; } - /** - * Maps a ResultSet of all treatments to an ArrayList with objects of class - * Treatment. - * - * @param result ResultSet with all rows. The columns will be mapped to objects of class Treatment. - * @return ArrayList with objects of class Treatment of all rows in the - * ResultSet. - */ @Override protected ArrayList getListFromResultSet(ResultSet result) throws SQLException { - ArrayList list = new ArrayList(); + ArrayList 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 PreparedStatement 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 PreparedStatement 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 ArrayList with - * objects of class Treatment. - * - * @param pid Patient id to query all treatments referencing this id. - * @return ArrayList with objects of class Treatment of all rows in the - * ResultSet. - */ - public List readTreatmentsByPid(long pid) throws SQLException { - ResultSet result = getReadAllTreatmentsOfOnePatientByPid(pid).executeQuery(); + public List readTreatmentsByPid(int patientId) throws SQLException { + ResultSet result = getReadAllTreatmentsOfOnePatientByPid(patientId).executeQuery(); return getListFromResultSet(result); } - /** - * Generates a PreparedStatement to update the given treatment, identified - * by the id of the treatment (tid). - * - * @param treatment Treatment object to update. - * @return PreparedStatement 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 { "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 PreparedStatement to delete a treatment with the given id. - * - * @param tid Id of the Treatment to delete. - * @return PreparedStatement 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(); } diff --git a/src/main/resources/de/hitec/nhplus/AllTreatmentView.fxml b/src/main/resources/de/hitec/nhplus/AllTreatmentView.fxml index c62332f..3728a46 100644 --- a/src/main/resources/de/hitec/nhplus/AllTreatmentView.fxml +++ b/src/main/resources/de/hitec/nhplus/AllTreatmentView.fxml @@ -18,7 +18,7 @@ - +