#32: Refactor Database Usage, to be more consistent and understandable #33
6 changed files with 128 additions and 7 deletions
|
@ -1,11 +1,17 @@
|
|||
package de.hitec.nhplus.datastorage;
|
||||
|
||||
import org.sqlite.SQLiteConfig;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
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 {
|
||||
|
||||
private static final String DB_NAME = "nursingHome.db";
|
||||
|
@ -13,6 +19,10 @@ public class ConnectionBuilder {
|
|||
|
||||
private static Connection connection;
|
||||
|
||||
/**
|
||||
* @return a Thread-safe {@link Connection} to the Database.
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
synchronized public static Connection getConnection() {
|
||||
try {
|
||||
if (ConnectionBuilder.connection == null) {
|
||||
|
@ -27,6 +37,11 @@ public class ConnectionBuilder {
|
|||
return ConnectionBuilder.connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the Connection to the Database.
|
||||
*
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
synchronized public static void closeConnection() {
|
||||
try {
|
||||
if (ConnectionBuilder.connection != null) {
|
||||
|
|
|
@ -3,14 +3,52 @@ package de.hitec.nhplus.datastorage;
|
|||
import java.sql.SQLException;
|
||||
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> {
|
||||
/**
|
||||
* Create a Database Entry from a Model object.
|
||||
*
|
||||
* @param t the Model instance
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* Read all Database Entries to a {@link List} of Model objects.
|
||||
*
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
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 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;
|
||||
}
|
||||
|
|
|
@ -4,13 +4,28 @@ import de.hitec.nhplus.nurse.database.NurseDao;
|
|||
import de.hitec.nhplus.patient.database.PatientDao;
|
||||
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 {
|
||||
|
||||
private static DaoFactory instance;
|
||||
|
||||
/**
|
||||
* A Private Constructor according to the Singleton Pattern.
|
||||
*
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
private DaoFactory() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@link DaoFactory}, the Singleton Instance
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
public static DaoFactory getInstance() {
|
||||
if (DaoFactory.instance == null) {
|
||||
DaoFactory.instance = new DaoFactory();
|
||||
|
@ -18,14 +33,26 @@ public class DaoFactory {
|
|||
return DaoFactory.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a {@link TreatmentDao}
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
public TreatmentDao createTreatmentDao() {
|
||||
return new TreatmentDao(ConnectionBuilder.getConnection());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a {@link PatientDao}
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
public PatientDao createPatientDAO() {
|
||||
return new PatientDao(ConnectionBuilder.getConnection());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a {@link NurseDao}
|
||||
* @author Dominik Säume
|
||||
*/
|
||||
public NurseDao createNurseDAO() {
|
||||
return new NurseDao(ConnectionBuilder.getConnection());
|
||||
}
|
||||
|
|
|
@ -6,18 +6,43 @@ import java.sql.ResultSet;
|
|||
import java.sql.SQLException;
|
||||
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> {
|
||||
protected final Connection connection;
|
||||
|
||||
/**
|
||||
* @param connection a Database Connection, which should be gotten from {@link ConnectionBuilder}
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
public DaoImp(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Database Entry from a Model object.
|
||||
*
|
||||
* @param t the Model instance
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
@Override
|
||||
public void create(T t) throws SQLException {
|
||||
getCreateStatement(t).executeUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a Database Entry to a Model object.
|
||||
*
|
||||
* @param id of the Element in the Database
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
@Override
|
||||
public T read(int id) throws SQLException {
|
||||
T object = null;
|
||||
|
@ -28,18 +53,35 @@ public abstract class DaoImp<T> implements Dao<T> {
|
|||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all Database Entries to a {@link List} of Model objects.
|
||||
*
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
@Override
|
||||
public List<T> readAll() throws SQLException {
|
||||
return getListFromResultSet(getReadAllStatement().executeQuery());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the Database according to the Values of the Model object.
|
||||
*
|
||||
* @param t the Model instance.
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
@Override
|
||||
public void update(T t) throws SQLException {
|
||||
getUpdateStatement(t).executeUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a Database Entry.
|
||||
*
|
||||
* @param id of the Element in the Database.
|
||||
* @author Bernd Heidemann
|
||||
*/
|
||||
@Override
|
||||
public void deleteById(int id) throws SQLException {
|
||||
public void delete(int id) throws SQLException {
|
||||
getDeleteStatement(id).executeUpdate();
|
||||
}
|
||||
|
||||
|
|
|
@ -188,7 +188,7 @@ public class AllPatientController {
|
|||
Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem();
|
||||
if (selectedItem != null) {
|
||||
try {
|
||||
DaoFactory.getInstance().createPatientDAO().deleteById(selectedItem.getId());
|
||||
DaoFactory.getInstance().createPatientDAO().delete(selectedItem.getId());
|
||||
this.tableView.getItems().remove(selectedItem);
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
|
|
|
@ -5,7 +5,6 @@ import de.hitec.nhplus.datastorage.DaoFactory;
|
|||
import de.hitec.nhplus.patient.Patient;
|
||||
import de.hitec.nhplus.patient.database.PatientDao;
|
||||
import de.hitec.nhplus.treatment.database.TreatmentDao;
|
||||
import javafx.beans.property.SimpleIntegerProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
|
@ -147,7 +146,7 @@ public class AllTreatmentController {
|
|||
Treatment t = this.treatments.remove(index);
|
||||
TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
|
||||
try {
|
||||
dao.deleteById(t.getId());
|
||||
dao.delete(t.getId());
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue