Merge pull request '#22 story/medikamente-modul-grundimplementierung' (#34) from story/medikamente-modul-grundimplementierung into main
Some checks failed
Quality Check / Qualty Check (push) Successful in 14s
Javadoc Deploy / Javadoc (push) Failing after 25s

Reviewed-on: #34
Reviewed-by: SZUT-Ole <ole.kueck@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-07 12:58:49 +00:00
commit f5dc9c3343
23 changed files with 725 additions and 33 deletions

View file

@ -25,6 +25,8 @@
<option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/patient/database/Patient.sql" /> <option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/patient/database/Patient.sql" />
<option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql" /> <option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/treatment/database/Treatment.sql" />
<option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql" /> <option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/nurse/database/Nurse.sql" />
<option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/medication/database/Medication.sql" />
<option value="file://$PROJECT_DIR$/src/main/resources/de/hitec/nhplus/medication/database/Medication_Ingredient.sql" />
</array> </array>
</option> </option>
<option name="outLayout" value="File per object by schema.groovy" /> <option name="outLayout" value="File per object by schema.groovy" />

View file

@ -3,7 +3,6 @@
<component name="SqlDialectMappings"> <component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java" dialect="GenericSQL" /> <file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/patient/database/PatientDao.java" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java" dialect="GenericSQL" /> <file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/treatment/database/TreatmentDao.java" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/main/java/de/hitec/nhplus/treatment/database/NurseDao.java" dialect="GenericSQL" />
<file url="PROJECT" dialect="SQLite" /> <file url="PROJECT" dialect="SQLite" />
</component> </component>
</project> </project>

Binary file not shown.

View file

@ -21,7 +21,6 @@ public class ConnectionBuilder {
/** /**
* @return a Thread-safe {@link Connection} to the Database. * @return a Thread-safe {@link Connection} to the Database.
* @author Bernd Heidemann
*/ */
synchronized public static Connection getConnection() { synchronized public static Connection getConnection() {
try { try {
@ -39,8 +38,6 @@ public class ConnectionBuilder {
/** /**
* Closes the Connection to the Database. * Closes the Connection to the Database.
*
* @author Bernd Heidemann
*/ */
synchronized public static void closeConnection() { synchronized public static void closeConnection() {
try { try {

View file

@ -17,7 +17,6 @@ public interface Dao<T> {
* Create a Database Entry from a Model object. * Create a Database Entry from a Model object.
* *
* @param t the Model instance * @param t the Model instance
* @author Bernd Heidemann
*/ */
void create(T t) throws SQLException; void create(T t) throws SQLException;
@ -25,14 +24,11 @@ public interface Dao<T> {
* Read a Database Entry to a Model object. * Read a Database Entry to a Model object.
* *
* @param id of the Element in the Database * @param id of the Element in the Database
* @author Bernd Heidemann
*/ */
T read(int id) throws SQLException; T read(int id) throws SQLException;
/** /**
* Read all Database Entries to a {@link List} of Model objects. * Read all Database Entries to a {@link List} of Model objects.
*
* @author Bernd Heidemann
*/ */
List<T> readAll() throws SQLException; List<T> readAll() throws SQLException;
@ -40,7 +36,6 @@ public interface Dao<T> {
* Update the Database according to the Values of the Model object. * Update the Database according to the Values of the Model object.
* *
* @param t the Model instance. * @param t the Model instance.
* @author Bernd Heidemann
*/ */
void update(T t) throws SQLException; void update(T t) throws SQLException;
@ -48,7 +43,6 @@ public interface Dao<T> {
* Delete a Database Entry. * Delete a Database Entry.
* *
* @param id of the Element in the Database. * @param id of the Element in the Database.
* @author Bernd Heidemann
*/ */
void delete(int id) throws SQLException; void delete(int id) throws SQLException;
} }

View file

@ -1,5 +1,6 @@
package de.hitec.nhplus.datastorage; package de.hitec.nhplus.datastorage;
import de.hitec.nhplus.medication.database.MedicationDao;
import de.hitec.nhplus.nurse.database.NurseDao; import de.hitec.nhplus.nurse.database.NurseDao;
import de.hitec.nhplus.patient.database.PatientDao; import de.hitec.nhplus.patient.database.PatientDao;
import de.hitec.nhplus.treatment.database.TreatmentDao; import de.hitec.nhplus.treatment.database.TreatmentDao;
@ -16,15 +17,12 @@ public class DaoFactory {
/** /**
* A Private Constructor according to the Singleton Pattern. * A Private Constructor according to the Singleton Pattern.
*
* @author Bernd Heidemann
*/ */
private DaoFactory() { private DaoFactory() {
} }
/** /**
* @return {@link DaoFactory}, the Singleton Instance * @return {@link DaoFactory}, the Singleton Instance
* @author Bernd Heidemann
*/ */
public static DaoFactory getInstance() { public static DaoFactory getInstance() {
if (DaoFactory.instance == null) { if (DaoFactory.instance == null) {
@ -35,7 +33,6 @@ public class DaoFactory {
/** /**
* @return a {@link TreatmentDao} * @return a {@link TreatmentDao}
* @author Bernd Heidemann
*/ */
public TreatmentDao createTreatmentDao() { public TreatmentDao createTreatmentDao() {
return new TreatmentDao(ConnectionBuilder.getConnection()); return new TreatmentDao(ConnectionBuilder.getConnection());
@ -43,7 +40,6 @@ public class DaoFactory {
/** /**
* @return a {@link PatientDao} * @return a {@link PatientDao}
* @author Bernd Heidemann
*/ */
public PatientDao createPatientDAO() { public PatientDao createPatientDAO() {
return new PatientDao(ConnectionBuilder.getConnection()); return new PatientDao(ConnectionBuilder.getConnection());
@ -51,9 +47,15 @@ public class DaoFactory {
/** /**
* @return a {@link NurseDao} * @return a {@link NurseDao}
* @author Dominik Säume
*/ */
public NurseDao createNurseDAO() { public NurseDao createNurseDAO() {
return new NurseDao(ConnectionBuilder.getConnection()); return new NurseDao(ConnectionBuilder.getConnection());
} }
/**
* @return a {@link MedicationDao}
*/
public MedicationDao createMedicationDAO() {
return new MedicationDao(ConnectionBuilder.getConnection());
}
} }

View file

@ -20,7 +20,6 @@ public abstract class DaoImp<T> implements Dao<T> {
/** /**
* @param connection a Database Connection, which should be gotten from {@link ConnectionBuilder} * @param connection a Database Connection, which should be gotten from {@link ConnectionBuilder}
* @author Bernd Heidemann
*/ */
public DaoImp(Connection connection) { public DaoImp(Connection connection) {
this.connection = connection; this.connection = connection;
@ -30,7 +29,6 @@ public abstract class DaoImp<T> implements Dao<T> {
* Creates a new Database Entry from a Model object. * Creates a new Database Entry from a Model object.
* *
* @param t the Model instance * @param t the Model instance
* @author Bernd Heidemann
*/ */
@Override @Override
public void create(T t) throws SQLException { public void create(T t) throws SQLException {
@ -41,7 +39,6 @@ public abstract class DaoImp<T> implements Dao<T> {
* Read a Database Entry to a Model object. * Read a Database Entry to a Model object.
* *
* @param id of the Element in the Database * @param id of the Element in the Database
* @author Bernd Heidemann
*/ */
@Override @Override
public T read(int id) throws SQLException { public T read(int id) throws SQLException {
@ -55,8 +52,6 @@ public abstract class DaoImp<T> implements Dao<T> {
/** /**
* Read all Database Entries to a {@link List} of Model objects. * Read all Database Entries to a {@link List} of Model objects.
*
* @author Bernd Heidemann
*/ */
@Override @Override
public List<T> readAll() throws SQLException { public List<T> readAll() throws SQLException {
@ -67,7 +62,6 @@ public abstract class DaoImp<T> implements Dao<T> {
* Update the Database according to the Values of the Model object. * Update the Database according to the Values of the Model object.
* *
* @param t the Model instance. * @param t the Model instance.
* @author Bernd Heidemann
*/ */
@Override @Override
public void update(T t) throws SQLException { public void update(T t) throws SQLException {
@ -78,7 +72,6 @@ public abstract class DaoImp<T> implements Dao<T> {
* Delete a Database Entry. * Delete a Database Entry.
* *
* @param id of the Element in the Database. * @param id of the Element in the Database.
* @author Bernd Heidemann
*/ */
@Override @Override
public void delete(int id) throws SQLException { public void delete(int id) throws SQLException {

View file

@ -29,6 +29,11 @@ public class Fixtures
nurseFixture.setupTable(connection); nurseFixture.setupTable(connection);
nurseFixture.load(); nurseFixture.load();
MedicationFixture medicationFixture = new MedicationFixture();
medicationFixture.dropTable(connection);
medicationFixture.setupTable(connection);
medicationFixture.load();
} catch (Exception exception){ } catch (Exception exception){
System.out.println(exception.getMessage()); System.out.println(exception.getMessage());
} }

View file

@ -0,0 +1,157 @@
package de.hitec.nhplus.fixtures;
import de.hitec.nhplus.Main;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.medication.Ingredient;
import de.hitec.nhplus.medication.Medication;
import de.hitec.nhplus.medication.database.MedicationDao;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
public class MedicationFixture implements Fixture<Medication> {
private static final String SCHEMA = "/de/hitec/nhplus/medication/database/Medication.sql";
private static final String INGREDIENT_SCHEMA = "/de/hitec/nhplus/medication/database/Medication_Ingredient.sql";
@Override
public void dropTable(Connection connection) throws SQLException {
connection.createStatement().execute("DROP TABLE IF EXISTS medication");
connection.createStatement().execute("DROP TABLE IF EXISTS medication_ingredient");
}
@Override
public void setupTable(Connection connection) throws SQLException {
final InputStream schema = Main.class.getResourceAsStream(SCHEMA);
final InputStream ingredientSchema = Main.class.getResourceAsStream(INGREDIENT_SCHEMA);
assert schema != null;
assert ingredientSchema != null;
String SQL = new Scanner(schema, StandardCharsets.UTF_8)
.useDelimiter("\\A")
.next();
String ingredientSQL = ";" + new Scanner(ingredientSchema, StandardCharsets.UTF_8)
.useDelimiter("\\A")
.next();
connection.createStatement().execute(SQL);
connection.createStatement().execute(ingredientSQL);
}
@Override
public Map<String, Medication> load() throws SQLException {
List<Medication> medications = new ArrayList<>();
Ingredient metforminhydrochlorid = new Ingredient("Metforminhydrochlorid");
Ingredient cellulose = new Ingredient("Cellulose");
Ingredient povidon = new Ingredient("Povidon");
Ingredient magnesiumstearat = new Ingredient("Magnesiumstearat");
Ingredient lisinoprilDihydrat = new Ingredient("Lisinopril-Dihydrat");
Ingredient mannitol = new Ingredient("Mannitol");
Ingredient calciumphosphat = new Ingredient("Calciumphosphat");
Ingredient simvastatin = new Ingredient("Simvastatin");
Ingredient laktose = new Ingredient("Laktose");
Ingredient enoxaparinNatrium = new Ingredient("Enoxaparin-Natrium");
Ingredient benzylalkohol = new Ingredient("Benzylalkohol");
Ingredient wasser = new Ingredient("Wasser");
Ingredient levothyroxinnatrium = new Ingredient("Levothyroxinnatrium");
Ingredient staerke = new Ingredient("Stärke");
Ingredient akaziengummi = new Ingredient("Akaziengummi");
Ingredient warfarinnatrium = new Ingredient("Warfarinnatrium");
medications.add(new Medication(
1,
"Metformin",
"AstraZeneca",
List.of(
metforminhydrochlorid,
cellulose,
povidon,
magnesiumstearat
),
"Übelkeit, Durchfall, Laktatazidose (selten)",
"Oral",
100
));
medications.add(new Medication(
2,
"Lisinopril",
"Teva Pharmaceuticals",
List.of(
lisinoprilDihydrat,
mannitol,
calciumphosphat,
magnesiumstearat
),
"Schwindel, trockener Husten",
"Oral",
150
));
medications.add(new Medication(
3,
"Simvastatin",
"Mylan",
List.of(
simvastatin,
laktose,
cellulose,
magnesiumstearat
),
"Muskelschmerzen, Leberprobleme(selten)",
"Oral",
80
));
medications.add(new Medication(
4,
"Enoxaparin",
"Sanofi",
List.of(
enoxaparinNatrium,
benzylalkohol,
wasser
),
"Blutungen, Reaktionen an der Injektionsstelle",
"Unterhautinjektion",
120
));
medications.add(new Medication(
5,
"Levothyroxin",
"Sandoz",
List.of(
levothyroxinnatrium,
laktose,
staerke,
akaziengummi
),
"Herzrasen, Gewichtsverlust",
"Oral",
90
));
medications.add(new Medication(
6,
"Warfarin",
"Apotex Inc.",
List.of(
warfarinnatrium,
laktose,
staerke,
magnesiumstearat
),
"Blutungen, Blutergüsse",
"Oral",
110
));
MedicationDao dao = DaoFactory.getInstance().createMedicationDAO();
Map<String, Medication> medicationsByName = new HashMap<>();
for (Medication medication : medications) {
dao.create(medication);
medicationsByName.put(medication.getName(), medication);
}
return medicationsByName;
}
}

View file

@ -14,7 +14,7 @@ import java.util.*;
public class NurseFixture implements Fixture<Nurse> { public class NurseFixture implements Fixture<Nurse> {
@Override @Override
public void dropTable(Connection connection) throws SQLException { public void dropTable(Connection connection) throws SQLException {
connection.createStatement().execute("DROP TABLE nurse"); connection.createStatement().execute("DROP TABLE IF EXISTS nurse");
} }
@Override @Override

View file

@ -16,7 +16,7 @@ import static de.hitec.nhplus.utils.DateConverter.convertStringToLocalDate;
public class PatientFixture implements Fixture<Patient> { public class PatientFixture implements Fixture<Patient> {
@Override @Override
public void dropTable(Connection connection) throws SQLException { public void dropTable(Connection connection) throws SQLException {
connection.createStatement().execute("DROP TABLE patient"); connection.createStatement().execute("DROP TABLE IF EXISTS patient");
} }
@Override @Override

View file

@ -24,7 +24,7 @@ public class TreatmentFixture implements Fixture<Treatment> {
@Override @Override
public void dropTable(Connection connection) throws SQLException { public void dropTable(Connection connection) throws SQLException {
connection.createStatement().execute("DROP TABLE treatment"); connection.createStatement().execute("DROP TABLE IF EXISTS treatment");
} }
@Override @Override

View file

@ -26,6 +26,10 @@ public class MainWindowController {
private AnchorPane nursePage; private AnchorPane nursePage;
@FXML @FXML
private Tab nurseTab; private Tab nurseTab;
@FXML
private AnchorPane medicationPage;
@FXML
private Tab medicationTab;
@FXML @FXML
public void initialize() { public void initialize() {
@ -35,7 +39,7 @@ public class MainWindowController {
patientTab.setOnSelectionChanged(event -> loadPatientPage()); patientTab.setOnSelectionChanged(event -> loadPatientPage());
treatmentTab.setOnSelectionChanged(event -> loadTreatmentsPage()); treatmentTab.setOnSelectionChanged(event -> loadTreatmentsPage());
nurseTab.setOnSelectionChanged(event -> loadNursePage()); nurseTab.setOnSelectionChanged(event -> loadNursePage());
medicationTab.setOnSelectionChanged(event -> loadMedicationPage());
} }
private void loadPatientPage() { private void loadPatientPage() {
@ -82,4 +86,19 @@ public class MainWindowController {
exception.printStackTrace(); exception.printStackTrace();
} }
} }
private void loadMedicationPage() {
try {
BorderPane medicationPane = FXMLLoader.load(
Objects.requireNonNull(Main.class.getResource("/de/hitec/nhplus/medication/AllMedicationView.fxml"))
);
medicationPage.getChildren().setAll(medicationPane);
AnchorPane.setTopAnchor(medicationPane, 0d);
AnchorPane.setBottomAnchor(medicationPane, 0d);
AnchorPane.setLeftAnchor(medicationPane, 0d);
AnchorPane.setRightAnchor(medicationPane, 0d);
} catch (IOException exception) {
exception.printStackTrace();
}
}
} }

View file

@ -0,0 +1,69 @@
package de.hitec.nhplus.medication;
import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.medication.database.MedicationDao;
import javafx.beans.property.SimpleStringProperty;
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 java.sql.SQLException;
import java.util.stream.Collectors;
public class AllMedicationController {
@FXML
private TableView<Medication> tableView;
@FXML
private TableColumn<Medication, Integer> columnId;
@FXML
private TableColumn<Medication, String> columnName;
@FXML
private TableColumn<Medication, String> columnManufacturer;
@FXML
private TableColumn<Medication, String> columnIngredient;
@FXML
private TableColumn<Medication, String> columnPossibleSideEffects;
@FXML
private TableColumn<Medication, String> columnAdministrationMethod;
@FXML
private TableColumn<Medication, Integer> columnCurrentStock;
private final ObservableList<Medication> medications = FXCollections.observableArrayList();
private MedicationDao dao;
public void initialize() {
readAllAndShowInTableView();
this.columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
this.columnName.setCellValueFactory(new PropertyValueFactory<>("name"));
this.columnManufacturer.setCellValueFactory(new PropertyValueFactory<>("manufacturer"));
this.columnIngredient.setCellValueFactory(
cellData -> {
return new SimpleStringProperty(
cellData
.getValue()
.getIngredients()
.stream()
.map(ingredient -> ingredient.getName())
.collect(Collectors.joining("\n"))
);
});
this.columnPossibleSideEffects.setCellValueFactory(new PropertyValueFactory<>("possibleSideEffects"));
this.columnAdministrationMethod.setCellValueFactory(new PropertyValueFactory<>("administrationMethod"));
this.columnCurrentStock.setCellValueFactory(new PropertyValueFactory<>("currentStock"));
this.tableView.setItems(this.medications);
}
public void readAllAndShowInTableView() {
this.dao = DaoFactory.getInstance().createMedicationDAO();
try {
this.medications.setAll(dao.readAll());
} catch (SQLException exception) {
exception.printStackTrace();
}
}
}

View 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);
}
}

View file

@ -0,0 +1,152 @@
package de.hitec.nhplus.medication;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.List;
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,
List<Ingredient> ingredients,
String possibleSideEffects,
String administrationMethod,
int currentStock
) {
this.name = new SimpleStringProperty(name);
this.manufacturer = new SimpleStringProperty(manufacturer);
this.ingredients = new SimpleListProperty<>(FXCollections.observableArrayList(ingredients));
this.possibleSideEffects = new SimpleStringProperty(possibleSideEffects);
this.administrationMethod = new SimpleStringProperty(administrationMethod);
this.currentStock = new SimpleIntegerProperty(currentStock);
}
public Medication(
int id,
String name,
String manufacturer,
List<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<>(FXCollections.observableArrayList(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(List<Ingredient> ingredients) {
this.ingredients.set(FXCollections.observableArrayList(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();
}
}

View file

@ -0,0 +1,175 @@
package de.hitec.nhplus.medication.database;
import de.hitec.nhplus.datastorage.Dao;
import de.hitec.nhplus.medication.Ingredient;
import de.hitec.nhplus.medication.Medication;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MedicationDao implements Dao<Medication> {
protected final Connection connection;
public MedicationDao(Connection connection) {
this.connection = connection;
}
@Override
public void create(Medication medication) throws SQLException {
final String medicationSQL = """
INSERT INTO medication
(name, manufacturer, possibleSideEffects, administrationMethod, currentStock)
VALUES (?, ?, ?, ?, ?);
""";
PreparedStatement medicationStatement = this.connection.prepareStatement(medicationSQL);
medicationStatement.setString(1, medication.getName());
medicationStatement.setString(2, medication.getManufacturer());
medicationStatement.setString(3, medication.getPossibleSideEffects());
medicationStatement.setString(4, medication.getAdministrationMethod());
medicationStatement.setInt(5, medication.getCurrentStock());
medicationStatement.execute();
final String ingredientSQL = """
INSERT INTO medication_ingredient
(id, name)
VALUES (?, ?);
""";
for (Ingredient ingredient : medication.getIngredients()) {
PreparedStatement ingredientStatement = this.connection.prepareStatement(ingredientSQL);
ingredientStatement.setInt(1, medication.getId());
ingredientStatement.setString(2, ingredient.getName());
ingredientStatement.execute();
}
}
@Override
public Medication read(int id) throws SQLException {
final String SQL = """
SELECT medication.*, medication_ingredient.id
FROM medication
LEFT JOIN medication_ingredient ON medication.id = medication_ingredient.id
WHERE medication.id = ?
""";
PreparedStatement statement = this.connection.prepareStatement(SQL);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();
return getInstanceFromResultSet(result);
}
@Override
public List<Medication> readAll() throws SQLException {
final String SQL = """
SELECT medication.*, medication_ingredient.name
FROM medication LEFT JOIN
medication_ingredient ON medication.id = medication_ingredient.id
""";
ResultSet result = connection.prepareStatement(SQL).executeQuery();
List<Medication> medications = new ArrayList<>();
Map<Integer, List<Ingredient>> ingredientMap = new HashMap<>();
int currentMedicationId;
int lastMedicationId = -1;
while (result.next()) {
currentMedicationId = result.getInt(1);
if (currentMedicationId != lastMedicationId) {
Medication medication = new Medication(
result.getInt(1),
result.getString(2),
result.getString(3),
List.of(),
result.getString(4),
result.getString(5),
result.getInt(6)
);
medications.add(medication);
lastMedicationId = currentMedicationId;
continue;
}
List<Ingredient> ingredients = ingredientMap.computeIfAbsent(currentMedicationId, k -> new ArrayList<>());
ingredients.add(new Ingredient(result.getString(7)));
lastMedicationId = currentMedicationId;
}
for (Medication medication : medications) {
medication.setIngredients(ingredientMap.get(medication.getId()));
}
return medications;
}
@Override
public void update(Medication medication) throws SQLException {
final String SQL = """
UPDATE medication SET
name = ?,
manufacturer = ?,
possibleSideEffects = ?,
administrationMethod = ?,
currentStock = ?
WHERE id = ?
""";
PreparedStatement 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());
preparedStatement.executeUpdate();
final String ingredientDeleteSQL = """
DELETE FROM medication_ingredient WHERE id = ?
""";
PreparedStatement ingredientStatement = this.connection.prepareStatement(ingredientDeleteSQL);
ingredientStatement.setInt(1, medication.getId());
ingredientStatement.executeUpdate();
final String ingredientCreateSQL = """
INSERT INTO medication_ingredient
(id, name)
VALUES (?, ?);
""";
for (Ingredient ingredient : medication.getIngredients()) {
PreparedStatement statement = this.connection.prepareStatement(ingredientCreateSQL);
statement.setInt(1, medication.getId());
statement.setString(2, ingredient.getName());
statement.execute();
}
}
@Override
public void delete(int id) throws SQLException {
final String SQL = """
DELETE FROM medication WHERE medication.id = ?;
""";
PreparedStatement preparedStatement = this.connection.prepareStatement(SQL);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
}
private Medication getInstanceFromResultSet(ResultSet result) throws SQLException {
Medication medication = new Medication(
result.getInt(1),
result.getString(2),
result.getString(3),
List.of(),
result.getString(4),
result.getString(5),
result.getInt(6)
);
List<Ingredient> ingredients = new ArrayList<>();
while (result.next()) {
ingredients.add(new Ingredient(result.getString(2)));
}
medication.setIngredients(ingredients);
return medication;
}
}

View file

@ -24,4 +24,9 @@ module de.hitec.nhplus {
exports de.hitec.nhplus.nurse.database; exports de.hitec.nhplus.nurse.database;
opens de.hitec.nhplus.nurse to javafx.base, javafx.fxml; opens de.hitec.nhplus.nurse to javafx.base, javafx.fxml;
opens de.hitec.nhplus.nurse.database to javafx.base, javafx.fxml; opens de.hitec.nhplus.nurse.database to javafx.base, javafx.fxml;
exports de.hitec.nhplus.medication;
exports de.hitec.nhplus.medication.database;
opens de.hitec.nhplus.medication to javafx.base, javafx.fxml;
opens de.hitec.nhplus.medication.database to javafx.base, javafx.fxml;
} }

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?> <?import javafx.scene.control.*?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.AnchorPane?>
<TabPane <TabPane
fx:id="mainTabPane" fx:id="mainTabPane"
@ -19,4 +18,7 @@
<Tab fx:id="nurseTab" text="Pfleger"> <Tab fx:id="nurseTab" text="Pfleger">
<AnchorPane fx:id="nursePage"/> <AnchorPane fx:id="nursePage"/>
</Tab> </Tab>
<Tab fx:id="medicationTab" text="Medikamente">
<AnchorPane fx:id="medicationPage"/>
</Tab>
</TabPane> </TabPane>

View file

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="de.hitec.nhplus.medication.AllMedicationController"
>
<padding>
<Insets top="8" left="8" right="8" bottom="8"/>
</padding>
<center>
<TableView fx:id="tableView">
<columns>
<TableColumn
fx:id="columnId"
minWidth="40.0"
text="ID"
/>
<TableColumn
fx:id="columnName"
minWidth="140.0"
text="Name"
/>
<TableColumn
fx:id="columnManufacturer"
minWidth="140.0"
text="Hersteller"
/>
<TableColumn
fx:id="columnIngredient"
minWidth="140.0"
text="Inhaltsstoffe"
/>
<TableColumn
fx:id="columnPossibleSideEffects"
minWidth="200.0"
text="Mögliche Nebenwirkungen"
/>
<TableColumn
fx:id="columnAdministrationMethod"
minWidth="180.0"
text="Verabreichungsmethode"
/>
<TableColumn
fx:id="columnCurrentStock"
minWidth="100.0"
text="Lagerbestand"
/>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
</TableView>
</center>
<bottom>
<BorderPane>
<BorderPane.margin>
<Insets top="8.0"/>
</BorderPane.margin>
<right>
<HBox spacing="8.0">
<Button
fx:id="buttonAdd"
mnemonicParsing="false"
prefWidth="90.0"
text="Hinzufügen"
/>
<Button
fx:id="buttonDelete"
mnemonicParsing="false"
prefWidth="90.0"
text="Löschen"
/>
</HBox>
</right>
</BorderPane>
</bottom>
</BorderPane>

View file

@ -0,0 +1,9 @@
CREATE TABLE medication
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
manufacturer TEXT NOT NULL,
possibleSideEffects TEXT NOT NULL,
administrationMethod TEXT NOT NULL,
currentStock INTEGER NOT NULL
)

View file

@ -0,0 +1,6 @@
CREATE TABLE medication_ingredient
(
id INTEGER NOT NULL ,
name TEXT NOT NULL,
FOREIGN KEY (id) REFERENCES medication (id) ON DELETE CASCADE
)

View file

@ -12,7 +12,7 @@
<Insets top="8" left="8" right="8" bottom="8"/> <Insets top="8" left="8" right="8" bottom="8"/>
</padding> </padding>
<center> <center>
<TableView fx:id="tableView" editable="true" layoutX="31.0" layoutY="40.0"> <TableView fx:id="tableView" editable="true">
<columns> <columns>
<TableColumn <TableColumn
fx:id="columnId" fx:id="columnId"