Imports System
Imports System.Net
Imports System.Web.Services.Protocols
Imports RadiologyResultSampleCode.RadiologyResultWebService


Public Class RadiologyResultSample
    ' USERNAME designates the Hub user name used to
    ' authenticate to the Hub web service
    Private Const USERNAME As String = "test"

    ' PASSWORD designates the password associated with
    ' the Hub user when authenticating to the web service
    Private Const PASSWORD As String = "customer1"

    ' WEB_SERVICE_URL designates the end point used to
    ' connect to the Hub's radiology results web service
    Private Const WEB_SERVICE_URL As String = "https://cert.hub.care360.com/radiology/result/service"

    ' SENDING_APPLICATION designates the application that is sending the radiology result
    ' message to Hub
    Private Const SENDING_APPLICATION As String = "HUBWS"

    ' SENDING_FACILITY designates the service provider name assigned to you by MedPlus.
    Private Const SENDING_FACILITY As String = "2135800"

    ' RECEIVING_FACILITY designates a provider account name that will be receiving the result.
    ' Provider accounts will be determined and provided by MedPlus as part of interface development.
    Private Const RECEIVING_FACILITY As String = "THO"


    ' The HL7 radiology result message template - The following "template" is used in
    ' constructing the radiology result.
    ' 
    ' 0 = sending application (field 3) 
    ' 1 = sending facility (field 4) represents the account with Quest Diagnostics 
    ' 2 = receiving facility (field 6) The Quest Business Unit for above account 
    ' 3 = date time (field 7) 
    ' 4 = message control id (field 10) Unique Number identifying this radiology result message

    Private Const RADIOLOGY_RESULT_MESSAGE As String = "MSH|^~\&|{0}|{1}||{2}|{3:yyyyMMddHHmm}||ORU^R01|{4}|P|2.3" + Chr(13) + _
        "PID|||123456789^^^F|123456789|TEST^WIFE||19560101|F|||||(206)783-4||*ENGLISH^E|S||9918210003|123456789" + Chr(13) + _
        "OBR||{4}|{4}|30070^MR CHEST W/O CONTRAST^RAD||20100514095201|20100514095201|20100514095201||||||||OTH030^DOCTOR^A^^^^^UPIN||||||20100514095201|||F|||||||999999&Transcriptionist&The&A|999999&Transcriptionist&The&A||999999&<None>" + Chr(13) + _
        "OBX|1|FT|RAD|| ||||||F|||||999999" + Chr(13) + _
        "OBX|2|FT|RAD||The findings: Frontal view of the chest compared to earlier studies of||||||F|||||999999" + Chr(13) + _
        "OBX|3|FT|RAD||12/11/2003 and 12/31/2003.||||||F|||||999999" + Chr(13) + _
        "OBX|4|FT|RAD|| ||||||F|||||999999" + Chr(13) + _
        "OBX|5|FT|RAD||Again, the study was obtained and a poor degree of inspiration. There||||||F|||||999999" + Chr(13) + _
        "OBX|6|FT|RAD||is a mild prominence of the interstitial markings particularly at the||||||F|||||999999" + Chr(13) + _
        "OBX|7|FT|RAD||bases and these could easily represent atelectasis. The cardiac||||||F|||||999999" + Chr(13) + _
        "OBX|8|FT|RAD||silhouette is borderline normal. Hilar and mediastinal silhouettes are||||||F|||||999999" + Chr(13) + _
        "OBX|9|FT|RAD||normal for this degree of inspiration. There is no effusion or||||||F|||||999999" + Chr(13) + _
        "OBX|10|FT|RAD||pneumothorax.||||||F|||||999999" + Chr(13) + _
        "OBX|11|FT|RAD|| ||||||F|||||999999" + Chr(13) + _
        "OBX|12|FT|RAD||Impression:||||||F|||||999999" + Chr(13) + _
        "OBX|13|FT|RAD|| ||||||F|||||999999" + Chr(13) + _
        "OBX|14|FT|RAD||Poor inspiration with compression of pulmonary markings. No definite||||||F|||||999999" + Chr(13) + _
        "OBX|15|FT|RAD||acute process is identified||||||F|||||999999" + Chr(13)

    Public Sub New()
        Dim webService As RadiologyResultService = Nothing

        Try
            '-------------------------------------------------------
            'STEP 1: CREATE WEB SERVICE REFS, SET PARAMS
            '-------------------------------------------------------
            webService = New RadiologyResultService
        Catch except As Exception
            Console.Write(except.StackTrace)
            webService = Nothing
        End Try

        If Not (webService Is Nothing) Then
            Dim response As RadiologyResultResponse = Nothing
            Dim result As RadiologyResult = Nothing

            Try
                '------------------------------------------------------------------
                'STEP 2: CREATE CREDENTIALS OBJECT CONTAINING USERNAME AND PASSWORD
                '------------------------------------------------------------------
                webService.Credentials = New NetworkCredential(USERNAME, PASSWORD)

                ' By default the client will attempt to call the web service without 
                ' sending the credentials. If the server returns a 401 HTTP response, 
                ' the client makes the call again with the credentials. If we set 
                ' PreAuthenticate to True, the client will include the credentials on 
                ' the initial call and eliminate this extra processing.
                '
                ' This should probably be set in the web service object's constructor,
                ' but has been included here to demonstrate.
                webService.PreAuthenticate = True

                If WEB_SERVICE_URL.Length > 0 Then
                    webService.Url = WEB_SERVICE_URL
                End If

                '-----------------------------------------------------------------------------
                'STEP 3: call function that will create the WebService Radiology Result Object
                '-----------------------------------------------------------------------------
                result = getWebServiceRadiologyResult()

                '--------------------------------------------------------------
                'STEP 4: call WebService function that submits order to the Hub
                '--------------------------------------------------------------
                response = webService.submitRadiologyResult(result)

            Catch soapEx As SoapException
                Dim exceptionMessage As String = soapEx.Detail.InnerText.Replace("" & _
                Microsoft.VisualBasic.Chr(10) & "", "" & Microsoft.VisualBasic.Chr(13) & "" & _
                Microsoft.VisualBasic.Chr(10) & "")
                Console.Write(exceptionMessage)
            End Try

            If Not (response Is Nothing) Then
                '------------------------------------------------
                'STEP 5: Examine response coming back for the Hub
                '------------------------------------------------
                Console.WriteLine("Status: <{0}>.", response.status)
                Console.WriteLine("Message Control Id: <{0}>.", response.messageControlId)
                Console.WriteLine("Response Message: <{0}>.", response.responseMsg)

                If Not (response.validationErrors Is Nothing) Then
                    Dim valErr As String
                    For Each valErr In response.validationErrors
                        Console.WriteLine("Validation Error: <{0}>.", valErr)
                    Next
                End If
            Else
                Console.WriteLine("Failed to properly call submitOrder WebService method.")
            End If
        End If

    End Sub

    Private Function getWebServiceRadiologyResult() As RadiologyResult
        Dim result As New RadiologyResult
        Dim resultMessage As String = Nothing
        Dim resultByteArray As Byte()

        ' Create Order Mesage by passing in facility information
        resultMessage = buildRadiologyResultMessage(SENDING_APPLICATION, SENDING_FACILITY, RECEIVING_FACILITY)

        ' Convert String to a Byte array and return in an Order object
        resultByteArray = System.Text.Encoding.ASCII.GetBytes(resultMessage)
        result.hl7RadiologyResult = resultByteArray
        Return result
    End Function

    'the buildRadiologyResultMessage function replaces values in the RADIOLOGY_RESULT_MESSAGE 
    'template to create a valid HL7 Order message
    Private Function buildRadiologyResultMessage(ByVal sendingApplication As String, _
                                       ByVal sendingFacility As String, _
                                       ByVal receivingFacility As String) As String
        ' 'now' is used for 2 purposes here
        ' 1 to specify the time that the message was created
        ' 2 to create a unique message control ID and order number
        Dim now As System.DateTime = System.DateTime.Now
        ' parameters for replacement in the template
        Dim msgParams(4) As Object

        msgParams(0) = sendingApplication
        msgParams(1) = sendingFacility
        msgParams(2) = receivingFacility
        msgParams(3) = now
        msgParams(4) = now.Ticks
        Return String.Format(RADIOLOGY_RESULT_MESSAGE, msgParams)
    End Function
End Class
