org.ehealth_connector.demo.iti.pix.DemoMPIClient.java Source code

Java tutorial

Introduction

Here is the source code for org.ehealth_connector.demo.iti.pix.DemoMPIClient.java

Source

/*******************************************************************************
 *
 * The authorship of this code and the accompanying materials is held by medshare GmbH, Switzerland.
 * All rights reserved. http://medshare.net
 *
 * Project Team: https://sourceforge.net/p/ehealthconnector/wiki/Team/
 *
 * This code is are made available under the terms of the Eclipse Public License v1.0.
 *
 * Accompanying materials are made available under the terms of the Creative Commons
 * Attribution-ShareAlike 4.0 License.
 *
 * Year of publication: 2015
 *
 *******************************************************************************/

package org.ehealth_connector.demo.iti.pix;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.xml.DOMConfigurator;
import org.ehealth_connector.communication.AtnaConfig;
import org.ehealth_connector.communication.ConvenienceMasterPatientIndexV3;
import org.ehealth_connector.communication.Destination;
import org.ehealth_connector.communication.mpi.FhirPatient;
import org.junit.Test;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;

/**
 * See http://pixpdqtests.nist.gov/pixpdqtool/ IHE PIX and PDQ Pre-Connectathon
 * Test Tool
 * 
 * Configuration for 2015 Europe
 * 
 * Application Name: provided by test tool Facility Name: provided by test tool
 * IP Address: provided by test tool Port Number: V3 Message (Non Secure: 9090 )
 * 
 * ATTENTION: You need to start the testtool and need to get the parameters from
 * webpage otherwise the test will not run through
 */
public class DemoMPIClient {

    private final Log log = LogFactory.getLog(DemoMPIClient.class);

    // TODO: fill below in from test tool before running test
    private String applicationName = "2.16.840.1.113883.3.72.6.5.100.1426";
    private String ipAddress = "129.6.24.79";
    final private String facilityName = null; // "2.16.840.1.113883.3.72.6.1";

    final private String senderApplicationOid = "1.2.3.4";
    final private String domainToReturnOid = "2.16.840.1.113883.3.72.5.9.3";

    private final FhirContext ctx = new FhirContext();
    private final Destination dest = new Destination();
    private AtnaConfig atna;

    public DemoMPIClient() {
        super();
        init();
    }

    public void doDemo() {
        // switch off logging for console output
        DOMConfigurator.configure(DemoMPIClient.class.getResource("/log4jConfigs/log4j.xml"));

        log.debug("PerformanceTimestamp: Starting DemoMPIClient.doDemo");
        System.out.println("DEMO - eHealthConnector MPI Client with IHE PIX and PDQ Pre-Connectathon Test Tool");
        System.out.println("----------------------------------------------------------------------------------");
        System.out.println("");
        System.out
                .println("see http://pixpdqtests.nist.gov/pixpdqtool/ IHE PIX and PDQ Pre-Connectathon Test Tool");
        System.out.println("Browser: Select V3, PIX Source, ITI-44-Source Feed");
        System.out.println("Browser: Click on Configuration Information, and press ENTER if ready");
        System.out.println("");
        if (readStringFromConsole() == null) {
            return;
        }
        System.out.println("Enter Application Name and press ENTER:");
        applicationName = this.readStringFromConsole();
        if (applicationName == null) {
            return;
        }
        System.out.println("Enter IP Address and press ENTER:");
        ipAddress = this.readStringFromConsole();
        System.out.println("Press OK in Configuration and Start Test, press ENTER:");
        if (readStringFromConsole() == null) {
            return;
        }
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo before init()");
        init();
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo after init()");
        System.out.println("Browser: Start ITI-44-Source-Feed Test and press ENTER");
        System.out.println("");
        System.out.println("ITI-44-Source-Feed Test");
        System.out.println("");
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo before ITI44SourceFeedTest()");
        ITI44SourceFeedTest();
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo after ITI44SourceFeedTest()");
        System.out.println("ITI-44-Source-Feed Test finsihed");
        System.out.println("");

        System.out.println(
                "Browser: Back Button, select ITI-44-Source-Merge, Configuration, Ok, Start Test and press ENTER:");
        if (readStringFromConsole() == null) {
            return;
        }
        System.out.println("");
        System.out.println("ITI-44-Source-Merge ");
        System.out.println("");
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo before ITI44SourceMergeTest()");
        ITI44SourceMergeTest();
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo after ITI44SourceMergeTest()");
        System.out.println("ITI-44-Source-Merge  finsihed");
        System.out.println("");

        System.out.println(
                "Browser: Back Button, select ITI-44-Source-Update, Configuration, Ok, Start Test and press ENTER:");
        if (readStringFromConsole() == null) {
            return;
        }
        System.out.println("");
        System.out.println("ITI-44-Source-Update ");
        System.out.println("");
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo before ITI44SourceUpdateTest()");
        ITI44SourceUpdateTest();
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo after ITI44SourceUpdateTest()");
        System.out.println("ITI-44-Source-Update finsihed");
        System.out.println("");

        System.out.println(
                "Browser: Back Button, select Actor PIX Consumer, ITI-45-Pix Consumer, Configuration Information, ok, Start Test and press ENTER:");
        if (readStringFromConsole() == null) {
            return;
        }
        System.out.println("");
        System.out.println("ITI-45-Consumer Test Step 1");
        System.out.println("");
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo before ITI45ConsumerTestStep1()");
        ITI45ConsumerTestStep1();
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo after ITI45ConsumerTestStep1()");
        System.out.println("ITI-45-Consumer Test Step 1 finsihed");
        System.out.println("");

        System.out.println("Select next Step press ENTER:");
        if (readStringFromConsole() == null) {
            return;
        }
        System.out.println("");
        System.out.println("ITI-45-Consumer Test Step 2");
        System.out.println("");
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo before ITI45ConsumerTestStep2()");
        ITI45ConsumerTestStep2();
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo after ITI45ConsumerTestStep2()");
        System.out.println("ITI-45-Consumer Test Step 2 finsihed");
        System.out.println("");
        System.out
                .println("DEMO END - eHealthConnector MPI Client with IHE PIX and PDQ Pre-Connectathon Test Tool");
        System.out
                .println("................................................. so long, and thanks for all the fish");
        System.out
                .println("--------------------------------------------------------------------------------------");
        log.debug("PerformanceTimestamp: DemoMPIClient.doDemo finished");
    }

