Advertisement
Guest User

BasicDBServiceImpl

a guest
Apr 28th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1. package de.hshn.mi.pdbg.basicservice;
  2.  
  3. import de.hshn.mi.pdbg.AbstractPersistentObject;
  4. import de.hshn.mi.pdbg.PersistentObject;
  5. import de.hshn.mi.pdbg.basicservice.Impl.HospitalStayImpl;
  6. import de.hshn.mi.pdbg.basicservice.Impl.PatientImpl;
  7. import de.hshn.mi.pdbg.basicservice.Impl.WardImpl;
  8. import de.hshn.mi.pdbg.basicservice.jdbc.AbstractPersistentJDBCObject;
  9. import de.hshn.mi.pdbg.exception.StoreException;
  10. import java.sql.*;
  11. import java.util.ArrayList;
  12. import java.util.Date;
  13. import java.util.List;
  14.  
  15. import static de.hshn.mi.pdbg.PersistentObject.INVALID_OBJECT_ID;
  16.  
  17. /**
  18.  * Created by jonas on 05.04.2017.
  19.  */
  20. public class BasicDBServiceImpl implements BasicDBService {
  21.  
  22.     private String url = "jdbc:postgresql://localhost:5432/db2";
  23.     private String psqlUser = "postgres";
  24.     private String psqlPw = "postgres";
  25.     private Connection connection;
  26.     private PreparedStatement preparedStatement;
  27.  
  28.     private PreparedStatement selectPatientById;
  29.  
  30.     public BasicDBServiceImpl() {
  31.     }
  32.  
  33.  
  34.     protected Connection getConnection() throws ClassNotFoundException, SQLException {
  35.         if (connection == null) {
  36.             connection = DriverManager.getConnection(url, psqlUser, psqlPw);
  37.         }
  38.  
  39.         selectPatientById = connection.prepareStatement("Select * FROM patient WHERE id = ?");
  40.         return connection;
  41.     }
  42.  
  43.  
  44.     public void prepareStatement (String[] SQL_STATEMENT){
  45.         try {
  46.             preparedStatement = connection.prepareStatement(SQL_STATEMENT[0]);
  47.         } catch (SQLException e) {
  48.             new StoreException("prepare statement failed" , e);
  49.         }
  50.     }
  51.  
  52.     public void executeStatement () {
  53.         try {
  54.             preparedStatement.execute();
  55.         } catch (SQLException e) {
  56.             new StoreException("execute failed", e);
  57.         }
  58.     }
  59.  
  60.     @Override
  61.     public Patient createPatient(String lastname, String firstname) {
  62.  
  63.         assert (lastname != null && !lastname.equals(""));
  64.         assert (firstname != null && !firstname.equals(""));
  65.  
  66.         PatientImpl patient = new PatientImpl(this, INVALID_OBJECT_ID, firstname, lastname);
  67.  
  68.  
  69.         return patient;
  70.     }
  71.  
  72.     @Override
  73.     public Ward createWard(String name, int numberOfBeds) {
  74.         assert (name != null && !name.equals(""));
  75.         assert (numberOfBeds > 0);
  76.  
  77.         WardImpl ward = new WardImpl(this, INVALID_OBJECT_ID, numberOfBeds, name);
  78.  
  79.         return ward;
  80.     }
  81.  
  82.     @Override
  83.     public HospitalStay createHospitalStay(Patient p, Ward s, Date admissionDate) {
  84.  
  85.         assert (p != null && s != null && admissionDate != null);
  86.  
  87.         HospitalStayImpl hospitalStay = new HospitalStayImpl(this, INVALID_OBJECT_ID, admissionDate, s, p );
  88.  
  89.  
  90.         return hospitalStay;
  91.     }
  92.  
  93.     @Override
  94.     public void removeHospitalStay(long hospitalStayID) {
  95.  
  96.  
  97.         assert (hospitalStayID > 0 && hospitalStayID != INVALID_OBJECT_ID);
  98.     }
  99.  
  100.     @Override
  101.     public List<Patient> getPatients(String lastname, String firstname, Date startDate, Date endDate) {
  102.  
  103.         ArrayList<Patient> patients = new ArrayList<>();
  104.  
  105.         //SQL WILDCARDS!
  106.  
  107.  
  108.       //  for SQL Result
  109.             Patient p = createPatient(lastnameFROMDB, firstnameFROMDB);
  110.             p.setHealthInsurance(hiFROMDB);
  111.             //....
  112.             patients.add(p);
  113.  
  114.  
  115.  
  116.         return patients;
  117.     }
  118.  
  119.     @Override
  120.     public Patient getPatient(long patientID) {
  121.         assert (patientID > 0 && patientID !=INVALID_OBJECT_ID);
  122.  
  123.         return null;
  124.     }
  125.  
  126.     @Override
  127.     public List<Ward> getWards() {
  128.         return new ArrayList<>();
  129.     }
  130.  
  131.     @Override
  132.     public Ward getWard(long wardID) {
  133.  
  134.         WardImpl ward = null;
  135.  
  136.         assert (wardID > 0 && wardID != INVALID_OBJECT_ID);
  137.         return null;
  138.     }
  139.  
  140.     @Override
  141.     public List<HospitalStay> getHospitalStays(long patientID) {
  142.         assert (patientID > 0 && patientID !=INVALID_OBJECT_ID);
  143.  
  144.         List<HospitalStay> hospitalStays = new ArrayList<HospitalStay>();
  145.         return hospitalStays;
  146.     }
  147.  
  148.     @Override
  149.     public List<HospitalStay> getHospitalStays(long patientID, Date startDate, Date endDate) {
  150.         return null;
  151.     }
  152.  
  153.     @Override
  154.     public long store(PersistentObject object) {
  155.         assert (object != null);
  156.         if (!(object instanceof AbstractPersistentJDBCObject)) throw new StoreException();
  157.  
  158.         try {
  159.             if (object instanceof HospitalStay) {
  160.                 HospitalStay h = (HospitalStay)object;
  161.  
  162.                 if (!h.getPatient().isPersistent()) store(h.getPatient());
  163.                 //auch mit ward
  164.             }
  165.  
  166.             ((AbstractPersistentJDBCObject) object).store(connection);
  167.  
  168.             return ((AbstractPersistentJDBCObject) object).getObjectID();
  169.  
  170.  
  171.  
  172.         } catch (SQLException e) {
  173.             throw new StoreException(e);
  174.         }
  175.     }
  176.  
  177.     @Override
  178.     public void close() {
  179.         try {
  180.             connection.close();
  181.             selectPatientById.close();
  182.             //PREPARED STATEMENTS SCHLIESSEN
  183.             //RESULT SETS SCHLIESEN
  184.         } catch (SQLException e) {
  185.             e.printStackTrace();
  186.         }
  187.     }
  188.  
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement