Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.hshn.mi.pdbg.basicservice;
- import de.hshn.mi.pdbg.AbstractPersistentObject;
- import de.hshn.mi.pdbg.PersistentObject;
- import de.hshn.mi.pdbg.basicservice.Impl.HospitalStayImpl;
- import de.hshn.mi.pdbg.basicservice.Impl.PatientImpl;
- import de.hshn.mi.pdbg.basicservice.Impl.WardImpl;
- import de.hshn.mi.pdbg.basicservice.jdbc.AbstractPersistentJDBCObject;
- import de.hshn.mi.pdbg.exception.StoreException;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import static de.hshn.mi.pdbg.PersistentObject.INVALID_OBJECT_ID;
- /**
- * Created by jonas on 05.04.2017.
- */
- public class BasicDBServiceImpl implements BasicDBService {
- private String url = "jdbc:postgresql://localhost:5432/db2";
- private String psqlUser = "postgres";
- private String psqlPw = "postgres";
- private Connection connection;
- private PreparedStatement preparedStatement;
- private PreparedStatement selectPatientById;
- public BasicDBServiceImpl() {
- }
- protected Connection getConnection() throws ClassNotFoundException, SQLException {
- if (connection == null) {
- connection = DriverManager.getConnection(url, psqlUser, psqlPw);
- }
- selectPatientById = connection.prepareStatement("Select * FROM patient WHERE id = ?");
- return connection;
- }
- public void prepareStatement (String[] SQL_STATEMENT){
- try {
- preparedStatement = connection.prepareStatement(SQL_STATEMENT[0]);
- } catch (SQLException e) {
- new StoreException("prepare statement failed" , e);
- }
- }
- public void executeStatement () {
- try {
- preparedStatement.execute();
- } catch (SQLException e) {
- new StoreException("execute failed", e);
- }
- }
- @Override
- public Patient createPatient(String lastname, String firstname) {
- assert (lastname != null && !lastname.equals(""));
- assert (firstname != null && !firstname.equals(""));
- PatientImpl patient = new PatientImpl(this, INVALID_OBJECT_ID, firstname, lastname);
- return patient;
- }
- @Override
- public Ward createWard(String name, int numberOfBeds) {
- assert (name != null && !name.equals(""));
- assert (numberOfBeds > 0);
- WardImpl ward = new WardImpl(this, INVALID_OBJECT_ID, numberOfBeds, name);
- return ward;
- }
- @Override
- public HospitalStay createHospitalStay(Patient p, Ward s, Date admissionDate) {
- assert (p != null && s != null && admissionDate != null);
- HospitalStayImpl hospitalStay = new HospitalStayImpl(this, INVALID_OBJECT_ID, admissionDate, s, p );
- return hospitalStay;
- }
- @Override
- public void removeHospitalStay(long hospitalStayID) {
- assert (hospitalStayID > 0 && hospitalStayID != INVALID_OBJECT_ID);
- }
- @Override
- public List<Patient> getPatients(String lastname, String firstname, Date startDate, Date endDate) {
- ArrayList<Patient> patients = new ArrayList<>();
- //SQL WILDCARDS!
- // for SQL Result
- Patient p = createPatient(lastnameFROMDB, firstnameFROMDB);
- p.setHealthInsurance(hiFROMDB);
- //....
- patients.add(p);
- return patients;
- }
- @Override
- public Patient getPatient(long patientID) {
- assert (patientID > 0 && patientID !=INVALID_OBJECT_ID);
- return null;
- }
- @Override
- public List<Ward> getWards() {
- return new ArrayList<>();
- }
- @Override
- public Ward getWard(long wardID) {
- WardImpl ward = null;
- assert (wardID > 0 && wardID != INVALID_OBJECT_ID);
- return null;
- }
- @Override
- public List<HospitalStay> getHospitalStays(long patientID) {
- assert (patientID > 0 && patientID !=INVALID_OBJECT_ID);
- List<HospitalStay> hospitalStays = new ArrayList<HospitalStay>();
- return hospitalStays;
- }
- @Override
- public List<HospitalStay> getHospitalStays(long patientID, Date startDate, Date endDate) {
- return null;
- }
- @Override
- public long store(PersistentObject object) {
- assert (object != null);
- if (!(object instanceof AbstractPersistentJDBCObject)) throw new StoreException();
- try {
- if (object instanceof HospitalStay) {
- HospitalStay h = (HospitalStay)object;
- if (!h.getPatient().isPersistent()) store(h.getPatient());
- //auch mit ward
- }
- ((AbstractPersistentJDBCObject) object).store(connection);
- return ((AbstractPersistentJDBCObject) object).getObjectID();
- } catch (SQLException e) {
- throw new StoreException(e);
- }
- }
- @Override
- public void close() {
- try {
- connection.close();
- selectPatientById.close();
- //PREPARED STATEMENTS SCHLIESSEN
- //RESULT SETS SCHLIESEN
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement