#32: Refactor Database Usage, to be more consistent and understandable #33

Merged
SZUT-Dominik merged 6 commits from task/refactor-dao into main 2024-05-06 12:23:41 +00:00
6 changed files with 128 additions and 7 deletions
Showing only changes of commit db367aaa5e - Show all commits

View file

@ -1,11 +1,17 @@
package de.hitec.nhplus.datastorage; package de.hitec.nhplus.datastorage;
import org.sqlite.SQLiteConfig;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import org.sqlite.SQLiteConfig; /**
* The {@link DaoFactory} allows a safe connection to the Database.
*
* @author Bernd Heidemann
* @version 1.0
*/
public class ConnectionBuilder { public class ConnectionBuilder {
private static final String DB_NAME = "nursingHome.db"; private static final String DB_NAME = "nursingHome.db";
@ -13,6 +19,10 @@ public class ConnectionBuilder {
private static Connection connection; private static Connection connection;
/**
* @return a Thread-safe {@link Connection} to the Database.
* @author Bernd Heidemann
*/
synchronized public static Connection getConnection() { synchronized public static Connection getConnection() {
try { try {
if (ConnectionBuilder.connection == null) { if (ConnectionBuilder.connection == null) {
@ -27,6 +37,11 @@ public class ConnectionBuilder {
return ConnectionBuilder.connection; return ConnectionBuilder.connection;
} }
/**
* Closes the Connection to the Database.
*
* @author Bernd Heidemann
*/
synchronized public static void closeConnection() { synchronized public static void closeConnection() {
try { try {
if (ConnectionBuilder.connection != null) { if (ConnectionBuilder.connection != null) {

View file

@ -3,14 +3,52 @@ package de.hitec.nhplus.datastorage;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
/**
* {@link Dao} is the Abbreviation of Data-Access-Object.
* This Interface has the Basic Methods which are needed on any DAO to work as expected.
*
* @param <T> The Model for which that DAO is implemented
* @author Bernd Heidemann
* @version 1.0
* @implSpec The Implementations should be added to the {@link DaoFactory}
*/
public interface Dao<T> { public interface Dao<T> {
/**
* Create a Database Entry from a Model object.
*
* @param t the Model instance
* @author Bernd Heidemann
*/
void create(T t) throws SQLException; void create(T t) throws SQLException;
/**
* Read a Database Entry to a Model object.
*
* @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.
*
* @author Bernd Heidemann
*/
List<T> readAll() throws SQLException; List<T> readAll() throws SQLException;
/**
* Update the Database according to the Values of the Model object.
*
* @param t the Model instance.
* @author Bernd Heidemann
*/
void update(T t) throws SQLException; void update(T t) throws SQLException;
void deleteById(int id) throws SQLException; /**
* Delete a Database Entry.
*
* @param id of the Element in the Database.
* @author Bernd Heidemann
*/
void delete(int id) throws SQLException;
} }

View file

@ -4,13 +4,28 @@ 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;
/**
* The {@link DaoFactory} is a Singleton({@link DaoFactory#getInstance}) which should be used to get {@link Dao}s.
*
* @author Bernd Heidemann
* @version 1.0
*/
public class DaoFactory { public class DaoFactory {
private static DaoFactory instance; private static DaoFactory instance;
/**
* A Private Constructor according to the Singleton Pattern.
*
* @author Bernd Heidemann
*/
private DaoFactory() { private DaoFactory() {
} }
/**
* @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) {
DaoFactory.instance = new DaoFactory(); DaoFactory.instance = new DaoFactory();
@ -18,14 +33,26 @@ public class DaoFactory {
return DaoFactory.instance; return DaoFactory.instance;
} }
/**
* @return a {@link TreatmentDao}
* @author Bernd Heidemann
*/
public TreatmentDao createTreatmentDao() { public TreatmentDao createTreatmentDao() {
return new TreatmentDao(ConnectionBuilder.getConnection()); return new TreatmentDao(ConnectionBuilder.getConnection());
} }
/**
* @return a {@link PatientDao}
* @author Bernd Heidemann
*/
public PatientDao createPatientDAO() { public PatientDao createPatientDAO() {
return new PatientDao(ConnectionBuilder.getConnection()); return new PatientDao(ConnectionBuilder.getConnection());
} }
/**
* @return a {@link NurseDao}
* @author Dominik Säume
*/
public NurseDao createNurseDAO() { public NurseDao createNurseDAO() {
return new NurseDao(ConnectionBuilder.getConnection()); return new NurseDao(ConnectionBuilder.getConnection());
} }

View file

@ -6,18 +6,43 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
/**
* The {@link DaoImp} is a Generic Base Implementation of the {@link Dao},
* which should fit most use cases.
*
* @param <T> The Model for which that DAO is implemented
* @author Bernd Heidemann
* @version 1.0
* @implSpec The Implementations should be added to the {@link DaoFactory}
*/
public abstract class DaoImp<T> implements Dao<T> { public abstract class DaoImp<T> implements Dao<T> {
protected final Connection connection; protected final Connection connection;
/**
* @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;
} }
/**
* Creates a new Database Entry from a Model object.
*
* @param t the Model instance
* @author Bernd Heidemann
*/
@Override @Override
public void create(T t) throws SQLException { public void create(T t) throws SQLException {
getCreateStatement(t).executeUpdate(); getCreateStatement(t).executeUpdate();
} }
/**
* Read a Database Entry to a Model object.
*
* @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 {
T object = null; T object = null;
@ -28,18 +53,35 @@ public abstract class DaoImp<T> implements Dao<T> {
return object; return object;
} }
/**
* 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 {
return getListFromResultSet(getReadAllStatement().executeQuery()); return getListFromResultSet(getReadAllStatement().executeQuery());
} }
/**
* Update the Database according to the Values of the Model object.
*
* @param t the Model instance.
* @author Bernd Heidemann
*/
@Override @Override
public void update(T t) throws SQLException { public void update(T t) throws SQLException {
getUpdateStatement(t).executeUpdate(); getUpdateStatement(t).executeUpdate();
} }
/**
* Delete a Database Entry.
*
* @param id of the Element in the Database.
* @author Bernd Heidemann
*/
@Override @Override
public void deleteById(int id) throws SQLException { public void delete(int id) throws SQLException {
getDeleteStatement(id).executeUpdate(); getDeleteStatement(id).executeUpdate();
} }

View file

@ -188,7 +188,7 @@ public class AllPatientController {
Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem(); Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem();
if (selectedItem != null) { if (selectedItem != null) {
try { try {
DaoFactory.getInstance().createPatientDAO().deleteById(selectedItem.getId()); DaoFactory.getInstance().createPatientDAO().delete(selectedItem.getId());
this.tableView.getItems().remove(selectedItem); this.tableView.getItems().remove(selectedItem);
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();

View file

@ -5,7 +5,6 @@ import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.patient.Patient; import de.hitec.nhplus.patient.Patient;
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;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
@ -147,7 +146,7 @@ public class AllTreatmentController {
Treatment t = this.treatments.remove(index); Treatment t = this.treatments.remove(index);
TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao(); TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
try { try {
dao.deleteById(t.getId()); dao.delete(t.getId());
} catch (SQLException exception) { } catch (SQLException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }