diff --git a/src/main/java/de/hitec/nhplus/medication/Ingredient.java b/src/main/java/de/hitec/nhplus/medication/Ingredient.java new file mode 100644 index 0000000..626e3c0 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/medication/Ingredient.java @@ -0,0 +1,25 @@ +package de.hitec.nhplus.medication; + +import javafx.beans.property.SimpleStringProperty; + +public class Ingredient { + private final SimpleStringProperty name; + + public Ingredient(String name) { + this.name = new SimpleStringProperty(name); + } + + public String getName() { + return name.get(); + } + + public SimpleStringProperty nameProperty() { + return name; + } + + public void setName(String name) { + this.name.set(name); + } + + +} diff --git a/src/main/java/de/hitec/nhplus/medication/Medication.java b/src/main/java/de/hitec/nhplus/medication/Medication.java new file mode 100644 index 0000000..bf6046c --- /dev/null +++ b/src/main/java/de/hitec/nhplus/medication/Medication.java @@ -0,0 +1,150 @@ +package de.hitec.nhplus.medication; + +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.collections.ObservableList; + +import java.util.StringJoiner; +import java.util.stream.Collectors; + +public class Medication { + private SimpleIntegerProperty id; + private final SimpleStringProperty name; + private final SimpleStringProperty manufacturer; + private final SimpleListProperty ingredients; + private final SimpleStringProperty possibleSideEffects; + private final SimpleStringProperty administrationMethod; + private final SimpleIntegerProperty currentStock; + + public Medication( + String name, + String manufacturer, + ObservableList ingredients, + String possibleSideEffects, + String administrationMethod, + int currentStock + ) { + this.name = new SimpleStringProperty(name); + this.manufacturer = new SimpleStringProperty(manufacturer); + this.ingredients = new SimpleListProperty<>(ingredients); + this.possibleSideEffects = new SimpleStringProperty(possibleSideEffects); + this.administrationMethod = new SimpleStringProperty(administrationMethod); + this.currentStock = new SimpleIntegerProperty(currentStock); + } + + public Medication( + int id, + String name, + String manufacturer, + ObservableList ingredients, + String possibleSideEffects, + String administrationMethod, + int currentStock + ) { + this.id = new SimpleIntegerProperty(id); + this.name = new SimpleStringProperty(name); + this.manufacturer = new SimpleStringProperty(manufacturer); + this.ingredients = new SimpleListProperty<>(ingredients); + this.possibleSideEffects = new SimpleStringProperty(possibleSideEffects); + this.administrationMethod = new SimpleStringProperty(administrationMethod); + this.currentStock = new SimpleIntegerProperty(currentStock); + } + + public int getId() { + return id.get(); + } + + public SimpleIntegerProperty idProperty() { + return id; + } + + public String getName() { + return name.get(); + } + + public SimpleStringProperty nameProperty() { + return name; + } + + public void setName(String name) { + this.name.set(name); + } + + public String getManufacturer() { + return manufacturer.get(); + } + + public SimpleStringProperty manufacturerProperty() { + return manufacturer; + } + + public void setManufacturer(String manufacturer) { + this.manufacturer.set(manufacturer); + } + + public ObservableList getIngredients() { + return ingredients.get(); + } + + public SimpleListProperty ingredientsProperty() { + return ingredients; + } + + public void setIngredients(ObservableList ingredients) { + this.ingredients.set(ingredients); + } + + public String getPossibleSideEffects() { + return possibleSideEffects.get(); + } + + public SimpleStringProperty possibleSideEffectsProperty() { + return possibleSideEffects; + } + + public void setPossibleSideEffects(String possibleSideEffects) { + this.possibleSideEffects.set(possibleSideEffects); + } + + public String getAdministrationMethod() { + return administrationMethod.get(); + } + + public SimpleStringProperty administrationMethodProperty() { + return administrationMethod; + } + + public void setAdministrationMethod(String administrationMethod) { + this.administrationMethod.set(administrationMethod); + } + + public int getCurrentStock() { + return currentStock.get(); + } + + public SimpleIntegerProperty currentStockProperty() { + return currentStock; + } + + public void setCurrentStock(int currentStock) { + this.currentStock.set(currentStock); + } + + @Override + public String toString() { + return new StringJoiner(System.lineSeparator()) + .add("MEDICATION") + .add("ID: " + this.getId()) + .add("Name: " + this.getName()) + .add("Manufacturer: " + this.getManufacturer()) + .add("Ingredients: " + this.getIngredients() + .stream() + .map(Ingredient::getName) + .collect(Collectors.joining(", ", "[", "]"))) + .add("Possible Side Effects: " + this.getPossibleSideEffects()) + .add("Administration Method: " + this.getAdministrationMethod()) + .add("Current Stock: " + this.getCurrentStock()) + .toString(); + } +} diff --git a/src/main/java/de/hitec/nhplus/medication/MedicationDao.java b/src/main/java/de/hitec/nhplus/medication/MedicationDao.java new file mode 100644 index 0000000..1c27561 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/medication/MedicationDao.java @@ -0,0 +1,157 @@ +package de.hitec.nhplus.medication; + +import de.hitec.nhplus.datastorage.DaoImp; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.StringJoiner; + +public class MedicationDao extends DaoImp { + public MedicationDao(Connection connection) { + super(connection); + } + + + @Override + protected PreparedStatement getCreateStatement(Medication medication) { + PreparedStatement preparedStatement = null; + try { + StringJoiner SQL = new StringJoiner(";"); + SQL.add( + "INSERT INTO medication " + + "(name, manufacturer, possibleSideEffects, administrationMethod, currentMethod) " + + "VALUES (?, ?, ?, ?, ?);" + ); + final ObservableList ingredients = medication.getIngredients(); + for (int i = 0; i < ingredients.size(); i++) { + SQL.add( + "INSERT INTO medication_ingredient (medicationId, ingredient) " + + "VALUES (?, ?)" + ); + } + preparedStatement = this.connection.prepareStatement(SQL.toString()); + preparedStatement.setString(1, medication.getName()); + preparedStatement.setString(2, medication.getManufacturer()); + preparedStatement.setString(3, medication.getPossibleSideEffects()); + preparedStatement.setString(4, medication.getAdministrationMethod()); + preparedStatement.setInt(5, medication.getCurrentStock()); + for (int i = 0; i < ingredients.size(); i++) { + preparedStatement.setInt(6 + i * 2, medication.getId()); + preparedStatement.setString(7 + i * 2, ingredients.get(i).getName()); + } + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } + + @Override + protected PreparedStatement getReadByIDStatement(int id) { + PreparedStatement preparedStatement = null; + try { + final String SQL = "SELECT * FROM treatment WHERE id = ?"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } + + @Override + protected Medication getInstanceFromResultSet(ResultSet result) throws SQLException { + Medication medication = new Medication( + result.getInt(1), + result.getString(2), + result.getString(3), + FXCollections.observableArrayList(), + result.getString(4), + result.getString(5), + result.getInt(6) + ); + PreparedStatement preparedStatement = null; + try { + final String SQL = "SELECT * FROM medication_ingredient WHERE id = ?"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, medication.getId()); + ResultSet ingredientResults = preparedStatement.executeQuery(); + while (result.next()) { + medication + .getIngredients() + .add(new Ingredient( + ingredientResults.getString(2) + )) + ; + } + } catch (SQLException exception) { + exception.printStackTrace(); + } + return medication; + } + + @Override + protected PreparedStatement getReadAllStatement() { + PreparedStatement statement = null; + try { + final String SQL = "SELECT * FROM medication"; + 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(getInstanceFromResultSet(result)); + } + return list; + } + + @Override + protected PreparedStatement getUpdateStatement(Medication medication) { + PreparedStatement preparedStatement = null; + try { + final String SQL = + "UPDATE medication SET " + + "name = ?, " + + "manufacturer = ?, " + + "possibleSideEffects = ?, " + + "administrationMethod = ?, " + + "currentStock = ? " + + "WHERE id = ?"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setString(1, medication.getName()); + preparedStatement.setString(2, medication.getManufacturer()); + preparedStatement.setString(3, medication.getPossibleSideEffects()); + preparedStatement.setString(4, medication.getAdministrationMethod()); + preparedStatement.setInt(5, medication.getCurrentStock()); + preparedStatement.setInt(6, medication.getId()); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } + + @Override + protected PreparedStatement getDeleteStatement(int id) { + PreparedStatement preparedStatement = null; + try { + final String SQL = + "DELETE FROM medication WHERE id = ?; DELETE FROM medication_ingredient WHERE id = ?"; + preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); + preparedStatement.setInt(2, id); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return preparedStatement; + } +}