package de.uniluebeck.mi.projmi6.hapi; import ca.uhn.hl7v2.DefaultHapiContext; import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.HapiContext; import ca.uhn.hl7v2.app.Connection; import ca.uhn.hl7v2.app.HL7Service; import ca.uhn.hl7v2.app.Initiator; import ca.uhn.hl7v2.llp.LLPException; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.model.v251.message.ADT_A01; import ca.uhn.hl7v2.model.v251.segment.*; import ca.uhn.hl7v2.parser.Parser; import ca.uhn.hl7v2.parser.PipeParser; import ca.uhn.hl7v2.validation.ValidationContext; import ca.uhn.hl7v2.validation.impl.ValidationContextFactory; import de.uniluebeck.mi.projmi6.model.Diagnose; import de.uniluebeck.mi.projmi6.model.Fall; import de.uniluebeck.mi.projmi6.model.Patient; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.sql.SQLException; import java.time.LocalDateTime; import java.util.List; import static de.uniluebeck.mi.projmi6.db.DBHandler.getDiagnosenByFall; /** * Created by taschi on 22.11.15. */ public class HL7Sender { /** * erstellt eine ADT_A0 nachricht, welche anschließend an die OPS Gruppe verschickt werden soll. * TODO Mit OPS Gruppe absprechen, welche Informationen sie auf jeden Fall benötigen und Code entsprechend anpassen. * @param fall nach dem Erstellen eines neuen Falls wird diese Methode aufgerufen um Hl7 Nachricht zu erzeugen * @throws Exception */ public void createMessageADTA01( Fall fall) throws HL7Exception, IOException, SQLException { Patient patient = fall.getPatient (); ADT_A01 adt = new ADT_A01 (); //default MSH Values (Sets Segments: 1,2,7,9,11 adt.initQuickstart ("ADT", "A01", "P"); //MSH Segment: MSH mshSegment = adt.getMSH(); mshSegment.getMsh3_SendingApplication ().getNamespaceID ().parse ( "KISGruppe6" ); mshSegment.getMsh5_ReceivingApplication ().getNamespaceID ().parse("OPS Gruppe von Maurice und Torben"); mshSegment.getMsh12_VersionID ().getVersionID ().parse ( "2.51" ); mshSegment.getMsh15_AcceptAcknowledgmentType ().parse ( "AL" ); //TODO check ob segment 10 gesetzt wurde //EVN Segment: EVN evnSegment = adt.getEVN (); evnSegment.getEvn1_EventTypeCode ().parse("A01"); evnSegment.getEvn2_RecordedDateTime ().parse ( String.valueOf ( LocalDateTime.now () ) ); evnSegment.getEvn4_EventReasonCode ().parse ( "01" ); //PID PID pidSegment = adt.getPID (); pidSegment.getPid2_PatientID ().getIDNumber ().parse ( String.valueOf ( patient.getPatID () ) ); pidSegment.getPid3_PatientIdentifierList (0).getIDNumber ().parse ( Integer.toString(patient.getPatID ()) ); pidSegment.getPid5_PatientName (0).getFamilyName ().getSurname ().parse ( patient.getNachname () ); pidSegment.getPid5_PatientName (0).getGivenName ().parse ( patient.getVorname () ); pidSegment.getPid7_DateTimeOfBirth ().getTime ().parse( patient.getGeburtsdatum ().toString () ); pidSegment.getPid8_AdministrativeSex ().parse ( patient.getGeschlecht ().toString () ); pidSegment.getPid11_PatientAddress (0).getStreetAddress ().getStreetName().parse ( patient.getStrasse () ); pidSegment.getPid11_PatientAddress (0).getStreetAddress ().getDwellingNumber ( ).parse( patient.getHausnummer () ); pidSegment.getPid11_PatientAddress (0).getCity ().parse (patient.getOrt () ); pidSegment.getPid13_PhoneNumberHome (0).getTelephoneNumber ().parse ( patient.getTelefon () ); pidSegment.getPid16_MaritalStatus ().getAlternateIdentifier ().parse ( patient.getFamilienstand ().toString()); //Diagnosen List diagnosen = getDiagnosenByFall ( fall ); diagnosen.add(fall.getHauptDiagnose ()); PV1 pv1Segment = adt.getPV1 (); pv1Segment.getPv12_PatientClass ().parse ( "U" ); if(!diagnosen.isEmpty ()) { int i =1; for (Diagnose diagnose : diagnosen) { DG1 dg1Segment = adt.getDG1 (); dg1Segment.getDg11_SetIDDG1 ().parse ( String.valueOf ( i ) ); dg1Segment.getDg13_DiagnosisCodeDG1 ().getIdentifier ().parse ( diagnose.getIcd10code ().getCode () ); dg1Segment.getDg116_DiagnosingClinician ()[0].parse ( diagnose.getArzt ().getEinweisenderArzt () ); dg1Segment.getDg14_DiagnosisDescription ().parse ( diagnose.getFreiText () ); dg1Segment.getDg16_DiagnosisType ().parse ( diagnose.getDiagArt ().toString () ); adt.getDG1All ().add ( dg1Segment ); i++; //Segment 2 Verschluesselung ? } } printXMLEncodedMessageADT(adt); } public void sendMessage(Message message){ /* * The following section of code establishes a server listening * on port 1011 for new connections, and then "handles" them by */ int port = 1011; // The port to listen on boolean useTls = false; // Don't use TLS/SSL HapiContext context = new DefaultHapiContext(); HL7Service server = context.newServer(port, useTls); /* * Create a client, which will connect to our waiting * server and send messages to it. */ // A connection object represents a socket attached to an HL7 server Connection connection = null; try { connection = context.newClient("localhost", port, useTls); } catch (HL7Exception e) { e.printStackTrace(); } // Create a message to send String msg = "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01|12345|P|2.2\r" + "PID|0001|00009874|00001122|A00977|SMITH^JOHN^M|MOM|19581119|F|NOTREAL^LINDA^M|C|564 SPRING ST^^NEEDHAM^MA^02494^US|0002|(818)565-1551|(425)828-3344|E|S|C|0000444444|252-00-4414||||SA|||SA||||NONE|V1|0001|I|D.ER^50A^M110^01|ER|P00055|11B^M011^02|070615^BATMAN^GEORGE^L|555888^NOTREAL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^NOTREAL^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|199904101200||||5555112333|||666097^NOTREAL^MANNY^P\r" + "NK1|0222555|NOTREAL^JAMES^R|FA|STREET^OTHER STREET^CITY^ST^55566|(222)111-3333|(888)999-0000|||||||ORGANIZATION\r" + "PV1|0001|I|D.ER^1F^M950^01|ER|P000998|11B^M011^02|070615^BATMAN^GEORGE^L|555888^OKNEL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^VOICE^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|||||5555112333|||666097^DNOTREAL^MANNY^P\r" + "PV2|||0112^TESTING|55555^PATIENT IS NORMAL|NONE|||19990225|19990226|1|1|TESTING|555888^NOTREAL^BOB^K^DR^MD||||||||||PROD^003^099|02|ER||NONE|19990225|19990223|19990316|NONE\r" + "AL1||SEV|001^POLLEN\r" + "GT1||0222PL|NOTREAL^BOB^B||STREET^OTHER STREET^CITY^ST^77787|(444)999-3333|(222)777-5555||||MO|111-33-5555||||NOTREAL GILL N|STREET^OTHER STREET^CITY^ST^99999|(111)222-3333\r" + "IN1||022254P|4558PD|BLUE CROSS|STREET^OTHER STREET^CITY^ST^00990||(333)333-6666||221K|LENIX|||19980515|19990515|||PATIENT01 TEST D||||||||||||||||||02LL|022LP554"; Parser p = context.getPipeParser(); Message adt = null; try { adt = p.parse(msg); } catch (HL7Exception e) { e.printStackTrace(); } // The initiator is used to transmit unsolicited messages Initiator initiator = connection.getInitiator(); Message response = null; try { response = initiator.sendAndReceive(adt); } catch (HL7Exception e) { e.printStackTrace(); } catch (LLPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String responseString = null; try { responseString = p.encode(response); } catch (HL7Exception e) { e.printStackTrace(); } System.out.println("Received response:\n" + responseString); /* * MSH|^~\&|||||20070218200627.515-0500||ACK|54|P|2.2 MSA|AA|12345 */ /* * If you want to send another message to the same destination, it's fine * to ask the context again for a client to attach to the same host/port. * The context will be smart about it and return the same (already * connected) client Connection instance, assuming it hasn't been closed. */ try { connection = context.newClient("localhost", port, useTls); } catch (HL7Exception e) { e.printStackTrace(); } initiator = connection.getInitiator(); try { response = initiator.sendAndReceive(adt); } catch (HL7Exception e) { e.printStackTrace(); } catch (LLPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /* * Close the connection when you are done with it. */ connection.close(); // Stop the receiving server and client server.stopAndWait(); } /** * gibt erzeugte hl7 Nachricht auf der Konsole aus (Kann nach Debug phase wieder geloescht werden) * @param msg * @throws HL7Exception */ public void printXMLEncodedMessageADT(Message msg) throws HL7Exception, IOException { HapiContext context = new DefaultHapiContext (); Parser parser = context.getXMLParser(); String encodedMessage = parser.encode(msg); LocalDateTime ldt = LocalDateTime.now(); File file = new File("ADTA01Messafge" +LocalDateTime.now().toString() +"xml" ); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(encodedMessage); bw.close(); System.out.println (encodedMessage); } /** * * @param msg */ public void validateMessage(Message msg) { HapiContext context = new DefaultHapiContext(); context.setValidationContext( ValidationContextFactory.defaultValidation()); PipeParser parser = context.getPipeParser(); try { parser.encode(msg); } catch (HL7Exception e) { System.out.println("invaid message!" +e); } } }