Im Rahmen der Veranstaltung "CS3330 - Projektpraktikum MedizinischeInformatik" an der Universität zu Lübeck entstandenes Krankenhausinformationssystem (KIS).
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1006 行
40 KiB

  1. package de.uniluebeck.mi.projmi6.db;
  2. import de.uniluebeck.mi.projmi6.model.*;
  3. import java.sql.*;
  4. import java.time.LocalDateTime;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * Handler fuer alle DB Interaktionen.
  9. */
  10. public class DBHandler {
  11. private static final String SELECT_ALL_PATIENTS = "SELECT * FROM `patient`";
  12. private static final String SELECT_PATIENT_BY_ID = "SELECT * FROM `patient` WHERE `id` = ?";
  13. private static final String UPDATE_PATIENT = "UPDATE `patient`" +
  14. "SET `CAVE`=?," +
  15. "`Vorname`=?," +
  16. "`Geburtsname`=?," +
  17. "`Nachname`=?," +
  18. "`Geburtsdatum`=?," +
  19. "`Geschlecht`=?," +
  20. "`Familienstand`=?," +
  21. "`Strasse`=?," +
  22. "`Hausnummer`=?," +
  23. "`PLZ`=?," +
  24. "`Ort`=?," +
  25. "`Telefon`=?," +
  26. "`KassenID`=?," +
  27. "`Versichertennummer`=?," +
  28. "`LetzterBearbeiter`=? " +
  29. "WHERE `ID`=?";
  30. private static final String INSERT_PATIENT = "INSERT INTO `patient`" +
  31. "(`CAVE`," +
  32. "`Vorname`," +
  33. "`Geburtsname`," +
  34. "`Nachname`," +
  35. "`Geburtsdatum`," +
  36. "`Geschlecht`," +
  37. "`Familienstand`," +
  38. "`Strasse`," +
  39. "`Hausnummer`," +
  40. "`PLZ`," +
  41. "`Ort`," +
  42. "`Telefon`," +
  43. "`KassenID`," +
  44. "`Versichertennummer`," +
  45. "`LetzterBearbeiter`," +
  46. "`Ersteller`)" +
  47. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  48. private static final String SELECT_ALL_STATIONEN = "SELECT *,`r`.`abteilung` AS abteilung " +
  49. "FROM `stammstation` s " +
  50. "INNER JOIN `relfachrichtungstation` r ON s.station = r.station";
  51. private static final String SELECT_FAELLE_BY_PATID = "SELECT * FROM `fall` WHERE `patientid` = ?";
  52. private static final String INSERT_FALL = "INSERT INTO `fall`" +
  53. "(`Aufnahmedatum`," +
  54. "`Entlassungsdatum`," +
  55. "`Vorstelldatum`," +
  56. "`EinweisenderArzt`," +
  57. "`Fallart`," +
  58. "`Selbsteinweisung`," +
  59. "`Hauptdiagnose`," +
  60. "`Versichertennummer`," +
  61. "`KassenID`," +
  62. "`storniert`," +
  63. "`LetzterBearbeiter`," +
  64. "`PatientID`," +
  65. "`Ersteller`)" +
  66. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  67. private static final String UPDATE_FALL = "UPDATE `fall`" +
  68. "SET `Aufnahmedatum`=?," +
  69. "`Entlassungsdatum`=?," +
  70. "`Vorstelldatum`=?," +
  71. "`EinweisenderArzt`=?," +
  72. "Fallart=?," +
  73. "`Selbsteinweisung`=?," +
  74. "Hauptdiagnose=?," +
  75. "`Versichertennummer`=?," +
  76. "KassenID=?," +
  77. "`storniert`=?," +
  78. "LetzterBearbeiter=? " +
  79. "WHERE `FallID`=?";
  80. private static final String SELECT_DIAGNOSE_BY_ID = "SELECT * FROM `diagnose` WHERE `diagid` = ?";
  81. private static final String SELECT_ALL_ICD10CODES = "SELECT * FROM `stammicd10`";
  82. private static final String SELECT_ALL_OPSCODES = "SELECT * FROM `stammops`";
  83. private static final String SELECT_ICD10CODE_BY_ID = "SELECT * FROM `stammicd10` WHERE `icd10code` = ? AND `version` = ?";
  84. private static final String SELECT_OPSCODE_BY_ID = "SELECT * FROM `stammops` WHERE `opscode` = ? AND `version` = ?";
  85. private static final String SELECT_ALL_MITARBEITER = "SELECT * FROM `mitarbeiter`";
  86. private static final String SELECT_UNTERS_BY_FALLID = "SELECT * FROM `untersuchung` WHERE `fallid` = ?";
  87. private static final String INSERT_UNTERSUCHUNG = "INSERT INTO `untersuchung`" +
  88. "(`DurchfuehrenderArzt`," +
  89. "`FallID`," +
  90. "`OPSCode`," +
  91. "`OPSVersion`," +
  92. "`storniert`," +
  93. "`Untersuchungsdatum`," +
  94. "`LetzterBearbeiter`," +
  95. "`Ersteller`)" +
  96. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
  97. private static final String UPDATE_UNTERSUCHUNG = "UPDATE `untersuchung` " +
  98. "SET `DurchfuehrenderArzt`=?," +
  99. "`FallID`=?," +
  100. "`OPSCode`=?," +
  101. "`OPSVersion`=?," +
  102. "`storniert`=?," +
  103. "`Untersuchungsdatum`=?," +
  104. "`LetzterBearbeiter`=? " +
  105. "WHERE `UntersID`=?";
  106. private static final String SELECT_MITARBEITER_BY_ID = "SELECT * FROM `mitarbeiter` WHERE `mitarbid` = ?";
  107. private static final String SELECT_DIAGNOSE_BY_FALLID = "SELECT * FROM `diagnose` WHERE `fallid` = ?";
  108. private static final String SELECT_ALL_KASSEN = "SELECT * FROM `kasse`";
  109. private static final String SELECT_KASSE_BY_KASSENID = "SELECT * FROM `kasse` WHERE `kasse`.`KassenID` = ?";
  110. private static final String SELECT_STATHIST_BY_FALLID = "SELECT * FROM `stationshistorie` WHERE `stationshistorie`.`fallid` = ?";
  111. private static final String SELECT_STATUBERITEMS_BY_STATION = "SELECT p.id AS patid," +
  112. "concat(p.nachname, ', ', p.vorname) AS patname," +
  113. "p.geburtsdatum AS dob," +
  114. "timestampdiff(YEAR, p.geburtsdatum, curdate()) AS patage," +
  115. "f.aufnahmedatum AS aufnahme," +
  116. "f.entlassungsdatum AS entlassung," +
  117. "f.fallid AS fallid " +
  118. "FROM stationshistorie s " +
  119. "INNER JOIN fall f ON s.fallid = f.fallid " +
  120. "INNER JOIN patient p ON f.patientid = p.id " +
  121. "WHERE s.station = ?";
  122. private static final String INSERT_STATHISTENTRY = "INSERT INTO `stationshistorie`" +
  123. "(`Aufnahmedatum`," +
  124. "`Entlassungsdatum`," +
  125. "`FallID`," +
  126. "`LetzterBearbeiter`," +
  127. "`Station`," +
  128. "`Ersteller`)" +
  129. "VALUES (?, ?, ?, ?, ?, ?)";
  130. private static final String UPDATE_STATHISTENTRY = "UPDATE `stationshistorie` " +
  131. "SET `Aufnahmedatum`=?," +
  132. "`Entlassungsdatum`=?," +
  133. "`FallID`=?," +
  134. "`LetzterBearbeiter`=?," +
  135. "`Station`=? " +
  136. "WHERE `StatHistID`=?";
  137. private static final String SELECT_LAST_HL7ENTRIES = "SELECT * FROM `hl7_nachrichten` limit ?";
  138. private static final String INSERT_HL7NACHRICHT = "INSERT INTO `hl7_nachrichten` " +
  139. "(`hl7msg`," +
  140. "`timestamp`," +
  141. "`source`) " +
  142. "VALUES (?, ?, ?)";
  143. private static final String INSERT_DIAGNOSE = "INSERT INTO `diagnose` " +
  144. "(`Arzt`," +
  145. "`DiagArt`," +
  146. "`Ersteller`," +
  147. "`Freitext`," +
  148. "`ICD10Code`," +
  149. "`ICD10Version`," +
  150. "`LetzterBearbeiter`," +
  151. "`FallID`) " +
  152. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
  153. private static final String DELETE_STATHIST = "DELETE FROM `stationshistorie` WHERE `StatHistID` =?";
  154. private static final String SELECT_FALLIDS_BY_PATID = "SELECT `fallid` FROM `fall` WHERE `patientid` =?";
  155. private static final String INSERT_HL7NACHRICHT_ = "INSERT INTO `hl7_nachrichten` " +
  156. "(`hl7msg`," +
  157. "`timestamp`," +
  158. "`source`," +
  159. "`direction`) " +
  160. "VALUES (?, ?, ?, ?)";
  161. private DBHandler() {
  162. }
  163. /**
  164. * Gibt alle {@link Patient} aus der DB zurueck.
  165. *
  166. * @return Liste aller {@link Patient}.
  167. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  168. */
  169. public static List<Patient> getAllPatients() throws SQLException {
  170. Statement statement = MySqlConnectionFactory.getConnection().createStatement();
  171. ResultSet rs = statement.executeQuery(SELECT_ALL_PATIENTS);
  172. List<Patient> patients = new ArrayList<>();
  173. while (rs.next()) {
  174. patients.add(getPatient(rs));
  175. }
  176. return patients;
  177. }
  178. /**
  179. * Extrahiert ein Objekt {@link Patient} aus einem gegebenen {@link ResultSet}.
  180. *
  181. * @param rs ResultSet from Database Query.
  182. * @return {@link Patient}
  183. * @throws SQLException
  184. */
  185. private static Patient getPatient(ResultSet rs) throws SQLException {
  186. Patient patient = new Patient();
  187. patient.setPatID(rs.getInt("id"));
  188. patient.setGeburtsname(rs.getString("geburtsname"));
  189. patient.setVorname(rs.getString("vorname"));
  190. patient.setNachname(rs.getString("nachname"));
  191. if (rs.getDate("geburtsdatum") != null) {
  192. patient.setGeburtsdatum(rs.getDate("geburtsdatum").toLocalDate());
  193. }
  194. patient.setCave(rs.getString("cave"));
  195. patient.setStrasse(rs.getString("strasse"));
  196. patient.setHausnummer(rs.getString("hausnummer"));
  197. patient.setPlz(rs.getString("plz"));
  198. patient.setOrt(rs.getString("ort"));
  199. patient.setTelefon(rs.getString("telefon"));
  200. if (rs.getString("familienstand") != null) {
  201. patient.setFamilienstand(Patient.Familienstand.parseChar(rs.getString("familienstand").charAt(0)));
  202. }
  203. if (rs.getString("geschlecht") != null) {
  204. patient.setGeschlecht(Patient.Geschlecht.parseChar(rs.getString("geschlecht").charAt(0)));
  205. }
  206. patient.setVersichertennummer(rs.getString("versichertennummer"));
  207. patient.setKassenID(rs.getInt("kassenid"));
  208. setVersionInformation(patient, rs);
  209. return patient;
  210. }
  211. // TODO: Never used.
  212. public static Patient getPatient(int id) throws SQLException {
  213. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_PATIENT_BY_ID);
  214. ResultSet rs;
  215. statement.setInt(1, id);
  216. rs = statement.executeQuery();
  217. return getPatient(rs);
  218. }
  219. /**
  220. * Fuehrt {@code INSERT} bei einem neuen Datensatz und {@code UPDATE} bei einem existierenden Datensatz aus.
  221. *
  222. * @param patient zu verarbeitender Datensatz.
  223. * @param mitarbid {@link Mitarbeiter#mitarbID} des aktuellen Benutzers/Mitarbeiters.
  224. * @param isUpdate {@code true} wenn der Datensatz bereits existiert, sonst {@code false}.
  225. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  226. */
  227. public static void setPatient(Patient patient, int mitarbid, boolean isUpdate) throws SQLException {
  228. PreparedStatement statement;
  229. if (isUpdate) {
  230. statement = MySqlConnectionFactory.getConnection().prepareStatement(UPDATE_PATIENT);
  231. } else {
  232. statement = MySqlConnectionFactory.getConnection().prepareStatement(INSERT_PATIENT);
  233. }
  234. statement.setString(1, patient.getCave()); // CAVE
  235. statement.setString(2, patient.getVorname()); // Vorname
  236. statement.setString(3, patient.getGeburtsname()); // Geburtsname
  237. statement.setString(4, patient.getNachname()); // Nachname
  238. statement.setDate(5, Date.valueOf(patient.getGeburtsdatum())); // Geburtsdatum
  239. if (patient.getGeschlecht() != null) {
  240. statement.setString(6, String.valueOf(patient.getGeschlecht().id())); // Geschlecht
  241. } else {
  242. statement.setString(6, String.valueOf(Patient.Geschlecht.OTHER.id()));
  243. }
  244. if (patient.getFamilienstand() != null) {
  245. statement.setString(7, String.valueOf(patient.getFamilienstand().id()));// Familienstand
  246. } else {
  247. statement.setString(7, String.valueOf(Patient.Familienstand.LEDIG.id()));
  248. }
  249. statement.setString(8, patient.getStrasse()); // Strasse
  250. statement.setString(9, patient.getHausnummer()); // Hausnummer
  251. statement.setString(10, patient.getPlz()); // PLZ
  252. statement.setString(11, patient.getOrt()); // Ort
  253. statement.setString(12, patient.getTelefon()); // Telefon
  254. statement.setInt(13, patient.getKassenID()); // KassenID
  255. statement.setString(14, patient.getVersichertennummer()); // Versichertennummer
  256. statement.setInt(15, mitarbid); // LetzterBearbeiter
  257. if (!isUpdate) {
  258. statement.setInt(16, mitarbid); // Ersteller
  259. } else {
  260. statement.setInt(16, patient.getPatID()); // PatID (WHERE)
  261. }
  262. statement.execute();
  263. }
  264. /**
  265. * Gibt Liste aller Stationen aus der DB zurueck.
  266. *
  267. * @return Liste der Stationen
  268. * @throws SQLException
  269. */
  270. public static List<Station> getAllStationen() throws SQLException {
  271. Statement statement = MySqlConnectionFactory.getConnection().createStatement();
  272. ResultSet rs = statement.executeQuery(SELECT_ALL_STATIONEN);
  273. List<Station> stationen = new ArrayList<>();
  274. while (rs.next()) {
  275. stationen.add(getStation(rs));
  276. }
  277. return stationen;
  278. }
  279. /**
  280. * Extrahiert ein Objekt {@link Station} aus einem gegebenen {@link ResultSet}.
  281. *
  282. * @param rs ResultSet from Database Query.
  283. * @return {@link Station}
  284. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  285. */
  286. private static Station getStation(ResultSet rs) throws SQLException {
  287. Station station = new Station();
  288. station.setStation(rs.getString("station"));
  289. station.setBezeichnung(rs.getString("bezeichnung"));
  290. station.setBezeichnungLang(rs.getString("bezeichnunglang"));
  291. station.setStationstyp(rs.getInt("stationstyp"));
  292. station.setAbteilung(rs.getString("abteilung"));
  293. return station;
  294. }
  295. /**
  296. * Gibt besondere Eintraege fuer die Stationsubersicht nach Station zurueck.
  297. *
  298. * @param station Station
  299. * @return Liste der Eintraege auf der Station.
  300. * @throws SQLException
  301. */
  302. public static List<StationsUebersichtsItem> getStationsUebersichtsItems(String station) throws SQLException {
  303. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_STATUBERITEMS_BY_STATION);
  304. statement.setString(1, station);
  305. ResultSet rs = statement.executeQuery();
  306. List<StationsUebersichtsItem> statUeberItems = new ArrayList<>();
  307. while (rs.next()) {
  308. statUeberItems.add(getStatUeberItem(rs));
  309. }
  310. return statUeberItems;
  311. }
  312. private static StationsUebersichtsItem getStatUeberItem(ResultSet rs) throws SQLException {
  313. StationsUebersichtsItem item = new StationsUebersichtsItem();
  314. item.setFallId(rs.getInt("fallid"));
  315. item.setPatId(rs.getInt("patid"));
  316. item.setPatName(rs.getString("patname"));
  317. item.setPatAge(rs.getInt("patage"));
  318. item.setPatBirthdate(rs.getDate("dob").toLocalDate());
  319. if (rs.getDate("aufnahme") != null) {
  320. item.setStationAufnahme(rs.getDate("aufnahme").toLocalDate());
  321. }
  322. if (rs.getDate("entlassung") != null) {
  323. item.setStationEntlassung(rs.getDate("entlassung").toLocalDate());
  324. }
  325. return item;
  326. }
  327. /**
  328. * Gibt die Stationshistorie zu einem Fall aus.
  329. *
  330. * @param fall Fall
  331. * @return Liste der StationsHistorie Eintraege.
  332. * @throws SQLException
  333. */
  334. public static List<StationsHistorie> getStationsHistorieByFall(Fall fall) throws SQLException {
  335. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_STATHIST_BY_FALLID);
  336. statement.setInt(1, fall.getFallID());
  337. ResultSet rs = statement.executeQuery();
  338. List<StationsHistorie> historie = new ArrayList<>();
  339. while (rs.next()) {
  340. historie.add(getStationsHistorie(rs));
  341. }
  342. return historie;
  343. }
  344. /**
  345. * Erstellt bzw. aktuallisiert einen Eintrag in die Stationshistorie.
  346. *
  347. * @param hist StationsHistorie Objekt
  348. * @param isUpdate true wenn Update.
  349. * @throws SQLException
  350. */
  351. public static void setStationsHistorie(StationsHistorie hist, boolean isUpdate) throws SQLException {
  352. PreparedStatement statement;
  353. if (isUpdate) {
  354. statement = MySqlConnectionFactory.getConnection().prepareStatement(UPDATE_STATHISTENTRY);
  355. } else {
  356. statement = MySqlConnectionFactory.getConnection().prepareStatement(INSERT_STATHISTENTRY);
  357. }
  358. if (hist.getAufnahmeDatum() != null) {
  359. statement.setTimestamp(1, Timestamp.valueOf(hist.getAufnahmeDatum())); // `Aufnahmedatum`
  360. } else {
  361. statement.setTimestamp(1, null);
  362. }
  363. if (hist.getEntlassungsDatum() != null) {
  364. statement.setTimestamp(2, Timestamp.valueOf(hist.getEntlassungsDatum())); // `Entlassungsdatum`
  365. } else {
  366. statement.setTimestamp(2, null);
  367. }
  368. statement.setInt(3, hist.getFallID()); // `FallID`
  369. statement.setInt(4, hist.getBearbeiter()); // `LetzterBearbeiter`
  370. statement.setString(5, hist.getStationKey()); // `Station`
  371. if (isUpdate) {
  372. statement.setInt(6, hist.getStatHistID()); // `StatHistID`
  373. statement.executeUpdate();
  374. } else {
  375. statement.setInt(6, hist.getErsteller()); // `Ersteller`
  376. statement.execute();
  377. }
  378. }
  379. /**
  380. * Entfernt einen StationsHistorie Eintrag aus der Datenbank.
  381. *
  382. * @param hist Zu entfernendes StationsHistorie Objekt
  383. * @throws SQLException
  384. */
  385. public static void delStationsHistorie(StationsHistorie hist) throws SQLException {
  386. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(DELETE_STATHIST);
  387. statement.setInt(1, hist.getStatHistID());
  388. statement.execute();
  389. }
  390. /**
  391. * Gibt einzelnes StationsHistorie Objekt aus einem ResultSet zurueck.
  392. *
  393. * @param rs DB Query Result
  394. * @return StationsHistorie
  395. * @throws SQLException
  396. */
  397. private static StationsHistorie getStationsHistorie(ResultSet rs) throws SQLException {
  398. StationsHistorie hist = new StationsHistorie();
  399. hist.setStatHistID(rs.getInt("stathistid"));
  400. if (rs.getTimestamp("aufnahmedatum") != null) {
  401. hist.setAufnahmeDatum(rs.getTimestamp("aufnahmedatum").toLocalDateTime());
  402. }
  403. if (rs.getTimestamp("entlassungsdatum") != null) {
  404. hist.setEntlassungsDatum(rs.getTimestamp("entlassungsdatum").toLocalDateTime());
  405. }
  406. hist.setStationKey(rs.getString("station"));
  407. return hist;
  408. }
  409. /**
  410. * Gibt alle Faelle eines Patienten zuruck.
  411. *
  412. * @param id Patienten ID
  413. * @return Liste aller Faelle.
  414. * @throws SQLException
  415. */
  416. public static List<Fall> getFaelleByPatID(int id) throws SQLException {
  417. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_FAELLE_BY_PATID);
  418. statement.setInt(1, id);
  419. ResultSet rs = statement.executeQuery();
  420. List<Fall> faelle = new ArrayList<>();
  421. while (rs.next()) {
  422. faelle.add(getFall(rs));
  423. }
  424. return faelle;
  425. }
  426. /**
  427. * Extrahiert ein Objekt {@link Fall} aus einem gegebenen {@link ResultSet}.
  428. *
  429. * @param rs ResultSet from Database Query.
  430. * @return {@link Fall}
  431. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  432. */
  433. private static Fall getFall(ResultSet rs) throws SQLException {
  434. Fall fall = new Fall();
  435. fall.setFallID(rs.getInt("fallid"));
  436. if (rs.getTimestamp("aufnahmedatum") != null) {
  437. fall.setAufnahmeDatum(rs.getTimestamp("aufnahmedatum").toLocalDateTime());
  438. }
  439. if (rs.getTimestamp("entlassungsdatum") != null) {
  440. fall.setEntlassungsDatum(rs.getTimestamp("entlassungsdatum").toLocalDateTime());
  441. }
  442. if (rs.getInt("hauptdiagnose") != 0) {
  443. fall.setHauptDiagnose(getDiagnose(rs.getInt("hauptdiagnose"), fall));
  444. fall.setHauptdiagnoseId(rs.getInt("hauptdiagnose"));
  445. }
  446. if (rs.getString("versichertennummer") != null) {
  447. fall.setVersichertenNummer(rs.getString("versichertennummer"));
  448. }
  449. if (rs.getInt("kassenid") != 0) {
  450. fall.setKasse(getKasse(rs.getInt("kassenid")));
  451. }
  452. fall.setFallArt(FallArt.parseString(rs.getString("fallart")));
  453. fall.setEinweisenderArzt(rs.getString("einweisenderarzt"));
  454. fall.setStorniert(rs.getBoolean("storniert"));
  455. return fall;
  456. }
  457. /**
  458. * Fuehrt {@code INSERT} bei einem neuen Datensatz und {@code UPDATE} bei einem existierenden Datensatz aus.
  459. *
  460. * @param fall zu verarbeitender Datensatz.
  461. * @param mitarbid {@link Mitarbeiter#mitarbID} des aktuellen Benutzers/Mitarbeiters.
  462. * @param isUpdate {@code true} wenn der Datensatz bereits existiert, sonst {@code false}.
  463. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  464. */
  465. public static int setFall(Fall fall, int mitarbid, boolean isUpdate) throws SQLException {
  466. PreparedStatement statement;
  467. if (isUpdate) {
  468. statement = MySqlConnectionFactory.getConnection().prepareStatement(UPDATE_FALL);
  469. } else {
  470. statement = MySqlConnectionFactory.getConnection().prepareStatement(INSERT_FALL, Statement.RETURN_GENERATED_KEYS);
  471. }
  472. if (fall.getAufnahmeDatum() != null) {
  473. statement.setTimestamp(1, Timestamp.valueOf(fall.getAufnahmeDatum())); // Aufnahmedatum
  474. } else {
  475. statement.setTimestamp(1, null);
  476. }
  477. if (fall.getEntlassungsDatum() != null) {
  478. statement.setTimestamp(2, Timestamp.valueOf(fall.getEntlassungsDatum())); // Entlassungsdatum
  479. } else {
  480. statement.setTimestamp(2, null);
  481. }
  482. if (fall.getVorstellDatum() != null) {
  483. statement.setTimestamp(3, Timestamp.valueOf(fall.getVorstellDatum())); // Vorstelldatum
  484. } else {
  485. statement.setTimestamp(3, null);
  486. }
  487. if (fall.getEinweisenderArzt() != null) {
  488. statement.setString(4, fall.getEinweisenderArzt()); // EinweisenderArzt
  489. } else {
  490. statement.setTimestamp(4, null);
  491. }
  492. if (fall.getFallArt() != null) {
  493. statement.setString(5, fall.getFallArt().id()); // Fallart
  494. } else {
  495. statement.setString(5, null);
  496. }
  497. statement.setBoolean(6, fall.getSelbsteinweisung()); // Selbsteinweisung
  498. if (fall.getHauptdiagnoseId() != 0) {
  499. statement.setInt(7, fall.getHauptdiagnoseId()); // Hauptdiagnose
  500. } else {
  501. statement.setTimestamp(7, null);
  502. }
  503. statement.setString(8, fall.getVersichertenNummer()); // Versichertennummer
  504. if (fall.getKasse() != null) {
  505. statement.setInt(9, fall.getKasse().getKassenID()); // KassenID
  506. } else {
  507. statement.setTimestamp(9, null);
  508. }
  509. statement.setBoolean(10, fall.getStorniert()); // storniert
  510. statement.setInt(11, mitarbid); // Letzter Bearbeiter
  511. if (!isUpdate) {
  512. statement.setInt(12, fall.getPatient().getPatID()); // PatientID
  513. statement.setInt(13, mitarbid); // Ersteller
  514. }
  515. if (isUpdate) {
  516. statement.setInt(12, fall.getFallID());
  517. }
  518. statement.execute();
  519. if (!isUpdate) {
  520. ResultSet genkeys = statement.getGeneratedKeys();
  521. while (genkeys.next()) {
  522. return genkeys.getInt(1);
  523. }
  524. return -1; // Sollte nie vorkommen.
  525. } else {
  526. return fall.getFallID();
  527. }
  528. }
  529. /**
  530. * Fuehrt {@code INSERT} eines neuen Datensatz durch.
  531. *
  532. * @param fall zu verarbeitender Datensatz.
  533. * @param mitarbid {@link Mitarbeiter#mitarbID} des aktuellen Benutzers/Mitarbeiters.
  534. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  535. */
  536. public static int setFall(Fall fall, int mitarbid) throws SQLException {
  537. return setFall(fall, mitarbid, false);
  538. }
  539. private static Diagnose getDiagnose(int diagid, Fall fall) throws SQLException {
  540. Diagnose diagnose = getDiagnose(diagid);
  541. diagnose.setFall(fall);
  542. return diagnose;
  543. }
  544. private static Diagnose getDiagnose(int id) throws SQLException {
  545. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_DIAGNOSE_BY_ID);
  546. statement.setInt(1, id);
  547. ResultSet rs = statement.executeQuery();
  548. rs.next(); // TODO
  549. return getDiagnose(rs);
  550. }
  551. /**
  552. * Extrahiert ein Objekt {@link Diagnose} aus einem gegebenen {@link ResultSet}.
  553. *
  554. * @param rs ResultSet from Database Query.
  555. * @return {@link Diagnose}
  556. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  557. */
  558. private static Diagnose getDiagnose(ResultSet rs) throws SQLException {
  559. Diagnose diagnose = new Diagnose();
  560. diagnose.setDiagID(rs.getInt("diagid"));
  561. diagnose.setFreiText(rs.getString("freitext"));
  562. diagnose.setArzt(new Mitarbeiter(rs.getInt("arzt")));
  563. diagnose.setDiagArt(DiagArt.parseInt(rs.getInt("diagart")));
  564. setVersionInformation(diagnose, rs);
  565. diagnose.setIcd10code(getIcd10Code(rs.getString("icd10code"), rs.getInt("icd10version")));
  566. return diagnose;
  567. }
  568. private static void setVersionInformation(Version version, ResultSet rs) throws SQLException {
  569. version.setErsteller(rs.getInt("ersteller"));
  570. if (rs.getTimestamp("erstelldatum") != null) {
  571. version.setErstellDatumZeit(rs.getTimestamp("erstelldatum").toLocalDateTime());
  572. }
  573. version.setBearbeiter(rs.getInt("letzterbearbeiter"));
  574. if (rs.getTimestamp("letztesbearbeitungsdatum") != null) {
  575. version.setBearbeitetDatumZeit(rs.getTimestamp("letztesbearbeitungsdatum").toLocalDateTime());
  576. }
  577. }
  578. /**
  579. * Gibt alle in der DB hinterlegten ICD10 Codes zurueck.
  580. *
  581. * @return Liste aller ICD10 Codes.
  582. * @throws SQLException
  583. */
  584. public static List<Icd10Code> getAllIcd10Codes() throws SQLException {
  585. Statement statement = MySqlConnectionFactory.getConnection().createStatement();
  586. ResultSet rs = statement.executeQuery(SELECT_ALL_ICD10CODES);
  587. List<Icd10Code> icd10codes = new ArrayList<>();
  588. while (rs.next()) {
  589. icd10codes.add(getIcd10Code(rs));
  590. }
  591. return icd10codes;
  592. }
  593. private static Icd10Code getIcd10Code(String code, int version) throws SQLException {
  594. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_ICD10CODE_BY_ID);
  595. ResultSet rs;
  596. statement.setString(1, code);
  597. statement.setInt(2, version);
  598. rs = statement.executeQuery();
  599. rs.next(); // TODO
  600. return getIcd10Code(rs);
  601. }
  602. /**
  603. * Extrahiert ein Objekt {@link Icd10Code} aus einem gegebenen {@link ResultSet}.
  604. *
  605. * @param rs ResultSet from Database Query.
  606. * @return {@link Icd10Code}
  607. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  608. */
  609. private static Icd10Code getIcd10Code(ResultSet rs) throws SQLException {
  610. String code = rs.getString("icd10code");
  611. String text = rs.getString("text");
  612. int version = rs.getInt("version");
  613. return new Icd10Code(code, text, version);
  614. }
  615. /**
  616. * Gibt alle in der Datanbank hinterlegten OPS Codes zurueck.
  617. *
  618. * @return Liste der OPS Codes.
  619. * @throws SQLException
  620. */
  621. public static List<OpsCode> getAllOpsCodes() throws SQLException {
  622. Statement statement = MySqlConnectionFactory.getConnection().createStatement();
  623. ResultSet rs = statement.executeQuery(SELECT_ALL_OPSCODES);
  624. List<OpsCode> opscodes = new ArrayList<>();
  625. while (rs.next()) {
  626. opscodes.add(getOpsCode(rs));
  627. }
  628. return opscodes;
  629. }
  630. private static OpsCode getOpsCode(String code, int version) throws SQLException {
  631. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_OPSCODE_BY_ID);
  632. ResultSet rs;
  633. statement.setString(1, code);
  634. statement.setInt(2, version);
  635. rs = statement.executeQuery();
  636. rs.next(); // TODO
  637. return getOpsCode(rs);
  638. }
  639. /**
  640. * Extrahiert ein Objekt {@link OpsCode} aus einem gegebenen {@link ResultSet}.
  641. *
  642. * @param rs ResultSet from Database Query.
  643. * @return {@link OpsCode}
  644. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  645. */
  646. private static OpsCode getOpsCode(ResultSet rs) throws SQLException {
  647. String code = rs.getString("opscode");
  648. String text = rs.getString("text");
  649. int version = rs.getInt("version");
  650. return new OpsCode(code, text, version);
  651. }
  652. /**
  653. * Gibt Liste der Mitarbeiter aus der DB zurueck.
  654. *
  655. * @return Liste der Mitarbeiter.
  656. * @throws SQLException
  657. */
  658. public static List<Mitarbeiter> getAllMitarbeiter() throws SQLException {
  659. Statement statement = MySqlConnectionFactory.getConnection().createStatement();
  660. ResultSet rs = statement.executeQuery(SELECT_ALL_MITARBEITER);
  661. List<Mitarbeiter> mitarbeiters = new ArrayList<>();
  662. while (rs.next()) {
  663. mitarbeiters.add(getMitarbeiter(rs));
  664. }
  665. return mitarbeiters;
  666. }
  667. private static Mitarbeiter getMitarbeiter(int id) throws SQLException {
  668. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_MITARBEITER_BY_ID);
  669. ResultSet rs;
  670. statement.setInt(1, id);
  671. rs = statement.executeQuery();
  672. rs.next(); // TODO
  673. return getMitarbeiter(rs);
  674. }
  675. /**
  676. * Extrahiert ein Objekt {@link Mitarbeiter} aus einem gegebenen {@link ResultSet}.
  677. *
  678. * @param rs ResultSet from Database Query.
  679. * @return {@link Mitarbeiter}
  680. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  681. */
  682. private static Mitarbeiter getMitarbeiter(ResultSet rs) throws SQLException {
  683. Mitarbeiter mitarbeiter = new Mitarbeiter(rs.getInt("mitarbid"));
  684. mitarbeiter.setTitel(rs.getString("titel"));
  685. mitarbeiter.setVorname(rs.getString("vorname"));
  686. mitarbeiter.setNachname(rs.getString("nachname"));
  687. mitarbeiter.setEinweisenderArzt(rs.getString("einweisenderarzt"));
  688. return mitarbeiter;
  689. }
  690. /**
  691. * Gibt gibt alle {@link Untersuchung} zu einem {@link Fall} aus.
  692. *
  693. * @param fall Parent {@link Fall}
  694. * @return Liste aller {@link Untersuchung} zu einem {@link Fall}.
  695. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  696. */
  697. public static List<Untersuchung> getUntersuchungByFall(Fall fall) throws SQLException {
  698. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_UNTERS_BY_FALLID);
  699. statement.setInt(1, fall.getFallID());
  700. ResultSet rs = statement.executeQuery();
  701. List<Untersuchung> untersuchungen = new ArrayList<>();
  702. while (rs.next()) {
  703. untersuchungen.add(getUntersuchung(rs, fall));
  704. }
  705. return untersuchungen;
  706. }
  707. private static Untersuchung getUntersuchung(ResultSet rs, Fall fall) throws SQLException {
  708. Untersuchung untersuchung = new Untersuchung();
  709. untersuchung.setUntersID(rs.getInt("untersid"));
  710. untersuchung.setFall(fall);
  711. untersuchung.setDurchfuehrenderArzt(getMitarbeiter(rs.getInt("durchfuehrenderarzt")));
  712. untersuchung.setUntersuchungsdatum(rs.getTimestamp("untersuchungsdatum").toLocalDateTime());
  713. untersuchung.setOpscode(getOpsCode(rs.getString("opscode"), rs.getInt("opsversion")));
  714. untersuchung.setStorniert(rs.getBoolean("storniert"));
  715. setVersionInformation(untersuchung, rs);
  716. return untersuchung;
  717. }
  718. /**
  719. * Fuehrt {@code INSERT} bei einem neuen Datensatz und {@code UPDATE} bei einem existierenden Datensatz aus.
  720. *
  721. * @param untersuchung zu verarbeitender Datensatz.
  722. * @param mitarbid {@link Mitarbeiter#mitarbID} des aktuellen Benutzers/Mitarbeiters.
  723. * @param isUpdate {@code true} wenn der Datensatz bereits existiert, sonst {@code false}.
  724. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  725. */
  726. public static void setUntersuchung(Untersuchung untersuchung, int mitarbid, boolean isUpdate) throws SQLException {
  727. PreparedStatement statement;
  728. if (isUpdate) {
  729. statement = MySqlConnectionFactory.getConnection().prepareStatement(UPDATE_UNTERSUCHUNG);
  730. } else {
  731. statement = MySqlConnectionFactory.getConnection().prepareStatement(INSERT_UNTERSUCHUNG);
  732. }
  733. statement.setInt(1, untersuchung.getDurchfuehrenderArzt().getMitarbID()); // DurchfuehrenderArzt
  734. statement.setInt(2, untersuchung.getFall().getFallID()); // FallID
  735. statement.setString(3, untersuchung.getOpscode().getOpsCode()); // OPSCode
  736. statement.setInt(4, untersuchung.getOpscode().getVersion()); // OPSVersion
  737. statement.setBoolean(5, untersuchung.getStorniert()); // storniert
  738. statement.setTimestamp(6, Timestamp.valueOf(untersuchung.getUntersuchungsdatum())); // Untersuchungsdatum
  739. statement.setInt(7, mitarbid); // Letzter Bearbeiter
  740. if (isUpdate) {
  741. statement.setInt(8, untersuchung.getUntersID()); // `UntersID`
  742. } else {
  743. statement.setInt(8, mitarbid); // Ersteller
  744. }
  745. statement.execute();
  746. }
  747. /**
  748. * Gibt alle Diagnosen zu einem Fall zurueck.
  749. *
  750. * @param fall Fall
  751. * @return Liste der Diagnosen.
  752. * @throws SQLException
  753. */
  754. public static List<Diagnose> getDiagnosenByFall(Fall fall) throws SQLException {
  755. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_DIAGNOSE_BY_FALLID);
  756. statement.setInt(1, fall.getFallID());
  757. ResultSet rs = statement.executeQuery();
  758. List<Diagnose> diagnosen = new ArrayList<>();
  759. Diagnose diagnose;
  760. while (rs.next()) {
  761. diagnose = getDiagnose(rs.getInt("diagid"), fall);
  762. if( diagnose != null) {
  763. diagnosen.add ( diagnose );
  764. }
  765. }
  766. return diagnosen;
  767. }
  768. /**
  769. * Gibt Liste der hinterlegten Kassen zurueck.
  770. *
  771. * @return Liste der hinterlegten Kassen
  772. * @throws SQLException
  773. */
  774. public static List<Kasse> getAllKassen() throws SQLException {
  775. Statement statement = MySqlConnectionFactory.getConnection().createStatement();
  776. ResultSet rs = statement.executeQuery(SELECT_ALL_KASSEN);
  777. List<Kasse> kassen = new ArrayList<>();
  778. while (rs.next()) {
  779. kassen.add(getKasse(rs));
  780. }
  781. return kassen;
  782. }
  783. private static Kasse getKasse(int kassenid) throws SQLException {
  784. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_KASSE_BY_KASSENID);
  785. statement.setInt(1, kassenid);
  786. ResultSet rs = statement.executeQuery();
  787. rs.next(); // TODO
  788. return getKasse(rs);
  789. }
  790. /**
  791. * Extrahiert ein Objekt {@link Kasse} aus einem gegebenen {@link ResultSet}.
  792. *
  793. * @param rs ResultSet from Database Query.
  794. * @return {@link Kasse}
  795. * @throws SQLException wenn Fehler bei der SQL Verarbeitung auftreten.
  796. */
  797. private static Kasse getKasse(ResultSet rs) throws SQLException {
  798. Kasse kasse = new Kasse();
  799. kasse.setKassenID(rs.getInt("kassenid"));
  800. kasse.setName(rs.getString("name"));
  801. kasse.setAdresse(rs.getString("adresse"));
  802. kasse.setPrivat(rs.getBoolean("privat"));
  803. setVersionInformation(kasse, rs);
  804. return kasse;
  805. }
  806. /**
  807. * Speichert Diagnose in die Datenbank.
  808. *
  809. * @param diagnose Diagnose
  810. * @throws SQLException
  811. */
  812. public static void setDiagnose(Diagnose diagnose) throws SQLException {
  813. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(INSERT_DIAGNOSE);
  814. statement.setInt(1, diagnose.getArzt().getMitarbID()); // `Arzt`
  815. statement.setInt(2, diagnose.getDiagArt().id()); // `DiagArt`
  816. statement.setInt(3, diagnose.getErsteller()); // `Ersteller`
  817. statement.setString(4, diagnose.getFreiText()); // `Freitext`
  818. statement.setString(5, diagnose.getIcd10code().getCode()); // `ICD10Code`
  819. statement.setInt(6, diagnose.getIcd10code().getVersion()); // `ICD10Version`
  820. statement.setInt(7, diagnose.getBearbeiter()); // `LetzterBearbeiter`
  821. statement.setInt(8, diagnose.getFall().getFallID()); // `FallID`
  822. statement.execute();
  823. }
  824. /**
  825. * Speichert eine raw HL7 Nachricht in die DB.
  826. *
  827. * @param hl7msg raw HL7 Nachricht
  828. * @param timestamp LocalDateTime
  829. * @param source Source bzw. Destination
  830. * @throws SQLException
  831. */
  832. public static void setHL7Nachricht(String hl7msg, LocalDateTime timestamp, String source) throws SQLException {
  833. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(INSERT_HL7NACHRICHT);
  834. statement.setString(1, hl7msg);
  835. statement.setTimestamp(2, Timestamp.valueOf(timestamp));
  836. statement.setString(3, source);
  837. statement.execute();
  838. }
  839. /**
  840. * Gibt die letzten 30 HL7 Logeintraege aus der DB zurueck.
  841. *
  842. * @return
  843. * @throws SQLException
  844. */
  845. public static List<HL7LogEntry> getLastHL7LogEntries() throws SQLException {
  846. return getLastHL7LogEntries(30);
  847. }
  848. /**
  849. * Gibt die angegebene Anzahl HL7 Logeintraege aus der DB zurueck.
  850. *
  851. * @param last Anzahl Eintraege
  852. * @return Liste mit HL7LogEntries zurueck.
  853. * @throws SQLException
  854. */
  855. public static List<HL7LogEntry> getLastHL7LogEntries(int last) throws SQLException {
  856. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_LAST_HL7ENTRIES);
  857. statement.setInt(1, last);
  858. ResultSet rs = statement.executeQuery();
  859. List<HL7LogEntry> hl7entries = new ArrayList<>();
  860. while (rs.next()) {
  861. hl7entries.add(getHL7LogEntry(rs));
  862. }
  863. return hl7entries;
  864. }
  865. /**
  866. * Speichert eine HL7 Nachricht in der hl7 Log Tabelle.
  867. *
  868. * @param entry HL7 Nachricht
  869. * @throws SQLException
  870. */
  871. public static void setHL7LogEntry(HL7LogEntry entry) throws SQLException {
  872. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(INSERT_HL7NACHRICHT_);
  873. statement.setString(1, entry.getMessage());
  874. statement.setTimestamp(2, Timestamp.valueOf(entry.getTimestamp()));
  875. statement.setString(3, entry.getSource());
  876. statement.setString(4, entry.getDirection().toString());
  877. statement.execute();
  878. }
  879. private static HL7LogEntry getHL7LogEntry(ResultSet rs) throws SQLException {
  880. HL7LogEntry entry = new HL7LogEntry(rs.getInt("msgid"));
  881. entry.setMessage(rs.getString("hl7msg"));
  882. entry.setTimestamp(rs.getTimestamp("timestamp").toLocalDateTime());
  883. entry.setSource(rs.getString("source"));
  884. entry.setDirection(HL7LogEntry.Direction.parseDirection(rs.getString("direction")));
  885. return entry;
  886. }
  887. /**
  888. * Gibt alle FallIDs eines Patienten zurueck.
  889. *
  890. * @param patid Patienten ID
  891. * @return Liste aller FallIDs
  892. * @throws SQLException
  893. */
  894. public static List<Integer> getAlleFallIdsByPatID(int patid) throws SQLException {
  895. PreparedStatement statement = MySqlConnectionFactory.getConnection().prepareStatement(SELECT_FALLIDS_BY_PATID);
  896. statement.setInt(1, patid);
  897. ResultSet rs = statement.executeQuery();
  898. List<Integer> fallids = new ArrayList<>();
  899. while (rs.next()) {
  900. fallids.add(rs.getInt("fallid"));
  901. }
  902. return fallids;
  903. }
  904. }