    @Test
    public void ITI44SourceFeedTest() {
        log.debug("ITI44SourceFeedTest");
        String stringPatient = getXmlResource("/demoMpiClient/ITI-44-Source-FeedTest.xml");
        log.debug("FHIR - newXmlParser");
        IParser parser = ctx.newXmlParser();
        log.debug("FHIR - parseResource");
        FhirPatient fhirPatient = parser.parseResource(FhirPatient.class, stringPatient);
        log.debug("ConvenienceMasterPatientIndexV3 - addPatientDemographics");
        boolean ret = ConvenienceMasterPatientIndexV3.addPatientDemographics(fhirPatient.getPatient(),
                fhirPatient.getIdentifierFirstRep().getSystem().substring(8), dest, atna);
        log.debug("ITI44SourceFeedTest - finished");
        assertTrue(ret);
    }

    @Test
    public void ITI44SourceMergeTest() {
        String stringPatient = getXmlResource("/demoMpiClient/ITI-44-Source-MergeTest.xml");
        IParser parser = ctx.newXmlParser();
        FhirPatient fhirPatient = parser.parseResource(FhirPatient.class, stringPatient);
        boolean ret = ConvenienceMasterPatientIndexV3.mergePatients(fhirPatient.getPatient(), "PIXW",
                fhirPatient.getIdentifierFirstRep().getSystem().substring(8), dest, atna);
        assertTrue(ret);
    }

    @Test
    public void ITI44SourceUpdateTest() {
        String stringPatient = getXmlResource("/demoMpiClient/ITI-44-Source-UpdateTest.xml");
        IParser parser = ctx.newXmlParser();
        FhirPatient fhirPatient = parser.parseResource(FhirPatient.class, stringPatient);
        boolean ret = ConvenienceMasterPatientIndexV3.updatePatientDemographics(fhirPatient.getPatient(),
                fhirPatient.getIdentifierFirstRep().getSystem().substring(8), dest, atna);
        assertTrue(ret);
    }

    @Test
    public void ITI45ConsumerTestStep1() {
        String stringPatient = getXmlResource("/demoMpiClient/ITI-45-Consumer-Test.xml");
        IParser parser = ctx.newXmlParser();
        FhirPatient fhirPatient = parser.parseResource(FhirPatient.class, stringPatient);
        String domainToReturnOids[] = new String[1];
        domainToReturnOids[0] = this.domainToReturnOid;
        boolean ret = ConvenienceMasterPatientIndexV3.queryPatientID(fhirPatient.getPatient(),
                fhirPatient.getIdentifierFirstRep().getSystem().substring(8), domainToReturnOids, dest,
                atna) != null;
        assertTrue(ret);
    }

    @Test
    public void ITI45ConsumerTestStep2() {
        String stringPatient = getXmlResource("/demoMpiClient/ITI-45-Consumer-Test.xml");
        IParser parser = ctx.newXmlParser();
        FhirPatient fhirPatient = parser.parseResource(FhirPatient.class, stringPatient);
        ConvenienceMasterPatientIndexV3.queryPatientID(fhirPatient.getPatient(),
                fhirPatient.getIdentifierFirstRep().getSystem().substring(8), null, dest, atna);
    }

    private String getXmlResource(String fileName) {
        InputStream inputStream = getClass().getResourceAsStream(fileName);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer();
        String line;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
        } catch (IOException e) {
        } finally {
        }
        return sb.toString();
    }

    private void init() {
        try {
            dest.setPixQueryUri(new URI("http://" + ipAddress + ":9090"));
            dest.setPixSourceUri(new URI("http://" + ipAddress + ":9090"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        dest.setSenderApplicationOid(senderApplicationOid);
        dest.setReceiverApplicationOid(applicationName);
        dest.setReceiverFacilityOid(facilityName);
    }

    private String readStringFromConsole() {
        try {
            Console console = System.console();
            return console.readLine();
        } catch (Exception e) {
            log.debug("note: eclipse console does not support input");
            System.out.println("aborted (eclipse console does not support input)");
            return null;
        }
    }

}