diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index c0f01c3..ea04f74 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -20,6 +20,12 @@ import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; +/** + * The controller for viewing all {@link Treatment}s. + * + * @author Bernd Heideman + * @author Dominik Säume + */ public class AllTreatmentController { @FXML @@ -54,6 +60,10 @@ public class AllTreatmentController { private final ObservableList patientSelection = FXCollections.observableArrayList(); private ArrayList patientList; + /** + * Initialization method that is called after the binding of all the fields. + */ + @FXML public void initialize() { readAllAndShowInTableView(); comboBoxPatientSelection.setItems(patientSelection); @@ -61,7 +71,7 @@ public class AllTreatmentController { this.columnId.setCellValueFactory(new PropertyValueFactory<>("id")); this.columnPatientName.setCellValueFactory( - cellData -> { + cellData -> { Patient patient = cellData.getValue().getPatient(); return new SimpleStringProperty(patient.getSurName() + ", " + patient.getFirstName()); } @@ -83,6 +93,9 @@ public class AllTreatmentController { this.createComboBoxData(); } + /** + * Internal method to read all data and set it to the table view. + */ public void readAllAndShowInTableView() { comboBoxPatientSelection.getSelectionModel().select(0); this.dao = DaoFactory.getInstance().createTreatmentDao(); @@ -93,6 +106,9 @@ public class AllTreatmentController { } } + /** + * Internal method to create the data set for the combobox that is used for creating a new {@link Treatment}. + */ private void createComboBoxData() { PatientDao dao = DaoFactory.getInstance().createPatientDAO(); try { @@ -106,6 +122,78 @@ public class AllTreatmentController { } } + /** + * Internal method to get the {@link Patient} object by its surname. + * + * @see AllTreatmentController#handleComboBox + * @see AllTreatmentController#handleNewTreatment + */ + private Patient searchInList(String surname) { + for (Patient patient : this.patientList) { + if (patient.getSurName().equals(surname)) { + return patient; + } + } + return null; + } + + /** + * Internal method to create a {@link TreatmentModalController TreatmentModal}. + * + * @param treatment The {@link Treatment} which should be edited. Set null to create a new one. + * @param title The Title of the created modal. + * @param patient The {@link Patient} whose {@link Treatment} this is. + */ + public void treatmentWindow(Treatment treatment, String title, Patient patient) { + try { + FXMLLoader loader = new FXMLLoader( + Main.class.getResource("/de/hitec/nhplus/treatment/TreatmentModal.fxml") + ); + BorderPane pane = loader.load(); + Scene scene = new Scene(pane); + Stage stage = new Stage(); + + TreatmentModalController controller = loader.getController(); + controller.initialize( + this, + stage, + treatment, + patient + ); + + stage.setScene(scene); + stage.setTitle(title); + stage.setResizable(true); + stage.setAlwaysOnTop(true); + stage.showAndWait(); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + + /** + * Method to create a new {@link Treatment}. + */ + protected void createTreatment(Treatment treatment) { + TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); + try { + dao.create(treatment); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } + + /** + * Method to save the changes to a {@link Treatment}. + */ + protected void updateTreatment(Treatment treatment) { + TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); + try { + dao.update(treatment); + } catch (SQLException exception) { + exception.printStackTrace(); + } + } @FXML public void handleComboBox() { @@ -131,14 +219,6 @@ public class AllTreatmentController { } } - private Patient searchInList(String surname) { - for (Patient patient : this.patientList) { - if (patient.getSurName().equals(surname)) { - return patient; - } - } - return null; - } @FXML public void handleDelete() { @@ -157,7 +237,7 @@ public class AllTreatmentController { try { String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem(); Patient patient = searchInList(selectedPatient); - newTreatmentWindow(patient); + treatmentWindow(null, "NHPlus - Neue Behandlung", patient); } catch (NullPointerException exception) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Information"); @@ -174,81 +254,18 @@ public class AllTreatmentController { if (event.getClickCount() == 2 && (tableView.getSelectionModel().getSelectedItem() != null)) { int index = this.tableView.getSelectionModel().getSelectedIndex(); Treatment treatment = this.treatments.get(index); - treatmentWindow(treatment); + try { + treatmentWindow( + treatment, + "NHPlus - Behandlung", + DaoFactory.getInstance().createPatientDAO().read( + treatment.getPatient().getId() + ) + ); + } catch (SQLException e) { + throw new RuntimeException(e); + } } }); } - - public void newTreatmentWindow(Patient patient) { - try { - FXMLLoader loader = new FXMLLoader( - Main.class.getResource("/de/hitec/nhplus/treatment/TreatmentModal.fxml") - ); - BorderPane pane = loader.load(); - Scene scene = new Scene(pane); - Stage stage = new Stage(); - - TreatmentModalController controller = loader.getController(); - controller.initialize( - this, - stage, - null, - patient - ); - - stage.setScene(scene); - stage.setTitle("NHPlus - Neue Behandlung"); - stage.setResizable(true); - stage.setAlwaysOnTop(true); - stage.showAndWait(); - } catch (IOException exception) { - exception.printStackTrace(); - } - } - - public void treatmentWindow(Treatment treatment) { - try { - FXMLLoader loader = new FXMLLoader( - Main.class.getResource("/de/hitec/nhplus/treatment/TreatmentModal.fxml") - ); - BorderPane pane = loader.load(); - Scene scene = new Scene(pane); - Stage stage = new Stage(); - - TreatmentModalController controller = loader.getController(); - PatientDao pDao = DaoFactory.getInstance().createPatientDAO(); - controller.initialize( - this, - stage, - treatment, - pDao.read(treatment.getPatient().getId()) - ); - - stage.setScene(scene); - stage.setTitle("NHPlus - Behandlung"); - stage.setResizable(true); - stage.setAlwaysOnTop(true); - stage.showAndWait(); - } catch (IOException | SQLException exception) { - exception.printStackTrace(); - } - } - - public void createTreatment(Treatment treatment) { - TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); - try { - dao.create(treatment); - } catch (SQLException exception) { - exception.printStackTrace(); - } - } - - public void updateTreatment(Treatment treatment) { - TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); - try { - dao.update(treatment); - } catch (SQLException exception) { - exception.printStackTrace(); - } - } } diff --git a/src/main/java/de/hitec/nhplus/treatment/Treatment.java b/src/main/java/de/hitec/nhplus/treatment/Treatment.java index 4551560..6b39af4 100644 --- a/src/main/java/de/hitec/nhplus/treatment/Treatment.java +++ b/src/main/java/de/hitec/nhplus/treatment/Treatment.java @@ -10,6 +10,12 @@ import java.time.LocalDate; import java.time.LocalTime; import java.util.StringJoiner; +/** + * The model for a {@link Treatment}. + * + * @author Bernd Heideman + * @author Dominik Säume + */ public class Treatment { private SimpleIntegerProperty id; private final SimpleObjectProperty patient; @@ -19,8 +25,21 @@ public class Treatment { private final SimpleStringProperty description; private final SimpleStringProperty remarks; - public Treatment(Patient patient, LocalDate date, LocalTime begin, - LocalTime end, String description, String remarks) { + /** + * This constructor allows instantiating a {@link Treatment} object, + * before it is stored in the database, by omitting the {@link Treatment#id ID} value. + * + * @implSpec Instances created with this constructor can be directly passed to + * {@link de.hitec.nhplus.treatment.database.TreatmentDao#create TreatmentDao.create}. + */ + public Treatment( + Patient patient, + LocalDate date, + LocalTime begin, + LocalTime end, + String description, + String remarks + ) { this.patient = new SimpleObjectProperty<>(patient); this.date = new SimpleObjectProperty<>(date); this.begin = new SimpleObjectProperty<>(begin); @@ -29,8 +48,18 @@ public class Treatment { this.remarks = new SimpleStringProperty(remarks); } - public Treatment(int id, Patient patient, LocalDate date, LocalTime begin, - LocalTime end, String description, String remarks) { + /** + * This constructor allows instantiating a {@link Treatment} object with all existing fields. + */ + public Treatment( + int id, + Patient patient, + LocalDate date, + LocalTime begin, + LocalTime end, + String description, + String remarks + ) { this.id = new SimpleIntegerProperty(id); this.patient = new SimpleObjectProperty<>(patient); this.date = new SimpleObjectProperty<>(date); diff --git a/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java b/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java index af788f0..cdf60f2 100644 --- a/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java +++ b/src/main/java/de/hitec/nhplus/treatment/TreatmentModalController.java @@ -13,6 +13,11 @@ import java.time.LocalTime; import static de.hitec.nhplus.utils.Validator.*; +/** + * The controller for creating and editing a specific {@link Treatment}. + * + * @author Dominik Säume + */ public class TreatmentModalController { @FXML private Label labelFirstName; @@ -36,6 +41,10 @@ public class TreatmentModalController { private Treatment treatment; private boolean isNewTreatment = false; + /** + * Initialization method that is called after the binding of all the fields. + */ + @FXML public void initialize(AllTreatmentController controller, Stage stage, Treatment treatment, Patient patient) { this.controller = controller; this.stage = stage; @@ -96,7 +105,9 @@ public class TreatmentModalController { }); } - + /** + * Internal method to show the data in the view. + */ private void showData() { this.labelFirstName.setText(patient.getFirstName()); this.labelSurName.setText(patient.getSurName()); @@ -136,8 +147,4 @@ public class TreatmentModalController { public void handleCancel() { stage.close(); } - - public Treatment getTreatment() { - return treatment; - } } diff --git a/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java index 51ccde3..c821e7b 100644 --- a/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java +++ b/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java @@ -2,6 +2,7 @@ package de.hitec.nhplus.treatment.database; import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoImp; +import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.treatment.Treatment; import de.hitec.nhplus.utils.DateConverter; @@ -12,6 +13,12 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +/** + * The {@link TreatmentDao} is an implementation of the {@link de.hitec.nhplus.datastorage.Dao Dao} for the {@link Treatment} model. + * + * @author Bernd Heidemann + * @author Dominik Säume + */ public class TreatmentDao extends DaoImp { public TreatmentDao(Connection connection) { @@ -71,15 +78,18 @@ public class TreatmentDao extends DaoImp { return list; } - private PreparedStatement getReadAllTreatmentsOfOnePatientByPid(int patientId) throws SQLException { + /** + * Retrieves a list of {@link Treatment}s associated with a specific {@link Patient#id patient ID} from the database. + * + * @param patientId The {@link Patient#id ID} of the {@link Patient} whose {@link Treatment}s are to be retrieved. + * @return A {@link List} of {@link Treatment} objects associated with the specified {@link Patient} ID. + */ + public List readTreatmentsByPid(int patientId) throws SQLException { final String SQL = "SELECT * FROM treatment WHERE patientId = ?"; PreparedStatement statement = this.connection.prepareStatement(SQL); statement.setInt(1, patientId); - return statement; - } - public List readTreatmentsByPid(int patientId) throws SQLException { - ResultSet result = getReadAllTreatmentsOfOnePatientByPid(patientId).executeQuery(); + ResultSet result = statement.executeQuery(); return getListFromResultSet(result); }