#32: Refactor DAO Code, without changing the Structure
All checks were successful
Quality Check / Qualty Check (push) Successful in 9s
All checks were successful
Quality Check / Qualty Check (push) Successful in 9s
Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
parent
cda74bbf82
commit
c71d3e128b
12 changed files with 42 additions and 47 deletions
|
@ -11,7 +11,7 @@ public class DaoFactory {
|
|||
private DaoFactory() {
|
||||
}
|
||||
|
||||
public static DaoFactory getDaoFactory() {
|
||||
public static DaoFactory getInstance() {
|
||||
if (DaoFactory.instance == null) {
|
||||
DaoFactory.instance = new DaoFactory();
|
||||
}
|
||||
|
|
|
@ -17,9 +17,9 @@ public abstract class DaoImp<T> implements Dao<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public T read(int key) throws SQLException {
|
||||
public T read(int id) throws SQLException {
|
||||
T object = null;
|
||||
ResultSet result = getReadByIDStatement(key).executeQuery();
|
||||
ResultSet result = getReadByIDStatement(id).executeQuery();
|
||||
if (result.next()) {
|
||||
object = getInstanceFromResultSet(result);
|
||||
}
|
||||
|
@ -37,21 +37,21 @@ public abstract class DaoImp<T> implements Dao<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(int key) throws SQLException {
|
||||
getDeleteStatement(key).executeUpdate();
|
||||
public void deleteById(int id) throws SQLException {
|
||||
getDeleteStatement(id).executeUpdate();
|
||||
}
|
||||
|
||||
protected abstract T getInstanceFromResultSet(ResultSet set) throws SQLException;
|
||||
protected abstract T getInstanceFromResultSet(ResultSet result) throws SQLException;
|
||||
|
||||
protected abstract ArrayList<T> getListFromResultSet(ResultSet set) throws SQLException;
|
||||
protected abstract List<T> getListFromResultSet(ResultSet result) throws SQLException;
|
||||
|
||||
protected abstract PreparedStatement getCreateStatement(T t);
|
||||
|
||||
protected abstract PreparedStatement getReadByIDStatement(int key);
|
||||
protected abstract PreparedStatement getReadByIDStatement(int id);
|
||||
|
||||
protected abstract PreparedStatement getReadAllStatement();
|
||||
|
||||
protected abstract PreparedStatement getUpdateStatement(T t);
|
||||
|
||||
protected abstract PreparedStatement getDeleteStatement(int key);
|
||||
protected abstract PreparedStatement getDeleteStatement(int id);
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ public class NurseFixture implements Fixture<Nurse> {
|
|||
"Armout",
|
||||
"9876543210"
|
||||
));
|
||||
NurseDao dao = DaoFactory.getDaoFactory().createNurseDAO();
|
||||
NurseDao dao = DaoFactory.getInstance().createNurseDAO();
|
||||
for(Nurse nurse : nurses){
|
||||
dao.create(nurse);
|
||||
}
|
||||
|
|
|
@ -96,7 +96,7 @@ public class PatientFixture implements Fixture<Patient>
|
|||
"110"
|
||||
));
|
||||
|
||||
PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO();
|
||||
PatientDao dao = DaoFactory.getInstance().createPatientDAO();
|
||||
for (Patient patient : patients){
|
||||
dao.create(patient);
|
||||
}
|
||||
|
|
|
@ -165,7 +165,7 @@ public class TreatmentFixture implements Fixture<Treatment>
|
|||
"Massage der Extremitäten zur Verbesserung der Durchblutung")
|
||||
);
|
||||
|
||||
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao();
|
||||
TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
|
||||
Map<String, Treatment> treatmentsById = new HashMap<>();
|
||||
for (Treatment treatment : treatments){
|
||||
dao.create(treatment);
|
||||
|
|
|
@ -73,7 +73,7 @@ public class AllNurseController {
|
|||
|
||||
private void readAllAndShowInTableView(){
|
||||
this.nurses.clear();
|
||||
this.dao = DaoFactory.getDaoFactory().createNurseDAO();
|
||||
this.dao = DaoFactory.getInstance().createNurseDAO();
|
||||
try {
|
||||
this.nurses.addAll(this.dao.readAll());
|
||||
}catch (SQLException exception){
|
||||
|
|
|
@ -7,6 +7,7 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NurseDao extends DaoImp<Nurse> {
|
||||
public NurseDao(Connection connection) {
|
||||
|
@ -65,7 +66,7 @@ public class NurseDao extends DaoImp<Nurse> {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<Nurse> getListFromResultSet(ResultSet result) throws SQLException {
|
||||
protected List<Nurse> getListFromResultSet(ResultSet result) throws SQLException {
|
||||
ArrayList<Nurse> list = new ArrayList<>();
|
||||
while (result.next()) {
|
||||
list.add(new Nurse(
|
||||
|
|
|
@ -174,7 +174,7 @@ public class AllPatientController {
|
|||
|
||||
private void readAllAndShowInTableView() {
|
||||
this.patients.clear();
|
||||
this.dao = DaoFactory.getDaoFactory().createPatientDAO();
|
||||
this.dao = DaoFactory.getInstance().createPatientDAO();
|
||||
try {
|
||||
this.patients.addAll(this.dao.readAll());
|
||||
} catch (SQLException exception) {
|
||||
|
@ -187,7 +187,7 @@ public class AllPatientController {
|
|||
Patient selectedItem = this.tableView.getSelectionModel().getSelectedItem();
|
||||
if (selectedItem != null) {
|
||||
try {
|
||||
DaoFactory.getDaoFactory().createPatientDAO().deleteById(selectedItem.getId());
|
||||
DaoFactory.getInstance().createPatientDAO().deleteById(selectedItem.getId());
|
||||
this.tableView.getItems().remove(selectedItem);
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PatientDao extends DaoImp<Patient> {
|
||||
|
||||
|
@ -70,7 +71,7 @@ public class PatientDao extends DaoImp<Patient> {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<Patient> getListFromResultSet(ResultSet result) throws SQLException {
|
||||
protected List<Patient> getListFromResultSet(ResultSet result) throws SQLException {
|
||||
ArrayList<Patient> list = new ArrayList<>();
|
||||
while (result.next()) {
|
||||
Patient patient = new Patient(
|
||||
|
|
|
@ -78,7 +78,7 @@ public class AllTreatmentController {
|
|||
|
||||
public void readAllAndShowInTableView() {
|
||||
comboBoxPatientSelection.getSelectionModel().select(0);
|
||||
this.dao = DaoFactory.getDaoFactory().createTreatmentDao();
|
||||
this.dao = DaoFactory.getInstance().createTreatmentDao();
|
||||
try {
|
||||
this.treatments.setAll(dao.readAll());
|
||||
} catch (SQLException exception) {
|
||||
|
@ -87,7 +87,7 @@ public class AllTreatmentController {
|
|||
}
|
||||
|
||||
private void createComboBoxData() {
|
||||
PatientDao dao = DaoFactory.getDaoFactory().createPatientDAO();
|
||||
PatientDao dao = DaoFactory.getInstance().createPatientDAO();
|
||||
try {
|
||||
patientList = (ArrayList<Patient>) dao.readAll();
|
||||
this.patientSelection.add("alle");
|
||||
|
@ -104,7 +104,7 @@ public class AllTreatmentController {
|
|||
public void handleComboBox() {
|
||||
String selectedPatient = this.comboBoxPatientSelection.getSelectionModel().getSelectedItem();
|
||||
this.treatments.clear();
|
||||
this.dao = DaoFactory.getDaoFactory().createTreatmentDao();
|
||||
this.dao = DaoFactory.getInstance().createTreatmentDao();
|
||||
|
||||
if (selectedPatient.equals("alle")) {
|
||||
try {
|
||||
|
@ -137,7 +137,7 @@ public class AllTreatmentController {
|
|||
public void handleDelete() {
|
||||
int index = this.tableView.getSelectionModel().getSelectedIndex();
|
||||
Treatment t = this.treatments.remove(index);
|
||||
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao();
|
||||
TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
|
||||
try {
|
||||
dao.deleteById(t.getId());
|
||||
} catch (SQLException exception) {
|
||||
|
@ -209,12 +209,12 @@ public class AllTreatmentController {
|
|||
Stage stage = new Stage();
|
||||
|
||||
TreatmentModalController controller = loader.getController();
|
||||
PatientDao pDao = DaoFactory.getDaoFactory().createPatientDAO();
|
||||
PatientDao pDao = DaoFactory.getInstance().createPatientDAO();
|
||||
controller.initialize(
|
||||
this,
|
||||
stage,
|
||||
treatment,
|
||||
pDao.read(treatment.getPatientId())
|
||||
pDao.read(treatment.getPatient().getId())
|
||||
);
|
||||
|
||||
stage.setScene(scene);
|
||||
|
@ -228,7 +228,7 @@ public class AllTreatmentController {
|
|||
}
|
||||
|
||||
public void createTreatment(Treatment treatment) {
|
||||
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao();
|
||||
TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
|
||||
try {
|
||||
dao.create(treatment);
|
||||
} catch (SQLException exception) {
|
||||
|
@ -237,7 +237,7 @@ public class AllTreatmentController {
|
|||
}
|
||||
|
||||
public void updateTreatment(Treatment treatment) {
|
||||
TreatmentDao dao = DaoFactory.getDaoFactory().createTreatmentDao();
|
||||
TreatmentDao dao = DaoFactory.getInstance().createTreatmentDao();
|
||||
try {
|
||||
dao.update(treatment);
|
||||
} catch (SQLException exception) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.hitec.nhplus.treatment;
|
||||
|
||||
import de.hitec.nhplus.patient.Patient;
|
||||
import de.hitec.nhplus.utils.DateConverter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
@ -8,16 +9,16 @@ import java.util.StringJoiner;
|
|||
|
||||
public class Treatment {
|
||||
private int id;
|
||||
private final int patientId;
|
||||
private final Patient patient;
|
||||
private LocalDate date;
|
||||
private LocalTime begin;
|
||||
private LocalTime end;
|
||||
private String description;
|
||||
private String remarks;
|
||||
|
||||
public Treatment(int patientId, LocalDate date, LocalTime begin,
|
||||
public Treatment(Patient patient, LocalDate date, LocalTime begin,
|
||||
LocalTime end, String description, String remarks) {
|
||||
this.patientId = patientId;
|
||||
this.patient = patient;
|
||||
this.date = date;
|
||||
this.begin = begin;
|
||||
this.end = end;
|
||||
|
@ -25,10 +26,10 @@ public class Treatment {
|
|||
this.remarks = remarks;
|
||||
}
|
||||
|
||||
public Treatment(int id, int patientId, LocalDate date, LocalTime begin,
|
||||
public Treatment(int id, Patient patient, LocalDate date, LocalTime begin,
|
||||
LocalTime end, String description, String remarks) {
|
||||
this.id = id;
|
||||
this.patientId = patientId;
|
||||
this.patient = patient;
|
||||
this.date = date;
|
||||
this.begin = begin;
|
||||
this.end = end;
|
||||
|
@ -40,8 +41,8 @@ public class Treatment {
|
|||
return id;
|
||||
}
|
||||
|
||||
public int getPatientId() {
|
||||
return this.patientId;
|
||||
public Patient getPatient() {
|
||||
return this.patient;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
|
@ -88,7 +89,7 @@ public class Treatment {
|
|||
return new StringJoiner(System.lineSeparator())
|
||||
.add("TREATMENT")
|
||||
.add("ID: " + this.getId())
|
||||
.add("PatientID: " + this.getPatientId())
|
||||
.add("Patient: " + this.getPatient().getSurname() + " " + this.getPatient().getFirstName())
|
||||
.add("Date: " + this.getDate())
|
||||
.add("Begin: " + this.getBegin())
|
||||
.add("End: " + this.getEnd())
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.hitec.nhplus.treatment;
|
||||
|
||||
import de.hitec.nhplus.datastorage.DaoFactory;
|
||||
import de.hitec.nhplus.datastorage.DaoImp;
|
||||
import de.hitec.nhplus.utils.DateConverter;
|
||||
|
||||
|
@ -23,7 +24,7 @@ public class TreatmentDao extends DaoImp<Treatment> {
|
|||
final String SQL = "INSERT INTO treatment (patientId, treatment_date, begin, end, description, remark) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?)";
|
||||
preparedStatement = this.connection.prepareStatement(SQL);
|
||||
preparedStatement.setInt(1, treatment.getPatientId());
|
||||
preparedStatement.setInt(1, treatment.getPatient().getId());
|
||||
preparedStatement.setString(2, treatment.getDate());
|
||||
preparedStatement.setString(3, treatment.getBegin());
|
||||
preparedStatement.setString(4, treatment.getEnd());
|
||||
|
@ -52,7 +53,7 @@ public class TreatmentDao extends DaoImp<Treatment> {
|
|||
protected Treatment getInstanceFromResultSet(ResultSet result) throws SQLException {
|
||||
return new Treatment(
|
||||
result.getInt(1),
|
||||
result.getInt(2),
|
||||
DaoFactory.getInstance().createPatientDAO().read(result.getInt(2)),
|
||||
DateConverter.convertStringToLocalDate(result.getString(3)),
|
||||
DateConverter.convertStringToLocalTime(result.getString(4)),
|
||||
DateConverter.convertStringToLocalTime(result.getString(5)),
|
||||
|
@ -74,19 +75,10 @@ public class TreatmentDao extends DaoImp<Treatment> {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<Treatment> getListFromResultSet(ResultSet result) throws SQLException {
|
||||
protected List<Treatment> getListFromResultSet(ResultSet result) throws SQLException {
|
||||
ArrayList<Treatment> list = new ArrayList<>();
|
||||
while (result.next()) {
|
||||
Treatment treatment = new Treatment(
|
||||
result.getInt(1),
|
||||
result.getInt(2),
|
||||
DateConverter.convertStringToLocalDate(result.getString(3)),
|
||||
DateConverter.convertStringToLocalTime(result.getString(4)),
|
||||
DateConverter.convertStringToLocalTime(result.getString(5)),
|
||||
result.getString(6),
|
||||
result.getString(7)
|
||||
);
|
||||
list.add(treatment);
|
||||
list.add(getInstanceFromResultSet(result));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
@ -122,7 +114,7 @@ public class TreatmentDao extends DaoImp<Treatment> {
|
|||
"remark = ? " +
|
||||
"WHERE id = ?";
|
||||
preparedStatement = this.connection.prepareStatement(SQL);
|
||||
preparedStatement.setInt(1, treatment.getPatientId());
|
||||
preparedStatement.setInt(1, treatment.getPatient().getId());
|
||||
preparedStatement.setString(2, treatment.getDate());
|
||||
preparedStatement.setString(3, treatment.getBegin());
|
||||
preparedStatement.setString(4, treatment.getEnd());
|
||||
|
|
Loading…
Reference in a new issue