#22: Setup Model and DAO
All checks were successful
Quality Check / Qualty Check (push) Successful in 8s
All checks were successful
Quality Check / Qualty Check (push) Successful in 8s
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
parent
40a9a793cd
commit
6411b15a37
3 changed files with 332 additions and 0 deletions
25
src/main/java/de/hitec/nhplus/medication/Ingredient.java
Normal file
25
src/main/java/de/hitec/nhplus/medication/Ingredient.java
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
150
src/main/java/de/hitec/nhplus/medication/Medication.java
Normal file
150
src/main/java/de/hitec/nhplus/medication/Medication.java
Normal file
|
@ -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<Ingredient> ingredients;
|
||||
private final SimpleStringProperty possibleSideEffects;
|
||||
private final SimpleStringProperty administrationMethod;
|
||||
private final SimpleIntegerProperty currentStock;
|
||||
|
||||
public Medication(
|
||||
String name,
|
||||
String manufacturer,
|
||||
ObservableList<Ingredient> 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<Ingredient> 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<Ingredient> getIngredients() {
|
||||
return ingredients.get();
|
||||
}
|
||||
|
||||
public SimpleListProperty<Ingredient> ingredientsProperty() {
|
||||
return ingredients;
|
||||
}
|
||||
|
||||
public void setIngredients(ObservableList<Ingredient> 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();
|
||||
}
|
||||
}
|
157
src/main/java/de/hitec/nhplus/medication/MedicationDao.java
Normal file
157
src/main/java/de/hitec/nhplus/medication/MedicationDao.java
Normal file
|
@ -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<Medication> {
|
||||
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<Ingredient> 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<Medication> getListFromResultSet(ResultSet result) throws SQLException {
|
||||
ArrayList<Medication> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue