package de.uniluebeck.mi.projmi6.db; import de.uniluebeck.mi.projmi6.model.*; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; /** * Created by nils on 15.11.2015. */ public class DBHandler { private static final String SELECT_ALL_PATIENTS = "SELECT * FROM `patient`"; private static final String SELECT_PATIENT_BY_ID = "SELECT * FROM `patient` WHERE `id` = ?"; private static final String UPDATE_PATIENT = "UPDATE `patient` SET `CAVE`=?, `Familienstand`=?, " + "`Geburtsdatum`=?, `Geburtsname`=?, `Geschlecht`=?, `KassenID`=?, `LetzterBearbeiter`=?, `Nachname`=?, " + "`Ort`=?, `PLZ`=?, `Strasse`=?, `Telefon`=?, `Versichertennummer`=?, `Vorname`=? WHERE `ID`=?"; private static final String SELECT_ALL_STATIONEN = "SELECT * FROM `stammstation`"; private static final String SELECT_FAELLE_BY_PATID = "SELECT * FROM `fall` WHERE `patientid` = ?"; private static final String SELECT_DIAGNOSE_BY_ID = "SELECT * FROM `diagnose` WHERE `diagid` = ?"; private static final String SELECT_ALL_ICD10CODES = "SELECT * FROM `stammicd10`"; private static final String SELECT_ALL_OPSCODES = "SELECT * FROM `stammops`"; private static final String SELECT_ICD10CODE_BY_ID = "SELECT * FROM `stammicd10` WHERE `icd10code` = '?' AND `version` = ?"; private static final String SELECT_OPSCODE_BY_ID = "SELECT * FROM `stammops` WHERE `opscode` = '?' AND `version` = ?"; public static List getAllPatients() throws SQLException { Statement statement = MySqlConnFactory.getConnection().createStatement(); ResultSet rs = statement.executeQuery(SELECT_ALL_PATIENTS); List patients = new ArrayList<>(); while (rs.next()) { patients.add(getPatient(rs)); } return patients; } public static Patient getPatient(int id) throws SQLException { PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_PATIENT_BY_ID); ResultSet rs; statement.setInt(1, id); rs = statement.executeQuery(); return getPatient(rs); } private static Patient getPatient(ResultSet rs) throws SQLException { Patient patient = new Patient(); patient.setPatID(rs.getInt("id")); patient.setGeburtsname(rs.getString("geburtsname")); patient.setVorname(rs.getString("vorname")); patient.setNachname(rs.getString("nachname")); if (rs.getDate("geburtsdatum") != null) patient.setGeburtsdatum(rs.getDate("geburtsdatum").toLocalDate()); patient.setStrasse(rs.getString("strasse")); patient.setHausnummer(rs.getString("hausnummer")); patient.setPlz(rs.getString("plz")); patient.setTelefon(rs.getString("telefon")); // patient.setFamilienstand(rs.getString("familienstand")); TODO // patient.setGeschlecht(Patient.Geschlecht.valueOf(rs.getString("geschlecht"))); TODO patient.setVersichertennummer(rs.getString("versichertennummer")); patient.setKassenID(rs.getInt("kassenid")); setVersionInformation(patient, rs); return patient; } public static void updatePatient(Patient patient) throws SQLException { PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(UPDATE_PATIENT); statement.setString(1, patient.getCave()); statement.setString(2, patient.getFamilienstand().toString()); // TODO } public static List getAllStationen() throws SQLException { Statement statement = MySqlConnFactory.getConnection().createStatement(); ResultSet rs = statement.executeQuery(SELECT_ALL_STATIONEN); List stationen = new ArrayList<>(); while (rs.next()) { stationen.add(getStation(rs)); } return stationen; } private static Station getStation(ResultSet rs) throws SQLException { Station station = new Station(); station.setStation(rs.getString("station")); station.setBezeichnung(rs.getString("bezeichnung")); station.setBezeichnungLang(rs.getString("bezeichnunglang")); station.setStationstyp(rs.getInt("stationstyp")); return station; } public static List getFaelleByPatID(int id) throws SQLException { PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_FAELLE_BY_PATID); statement.setInt(1, id); ResultSet rs = statement.executeQuery(); List faelle = new ArrayList<>(); while (rs.next()) { faelle.add(getFall(rs)); } return faelle; } private static Fall getFall(ResultSet rs) throws SQLException { Fall fall = new Fall(); fall.setFallID(rs.getInt("fallid")); if (rs.getTimestamp("aufnahmedatum") != null) fall.setAufnahmeDatum(rs.getTimestamp("aufnahmedatum").toLocalDateTime()); if (rs.getTimestamp("entlassungsdatum") != null) fall.setEntlassungsDatum(rs.getTimestamp("entlassungsdatum").toLocalDateTime()); if (rs.getInt("hauptdiagnose") != 0) fall.setHauptDiagnose(getDiagnose(rs.getInt("hauptdiagnose"), fall)); return fall; } private static Diagnose getDiagnose(int hauptdiagnose, Fall fall) throws SQLException { Diagnose diagnose = getDiagnose(hauptdiagnose); diagnose.setFall(fall); return diagnose; } private static Diagnose getDiagnose(int id) throws SQLException { PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_DIAGNOSE_BY_ID); ResultSet rs = statement.executeQuery(); return getDiagnose(rs); } private static Diagnose getDiagnose(ResultSet rs) throws SQLException { Diagnose diagnose = new Diagnose(); diagnose.setFreiText(rs.getString("freitext")); diagnose.setArzt(new Mitarbeiter(rs.getInt("arzt"))); diagnose.setDiagArt(DiagArt.parseInt(rs.getInt("diagart"))); setVersionInformation(diagnose, rs); diagnose.setIcd10code(getIcd10Code(rs.getString("icd10code"), rs.getInt("icd10version"))); return diagnose; } private static void setVersionInformation(Version version, ResultSet rs) throws SQLException { version.setErsteller(rs.getInt("ersteller")); if (rs.getTimestamp("erstelldatum") != null) version.setErstellDatumZeit(rs.getTimestamp("erstelldatum").toLocalDateTime()); version.setBearbeiter(rs.getInt("letzterbearbeiter")); if (rs.getTimestamp("letztesbearbeitungsdatum") != null) version.setBearbeitetDatumZeit(rs.getTimestamp("letztesbearbeitungsdatum").toLocalDateTime()); } public static List getAllIcd10Codes() throws SQLException { Statement statement = MySqlConnFactory.getConnection().createStatement(); ResultSet rs = statement.executeQuery(SELECT_ALL_ICD10CODES); List icd10codes = new ArrayList<>(); while (rs.next()) { icd10codes.add(getIcd10Code(rs)); } return icd10codes; } private static Icd10Code getIcd10Code(String code, int version) throws SQLException { PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_ICD10CODE_BY_ID); ResultSet rs; statement.setString(1, code); statement.setInt(2, version); rs = statement.executeQuery(); return getIcd10Code(rs); } private static Icd10Code getIcd10Code(ResultSet rs) throws SQLException { String code = rs.getString("icd10code"); String text = rs.getString("text"); int version = rs.getInt("version"); return new Icd10Code(code, text, version); } public static List getAllOpsCodes() throws SQLException { Statement statement = MySqlConnFactory.getConnection().createStatement(); ResultSet rs = statement.executeQuery(SELECT_ALL_OPSCODES); List opscodes = new ArrayList<>(); while (rs.next()) { opscodes.add(getOpsCode(rs)); } return opscodes; } public static OpsCode getOpsCode(String code, int version) throws SQLException { PreparedStatement statement = MySqlConnFactory.getConnection().prepareStatement(SELECT_OPSCODE_BY_ID); ResultSet rs; statement.setString(1, code); statement.setInt(2, version); rs = statement.executeQuery(); return getOpsCode(rs); } private static OpsCode getOpsCode(ResultSet rs) throws SQLException { String code = rs.getString("opscode"); String text = rs.getString("text"); int version = rs.getInt("version"); return new OpsCode(code, text, version); } }