diff --git a/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java b/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java index ea22873..82cea08 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java +++ b/src/main/java/de/hitec/nhplus/datastorage/ConnectionBuilder.java @@ -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) { diff --git a/src/main/java/de/hitec/nhplus/datastorage/Dao.java b/src/main/java/de/hitec/nhplus/datastorage/Dao.java index 172bea1..0a38d48 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/Dao.java +++ b/src/main/java/de/hitec/nhplus/datastorage/Dao.java @@ -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 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 { + /** + * 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 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; } diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java index 08c2ba7..f4fc8dc 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoFactory.java @@ -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()); } diff --git a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java index 632e21e..417c285 100644 --- a/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java +++ b/src/main/java/de/hitec/nhplus/datastorage/DaoImp.java @@ -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 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 implements Dao { 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 implements Dao { return object; } + /** + * Read all Database Entries to a {@link List} of Model objects. + * + * @author Bernd Heidemann + */ @Override public List 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(); } diff --git a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java index e9df51d..1e99995 100644 --- a/src/main/java/de/hitec/nhplus/patient/AllPatientController.java +++ b/src/main/java/de/hitec/nhplus/patient/AllPatientController.java @@ -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(); diff --git a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java index fd982ae..c0f01c3 100644 --- a/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java +++ b/src/main/java/de/hitec/nhplus/treatment/AllTreatmentController.java @@ -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(); }