#22: Setup MedicationDao

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-05 11:14:54 +02:00
parent cbecf482b8
commit 770d320a93
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
2 changed files with 158 additions and 13 deletions

View file

@ -1,25 +1,14 @@
package de.hitec.nhplus.medication; package de.hitec.nhplus.medication;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
public class Ingredient { public class Ingredient {
private SimpleIntegerProperty id; private final SimpleStringProperty name;
private SimpleStringProperty name;
public Ingredient(int id, String name) { public Ingredient(String name) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name); this.name = new SimpleStringProperty(name);
} }
public int getId() {
return id.get();
}
public SimpleIntegerProperty idProperty() {
return id;
}
public String getName() { public String getName() {
return name.get(); return name.get();
} }

View file

@ -0,0 +1,156 @@
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;
}
}