diff --git a/db/nursingHome.db b/db/nursingHome.db index fbd0833..4febd50 100644 Binary files a/db/nursingHome.db and b/db/nursingHome.db differ diff --git a/src/main/java/de/hitec/nhplus/controller/AllNurseController.java b/src/main/java/de/hitec/nhplus/controller/AllNurseController.java new file mode 100644 index 0000000..3bd75bb --- /dev/null +++ b/src/main/java/de/hitec/nhplus/controller/AllNurseController.java @@ -0,0 +1,59 @@ +package de.hitec.nhplus.controller; + +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.datastorage.NurseDao; +import de.hitec.nhplus.model.Nurse; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.control.cell.PropertyValueFactory; +import javafx.scene.control.cell.TextFieldTableCell; + +import java.sql.SQLException; + +public class AllNurseController { + + @FXML + private TableView tableView; + @FXML + private TableColumn columnId; + @FXML + private TableColumn columnFirstName; + @FXML + private TableColumn columnSurname; + @FXML + private TableColumn columnPhoneNumber; + + private final ObservableList nurses = FXCollections.observableArrayList(); + private NurseDao dao; + + public void initialize() { + this.readAllAndSHowInTableView(); + + this.columnId.setCellValueFactory(new PropertyValueFactory<>("nid")); + + this.columnFirstName.setCellValueFactory(new PropertyValueFactory<>("firstName")); + this.columnFirstName.setCellFactory(TextFieldTableCell.forTableColumn()); + + this.columnSurname.setCellValueFactory(new PropertyValueFactory<>("surname")); + this.columnSurname.setCellFactory(TextFieldTableCell.forTableColumn()); + + + this.columnPhoneNumber.setCellValueFactory(new PropertyValueFactory<>("phoneNumber")); + this.columnPhoneNumber.setCellFactory(TextFieldTableCell.forTableColumn()); + + this.tableView.setItems(this.nurses); + } + + private void readAllAndSHowInTableView(){ + this.nurses.clear(); + this.dao = DaoFactory.getDaoFactory().createNurseDAO(); + try { + this.nurses.addAll(this.dao.readAll()); + }catch (SQLException exception){ + exception.printStackTrace(); + } + } +} diff --git a/src/main/java/de/hitec/nhplus/controller/MainWindowController.java b/src/main/java/de/hitec/nhplus/controller/MainWindowController.java index 862380d..85b9f36 100644 --- a/src/main/java/de/hitec/nhplus/controller/MainWindowController.java +++ b/src/main/java/de/hitec/nhplus/controller/MainWindowController.java @@ -36,4 +36,14 @@ public class MainWindowController { exception.printStackTrace(); } } + + @FXML + private void handleShowAllNurses(ActionEvent event){ + FXMLLoader loader = new FXMLLoader(Main.class.getResource("/de/hitec/nhplus/AllNurseView.fxml")); + try { + mainBorderPane.setCenter(loader.load()); + }catch (IOException exception){ + exception.printStackTrace(); + } + } } diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java index 063e29f..17f1840 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java @@ -21,4 +21,8 @@ public class DaoFactory { public PatientDao createPatientDAO() { return new PatientDao(ConnectionBuilder.getConnection()); } + + public NurseDao createNurseDAO() { + return new NurseDao(ConnectionBuilder.getConnection()); + } } diff --git a/src/main/java/de/hitec/nhplus/datastorage/NurseDao.java b/src/main/java/de/hitec/nhplus/datastorage/NurseDao.java new file mode 100644 index 0000000..1400e5d --- /dev/null +++ b/src/main/java/de/hitec/nhplus/datastorage/NurseDao.java @@ -0,0 +1,115 @@ +package de.hitec.nhplus.datastorage; + +import de.hitec.nhplus.model.Nurse; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class NurseDao extends DaoImp { + public NurseDao(Connection connection) { + super(connection); + } + + @Override + protected PreparedStatement getCreateStatement(Nurse nurse) { + PreparedStatement preparedStatement = null; + try { + final String SQL = "INSERT INTO nurse (firstname, surname, phoneNumber)" + + "VALUES (?, ?, ?)"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setString(1, nurse.getFirstName()); + preparedStatement.setString(2, nurse.getSurname()); + preparedStatement.setString(3, nurse.getPhoneNumber()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } + + @Override + protected PreparedStatement getReadByIDStatement(long nid) { + PreparedStatement preparedStatement = null; + try { + final String SQL = "SELECT * FROM nurse WHERE nid = ?"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setLong(1, nid); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } + + @Override + protected Nurse getInstanceFromResultSet(ResultSet result) throws SQLException { + return new Nurse( + result.getLong(1), + result.getString(2), + result.getString(3), + result.getString(4) + ); + } + + @Override + protected PreparedStatement getReadAllStatement() { + PreparedStatement statement = null; + try { + final String SQL = "SELECT * FROM nurse"; + statement = this.connection.prepareStatement(SQL); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return statement; + } + + @Override + protected ArrayList getListFromResultSet(ResultSet result) throws SQLException { + ArrayList list = new ArrayList<>(); + while (result.next()) { + list.add(new Nurse( + result.getLong(1), + result.getString(2), + result.getString(3), + result.getString(4) + )); + } + return list; + } + + + @Override + protected PreparedStatement getUpdateStatement(Nurse nurse) { + PreparedStatement preparedStatement = null; + try { + final String SQL = + "UPDATE nurse SET " + + "firstname = ?, " + + "surname = ?, " + + "phoneNumber = ?, " + + "WHERE nid = ?"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setString(1, nurse.getFirstName()); + preparedStatement.setString(2, nurse.getSurname()); + preparedStatement.setString(3, nurse.getPhoneNumber()); + preparedStatement.setLong(4, nurse.getNid()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } + + @Override + protected PreparedStatement getDeleteStatement(long nid) { + PreparedStatement preparedStatement = null; + try { + final String SQL = "DELETE FROM nurse WHERE nid = ?"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setLong(1, nid); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } +} diff --git a/src/main/java/de/hitec/nhplus/fixtures/Fixtures.java b/src/main/java/de/hitec/nhplus/fixtures/Fixtures.java index 6643f1e..a9634cd 100644 --- a/src/main/java/de/hitec/nhplus/fixtures/Fixtures.java +++ b/src/main/java/de/hitec/nhplus/fixtures/Fixtures.java @@ -23,6 +23,12 @@ public class Fixtures treatmentFixture.dropTable(connection); treatmentFixture.setupTable(connection); treatmentFixture.load(); + + NurseFixture nurseFixture = new NurseFixture(); + nurseFixture.dropTable(connection); + nurseFixture.setupTable(connection); + nurseFixture.load(); + } catch (Exception exception){ System.out.println(exception.getMessage()); } diff --git a/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java new file mode 100644 index 0000000..aab03e7 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/fixtures/NurseFixture.java @@ -0,0 +1,65 @@ +package de.hitec.nhplus.fixtures; + +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.datastorage.NurseDao; +import de.hitec.nhplus.model.Nurse; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class NurseFixture implements Fixture { + @Override + public void dropTable(Connection connection) { + try (Statement statement = connection.createStatement()) { + statement.execute("DROP TABLE nurse"); + } catch (SQLException exception) { + System.out.println(exception.getMessage()); + } + } + + @Override + public void setupTable(Connection connection) { + final String SQL = "CREATE TABLE IF NOT EXISTS nurse (" + + "tid INTEGER PRIMARY KEY AUTOINCREMENT, " + + "firstname TEXT NOT NULL, " + + "surname TEXT NOT NULL, " + + "phoneNumber TEXT NOT NULL" + + ");"; + try (Statement statement = connection.createStatement()) { + statement.execute(SQL); + } catch (SQLException exception) { + System.out.println(exception.getMessage()); + } + } + + @Override + public Map load() throws SQLException { + List nurses = new ArrayList<>(); + + nurses.add(new Nurse( + "Ole", + "Kück", + "0123456789" + )); + + nurses.add(new Nurse( + "Armin", + "Armout", + "9876543210" + )); + NurseDao dao = DaoFactory.getDaoFactory().createNurseDAO(); + for(Nurse nurse : nurses){ + dao.create(nurse); + } + Map nursesByName = new HashMap<>(); + for (Nurse nurse : dao.readAll()){ + nursesByName.put(nurse.getFirstName(), nurse); + } + return nursesByName; + } +} diff --git a/src/main/java/de/hitec/nhplus/model/Nurse.java b/src/main/java/de/hitec/nhplus/model/Nurse.java new file mode 100644 index 0000000..20790e3 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/model/Nurse.java @@ -0,0 +1,49 @@ +package de.hitec.nhplus.model; + +import javafx.beans.property.SimpleLongProperty; +import javafx.beans.property.SimpleStringProperty; + +public class Nurse extends Person { + private SimpleLongProperty nid; + private final SimpleStringProperty phoneNumber; + + public Nurse( + String firstName, + String surname, + String phoneNumber + ) { + super(firstName, surname); + this.phoneNumber = new SimpleStringProperty(phoneNumber); + } + + public Nurse( + long nid, + String firstName, + String surname, + String phoneNumber + ) { + super(firstName, surname); + this.nid = new SimpleLongProperty(nid); + this.phoneNumber = new SimpleStringProperty(phoneNumber); + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber.set(phoneNumber); + } + + public long getNid() { + return nid.get(); + } + + public SimpleLongProperty nidProperty() { + return nid; + } + + public String getPhoneNumber() { + return phoneNumber.get(); + } + + public SimpleStringProperty phoneNumberProperty() { + return phoneNumber; + } +} diff --git a/src/main/resources/de/hitec/nhplus/AllCaregiverView.fxml b/src/main/resources/de/hitec/nhplus/AllCaregiverView.fxml deleted file mode 100644 index 8e82ca1..0000000 --- a/src/main/resources/de/hitec/nhplus/AllCaregiverView.fxml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + +